From 55852bd15bc709d61fbba8cbea01ceca791d154c Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Thu, 25 Aug 2022 11:22:52 +0600 Subject: [PATCH] fix: album & playlist card, player view and album view play button logic --- lib/components/Album/AlbumCard.dart | 8 ++++++-- lib/components/Album/AlbumView.dart | 25 ++++++++++++++--------- lib/components/Playlist/PlaylistCard.dart | 8 ++++++-- lib/components/Playlist/PlaylistView.dart | 14 ++++++++----- lib/components/Shared/TrackTile.dart | 20 +++++++++--------- lib/provider/UserPreferences.dart | 2 +- 6 files changed, 47 insertions(+), 30 deletions(-) diff --git a/lib/components/Album/AlbumCard.dart b/lib/components/Album/AlbumCard.dart index 041c81c6..c439d609 100644 --- a/lib/components/Album/AlbumCard.dart +++ b/lib/components/Album/AlbumCard.dart @@ -23,7 +23,7 @@ class AlbumCard extends HookConsumerWidget { return PlaybuttonCard( imageUrl: TypeConversionUtils.image_X_UrlString(album.images), margin: EdgeInsets.symmetric(horizontal: marginH.toDouble()), - isPlaying: playback.playlist?.id == album.id, + isPlaying: isPlaylistPlaying && playback.isPlaying, isLoading: playback.status == PlaybackStatus.loading && playback.playlist?.id == album.id, title: album.name!, @@ -34,7 +34,11 @@ class AlbumCard extends HookConsumerWidget { }, onPlaybuttonPressed: () async { SpotifyApi spotify = ref.read(spotifyProvider); - if (isPlaylistPlaying) return; + if (isPlaylistPlaying && playback.isPlaying) { + return playback.pause(); + } else if (isPlaylistPlaying && !playback.isPlaying) { + return playback.resume(); + } List tracks = (await spotify.albums.getTracks(album.id!).all()) .map((track) => TypeConversionUtils.simpleTrack_X_Track(track, album)) diff --git a/lib/components/Album/AlbumView.dart b/lib/components/Album/AlbumView.dart index cd0dff88..701aaa5f 100644 --- a/lib/components/Album/AlbumView.dart +++ b/lib/components/Album/AlbumView.dart @@ -55,10 +55,11 @@ class AlbumView extends HookConsumerWidget { final breakpoint = useBreakpoints(); + final isAlbumPlaying = + playback.playlist?.id != null && playback.playlist?.id == album.id; return TrackCollectionView( id: album.id!, - isPlaying: - playback.playlist?.id != null && playback.playlist?.id == album.id, + isPlaying: isAlbumPlaying, title: album.name!, titleImage: albumArt, tracksSnapshot: tracksSnapshot, @@ -67,14 +68,18 @@ class AlbumView extends HookConsumerWidget { bottomSpace: breakpoint.isLessThanOrEqualTo(Breakpoints.md), onPlay: ([track]) { if (tracksSnapshot.asData?.value != null) { - playPlaylist( - playback, - tracksSnapshot.asData!.value - .map((track) => - TypeConversionUtils.simpleTrack_X_Track(track, album)) - .toList(), - currentTrack: track, - ); + if (!isAlbumPlaying) { + playPlaylist( + playback, + tracksSnapshot.asData!.value + .map((track) => + TypeConversionUtils.simpleTrack_X_Track(track, album)) + .toList(), + currentTrack: track, + ); + } else { + playback.stop(); + } } }, onShare: () { diff --git a/lib/components/Playlist/PlaylistCard.dart b/lib/components/Playlist/PlaylistCard.dart index fd841da7..1fe96c57 100644 --- a/lib/components/Playlist/PlaylistCard.dart +++ b/lib/components/Playlist/PlaylistCard.dart @@ -24,7 +24,7 @@ class PlaylistCard extends HookConsumerWidget { margin: EdgeInsets.symmetric(horizontal: marginH.toDouble()), title: playlist.name!, imageUrl: TypeConversionUtils.image_X_UrlString(playlist.images), - isPlaying: isPlaylistPlaying, + isPlaying: isPlaylistPlaying && playback.isPlaying, isLoading: playback.status == PlaybackStatus.loading && isPlaylistPlaying, onTap: () { GoRouter.of(context).push( @@ -33,7 +33,11 @@ class PlaylistCard extends HookConsumerWidget { ); }, onPlaybuttonPressed: () async { - if (isPlaylistPlaying) return; + if (isPlaylistPlaying && playback.isPlaying) { + return playback.pause(); + } else if (isPlaylistPlaying && !playback.isPlaying) { + return playback.resume(); + } SpotifyApi spotifyApi = ref.read(spotifyProvider); List tracks = (playlist.id != "user-liked-tracks" diff --git a/lib/components/Playlist/PlaylistView.dart b/lib/components/Playlist/PlaylistView.dart index 126dfce3..00b3bd6f 100644 --- a/lib/components/Playlist/PlaylistView.dart +++ b/lib/components/Playlist/PlaylistView.dart @@ -77,11 +77,15 @@ class PlaylistView extends HookConsumerWidget { playlist.owner!.id == meSnapshot.asData?.value.id, onPlay: ([track]) { if (tracksSnapshot.asData?.value != null) { - playPlaylist( - playback, - tracksSnapshot.asData!.value, - currentTrack: track, - ); + if (!isPlaylistPlaying) { + playPlaylist( + playback, + tracksSnapshot.asData!.value, + currentTrack: track, + ); + } else { + playback.stop(); + } } }, bottomSpace: breakpoint.isLessThanOrEqualTo(Breakpoints.md), diff --git a/lib/components/Shared/TrackTile.dart b/lib/components/Shared/TrackTile.dart index d5ef5a64..e0988905 100644 --- a/lib/components/Shared/TrackTile.dart +++ b/lib/components/Shared/TrackTile.dart @@ -272,6 +272,16 @@ class TrackTile extends HookConsumerWidget { const SizedBox(width: 10), AdaptiveActions( actions: [ + if (auth.isLoggedIn) + Action( + icon: Icon(isSaved + ? Icons.favorite_rounded + : Icons.favorite_border_rounded), + text: const Text("Save as favorite"), + onPressed: () { + actionFavorite(isSaved); + }, + ), if (auth.isLoggedIn) Action( icon: const Icon(Icons.add_box_rounded), @@ -284,16 +294,6 @@ class TrackTile extends HookConsumerWidget { text: const Text("Remove from playlist"), onPressed: actionRemoveFromPlaylist, ), - if (auth.isLoggedIn) - Action( - icon: Icon(isSaved - ? Icons.favorite_rounded - : Icons.favorite_border_rounded), - text: const Text("Save as favorite"), - onPressed: () { - actionFavorite(isSaved); - }, - ), Action( icon: const Icon(Icons.share_rounded), text: const Text("Share"), diff --git a/lib/provider/UserPreferences.dart b/lib/provider/UserPreferences.dart index aa84e5b3..3f9f0020 100644 --- a/lib/provider/UserPreferences.dart +++ b/lib/provider/UserPreferences.dart @@ -39,7 +39,7 @@ class UserPreferences extends PersistedChangeNotifier { this.accentColorScheme = Colors.green, this.backgroundColorScheme = Colors.grey, this.checkUpdate = true, - this.trackMatchAlgorithm = SpotubeTrackMatchAlgorithm.authenticPopular, + this.trackMatchAlgorithm = SpotubeTrackMatchAlgorithm.youtube, this.audioQuality = AudioQuality.high, this.skipSponsorSegments = true, this.downloadLocation = "",