Move tracks already in queue to head of queue when pressing play next

This commit is contained in:
G1org1owo 2025-11-18 00:51:51 +01:00
parent eef84a7ac0
commit d291ae4bce
No known key found for this signature in database
GPG Key ID: F93BD731C3534CD6

View File

@ -228,24 +228,43 @@ class AudioPlayerNotifier extends Notifier<AudioPlayerState> {
final addableTracks = _blacklist final addableTracks = _blacklist
.filter(tracks) .filter(tracks)
.where( .toList();
final remainingTracks = state.tracks.where(
(track) => (track) =>
allowDuplicates || allowDuplicates ||
!state.tracks.any((element) => _compareTracks(element, track)), !addableTracks.any((element) => _compareTracks(element, track))
) )
.toList(); .toList();
state = state.copyWith( state = state.copyWith(
tracks: [...addableTracks, ...state.tracks], tracks: [...addableTracks, ...remainingTracks],
); );
for (int i = 0; i < addableTracks.length; i++) { for (int i = 0; i < addableTracks.length; i++) {
final track = addableTracks.elementAt(i); final track = addableTracks.elementAt(i);
final (currentIndex, _) = remainingTracks
.indexed
.cast<(int, SpotubeTrackObject?)>()
.firstWhere(
(record) {
final (idx, element) = record;
return _compareTracks(element!, track);
},
orElse: () => (-1, null)
);
final newIndex = max(state.currentIndex, 0) + i + 1;
if (allowDuplicates || newIndex < 0) {
await audioPlayer.addTrackAt( await audioPlayer.addTrackAt(
SpotubeMedia(track), SpotubeMedia(track),
max(state.currentIndex, 0) + i + 1, newIndex
); );
} else {
await audioPlayer.moveTrack(currentIndex, newIndex);
}
} }
await _updatePlayerState( await _updatePlayerState(