Skip to content

Commit

Permalink
chore(playback): re-enable shuffling
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingkor Roy Tirtho committed May 16, 2023
1 parent 8bcce92 commit 696eeee
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
11 changes: 11 additions & 0 deletions lib/provider/proxy_playlist/next_fetcher_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 22 additions & 4 deletions lib/provider/proxy_playlist/proxy_playlist_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions lib/services/audio_player/mk_state_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -105,6 +103,8 @@ class MkPlayerWithState extends Player {
);
_tempMedias = null;
}
await super.setShuffle(shuffle);
_shuffleStream.add(shuffle);
}

@override
Expand Down

0 comments on commit 696eeee

Please sign in to comment.