From 038856a1f798d83a4345d0678ecd81377c848811 Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Thu, 16 Jun 2022 22:59:21 +0600 Subject: [PATCH] fixed API rate limit exit bug on TrackTile --- lib/components/Player/PlayerActions.dart | 5 + lib/components/Playlist/PlaylistView.dart | 1 - lib/components/Shared/TrackTile.dart | 150 +++++++++++----------- lib/provider/SpotifyRequests.dart | 7 + 4 files changed, 90 insertions(+), 73 deletions(-) diff --git a/lib/components/Player/PlayerActions.dart b/lib/components/Player/PlayerActions.dart index 5e793f1f..abe315f3 100644 --- a/lib/components/Player/PlayerActions.dart +++ b/lib/components/Player/PlayerActions.dart @@ -8,6 +8,7 @@ import 'package:spotube/models/Logger.dart'; import 'package:spotube/provider/Auth.dart'; import 'package:spotube/provider/Playback.dart'; import 'package:spotube/provider/SpotifyDI.dart'; +import 'package:spotube/provider/SpotifyRequests.dart'; class PlayerActions extends HookConsumerWidget { final MainAxisAlignment mainAxisAlignment; @@ -51,6 +52,10 @@ class PlayerActions extends HookConsumerWidget { logger.e("FavoriteButton.onPressed", e, stack); } finally { update(); + ref.refresh(currentUserSavedTracksQuery); + ref.refresh( + playlistTracksQuery("user-liked-tracks"), + ); } }); }), diff --git a/lib/components/Playlist/PlaylistView.dart b/lib/components/Playlist/PlaylistView.dart index b6701d44..203f1948 100644 --- a/lib/components/Playlist/PlaylistView.dart +++ b/lib/components/Playlist/PlaylistView.dart @@ -2,7 +2,6 @@ import 'dart:convert'; import 'package:flutter/services.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; -import 'package:go_router/go_router.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:spotube/components/Shared/HeartButton.dart'; import 'package:spotube/components/Shared/TrackCollectionView.dart'; diff --git a/lib/components/Shared/TrackTile.dart b/lib/components/Shared/TrackTile.dart index ddd968b3..789985d0 100644 --- a/lib/components/Shared/TrackTile.dart +++ b/lib/components/Shared/TrackTile.dart @@ -12,6 +12,7 @@ import 'package:spotube/models/Logger.dart'; import 'package:spotube/provider/Auth.dart'; import 'package:spotube/provider/Playback.dart'; import 'package:spotube/provider/SpotifyDI.dart'; +import 'package:spotube/provider/SpotifyRequests.dart'; class TrackTile extends HookConsumerWidget { final Playback playback; @@ -41,6 +42,13 @@ class TrackTile extends HookConsumerWidget { final spotify = ref.watch(spotifyProvider); final update = useForceUpdate(); + final savedTracksSnapshot = ref.watch(currentUserSavedTracksQuery); + + final isSaved = savedTracksSnapshot.asData?.value.any( + (e) => track.value.id! == e.id, + ) ?? + false; + final actionFavorite = useCallback((bool isLiked) async { try { isLiked @@ -50,6 +58,8 @@ class TrackTile extends HookConsumerWidget { logger.e("FavoriteButton.onPressed", e, stack); } finally { update(); + ref.refresh(currentUserSavedTracksQuery); + ref.refresh(playlistTracksQuery("user-liked-tracks")); } }, [track.value.id, spotify]); @@ -229,78 +239,74 @@ class TrackTile extends HookConsumerWidget { Text(duration), ], const SizedBox(width: 10), - FutureBuilder( - future: spotify.tracks.me.containsOne(track.value.id!), - builder: (context, snapshot) { - return PopupMenuButton( - icon: const Icon(Icons.more_horiz_rounded), - itemBuilder: (context) { - return [ - if (auth.isLoggedIn) - PopupMenuItem( - child: Row( - children: const [ - Icon(Icons.add_box_rounded), - SizedBox(width: 10), - Text("Add to Playlist"), - ], - ), - value: "add-playlist", - ), - if (userPlaylist && auth.isLoggedIn) - PopupMenuItem( - child: Row( - children: const [ - Icon(Icons.remove_circle_outline_rounded), - SizedBox(width: 10), - Text("Remove from Playlist"), - ], - ), - value: "remove-playlist", - ), - if (auth.isLoggedIn) - PopupMenuItem( - child: Row( - children: [ - Icon(snapshot.data == true - ? Icons.favorite_rounded - : Icons.favorite_border_rounded), - const SizedBox(width: 10), - const Text("Favorite") - ], - ), - value: "favorite", - ), - PopupMenuItem( - child: Row( - children: const [ - Icon(Icons.share_rounded), - SizedBox(width: 10), - Text("Share") - ], - ), - value: "share", - ) - ]; - }, - onSelected: (value) { - switch (value) { - case "favorite": - actionFavorite(snapshot.data == true); - break; - case "add-playlist": - actionAddToPlaylist(); - break; - case "remove-playlist": - actionRemoveFromPlaylist(); - break; - case "share": - actionShare(track.value); - break; - } - }, - ); - }) + PopupMenuButton( + icon: const Icon(Icons.more_horiz_rounded), + itemBuilder: (context) { + return [ + if (auth.isLoggedIn) + PopupMenuItem( + child: Row( + children: const [ + Icon(Icons.add_box_rounded), + SizedBox(width: 10), + Text("Add to Playlist"), + ], + ), + value: "add-playlist", + ), + if (userPlaylist && auth.isLoggedIn) + PopupMenuItem( + child: Row( + children: const [ + Icon(Icons.remove_circle_outline_rounded), + SizedBox(width: 10), + Text("Remove from Playlist"), + ], + ), + value: "remove-playlist", + ), + if (auth.isLoggedIn) + PopupMenuItem( + child: Row( + children: [ + Icon(isSaved + ? Icons.favorite_rounded + : Icons.favorite_border_rounded), + const SizedBox(width: 10), + const Text("Favorite") + ], + ), + value: "favorite", + ), + PopupMenuItem( + child: Row( + children: const [ + Icon(Icons.share_rounded), + SizedBox(width: 10), + Text("Share") + ], + ), + value: "share", + ) + ]; + }, + onSelected: (value) { + switch (value) { + case "favorite": + actionFavorite(isSaved); + break; + case "add-playlist": + actionAddToPlaylist(); + break; + case "remove-playlist": + actionRemoveFromPlaylist(); + break; + case "share": + actionShare(track.value); + break; + } + }, + ), ], ); } diff --git a/lib/provider/SpotifyRequests.dart b/lib/provider/SpotifyRequests.dart index ea9d9097..76fe6f5f 100644 --- a/lib/provider/SpotifyRequests.dart +++ b/lib/provider/SpotifyRequests.dart @@ -98,6 +98,13 @@ final artistRelatedArtistsQuery = }, ); +final currentUserSavedTracksQuery = FutureProvider>((ref) { + final spotify = ref.watch(spotifyProvider); + return spotify.tracks.me.saved.all().then( + (tracks) => tracks.map((e) => e.track!).toList(), + ); +}); + final playlistTracksQuery = FutureProvider.family, String>( (ref, id) { final spotify = ref.watch(spotifyProvider);