From 761743991520609dd2b2dcb12cd6e4e75a8f6925 Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Sun, 26 Feb 2023 14:01:53 +0600 Subject: [PATCH] feat: compatibility with fl-query nextPage method change --- lib/components/album/album_card.dart | 2 +- lib/components/genre/category_card.dart | 9 ++------- lib/components/playlist/playlist_card.dart | 2 +- lib/components/root/sidebar.dart | 11 ----------- lib/hooks/use_spotify_infinite_query.dart | 12 +++++++++--- lib/hooks/use_spotify_query.dart | 12 +++++++++--- lib/services/queries/album.dart | 13 +++++-------- lib/services/queries/artist.dart | 18 +++++++++--------- lib/services/queries/category.dart | 20 +++++++++++--------- lib/services/queries/playlist.dart | 10 ++++++---- lib/services/queries/search.dart | 12 ++++++++---- pubspec.lock | 4 ++-- 12 files changed, 63 insertions(+), 62 deletions(-) diff --git a/lib/components/album/album_card.dart b/lib/components/album/album_card.dart index 8e944c618..e06b559ef 100644 --- a/lib/components/album/album_card.dart +++ b/lib/components/album/album_card.dart @@ -48,7 +48,7 @@ class AlbumCard extends HookConsumerWidget { final playlistNotifier = ref.watch(PlaylistQueueNotifier.notifier); final queryBowl = QueryClient.of(context); final query = queryBowl - .getQuery, SpotifyApi>("album-tracks/${album.id}"); + .getQuery, dynamic>("album-tracks/${album.id}"); final tracks = useState(query?.data ?? album.tracks ?? []); bool isPlaylistPlaying = playlistNotifier.isPlayingPlaylist(tracks.value); final int marginH = diff --git a/lib/components/genre/category_card.dart b/lib/components/genre/category_card.dart index 02c66e62f..5b2bbf9cf 100644 --- a/lib/components/genre/category_card.dart +++ b/lib/components/genre/category_card.dart @@ -8,7 +8,6 @@ import 'package:spotube/components/shared/shimmers/shimmer_playbutton_card.dart' import 'package:spotube/components/shared/waypoint.dart'; import 'package:spotube/components/playlist/playlist_card.dart'; import 'package:spotube/models/logger.dart'; -import 'package:spotube/provider/spotify_provider.dart'; import 'package:spotube/services/queries/queries.dart'; class CategoryCard extends HookConsumerWidget { @@ -23,14 +22,10 @@ class CategoryCard extends HookConsumerWidget { @override Widget build(BuildContext context, ref) { final scrollController = useScrollController(); - final spotify = ref.watch(spotifyProvider); final playlistQuery = useQueries.category.playlistsOf( ref, category.id!, ); - final hasNextPage = playlistQuery.pages.isEmpty - ? false - : (playlistQuery.pages.last.items?.length ?? 0) == 5; final playlists = playlistQuery.pages .expand( @@ -48,7 +43,7 @@ class CategoryCard extends HookConsumerWidget { ], ), ), - playlistQuery.hasErrors + playlistQuery.hasPageError && !playlistQuery.hasPageData ? PlatformText( "Something Went Wrong\n${playlistQuery.errors.first}") : SizedBox( @@ -75,7 +70,7 @@ class CategoryCard extends HookConsumerWidget { children: [ ...playlists .map((playlist) => PlaylistCard(playlist)), - if (hasNextPage) + if (playlistQuery.hasNextPage) const ShimmerPlaybuttonCard(count: 1), ], ), diff --git a/lib/components/playlist/playlist_card.dart b/lib/components/playlist/playlist_card.dart index 8d0c92150..4065e714d 100644 --- a/lib/components/playlist/playlist_card.dart +++ b/lib/components/playlist/playlist_card.dart @@ -26,7 +26,7 @@ class PlaylistCard extends HookConsumerWidget { final playing = useStream(PlaylistQueueNotifier.playing).data ?? PlaylistQueueNotifier.isPlaying; final queryBowl = QueryClient.of(context); - final query = queryBowl.getQuery, SpotifyApi>( + final query = queryBowl.getQuery, dynamic>( "playlist-tracks/${playlist.id}", ); final tracks = useState(query?.data ?? []); diff --git a/lib/components/root/sidebar.dart b/lib/components/root/sidebar.dart index 451dfa783..e8197b769 100644 --- a/lib/components/root/sidebar.dart +++ b/lib/components/root/sidebar.dart @@ -202,19 +202,8 @@ class SidebarFooter extends HookConsumerWidget { placeholder: ImagePlaceholder.artist, ); - // TODO: Remove below code after fl-query ^0.4.0 - /// Temporary fix before fl-query 0.4.0 final auth = ref.watch(AuthenticationNotifier.provider); - useEffect(() { - if (auth != null && me.hasError) { - me.refresh(); - } - return null; - }, [auth, me.hasError]); - - /// =================================== - return Padding( padding: const EdgeInsets.all(16).copyWith(left: 0), child: Row( diff --git a/lib/hooks/use_spotify_infinite_query.dart b/lib/hooks/use_spotify_infinite_query.dart index 0507a9f8f..d56d3ca7a 100644 --- a/lib/hooks/use_spotify_infinite_query.dart +++ b/lib/hooks/use_spotify_infinite_query.dart @@ -39,9 +39,15 @@ InfiniteQuery ); useEffect(() { - query.refreshAll(); - return null; - }, [spotify]); + return ref.listenManual( + spotifyProvider, + (previous, next) { + if (previous != next) { + query.refreshAll(); + } + }, + ).close; + }, [query]); return query; } diff --git a/lib/hooks/use_spotify_query.dart b/lib/hooks/use_spotify_query.dart index 700698d6e..209f3391a 100644 --- a/lib/hooks/use_spotify_query.dart +++ b/lib/hooks/use_spotify_query.dart @@ -38,9 +38,15 @@ Query useSpotifyQuery( ); useEffect(() { - query.refresh(); - return null; - }, [spotify]); + return ref.listenManual( + spotifyProvider, + (previous, next) { + if (previous != next) { + query.refresh(); + } + }, + ).close; + }, [query]); return query; } diff --git a/lib/services/queries/album.dart b/lib/services/queries/album.dart index c7860ab41..629f732ad 100644 --- a/lib/services/queries/album.dart +++ b/lib/services/queries/album.dart @@ -1,5 +1,4 @@ import 'package:catcher/catcher.dart'; -import 'package:collection/collection.dart'; import 'package:fl_query/fl_query.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:spotify/spotify.dart'; @@ -68,13 +67,11 @@ class AlbumQueries { }, ref: ref, initialPage: 0, - nextPage: (lastParam, pages) { - final lastPage = pages.elementAtOrNull(lastParam); - if (lastPage == null || - lastPage.isLast || - (lastPage.items ?? []).length < 5) return null; - - return lastPage.nextOffset; + nextPage: (lastPage, lastPageData) { + if (lastPageData.isLast || (lastPageData.items ?? []).length < 5) { + return null; + } + return lastPageData.nextOffset; }, ); } diff --git a/lib/services/queries/artist.dart b/lib/services/queries/artist.dart index e183d702a..fed55429e 100644 --- a/lib/services/queries/artist.dart +++ b/lib/services/queries/artist.dart @@ -1,4 +1,3 @@ -import 'package:collection/collection.dart'; import 'package:fl_query/fl_query.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:spotify/spotify.dart'; @@ -29,10 +28,12 @@ class ArtistQueries { .getPage(15, pageParam); }, initialPage: "", - nextPage: (lastPage, pages) => - pages.last.isLast || (pages.last.items?.length ?? 0) < 15 - ? null - : pages.last.after, + nextPage: (lastPage, lastPageData) { + if (lastPageData.isLast || (lastPageData.items ?? []).length < 15) { + return null; + } + return lastPageData.after; + }, ref: ref, ); } @@ -77,12 +78,11 @@ class ArtistQueries { return spotify.artists.albums(artist).getPage(5, pageParam); }, initialPage: 0, - nextPage: (lastPage, pages) { - final page = pages.elementAtOrNull(lastPage); - if (page == null || page.isLast || (page.items ?? []).length < 5) { + nextPage: (lastPage, lastPageData) { + if (lastPageData.isLast || (lastPageData.items ?? []).length < 5) { return null; } - return page.nextOffset; + return lastPageData.nextOffset; }, ref: ref, ); diff --git a/lib/services/queries/category.dart b/lib/services/queries/category.dart index 2cfa61267..4f646b13d 100644 --- a/lib/services/queries/category.dart +++ b/lib/services/queries/category.dart @@ -18,11 +18,11 @@ class CategoryQueries { return categories; }, initialPage: 0, - nextPage: (lastPage, pages) { - if (pages.isEmpty) return lastPage + 1; - return pages.last.isLast || (pages.last.items?.length ?? 0) < 15 - ? null - : pages.last.nextOffset; + nextPage: (lastPage, lastPageData) { + if (lastPageData.isLast || (lastPageData.items ?? []).length < 15) { + return null; + } + return lastPageData.nextOffset; }, ref: ref, ); @@ -42,10 +42,12 @@ class CategoryQueries { return playlists; }, initialPage: 0, - nextPage: (lastPage, pages) => - pages.last.isLast || (pages.last.items?.length ?? 0) < 5 - ? null - : pages.last.nextOffset, + nextPage: (lastPage, lastPageData) { + if (lastPageData.isLast || (lastPageData.items ?? []).length < 5) { + return null; + } + return lastPageData.nextOffset; + }, ref: ref, ); } diff --git a/lib/services/queries/playlist.dart b/lib/services/queries/playlist.dart index e927779bf..55e4ed6f4 100644 --- a/lib/services/queries/playlist.dart +++ b/lib/services/queries/playlist.dart @@ -71,10 +71,12 @@ class PlaylistQueries { } }, initialPage: 0, - nextPage: (lastPage, pages) => - pages.last.isLast || (pages.last.items?.length ?? 0) < 5 - ? null - : pages.last.nextOffset, + nextPage: (lastPage, lastPageData) { + if (lastPageData.isLast || (lastPageData.items ?? []).length < 5) { + return null; + } + return lastPageData.nextOffset; + }, ref: ref, ); } diff --git a/lib/services/queries/search.dart b/lib/services/queries/search.dart index 4717a412d..e68546202 100644 --- a/lib/services/queries/search.dart +++ b/lib/services/queries/search.dart @@ -22,10 +22,14 @@ class SearchQueries { }, ref: ref, initialPage: 0, - nextPage: (lastPage, pages) => - pages.last.isNotEmpty && (pages.last.first.items?.length ?? 0) < 10 - ? null - : pages.last.last.nextOffset, + nextPage: (lastPage, lastPageData) { + if (lastPageData.isEmpty) return null; + if ((lastPageData.first.isLast || + (lastPageData.first.items ?? []).length < 10)) { + return null; + } + return lastPageData.first.nextOffset; + }, ); } } diff --git a/pubspec.lock b/pubspec.lock index 992e73fcf..a95d9301d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -535,7 +535,7 @@ packages: description: path: "packages/fl_query" ref: new-architecture - resolved-ref: d964216ee17e600f79c33f1811080877c8c1b510 + resolved-ref: f2a23b085cd657a1612d87749f6592b4d67814c5 url: "https://github.com/KRTirtho/fl-query.git" source: git version: "0.3.1" @@ -544,7 +544,7 @@ packages: description: path: "packages/fl_query_hooks" ref: new-architecture - resolved-ref: d964216ee17e600f79c33f1811080877c8c1b510 + resolved-ref: f2a23b085cd657a1612d87749f6592b4d67814c5 url: "https://github.com/KRTirtho/fl-query.git" source: git version: "0.3.1"