Skip to content

Commit

Permalink
feat: compatibility with fl-query nextPage method change
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Feb 26, 2023
1 parent 3d9da8b commit 7617439
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 62 deletions.
2 changes: 1 addition & 1 deletion lib/components/album/album_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class AlbumCard extends HookConsumerWidget {
final playlistNotifier = ref.watch(PlaylistQueueNotifier.notifier);
final queryBowl = QueryClient.of(context);
final query = queryBowl
.getQuery<List<TrackSimple>, SpotifyApi>("album-tracks/${album.id}");
.getQuery<List<TrackSimple>, dynamic>("album-tracks/${album.id}");
final tracks = useState(query?.data ?? album.tracks ?? <Track>[]);
bool isPlaylistPlaying = playlistNotifier.isPlayingPlaylist(tracks.value);
final int marginH =
Expand Down
9 changes: 2 additions & 7 deletions lib/components/genre/category_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand All @@ -48,7 +43,7 @@ class CategoryCard extends HookConsumerWidget {
],
),
),
playlistQuery.hasErrors
playlistQuery.hasPageError && !playlistQuery.hasPageData
? PlatformText(
"Something Went Wrong\n${playlistQuery.errors.first}")
: SizedBox(
Expand All @@ -75,7 +70,7 @@ class CategoryCard extends HookConsumerWidget {
children: [
...playlists
.map((playlist) => PlaylistCard(playlist)),
if (hasNextPage)
if (playlistQuery.hasNextPage)
const ShimmerPlaybuttonCard(count: 1),
],
),
Expand Down
2 changes: 1 addition & 1 deletion lib/components/playlist/playlist_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Track>, SpotifyApi>(
final query = queryBowl.getQuery<List<Track>, dynamic>(
"playlist-tracks/${playlist.id}",
);
final tracks = useState(query?.data ?? []);
Expand Down
11 changes: 0 additions & 11 deletions lib/components/root/sidebar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 9 additions & 3 deletions lib/hooks/use_spotify_infinite_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ InfiniteQuery<DataType, ErrorType, PageType>
);

useEffect(() {
query.refreshAll();
return null;
}, [spotify]);
return ref.listenManual(
spotifyProvider,
(previous, next) {
if (previous != next) {
query.refreshAll();
}
},
).close;
}, [query]);

return query;
}
12 changes: 9 additions & 3 deletions lib/hooks/use_spotify_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ Query<DataType, ErrorType> useSpotifyQuery<DataType, ErrorType>(
);

useEffect(() {
query.refresh();
return null;
}, [spotify]);
return ref.listenManual(
spotifyProvider,
(previous, next) {
if (previous != next) {
query.refresh();
}
},
).close;
}, [query]);

return query;
}
13 changes: 5 additions & 8 deletions lib/services/queries/album.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
},
);
}
Expand Down
18 changes: 9 additions & 9 deletions lib/services/queries/artist.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
);
}
Expand Down Expand Up @@ -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,
);
Expand Down
20 changes: 11 additions & 9 deletions lib/services/queries/category.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -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,
);
}
Expand Down
10 changes: 6 additions & 4 deletions lib/services/queries/playlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
Expand Down
12 changes: 8 additions & 4 deletions lib/services/queries/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
);
}
}
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: d964216ee17e600f79c33f1811080877c8c1b510
resolved-ref: f2a23b085cd657a1612d87749f6592b4d67814c5
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: d964216ee17e600f79c33f1811080877c8c1b510
resolved-ref: f2a23b085cd657a1612d87749f6592b4d67814c5
url: "https://github.com/KRTirtho/fl-query.git"
source: git
version: "0.3.1"
Expand Down

0 comments on commit 7617439

Please sign in to comment.