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
/// it can increase the latency
Future<void> storeTrack(Track track, SpotubeTrack spotubeTrack) async {

View File

@ -58,16 +58,34 @@ class ProxyPlaylistNotifier extends StateNotifier<ProxyPlaylist>
audioPlayer.currentIndexChangedStream.listen((index) async {
if (index == -1 || index == state.active) return;
final track = state.tracks.elementAtOrNull(index);
if (track == null) return;
notificationService.addTrack(track);
state = state.copyWith(active: index);
final newIndexedTrack =
mapSourcesToTracks([audioPlayer.sources[index]]).firstOrNull;
if (newIndexedTrack == null) return;
notificationService.addTrack(newIndexedTrack);
state = state.copyWith(
active: state.tracks
.toList()
.indexWhere((element) => element.id == newIndexedTrack.id),
);
if (preferences.albumColorSync) {
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;
audioPlayer.percentCompletedStream(60).listen((percent) async {
if (isPreSearching) return;

View File

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