Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Immutable fl query integration #422

Merged
merged 7 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion 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 All @@ -16,7 +17,7 @@ import 'package:spotube/pages/lyrics/lyrics.dart';
import 'package:spotube/pages/player/player.dart';
import 'package:spotube/pages/playlist/playlist.dart';
import 'package:spotube/pages/root/root_app.dart';
import 'package:spotube/pages/search/search.dart';
// import 'package:spotube/pages/search/search.dart';
import 'package:spotube/pages/settings/settings.dart';
import 'package:spotube/pages/mobile_login/mobile_login.dart';

Expand Down
27 changes: 16 additions & 11 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 All @@ -7,10 +7,8 @@ import 'package:spotube/components/shared/playbutton_card.dart';
import 'package:spotube/hooks/use_breakpoint_value.dart';
import 'package:spotube/provider/playlist_queue_provider.dart';
import 'package:spotube/provider/spotify_provider.dart';
import 'package:spotube/services/queries/queries.dart';
import 'package:spotube/utils/service_utils.dart';
import 'package:spotube/utils/type_conversion_utils.dart';
import 'package:uuid/uuid.dart';

enum AlbumType {
album,
Expand Down Expand Up @@ -48,15 +46,18 @@ class AlbumCard extends HookConsumerWidget {
final playing = useStream(PlaylistQueueNotifier.playing).data ??
PlaylistQueueNotifier.isPlaying;
final playlistNotifier = ref.watch(PlaylistQueueNotifier.notifier);
final queryBowl = QueryBowl.of(context);
final query = queryBowl.getQuery<List<TrackSimple>, SpotifyApi>(
Queries.album.tracksOf(album.id!).queryKey);
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);
final int marginH =
useBreakpointValue(sm: 10, md: 15, lg: 20, xl: 20, xxl: 20);

final updating = useState(false);
final spotify = ref.watch(spotifyProvider);

final scaffold = ScaffoldMessenger.of(context);

return PlaybuttonCard(
imageUrl: TypeConversionUtils.image_X_UrlString(
Expand Down Expand Up @@ -99,10 +100,14 @@ class AlbumCard extends HookConsumerWidget {
updating.value = true;
try {
final fetchedTracks =
await queryBowl.fetchQuery<List<TrackSimple>, SpotifyApi>(
Queries.album.tracksOf(album.id!),
externalData: ref.read(spotifyProvider),
key: ValueKey(const Uuid().v4()),
await queryClient.fetchQuery<List<TrackSimple>, SpotifyApi>(
"album-tracks/${album.id}",
() {
return spotify.albums
.getTracks(album.id!)
.all()
.then((value) => value.toList());
},
);

if (fetchedTracks == null || fetchedTracks.isEmpty) return;
Expand All @@ -113,7 +118,7 @@ class AlbumCard extends HookConsumerWidget {
.toList(),
);
tracks.value = fetchedTracks;
ScaffoldMessenger.of(context).showSnackBar(
scaffold.showSnackBar(
SnackBar(
content: Text("Added ${album.tracks?.length} tracks to queue"),
),
Expand Down
15 changes: 4 additions & 11 deletions lib/components/artist/artist_album_list.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 @@ -8,7 +7,6 @@ import 'package:spotube/components/album/album_card.dart';
import 'package:spotube/components/shared/shimmers/shimmer_playbutton_card.dart';
import 'package:spotube/components/shared/waypoint.dart';
import 'package:spotube/models/logger.dart';
import 'package:spotube/provider/spotify_provider.dart';
import 'package:spotube/services/queries/queries.dart';

class ArtistAlbumList extends HookConsumerWidget {
Expand All @@ -23,20 +21,17 @@ class ArtistAlbumList extends HookConsumerWidget {
@override
Widget build(BuildContext context, ref) {
final scrollController = useScrollController();
final albumsQuery = useInfiniteQuery(
job: Queries.artist.albumsOf(artistId),
externalData: ref.watch(spotifyProvider),
);
final albumsQuery = useQueries.artist.albumsOf(ref, artistId);

final albums = useMemoized(() {
return albumsQuery.pages
.expand<Album>((page) => page?.items ?? const Iterable.empty())
.expand<Album>((page) => page.items ?? const Iterable.empty())
.toList();
}, [albumsQuery.pages]);

final hasNextPage = albumsQuery.pages.isEmpty
? false
: (albumsQuery.pages.last?.items?.length ?? 0) == 5;
: (albumsQuery.pages.last.items?.length ?? 0) == 5;

return SizedBox(
height: 300,
Expand All @@ -52,9 +47,7 @@ class ArtistAlbumList extends HookConsumerWidget {
controller: scrollController,
child: Waypoint(
controller: scrollController,
onTouchEdge: () {
albumsQuery.fetchNextPage();
},
onTouchEdge: albumsQuery.fetchNext,
child: ListView.builder(
itemCount: albums.length,
controller: scrollController,
Expand Down
20 changes: 7 additions & 13 deletions lib/components/genre/category_card.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 @@ -9,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 @@ -24,18 +22,14 @@ class CategoryCard extends HookConsumerWidget {
@override
Widget build(BuildContext context, ref) {
final scrollController = useScrollController();
final spotify = ref.watch(spotifyProvider);
final playlistQuery = useInfiniteQuery(
job: Queries.category.playlistsOf(category.id!),
externalData: spotify,
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(
(page) => page?.items ?? const Iterable.empty(),
(page) => page.items ?? const Iterable.empty(),
)
.toList();

Expand All @@ -49,7 +43,7 @@ class CategoryCard extends HookConsumerWidget {
],
),
),
playlistQuery.hasError
playlistQuery.hasPageError && !playlistQuery.hasPageData
? PlatformText(
"Something Went Wrong\n${playlistQuery.errors.first}")
: SizedBox(
Expand All @@ -67,7 +61,7 @@ class CategoryCard extends HookConsumerWidget {
child: Waypoint(
controller: scrollController,
onTouchEdge: () {
playlistQuery.fetchNextPage();
playlistQuery.fetchNext();
},
child: ListView(
scrollDirection: Axis.horizontal,
Expand All @@ -76,7 +70,7 @@ class CategoryCard extends HookConsumerWidget {
children: [
...playlists
.map((playlist) => PlaylistCard(playlist)),
if (hasNextPage)
if (playlistQuery.hasNextPage)
const ShimmerPlaybuttonCard(count: 1),
],
),
Expand Down
9 changes: 2 additions & 7 deletions lib/components/library/user_albums.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/material.dart' hide Image;
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:collection/collection.dart';
Expand All @@ -12,7 +11,6 @@ import 'package:spotube/components/shared/shimmers/shimmer_playbutton_card.dart'
import 'package:spotube/components/shared/fallbacks/anonymous_fallback.dart';
import 'package:spotube/hooks/use_breakpoint_value.dart';
import 'package:spotube/provider/authentication_provider.dart';
import 'package:spotube/provider/spotify_provider.dart';
import 'package:spotube/services/queries/queries.dart';

import 'package:spotube/utils/type_conversion_utils.dart';
Expand All @@ -24,10 +22,7 @@ class UserAlbums extends HookConsumerWidget {
@override
Widget build(BuildContext context, ref) {
final auth = ref.watch(AuthenticationNotifier.provider);
final albumsQuery = useQuery(
job: Queries.album.ofMine,
externalData: ref.watch(spotifyProvider),
);
final albumsQuery = useQueries.album.ofMine(ref);

final spacing = useBreakpointValue<double>(
sm: 0,
Expand Down Expand Up @@ -64,7 +59,7 @@ class UserAlbums extends HookConsumerWidget {

return RefreshIndicator(
onRefresh: () async {
await albumsQuery.refetch();
await albumsQuery.refresh();
},
child: SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
Expand Down
15 changes: 5 additions & 10 deletions lib/components/library/user_artists.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/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:collection/collection.dart';
Expand All @@ -11,7 +10,6 @@ import 'package:spotube/components/shared/fallbacks/anonymous_fallback.dart';
import 'package:spotube/components/shared/waypoint.dart';
import 'package:spotube/components/artist/artist_card.dart';
import 'package:spotube/provider/authentication_provider.dart';
import 'package:spotube/provider/spotify_provider.dart';
import 'package:spotube/services/queries/queries.dart';
import 'package:tuple/tuple.dart';

Expand All @@ -22,20 +20,17 @@ class UserArtists extends HookConsumerWidget {
Widget build(BuildContext context, ref) {
final auth = ref.watch(AuthenticationNotifier.provider);

final artistQuery = useInfiniteQuery(
job: Queries.artist.followedByMe,
externalData: ref.watch(spotifyProvider),
);
final artistQuery = useQueries.artist.followedByMe(ref);

final hasNextPage = artistQuery.pages.isEmpty
? false
: (artistQuery.pages.last?.items?.length ?? 0) == 15;
: (artistQuery.pages.last.items?.length ?? 0) == 15;

final searchText = useState('');

final filteredArtists = useMemoized(() {
final artists = artistQuery.pages
.expand<Artist>((page) => page?.items ?? const Iterable.empty());
.expand<Artist>((page) => page.items ?? const Iterable.empty());

if (searchText.value.isEmpty) {
return artists.toList();
Expand Down Expand Up @@ -85,7 +80,7 @@ class UserArtists extends HookConsumerWidget {
)
: RefreshIndicator(
onRefresh: () async {
await artistQuery.refetchPages();
await artistQuery.refreshAll();
},
child: GridView.builder(
itemCount: filteredArtists.length,
Expand All @@ -104,7 +99,7 @@ class UserArtists extends HookConsumerWidget {
controller: useScrollController(),
isGrid: true,
onTouchEdge: () {
artistQuery.fetchNextPage();
artistQuery.fetchNext();
},
child: ArtistCard(filteredArtists[index]),
);
Expand Down
35 changes: 17 additions & 18 deletions lib/components/library/user_playlists.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/material.dart' hide Image;
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:fuzzywuzzy/fuzzywuzzy.dart';
Expand All @@ -15,7 +14,6 @@ import 'package:spotube/components/playlist/playlist_card.dart';
import 'package:spotube/hooks/use_breakpoint_value.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/services/queries/queries.dart';
import 'package:tuple/tuple.dart';

Expand All @@ -35,22 +33,23 @@ class UserPlaylists extends HookConsumerWidget {
: PlaybuttonCardViewType.square;
final auth = ref.watch(AuthenticationNotifier.provider);

final playlistsQuery = useQuery(
job: Queries.playlist.ofMine,
externalData: ref.watch(spotifyProvider),
);
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 Expand Up @@ -90,7 +89,7 @@ class UserPlaylists extends HookConsumerWidget {
.toList(),
];
return RefreshIndicator(
onRefresh: () => playlistsQuery.refetch(),
onRefresh: playlistsQuery.refresh,
child: SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
child: Material(
Expand Down
Loading