Skip to content

Commit

Permalink
fix(playbutton_card): play and non play state correction
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Mar 7, 2023
1 parent 7803a48 commit b327ffb
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
14 changes: 6 additions & 8 deletions lib/components/album/album_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ 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';
import 'package:platform_ui/platform_ui.dart';
import 'package:spotify/spotify.dart';
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/utils/service_utils.dart';
import 'package:spotube/utils/type_conversion_utils.dart';
import 'package:fluent_ui/fluent_ui.dart' as fluent;

enum AlbumType {
album,
Expand Down Expand Up @@ -51,24 +49,25 @@ class AlbumCard extends HookConsumerWidget {
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);
bool isPlaylistPlaying = useMemoized(
() =>
playlistNotifier.isPlayingPlaylist(query?.data ?? album.tracks ?? []),
[playlistNotifier, query?.data, album.tracks],
);
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(
album.images,
placeholder: ImagePlaceholder.collection,
),
viewType: viewType,
margin: EdgeInsets.symmetric(horizontal: marginH.toDouble()),
isPlaying: isPlaylistPlaying && playing,
isPlaying: isPlaylistPlaying,
isLoading: isPlaylistPlaying && playlist?.isLoading == true,
title: album.name!,
description:
Expand Down Expand Up @@ -121,7 +120,6 @@ class AlbumCard extends HookConsumerWidget {
playlistNotifier.add(
fetchedTracks,
);
tracks.value = fetchedTracks;
if (context.mounted) {
final snackbar = SnackBar(
content: Text("Added ${album.tracks?.length} tracks to queue"),
Expand Down
12 changes: 8 additions & 4 deletions lib/components/playlist/playlist_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ class PlaylistCard extends HookConsumerWidget {
final query = queryBowl.getQuery<List<Track>, dynamic>(
"playlist-tracks/${playlist.id}",
);
final tracks = useState(query?.data ?? []);
bool isPlaylistPlaying = playlistNotifier.isPlayingPlaylist(tracks.value);
final tracks = useState<List<TrackSimple>?>(null);
bool isPlaylistPlaying = useMemoized(
() =>
playlistNotifier.isPlayingPlaylist(tracks.value ?? query?.data ?? []),
[playlistNotifier, tracks.value, query?.data],
);

final int marginH =
useBreakpointValue(sm: 10, md: 15, lg: 20, xl: 20, xxl: 20);
Expand All @@ -46,7 +50,7 @@ class PlaylistCard extends HookConsumerWidget {
playlist.images,
placeholder: ImagePlaceholder.collection,
),
isPlaying: isPlaylistPlaying && playing,
isPlaying: isPlaylistPlaying,
isLoading: (isPlaylistPlaying && playlistQueue?.isLoading == true) ||
updating.value,
onTap: () {
Expand Down Expand Up @@ -95,7 +99,7 @@ class PlaylistCard extends HookConsumerWidget {
tracks.value = fetchedTracks;
if (context.mounted) {
final snackbar = SnackBar(
content: Text("Added ${tracks.value.length} tracks to queue"),
content: Text("Added ${tracks.value?.length} tracks to queue"),
action: SnackBarAction(
label: "Undo",
onPressed: () {
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/album/album.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class AlbumPage extends HookConsumerWidget {

final isAlbumPlaying = useMemoized(
() => playback.isPlayingPlaylist(tracksSnapshot.data ?? []),
[tracksSnapshot.data],
[playback, tracksSnapshot.data],
);
return TrackCollectionView(
id: album.id!,
Expand Down
7 changes: 5 additions & 2 deletions lib/pages/playlist/playlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ class PlaylistView extends HookConsumerWidget {

@override
Widget build(BuildContext context, ref) {
ref.watch(PlaylistQueueNotifier.provider);
final playlistNotifier = ref.watch(PlaylistQueueNotifier.notifier);

final breakpoint = useBreakpoints();

final meSnapshot = useQueries.user.me(ref);
final tracksSnapshot = useQueries.playlist.tracksOfQuery(ref, playlist.id!);

final isPlaylistPlaying =
playlistNotifier.isPlayingPlaylist(tracksSnapshot.data ?? []);
final isPlaylistPlaying = useMemoized(
() => playlistNotifier.isPlayingPlaylist(tracksSnapshot.data ?? []),
[playlistNotifier, tracksSnapshot.data],
);

final titleImage = useMemoized(
() => TypeConversionUtils.image_X_UrlString(
Expand Down
10 changes: 3 additions & 7 deletions lib/provider/playlist_queue_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,9 @@ class PlaylistQueueNotifier extends PersistedStateNotifier<PlaylistQueue?> {
// utility
bool isPlayingPlaylist(Iterable<TrackSimple> playlist) {
if (!isLoaded || playlist.isEmpty) return false;
if (state!.isShuffled) {
final trackIds = state!.tempTracks.map((track) => track.id!);
return blacklist
.filter(playlist)
.every((track) => trackIds.contains(track.id!));
}
final trackIds = state!.tracks.map((track) => track.id!);

final trackIds = (state!.isShuffled ? state!.tempTracks : state!.tracks)
.map((track) => track.id!);
return blacklist
.filter(playlist)
.every((track) => trackIds.contains(track.id!));
Expand Down

0 comments on commit b327ffb

Please sign in to comment.