Skip to content

Commit

Permalink
chore: enable search again
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Feb 26, 2023
1 parent 7617439 commit b78b657
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 70 deletions.
13 changes: 7 additions & 6 deletions lib/collections/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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",
Expand Down
94 changes: 31 additions & 63 deletions lib/pages/search/search.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<String>((ref) => "");
Expand All @@ -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 [
Expand All @@ -75,10 +57,7 @@ class SearchPage extends HookConsumerWidget {
searchPlaylist,
searchArtist,
]) {
query.enabled = false;
query.fetched = false;
query.setExternalData(getVariables());
query.refetchPages();
query.refreshAll();
}
}

Expand All @@ -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:
Expand All @@ -108,6 +83,8 @@ class SearchPage extends HookConsumerWidget {
).resolve(platform!),
placeholder: "Search...",
onSubmitted: (value) {
ref.read(searchTermStateProvider.notifier).state =
value;
onSearch();
},
),
Expand All @@ -127,7 +104,7 @@ class SearchPage extends HookConsumerWidget {
...searchAlbum.pages,
...searchPlaylist.pages,
...searchArtist.pages,
].expand<Page>((page) => page ?? []).toList();
].expand<Page>((page) => page).toList();
for (MapEntry<int, Page> page in pages.asMap().entries) {
for (var item in page.value.items ?? []) {
if (item is AlbumSimple) {
Expand All @@ -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
Expand Down Expand Up @@ -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"),
),
Expand All @@ -231,7 +206,7 @@ class SearchPage extends HookConsumerWidget {
controller: playlistController,
child: Waypoint(
onTouchEdge: () {
searchPlaylist.fetchNextPage();
searchPlaylist.fetchNext();
},
controller: playlistController,
child: SingleChildScrollView(
Expand All @@ -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() ??
"",
),
Expand All @@ -283,7 +256,7 @@ class SearchPage extends HookConsumerWidget {
child: Waypoint(
controller: artistController,
onTouchEdge: () {
searchArtist.fetchNextPage();
searchArtist.fetchNext();
},
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
Expand Down Expand Up @@ -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() ??
"",
),
Expand All @@ -338,7 +309,7 @@ class SearchPage extends HookConsumerWidget {
child: Waypoint(
controller: albumController,
onTouchEdge: () {
searchAlbum.fetchNextPage();
searchAlbum.fetchNext();
},
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
Expand All @@ -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() ??
"",
),
],
Expand Down
3 changes: 2 additions & 1 deletion lib/services/queries/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class SearchQueries {
SearchType searchType,
) {
return useSpotifyInfiniteQuery<List<Page>, dynamic, int>(
"search-query/$query",
"search-query/${searchType.key}",
(page, spotify) {
if (query.trim().isEmpty) return [];
final queryString = query;
Expand All @@ -20,6 +20,7 @@ class SearchQueries {
types: [searchType],
).getPage(10, page);
},
enabled: false,
ref: ref,
initialPage: 0,
nextPage: (lastPage, lastPageData) {
Expand Down

0 comments on commit b78b657

Please sign in to comment.