From 1c89e3efb0f05c648fc1c8e09039e62333de18d1 Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Tue, 20 Jun 2023 11:55:23 +0600 Subject: [PATCH] fix: collection currently playing state persist on restart --- lib/components/album/album_card.dart | 8 ++++---- lib/components/playlist/playlist_card.dart | 9 ++++----- .../shared/track_table/tracks_table_view.dart | 7 ++++++- lib/pages/album/album.dart | 2 ++ lib/pages/playlist/playlist.dart | 2 ++ lib/provider/proxy_playlist/proxy_playlist.dart | 14 +++++++++++++- .../proxy_playlist/proxy_playlist_provider.dart | 15 +++++++++++++++ 7 files changed, 46 insertions(+), 11 deletions(-) diff --git a/lib/components/album/album_card.dart b/lib/components/album/album_card.dart index da66f2762..afb637a04 100644 --- a/lib/components/album/album_card.dart +++ b/lib/components/album/album_card.dart @@ -46,11 +46,9 @@ class AlbumCard extends HookConsumerWidget { useStream(audioPlayer.playingStream).data ?? audioPlayer.isPlaying; final playlistNotifier = ref.watch(ProxyPlaylistNotifier.notifier); final queryClient = useQueryClient(); - final query = queryClient - .getQuery, dynamic>("album-tracks/${album.id}"); bool isPlaylistPlaying = useMemoized( - () => playlist.containsTracks(query?.data ?? album.tracks ?? []), - [playlistNotifier, query?.data, album.tracks], + () => playlist.containsCollection(album.id!), + [playlist, album.id], ); final int marginH = useBreakpointValue(xs: 10, sm: 10, md: 15, lg: 20, xl: 20, xxl: 20); @@ -89,6 +87,7 @@ class AlbumCard extends HookConsumerWidget { [], autoPlay: true, ); + playlistNotifier.addCollection(album.id!); } finally { updating.value = false; } @@ -118,6 +117,7 @@ class AlbumCard extends HookConsumerWidget { if (fetchedTracks == null || fetchedTracks.isEmpty) return; playlistNotifier.addTracks(fetchedTracks); + playlistNotifier.addCollection(album.id!); if (context.mounted) { final snackbar = SnackBar( content: Text("Added ${album.tracks?.length} tracks to queue"), diff --git a/lib/components/playlist/playlist_card.dart b/lib/components/playlist/playlist_card.dart index 5c801ee5b..579fbf93c 100644 --- a/lib/components/playlist/playlist_card.dart +++ b/lib/components/playlist/playlist_card.dart @@ -24,13 +24,10 @@ class PlaylistCard extends HookConsumerWidget { final playing = useStream(audioPlayer.playingStream).data ?? audioPlayer.isPlaying; final queryBowl = QueryClient.of(context); - final query = queryBowl.getQuery, dynamic>( - "playlist-tracks/${playlist.id}", - ); final tracks = useState?>(null); bool isPlaylistPlaying = useMemoized( - () => playlistQueue.containsTracks(tracks.value ?? query?.data ?? []), - [playlistNotifier, tracks.value, query?.data], + () => playlistQueue.containsCollection(playlist.id!), + [playlistQueue, playlist.id], ); final updating = useState(false); @@ -72,6 +69,7 @@ class PlaylistCard extends HookConsumerWidget { if (fetchedTracks.isEmpty) return; await playlistNotifier.load(fetchedTracks, autoPlay: true); + playlistNotifier.addCollection(playlist.id!); tracks.value = fetchedTracks; } finally { updating.value = false; @@ -90,6 +88,7 @@ class PlaylistCard extends HookConsumerWidget { if (fetchedTracks.isEmpty) return; playlistNotifier.addTracks(fetchedTracks); + playlistNotifier.addCollection(playlist.id!); tracks.value = fetchedTracks; if (context.mounted) { final snackbar = SnackBar( diff --git a/lib/components/shared/track_table/tracks_table_view.dart b/lib/components/shared/track_table/tracks_table_view.dart index 5c4333e79..d41d47381 100644 --- a/lib/components/shared/track_table/tracks_table_view.dart +++ b/lib/components/shared/track_table/tracks_table_view.dart @@ -1,6 +1,5 @@ import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:fuzzywuzzy/fuzzywuzzy.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; @@ -219,6 +218,9 @@ class TracksTableView extends HookConsumerWidget { case "play-next": { playback.addTracksAtFirst(selectedTracks); + if (playlistId != null) { + playback.addCollection(playlistId!); + } selected.value = []; showCheck.value = false; break; @@ -226,6 +228,9 @@ class TracksTableView extends HookConsumerWidget { case "add-to-queue": { playback.addTracks(selectedTracks); + if (playlistId != null) { + playback.addCollection(playlistId!); + } selected.value = []; showCheck.value = false; break; diff --git a/lib/pages/album/album.dart b/lib/pages/album/album.dart index 69b542ee4..0e9609d59 100644 --- a/lib/pages/album/album.dart +++ b/lib/pages/album/album.dart @@ -32,6 +32,7 @@ class AlbumPage extends HookConsumerWidget { sortedTracks, initialIndex: sortedTracks.indexWhere((s) => s.id == currentTrack?.id), ); + playback.addCollection(album.id!); } else if (isPlaylistPlaying && currentTrack.id != null && currentTrack.id != playlist.activeTrack?.id) { @@ -101,6 +102,7 @@ class AlbumPage extends HookConsumerWidget { TypeConversionUtils.simpleTrack_X_Track(track, album)) .toList(), ); + playback.addCollection(album.id!); } }, onShare: () { diff --git a/lib/pages/playlist/playlist.dart b/lib/pages/playlist/playlist.dart index 170d9693c..de89429ac 100644 --- a/lib/pages/playlist/playlist.dart +++ b/lib/pages/playlist/playlist.dart @@ -36,6 +36,7 @@ class PlaylistView extends HookConsumerWidget { initialIndex: sortedTracks.indexWhere((s) => s.id == currentTrack?.id), autoPlay: true, ); + playback.addCollection(playlist.id!); } else if (isPlaylistPlaying && currentTrack.id != null && currentTrack.id != proxyPlaylist.activeTrack?.id) { @@ -97,6 +98,7 @@ class PlaylistView extends HookConsumerWidget { onAddToQueue: () { if (tracksSnapshot.hasData && !isPlaylistPlaying) { playlistNotifier.addTracks(tracksSnapshot.data!); + playlistNotifier.addCollection(playlist.id!); } }, bottomSpace: mediaQuery.mdAndDown, diff --git a/lib/provider/proxy_playlist/proxy_playlist.dart b/lib/provider/proxy_playlist/proxy_playlist.dart index 2461f4b69..c0563f21e 100644 --- a/lib/provider/proxy_playlist/proxy_playlist.dart +++ b/lib/provider/proxy_playlist/proxy_playlist.dart @@ -6,15 +6,20 @@ import 'package:spotube/models/spotube_track.dart'; class ProxyPlaylist { final Set tracks; + final Set collections; final int? active; - ProxyPlaylist(this.tracks, [this.active]); + ProxyPlaylist(this.tracks, [this.active, this.collections = const {}]); + factory ProxyPlaylist.fromJson(Map json) { return ProxyPlaylist( List.castFrom>( json['tracks'] ?? >[], ).map(_makeAppropriateTrack).toSet(), json['active'] as int?, + json['collections'] == null + ? {} + : (json['collections'] as List).toSet().cast(), ); } @@ -26,6 +31,10 @@ class ProxyPlaylist { activeTrack is! SpotubeTrack && activeTrack is! LocalTrack; + bool containsCollection(String collection) { + return collections.contains(collection); + } + bool containsTrack(TrackSimple track) { return tracks.firstWhereOrNull((element) => element.id == track.id) != null; } @@ -57,16 +66,19 @@ class ProxyPlaylist { return { 'tracks': tracks.map(_makeAppropriateTrackJson).toList(), 'active': active, + 'collections': collections.toList(), }; } ProxyPlaylist copyWith({ Set? tracks, int? active, + Set? collections, }) { return ProxyPlaylist( tracks ?? this.tracks, active ?? this.active, + collections ?? this.collections, ); } } diff --git a/lib/provider/proxy_playlist/proxy_playlist_provider.dart b/lib/provider/proxy_playlist/proxy_playlist_provider.dart index 166b40e99..7c59af627 100644 --- a/lib/provider/proxy_playlist/proxy_playlist_provider.dart +++ b/lib/provider/proxy_playlist/proxy_playlist_provider.dart @@ -185,6 +185,19 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier } } + void addCollection(String collectionId) { + state = state.copyWith(collections: { + ...state.collections, + collectionId, + }); + } + + void removeCollection(String collectionId) { + state = state.copyWith(collections: { + ...state.collections..remove(collectionId), + }); + } + // TODO: Safely Remove playing tracks Future removeTrack(String trackId) async { @@ -224,6 +237,7 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier state = state.copyWith( tracks: tracks.toSet(), active: initialIndex, + collections: {}, ); await notificationService.addTrack(indexTrack); } else { @@ -236,6 +250,7 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier state = state.copyWith( tracks: mergeTracks([addableTrack], tracks), active: initialIndex, + collections: {}, ); await notificationService.addTrack(addableTrack); await storeTrack(