Skip to content

Commit

Permalink
fix: use chosen market for new release
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Mar 5, 2023
1 parent 60a5847 commit c6bf9b6
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
10 changes: 10 additions & 0 deletions lib/pages/home/personalized.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:spotube/components/album/album_card.dart';
import 'package:spotube/components/playlist/playlist_card.dart';
import 'package:spotube/components/shared/shimmers/shimmer_playbutton_card.dart';
import 'package:spotube/components/shared/waypoint.dart';
import 'package:spotube/hooks/use_async_effect.dart';
import 'package:spotube/models/logger.dart';
import 'package:spotube/services/queries/queries.dart';
import 'package:spotube/utils/type_conversion_utils.dart';
Expand Down Expand Up @@ -106,6 +107,15 @@ class PersonalizedPage extends HookConsumerWidget {

final newReleases = useQueries.album.newReleases(ref);

useAsyncEffect(() async {
final releases = newReleases.pages.expand(
(page) => page.items ?? const Iterable<AlbumSimple>.empty(),
);
if (releases.isEmpty) {
await newReleases.fetchNext();
}
}, null, [newReleases.pages]);

return ListView(
children: [
PersonalizedItemCard(
Expand Down
17 changes: 9 additions & 8 deletions lib/services/queries/album.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:spotify/spotify.dart';
import 'package:spotube/hooks/use_spotify_infinite_query.dart';
import 'package:spotube/hooks/use_spotify_query.dart';
import 'package:spotube/provider/user_preferences_provider.dart';

class AlbumQueries {
const AlbumQueries();
Expand Down Expand Up @@ -48,17 +49,17 @@ class AlbumQueries {
}

InfiniteQuery<Page<AlbumSimple>, dynamic, int> newReleases(WidgetRef ref) {
final market = ref
.watch(userPreferencesProvider.select((s) => s.recommendationMarket));

return useSpotifyInfiniteQuery<Page<AlbumSimple>, dynamic, int>(
"new-releases",
(pageParam, spotify) async {
try {
final albums = await Pages(
spotify,
'v1/browse/new-releases',
(json) => AlbumSimple.fromJson(json),
'albums',
(json) => AlbumSimple.fromJson(json),
).getPage(5, pageParam);
final albums = await spotify.browse
.getNewReleases(country: market)
.getPage(5, pageParam);

return albums;
} catch (e, stack) {
Catcher.reportCheckedError(e, stack);
Expand All @@ -68,7 +69,7 @@ class AlbumQueries {
ref: ref,
initialPage: 0,
nextPage: (lastPage, lastPageData) {
if (lastPageData.isLast || (lastPageData.items ?? []).length < 5) {
if (lastPageData.isLast) {
return null;
}
return lastPageData.nextOffset;
Expand Down
7 changes: 7 additions & 0 deletions lib/services/queries/category.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:fl_query/fl_query.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:spotify/spotify.dart';
import 'package:spotube/extensions/map.dart';
Expand All @@ -10,6 +11,8 @@ class CategoryQueries {

InfiniteQuery<Page<Category>, dynamic, int> list(
WidgetRef ref, String recommendationMarket) {
final context = useContext();

return useSpotifyInfiniteQuery<Page<Category>, dynamic, int>(
"category-playlists",
(pageParam, spotify) async {
Expand All @@ -35,6 +38,10 @@ class CategoryQueries {
},
),
),
refreshConfig: RefreshConfig.withDefaults(
context,
staleDuration: const Duration(minutes: 30),
),
ref: ref,
);
}
Expand Down
11 changes: 6 additions & 5 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1481,11 +1481,12 @@ packages:
spotify:
dependency: "direct main"
description:
name: spotify
sha256: db51b46216465631f90218581b97ea08b7bf5c93f1c9a7066fbf3499fe32c774
url: "https://pub.dev"
source: hosted
version: "0.8.0"
path: "."
ref: "9e8ef4556942d42d74874de5491253156e7e6f43"
resolved-ref: "9e8ef4556942d42d74874de5491253156e7e6f43"
url: "https://github.com/rinukkusu/spotify-dart"
source: git
version: "0.9.0"
sqflite:
dependency: transitive
description:
Expand Down
5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ dependencies:
scroll_to_index: ^3.0.1
shared_preferences: ^2.0.11
skeleton_text: ^3.0.0
spotify: ^0.8.0
spotify:
git:
url: https://github.com/rinukkusu/spotify-dart
ref: 9e8ef4556942d42d74874de5491253156e7e6f43
tuple: ^2.0.1
url_launcher: ^6.1.7
uuid: ^3.0.7
Expand Down

0 comments on commit c6bf9b6

Please sign in to comment.