Skip to content

Commit

Permalink
fix: categories not showing for oauth exception, maximized window siz…
Browse files Browse the repository at this point in the history
…e is stored

feat: UserLibrary and UserAlbums using fl_query
  • Loading branch information
KRTirtho committed Oct 10, 2022
1 parent 3e498a4 commit 4df917e
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 65 deletions.
5 changes: 4 additions & 1 deletion lib/components/Album/AlbumView.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -120,7 +121,9 @@ class AlbumView extends HookConsumerWidget {
album.id!,
),
);
ref.refresh(currentUserAlbumsQuery);
QueryBowl.of(context).refetchQueries(
[currentUserAlbumsQueryJob.queryKey],
);
});
},
);
Expand Down
41 changes: 23 additions & 18 deletions lib/components/Library/UserAlbums.dart
Original file line number Diff line number Diff line change
@@ -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"),
);
}
}
75 changes: 40 additions & 35 deletions lib/components/Library/UserPlaylists.dart
Original file line number Diff line number Diff line change
@@ -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(),
],
),
),
);
}
}
5 changes: 4 additions & 1 deletion lib/components/Playlist/PlaylistCreateDialog.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -51,7 +52,9 @@ class PlaylistCreateDialog extends HookConsumerWidget {
description: description.text,
)
.then((_) {
ref.refresh(currentUserPlaylistsQuery);
QueryBowl.of(context).refetchQueries([
currentUserPlaylistsQueryJob.queryKey,
]);
Navigator.pop(context);
});
},
Expand Down
5 changes: 4 additions & 1 deletion lib/components/Playlist/PlaylistView.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
]);
}
},
);
Expand Down
1 change: 1 addition & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class _SpotubeState extends ConsumerState<Spotube> with WidgetsBindingObserver {
prevSize?.height == appWindow.size.height;

if (localStorage == null || windowSameDimension || kIsMobile) return;
if (appWindow.isMaximized) return;
localStorage!.setString(
LocalStorageKeys.windowSizeInfo,
jsonEncode({
Expand Down
8 changes: 5 additions & 3 deletions lib/provider/Auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,18 @@ class Auth extends PersistedChangeNotifier {
}

@override
FutureOr<void> loadFromLocal(Map<String, dynamic> map) {
FutureOr<void> loadFromLocal(Map<String, dynamic> 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
Expand Down
13 changes: 7 additions & 6 deletions lib/provider/SpotifyRequests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@ final categoryPlaylistsQueryJob =
},
);

final currentUserPlaylistsQuery = FutureProvider<Iterable<PlaylistSimple>>(
(ref) {
final spotify = ref.watch(spotifyProvider);
final currentUserPlaylistsQueryJob =
QueryJob<Iterable<PlaylistSimple>, SpotifyApi>(
queryKey: "current-user-query",
task: (_, spotify) {
return spotify.playlists.me.all();
},
);

final currentUserAlbumsQuery = FutureProvider<Iterable<AlbumSimple>>(
(ref) {
final spotify = ref.watch(spotifyProvider);
final currentUserAlbumsQueryJob = QueryJob<Iterable<AlbumSimple>, SpotifyApi>(
queryKey: "current-user-albums",
task: (_, spotify) {
return spotify.me.savedAlbums().all();
},
);
Expand Down

0 comments on commit 4df917e

Please sign in to comment.