From 9bad8c9eb88f7c91091a669b642b92474df0f128 Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Sun, 5 Feb 2023 12:36:07 +0600 Subject: [PATCH] fix(playbutton): playing state is not updating when playlist is actually playing --- README.md | 2 +- lib/components/playlist/playlist_card.dart | 35 +++++++++++----------- lib/pages/artist/artist.dart | 34 ++++++++++----------- lib/pages/playlist/playlist.dart | 1 - 4 files changed, 33 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 663181a0f..94e02b273 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ I'm always releasing newer versions of binary of the software each 2-3 month wit > **Note!:** If you don't understand this download table. You can read [installation instructions][wiki-installation-instructions] from the wiki ## Nightly Builds -Get the latest nightly builds of Spotube [here](https://nightly.link/KRTirtho/spotube/workflows/spotube-nightly/build) +Get the latest nightly builds of Spotube [here](https://github.com/KRTirtho/spotube/releases/tag/nightly) # TODO: - [ ] Windows OS Media Control & Media Keys Support diff --git a/lib/components/playlist/playlist_card.dart b/lib/components/playlist/playlist_card.dart index 29bb5f060..29781eacc 100644 --- a/lib/components/playlist/playlist_card.dart +++ b/lib/components/playlist/playlist_card.dart @@ -5,11 +5,12 @@ import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:spotify/spotify.dart'; import 'package:spotube/components/shared/playbutton_card.dart'; import 'package:spotube/hooks/use_breakpoint_value.dart'; -import 'package:spotube/provider/spotify_provider.dart'; import 'package:spotube/provider/playlist_queue_provider.dart'; +import 'package:spotube/provider/spotify_provider.dart'; import 'package:spotube/services/queries/queries.dart'; import 'package:spotube/utils/service_utils.dart'; import 'package:spotube/utils/type_conversion_utils.dart'; +import 'package:uuid/uuid.dart'; class PlaylistCard extends HookConsumerWidget { final PlaylistSimple playlist; @@ -24,12 +25,12 @@ class PlaylistCard extends HookConsumerWidget { final playlistQueue = ref.watch(PlaylistQueueNotifier.provider); final playlistNotifier = ref.watch(PlaylistQueueNotifier.notifier); final playing = useStream(PlaylistQueueNotifier.playing).data ?? false; - final tracks = QueryBowl.of(context) - .getQuery, SpotifyApi>( - Queries.playlist.tracksOf(playlist.id!).queryKey) - ?.data ?? - []; - bool isPlaylistPlaying = playlistNotifier.isPlayingPlaylist(tracks); + final queryBowl = QueryBowl.of(context); + final query = queryBowl.getQuery, SpotifyApi>( + Queries.playlist.tracksOf(playlist.id!).queryKey, + ); + final tracks = useState(query?.data ?? []); + bool isPlaylistPlaying = playlistNotifier.isPlayingPlaylist(tracks.value); final int marginH = useBreakpointValue(sm: 10, md: 15, lg: 20, xl: 20, xxl: 20); @@ -56,20 +57,18 @@ class PlaylistCard extends HookConsumerWidget { } else if (isPlaylistPlaying && !playing) { return playlistNotifier.resume(); } - SpotifyApi spotifyApi = ref.read(spotifyProvider); - List tracks = (playlist.id != "user-liked-tracks" - ? await spotifyApi.playlists - .getTracksByPlaylistId(playlist.id!) - .all() - : await spotifyApi.tracks.me.saved - .all() - .then((tracks) => tracks.map((e) => e.track!))) - .toList(); + List fetchedTracks = await queryBowl.fetchQuery( + key: ValueKey(const Uuid().v4()), + Queries.playlist.tracksOf(playlist.id!), + externalData: ref.read(spotifyProvider), + ) ?? + []; - if (tracks.isEmpty) return; + if (fetchedTracks.isEmpty) return; - await playlistNotifier.loadAndPlay(tracks); + await playlistNotifier.loadAndPlay(fetchedTracks); + tracks.value = fetchedTracks; }, ); } diff --git a/lib/pages/artist/artist.dart b/lib/pages/artist/artist.dart index 4b5847276..3029b296c 100644 --- a/lib/pages/artist/artist.dart +++ b/lib/pages/artist/artist.dart @@ -286,6 +286,11 @@ class ArtistPage extends HookConsumerWidget { externalData: spotify, ); + final isPlaylistPlaying = + playlistNotifier.isPlayingPlaylist( + topTracksQuery.data ?? [], + ); + if (topTracksQuery.isLoading || !topTracksQuery.hasData) { return const PlatformCircularProgressIndicator(); } else if (topTracksQuery.hasError) { @@ -296,10 +301,7 @@ class ArtistPage extends HookConsumerWidget { final topTracks = topTracksQuery.data!; - final isPlaylistPlaying = useMemoized(() { - return playlistNotifier.isPlayingPlaylist(topTracks); - }, [topTracks]); - playPlaylist(List tracks, + void playPlaylist(List tracks, {Track? currentTrack}) async { currentTrack ??= tracks.first; if (!isPlaylistPlaying) { @@ -321,22 +323,16 @@ class ArtistPage extends HookConsumerWidget { style: PlatformTheme.of(context).textTheme?.headline, ), - Container( - margin: const EdgeInsets.symmetric(horizontal: 5), - decoration: BoxDecoration( - color: PlatformTheme.of(context).primaryColor, - borderRadius: BorderRadius.circular(50), - ), - child: PlatformIconButton( - icon: Icon( - isPlaylistPlaying - ? SpotubeIcons.stop - : SpotubeIcons.play, - color: Colors.white, - ), - onPressed: () => - playPlaylist(topTracks.toList()), + PlatformIconButton( + icon: Icon( + isPlaylistPlaying + ? SpotubeIcons.stop + : SpotubeIcons.play, + color: Colors.white, ), + backgroundColor: + PlatformTheme.of(context).primaryColor, + onPressed: () => playPlaylist(topTracks.toList()), ) ], ), diff --git a/lib/pages/playlist/playlist.dart b/lib/pages/playlist/playlist.dart index 3bbf1b254..d1a24ebc2 100644 --- a/lib/pages/playlist/playlist.dart +++ b/lib/pages/playlist/playlist.dart @@ -45,7 +45,6 @@ class PlaylistView extends HookConsumerWidget { @override Widget build(BuildContext context, ref) { - final playlistQueue = ref.watch(PlaylistQueueNotifier.provider); final playlistNotifier = ref.watch(PlaylistQueueNotifier.notifier); SpotifyApi spotify = ref.watch(spotifyProvider);