Skip to content

Commit

Permalink
fix(search): has to submit twice for search results
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Mar 2, 2023
1 parent b78b657 commit f5dc76a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 102 deletions.
8 changes: 4 additions & 4 deletions lib/components/album/album_card.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:fl_query/fl_query.dart';
import 'package:fl_query_hooks/fl_query_hooks.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
Expand Down Expand Up @@ -46,8 +46,8 @@ class AlbumCard extends HookConsumerWidget {
final playing = useStream(PlaylistQueueNotifier.playing).data ??
PlaylistQueueNotifier.isPlaying;
final playlistNotifier = ref.watch(PlaylistQueueNotifier.notifier);
final queryBowl = QueryClient.of(context);
final query = queryBowl
final queryClient = useQueryClient();
final query = queryClient
.getQuery<List<TrackSimple>, dynamic>("album-tracks/${album.id}");
final tracks = useState(query?.data ?? album.tracks ?? <Track>[]);
bool isPlaylistPlaying = playlistNotifier.isPlayingPlaylist(tracks.value);
Expand Down Expand Up @@ -100,7 +100,7 @@ class AlbumCard extends HookConsumerWidget {
updating.value = true;
try {
final fetchedTracks =
await queryBowl.fetchQuery<List<TrackSimple>, SpotifyApi>(
await queryClient.fetchQuery<List<TrackSimple>, SpotifyApi>(
"album-tracks/${album.id}",
() {
return spotify.albums
Expand Down
26 changes: 15 additions & 11 deletions lib/components/library/user_playlists.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,21 @@ class UserPlaylists extends HookConsumerWidget {

final playlistsQuery = useQueries.playlist.ofMine(ref);

Image image = Image();
image.height = 300;
image.width = 300;
PlaylistSimple likedTracksPlaylist = PlaylistSimple();
likedTracksPlaylist.name = "Liked Tracks";
likedTracksPlaylist.type = "playlist";
likedTracksPlaylist.collaborative = false;
likedTracksPlaylist.public = false;
likedTracksPlaylist.id = "user-liked-tracks";
image.url = "https://t.scdn.co/images/3099b3803ad9496896c43f22fe9be8c4.png";
likedTracksPlaylist.images = [image];
final likedTracksPlaylist = useMemoized(
() => PlaylistSimple()
..name = "Liked Tracks"
..type = "playlist"
..collaborative = false
..public = false
..id = "user-liked-tracks"
..images = [
Image()
..height = 300
..width = 300
..url =
"https://t.scdn.co/images/3099b3803ad9496896c43f22fe9be8c4.png"
],
[]);

final playlists = useMemoized(
() {
Expand Down
73 changes: 0 additions & 73 deletions lib/components/playlist/playlist_genre_view.dart

This file was deleted.

2 changes: 1 addition & 1 deletion lib/components/shared/heart_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class PlaylistHeartButton extends HookConsumerWidget {
final isLikedQuery = useQueries.playlist.doesUserFollow(
ref,
playlist.id!,
me.data!.id!,
me.data?.id ?? '',
);

final togglePlaylistLike = useMutations.playlist.toggleFavorite(
Expand Down
26 changes: 15 additions & 11 deletions lib/pages/search/search.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart' hide Page;
import 'package:flutter_hooks/flutter_hooks.dart';
Expand Down Expand Up @@ -50,15 +52,13 @@ class SearchPage extends HookConsumerWidget {
final searchArtist =
useQueries.search.query(ref, searchTerm, SearchType.artist);

void onSearch() {
for (final query in [
searchTrack,
searchAlbum,
searchPlaylist,
searchArtist,
]) {
query.refreshAll();
}
Future<void> onSearch() async {
await Future.wait([
searchTrack.refreshAll(),
searchAlbum.refreshAll(),
searchPlaylist.refreshAll(),
searchArtist.refreshAll(),
]);
}

return SafeArea(
Expand All @@ -82,10 +82,14 @@ class SearchPage extends HookConsumerWidget {
other: null,
).resolve(platform!),
placeholder: "Search...",
onSubmitted: (value) {
onSubmitted: (value) async {
ref.read(searchTermStateProvider.notifier).state =
value;
onSearch();
// Fl-Query is too fast, so we need to delay the search
// to prevent spamming the API :)
Timer(const Duration(milliseconds: 50), () {
onSearch();
});
},
),
),
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ packages:
description:
path: "packages/fl_query"
ref: new-architecture
resolved-ref: f2a23b085cd657a1612d87749f6592b4d67814c5
resolved-ref: cf2550a2909d0cb957324fe114acacb431a5f33a
url: "https://github.com/KRTirtho/fl-query.git"
source: git
version: "0.3.1"
Expand All @@ -544,7 +544,7 @@ packages:
description:
path: "packages/fl_query_hooks"
ref: new-architecture
resolved-ref: f2a23b085cd657a1612d87749f6592b4d67814c5
resolved-ref: cf2550a2909d0cb957324fe114acacb431a5f33a
url: "https://github.com/KRTirtho/fl-query.git"
source: git
version: "0.3.1"
Expand Down

0 comments on commit f5dc76a

Please sign in to comment.