From 4df917e65ee20cbcf42394cc141b1cdcdd6cc914 Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Mon, 10 Oct 2022 22:54:10 +0600 Subject: [PATCH] fix: categories not showing for oauth exception, maximized window size is stored feat: UserLibrary and UserAlbums using fl_query --- lib/components/Album/AlbumView.dart | 5 +- lib/components/Library/UserAlbums.dart | 41 +++++----- lib/components/Library/UserPlaylists.dart | 75 ++++++++++--------- .../Playlist/PlaylistCreateDialog.dart | 5 +- lib/components/Playlist/PlaylistView.dart | 5 +- lib/main.dart | 1 + lib/provider/Auth.dart | 8 +- lib/provider/SpotifyRequests.dart | 13 ++-- 8 files changed, 88 insertions(+), 65 deletions(-) diff --git a/lib/components/Album/AlbumView.dart b/lib/components/Album/AlbumView.dart index adb6f1884..d545e8906 100644 --- a/lib/components/Album/AlbumView.dart +++ b/lib/components/Album/AlbumView.dart @@ -1,3 +1,4 @@ +import 'package:fl_query/fl_query.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; @@ -120,7 +121,9 @@ class AlbumView extends HookConsumerWidget { album.id!, ), ); - ref.refresh(currentUserAlbumsQuery); + QueryBowl.of(context).refetchQueries( + [currentUserAlbumsQueryJob.queryKey], + ); }); }, ); diff --git a/lib/components/Library/UserAlbums.dart b/lib/components/Library/UserAlbums.dart index 7055a596a..520afa0b3 100644 --- a/lib/components/Library/UserAlbums.dart +++ b/lib/components/Library/UserAlbums.dart @@ -1,34 +1,39 @@ +import 'package:fl_query_hooks/fl_query_hooks.dart'; import 'package:flutter/material.dart' hide Image; -import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:spotube/components/Album/AlbumCard.dart'; import 'package:spotube/components/LoaderShimmers/ShimmerPlaybuttonCard.dart'; +import 'package:spotube/provider/SpotifyDI.dart'; import 'package:spotube/provider/SpotifyRequests.dart'; import 'package:spotube/utils/type_conversion_utils.dart'; -class UserAlbums extends ConsumerWidget { +class UserAlbums extends HookConsumerWidget { const UserAlbums({Key? key}) : super(key: key); @override Widget build(BuildContext context, ref) { - final albums = ref.watch(currentUserAlbumsQuery); + final albumsQuery = useQuery( + job: currentUserAlbumsQueryJob, + externalData: ref.watch(spotifyProvider), + ); + + if (albumsQuery.isLoading || !albumsQuery.hasData) { + return const Center(child: ShimmerPlaybuttonCard(count: 7)); + } - return albums.when( - data: (data) => SingleChildScrollView( - child: Padding( - padding: const EdgeInsets.all(8.0), - child: Wrap( - spacing: 20, // gap between adjacent chips - runSpacing: 20, // gap between lines - alignment: WrapAlignment.center, - children: data - .map((album) => - AlbumCard(TypeConversionUtils.simpleAlbum_X_Album(album))) - .toList(), - ), + return SingleChildScrollView( + child: Padding( + padding: const EdgeInsets.all(8.0), + child: Wrap( + spacing: 20, // gap between adjacent chips + runSpacing: 20, // gap between lines + alignment: WrapAlignment.center, + children: albumsQuery.data! + .map((album) => + AlbumCard(TypeConversionUtils.simpleAlbum_X_Album(album))) + .toList(), ), ), - loading: () => const Center(child: ShimmerPlaybuttonCard(count: 7)), - error: (_, __) => const Text("Failure is the pillar of success"), ); } } diff --git a/lib/components/Library/UserPlaylists.dart b/lib/components/Library/UserPlaylists.dart index 49b1dca14..26d2bf91d 100644 --- a/lib/components/Library/UserPlaylists.dart +++ b/lib/components/Library/UserPlaylists.dart @@ -1,49 +1,54 @@ +import 'package:fl_query_hooks/fl_query_hooks.dart'; import 'package:flutter/material.dart' hide Image; -import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:spotify/spotify.dart'; import 'package:spotube/components/LoaderShimmers/ShimmerPlaybuttonCard.dart'; import 'package:spotube/components/Playlist/PlaylistCard.dart'; import 'package:spotube/components/Playlist/PlaylistCreateDialog.dart'; +import 'package:spotube/provider/SpotifyDI.dart'; import 'package:spotube/provider/SpotifyRequests.dart'; -class UserPlaylists extends ConsumerWidget { +class UserPlaylists extends HookConsumerWidget { const UserPlaylists({Key? key}) : super(key: key); @override Widget build(BuildContext context, ref) { - final playlists = ref.watch(currentUserPlaylistsQuery); + final playlistsQuery = useQuery( + job: currentUserPlaylistsQueryJob, + externalData: ref.watch(spotifyProvider), + ); + 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]; - return playlists.when( - loading: () => const Center(child: ShimmerPlaybuttonCard(count: 7)), - data: (data) { - 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]; - return SingleChildScrollView( - child: Padding( - padding: const EdgeInsets.all(8.0), - child: Wrap( - spacing: 20, // gap between adjacent chips - runSpacing: 20, // gap between lines - alignment: WrapAlignment.center, - children: [ - const PlaylistCreateDialog(), - PlaylistCard(likedTracksPlaylist), - ...data.map((playlist) => PlaylistCard(playlist)).toList(), - ], - ), - ), - ); - }, - error: (_, __) => const Text("Failure is the pillar of success")); + if (playlistsQuery.isLoading || !playlistsQuery.hasData) { + return const Center(child: ShimmerPlaybuttonCard(count: 7)); + } + + return SingleChildScrollView( + child: Padding( + padding: const EdgeInsets.all(8.0), + child: Wrap( + spacing: 20, // gap between adjacent chips + runSpacing: 20, // gap between lines + alignment: WrapAlignment.center, + children: [ + const PlaylistCreateDialog(), + PlaylistCard(likedTracksPlaylist), + ...playlistsQuery.data! + .map((playlist) => PlaylistCard(playlist)) + .toList(), + ], + ), + ), + ); } } diff --git a/lib/components/Playlist/PlaylistCreateDialog.dart b/lib/components/Playlist/PlaylistCreateDialog.dart index e55581eaf..2451e58d8 100644 --- a/lib/components/Playlist/PlaylistCreateDialog.dart +++ b/lib/components/Playlist/PlaylistCreateDialog.dart @@ -1,3 +1,4 @@ +import 'package:fl_query/fl_query.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; @@ -51,7 +52,9 @@ class PlaylistCreateDialog extends HookConsumerWidget { description: description.text, ) .then((_) { - ref.refresh(currentUserPlaylistsQuery); + QueryBowl.of(context).refetchQueries([ + currentUserPlaylistsQueryJob.queryKey, + ]); Navigator.pop(context); }); }, diff --git a/lib/components/Playlist/PlaylistView.dart b/lib/components/Playlist/PlaylistView.dart index 868c8fa3b..07dd593fc 100644 --- a/lib/components/Playlist/PlaylistView.dart +++ b/lib/components/Playlist/PlaylistView.dart @@ -1,5 +1,6 @@ import 'dart:convert'; +import 'package:fl_query/fl_query.dart'; import 'package:flutter/services.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; @@ -146,7 +147,9 @@ class PlaylistView extends HookConsumerWidget { logger.e("FollowButton.onPressed", e, stack); } finally { ref.refresh(query); - ref.refresh(currentUserPlaylistsQuery); + QueryBowl.of(context).refetchQueries([ + currentUserPlaylistsQueryJob.queryKey, + ]); } }, ); diff --git a/lib/main.dart b/lib/main.dart index ebc4312b6..a0d10ef1b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -170,6 +170,7 @@ class _SpotubeState extends ConsumerState with WidgetsBindingObserver { prevSize?.height == appWindow.size.height; if (localStorage == null || windowSameDimension || kIsMobile) return; + if (appWindow.isMaximized) return; localStorage!.setString( LocalStorageKeys.windowSizeInfo, jsonEncode({ diff --git a/lib/provider/Auth.dart b/lib/provider/Auth.dart index c286b97cb..36dce5aa1 100644 --- a/lib/provider/Auth.dart +++ b/lib/provider/Auth.dart @@ -95,16 +95,18 @@ class Auth extends PersistedChangeNotifier { } @override - FutureOr loadFromLocal(Map map) { + FutureOr loadFromLocal(Map map) async { _accessToken = map["accessToken"]; _expiration = map["expiration"] != null ? DateTime.tryParse(map["expiration"]) : _expiration; _authCookie = map["authCookie"]; - _restartRefresher(); if (isExpired) { - refresh(); + final data = await ServiceUtils.getAccessToken(authCookie!); + _accessToken = data.accessToken; + _expiration = data.expiration; } + _restartRefresher(); } @override diff --git a/lib/provider/SpotifyRequests.dart b/lib/provider/SpotifyRequests.dart index c458966d4..5cf2f0fb3 100644 --- a/lib/provider/SpotifyRequests.dart +++ b/lib/provider/SpotifyRequests.dart @@ -45,16 +45,17 @@ final categoryPlaylistsQueryJob = }, ); -final currentUserPlaylistsQuery = FutureProvider>( - (ref) { - final spotify = ref.watch(spotifyProvider); +final currentUserPlaylistsQueryJob = + QueryJob, SpotifyApi>( + queryKey: "current-user-query", + task: (_, spotify) { return spotify.playlists.me.all(); }, ); -final currentUserAlbumsQuery = FutureProvider>( - (ref) { - final spotify = ref.watch(spotifyProvider); +final currentUserAlbumsQueryJob = QueryJob, SpotifyApi>( + queryKey: "current-user-albums", + task: (_, spotify) { return spotify.me.savedAlbums().all(); }, );