From b78b65737781a0519cb0d9658aab1fd3378c0a0f Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Sun, 26 Feb 2023 22:07:02 +0600 Subject: [PATCH] chore: enable search again --- lib/collections/routes.dart | 13 +++-- lib/pages/search/search.dart | 94 +++++++++++--------------------- lib/services/queries/search.dart | 3 +- 3 files changed, 40 insertions(+), 70 deletions(-) diff --git a/lib/collections/routes.dart b/lib/collections/routes.dart index 5fbed70f2..353d4e5c8 100644 --- a/lib/collections/routes.dart +++ b/lib/collections/routes.dart @@ -3,6 +3,7 @@ import 'package:flutter/widgets.dart'; import 'package:go_router/go_router.dart'; import 'package:spotify/spotify.dart' hide Search; import 'package:spotube/pages/home/home.dart'; +import 'package:spotube/pages/search/search.dart'; import 'package:spotube/pages/settings/blacklist.dart'; import 'package:spotube/pages/settings/about.dart'; import 'package:spotube/utils/platform.dart'; @@ -33,12 +34,12 @@ final router = GoRouter( path: "/", pageBuilder: (context, state) => SpotubePage(child: const HomePage()), ), - // GoRoute( - // path: "/search", - // name: "Search", - // pageBuilder: (context, state) => - // SpotubePage(child: const SearchPage()), - // ), + GoRoute( + path: "/search", + name: "Search", + pageBuilder: (context, state) => + SpotubePage(child: const SearchPage()), + ), GoRoute( path: "/library", name: "Library", diff --git a/lib/pages/search/search.dart b/lib/pages/search/search.dart index 6cbcefc87..f7dddaefc 100644 --- a/lib/pages/search/search.dart +++ b/lib/pages/search/search.dart @@ -1,4 +1,3 @@ -import 'package:fl_query_hooks/fl_query_hooks.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart' hide Page; import 'package:flutter_hooks/flutter_hooks.dart'; @@ -17,14 +16,12 @@ import 'package:spotube/components/artist/artist_card.dart'; import 'package:spotube/components/playlist/playlist_card.dart'; import 'package:spotube/hooks/use_breakpoints.dart'; import 'package:spotube/provider/authentication_provider.dart'; -import 'package:spotube/provider/spotify_provider.dart'; import 'package:spotube/provider/playlist_queue_provider.dart'; import 'package:spotube/services/queries/queries.dart'; import 'package:spotube/utils/platform.dart'; import 'package:spotube/utils/primitive_utils.dart'; import 'package:spotube/utils/type_conversion_utils.dart'; -import 'package:tuple/tuple.dart'; import 'package:collection/collection.dart'; final searchTermStateProvider = StateProvider((ref) => ""); @@ -37,36 +34,21 @@ class SearchPage extends HookConsumerWidget { ref.watch(AuthenticationNotifier.provider); final authenticationNotifier = ref.watch(AuthenticationNotifier.provider.notifier); - final spotify = ref.watch(spotifyProvider); final albumController = useScrollController(); final playlistController = useScrollController(); final artistController = useScrollController(); final breakpoint = useBreakpoints(); - final getVariables = useCallback( - () => Tuple2( - ref.read(searchTermStateProvider), - spotify, - ), - [], - ); + final searchTerm = ref.watch(searchTermStateProvider); - final searchTrack = useInfiniteQuery( - job: useQueries.search.get(SearchType.track.key), - externalData: Tuple2("", spotify), - ); - final searchAlbum = useInfiniteQuery( - job: useQueries.search.get(SearchType.album.key), - externalData: Tuple2("", spotify), - ); - final searchPlaylist = useInfiniteQuery( - job: useQueries.search.get(SearchType.playlist.key), - externalData: Tuple2("", spotify), - ); - final searchArtist = useInfiniteQuery( - job: useQueries.search.get(SearchType.artist.key), - externalData: Tuple2("", spotify), - ); + final searchTrack = + useQueries.search.query(ref, searchTerm, SearchType.track); + final searchAlbum = + useQueries.search.query(ref, searchTerm, SearchType.album); + final searchPlaylist = + useQueries.search.query(ref, searchTerm, SearchType.playlist); + final searchArtist = + useQueries.search.query(ref, searchTerm, SearchType.artist); void onSearch() { for (final query in [ @@ -75,10 +57,7 @@ class SearchPage extends HookConsumerWidget { searchPlaylist, searchArtist, ]) { - query.enabled = false; - query.fetched = false; - query.setExternalData(getVariables()); - query.refetchPages(); + query.refreshAll(); } } @@ -96,10 +75,6 @@ class SearchPage extends HookConsumerWidget { ), color: PlatformTheme.of(context).scaffoldBackgroundColor, child: PlatformTextField( - onChanged: (value) { - ref.read(searchTermStateProvider.notifier).state = - value; - }, prefixIcon: SpotubeIcons.search, prefixIconColor: PlatformProperty.only( ios: @@ -108,6 +83,8 @@ class SearchPage extends HookConsumerWidget { ).resolve(platform!), placeholder: "Search...", onSubmitted: (value) { + ref.read(searchTermStateProvider.notifier).state = + value; onSearch(); }, ), @@ -127,7 +104,7 @@ class SearchPage extends HookConsumerWidget { ...searchAlbum.pages, ...searchPlaylist.pages, ...searchArtist.pages, - ].expand((page) => page ?? []).toList(); + ].expand((page) => page).toList(); for (MapEntry page in pages.asMap().entries) { for (var item in page.value.items ?? []) { if (item is AlbumSimple) { @@ -153,12 +130,10 @@ class SearchPage extends HookConsumerWidget { children: [ if (tracks.isNotEmpty) PlatformText.headline("Songs"), - if (searchTrack.isLoading && - !searchTrack.isFetchingNextPage) + if (searchTrack.isLoadingPage) const PlatformCircularProgressIndicator() - else if (searchTrack.hasError) - PlatformText(searchTrack - .error?[searchTrack.pageParams.last] + else if (searchTrack.hasPageError) + PlatformText(searchTrack.errors.lastOrNull ?.toString() ?? "") else @@ -204,10 +179,10 @@ class SearchPage extends HookConsumerWidget { tracks.isNotEmpty) Center( child: PlatformTextButton( - onPressed: searchTrack.isFetchingNextPage + onPressed: searchTrack.isRefreshingPage ? null - : () => searchTrack.fetchNextPage(), - child: searchTrack.isFetchingNextPage + : () => searchTrack.fetchNext(), + child: searchTrack.isRefreshingPage ? const PlatformCircularProgressIndicator() : const PlatformText("Load more"), ), @@ -231,7 +206,7 @@ class SearchPage extends HookConsumerWidget { controller: playlistController, child: Waypoint( onTouchEdge: () { - searchPlaylist.fetchNextPage(); + searchPlaylist.fetchNext(); }, controller: playlistController, child: SingleChildScrollView( @@ -256,13 +231,11 @@ class SearchPage extends HookConsumerWidget { ), ), ), - if (searchPlaylist.isLoading && - !searchPlaylist.isFetchingNextPage) + if (searchPlaylist.isLoadingPage) const PlatformCircularProgressIndicator(), - if (searchPlaylist.hasError) + if (searchPlaylist.hasPageError) PlatformText( - searchPlaylist.error?[ - searchPlaylist.pageParams.last] + searchPlaylist.errors.lastOrNull ?.toString() ?? "", ), @@ -283,7 +256,7 @@ class SearchPage extends HookConsumerWidget { child: Waypoint( controller: artistController, onTouchEdge: () { - searchArtist.fetchNextPage(); + searchArtist.fetchNext(); }, child: SingleChildScrollView( scrollDirection: Axis.horizontal, @@ -311,13 +284,11 @@ class SearchPage extends HookConsumerWidget { ), ), ), - if (searchArtist.isLoading && - !searchArtist.isFetchingNextPage) + if (searchArtist.isLoadingPage) const PlatformCircularProgressIndicator(), - if (searchArtist.hasError) + if (searchArtist.hasPageError) PlatformText( - searchArtist.error?[ - searchArtist.pageParams.last] + searchArtist.errors.lastOrNull ?.toString() ?? "", ), @@ -338,7 +309,7 @@ class SearchPage extends HookConsumerWidget { child: Waypoint( controller: albumController, onTouchEdge: () { - searchAlbum.fetchNextPage(); + searchAlbum.fetchNext(); }, child: SingleChildScrollView( scrollDirection: Axis.horizontal, @@ -364,14 +335,11 @@ class SearchPage extends HookConsumerWidget { ), ), ), - if (searchAlbum.isLoading && - !searchAlbum.isFetchingNextPage) + if (searchAlbum.isLoadingPage) const PlatformCircularProgressIndicator(), - if (searchAlbum.hasError) + if (searchAlbum.hasPageError) PlatformText( - searchAlbum - .error?[searchAlbum.pageParams.last] - ?.toString() ?? + searchAlbum.errors.lastOrNull?.toString() ?? "", ), ], diff --git a/lib/services/queries/search.dart b/lib/services/queries/search.dart index e68546202..7eb3e1392 100644 --- a/lib/services/queries/search.dart +++ b/lib/services/queries/search.dart @@ -11,7 +11,7 @@ class SearchQueries { SearchType searchType, ) { return useSpotifyInfiniteQuery, dynamic, int>( - "search-query/$query", + "search-query/${searchType.key}", (page, spotify) { if (query.trim().isEmpty) return []; final queryString = query; @@ -20,6 +20,7 @@ class SearchQueries { types: [searchType], ).getPage(10, page); }, + enabled: false, ref: ref, initialPage: 0, nextPage: (lastPage, lastPageData) {