chore(playback): re-enable shuffling

This commit is contained in:
Kingkor Roy Tirtho 2023-05-16 22:34:20 +06:00
parent 8bcce9282e
commit 696eeee882
3 changed files with 35 additions and 6 deletions

View File

@ -82,6 +82,17 @@ mixin NextFetcher on StateNotifier<ProxyPlaylist> {
} }
} }
List<Track> mapSourcesToTracks(List<String> sources) {
final tracks = state.tracks;
return sources.map((source) {
final track = tracks.firstWhereOrNull(
(track) => makeAppropriateSource(track) == source,
);
return track!;
}).toList();
}
/// This method must be called after any playback operation as /// This method must be called after any playback operation as
/// it can increase the latency /// it can increase the latency
Future<void> storeTrack(Track track, SpotubeTrack spotubeTrack) async { Future<void> storeTrack(Track track, SpotubeTrack spotubeTrack) async {

View File

@ -58,16 +58,34 @@ class ProxyPlaylistNotifier extends StateNotifier<ProxyPlaylist>
audioPlayer.currentIndexChangedStream.listen((index) async { audioPlayer.currentIndexChangedStream.listen((index) async {
if (index == -1 || index == state.active) return; if (index == -1 || index == state.active) return;
final track = state.tracks.elementAtOrNull(index); final newIndexedTrack =
if (track == null) return; mapSourcesToTracks([audioPlayer.sources[index]]).firstOrNull;
notificationService.addTrack(track);
state = state.copyWith(active: index); if (newIndexedTrack == null) return;
notificationService.addTrack(newIndexedTrack);
state = state.copyWith(
active: state.tracks
.toList()
.indexWhere((element) => element.id == newIndexedTrack.id),
);
if (preferences.albumColorSync) { if (preferences.albumColorSync) {
updatePalette(); updatePalette();
} }
}); });
audioPlayer.shuffledStream.listen((event) {
final newlyOrderedTracks = mapSourcesToTracks(audioPlayer.sources);
final newIndex = newlyOrderedTracks.indexWhere(
(element) => element.id == state.activeTrack?.id,
);
state = state.copyWith(
tracks: newlyOrderedTracks.toSet(),
active: newIndex,
);
});
bool isPreSearching = false; bool isPreSearching = false;
audioPlayer.percentCompletedStream(60).listen((percent) async { audioPlayer.percentCompletedStream(60).listen((percent) async {
if (isPreSearching) return; if (isPreSearching) return;

View File

@ -85,8 +85,6 @@ class MkPlayerWithState extends Player {
@override @override
Future<void> setShuffle(bool shuffle) async { Future<void> setShuffle(bool shuffle) async {
_shuffled = shuffle; _shuffled = shuffle;
await super.setShuffle(shuffle);
_shuffleStream.add(shuffle);
if (shuffle) { if (shuffle) {
_tempMedias = _playlist!.medias; _tempMedias = _playlist!.medias;
final active = _playlist!.medias[_playlist!.index]; final active = _playlist!.medias[_playlist!.index];
@ -105,6 +103,8 @@ class MkPlayerWithState extends Player {
); );
_tempMedias = null; _tempMedias = null;
} }
await super.setShuffle(shuffle);
_shuffleStream.add(shuffle);
} }
@override @override