From 1fad95f6e370606a9faf6f2bb9738dc360e23918 Mon Sep 17 00:00:00 2001 From: tiaxter Date: Thu, 8 Dec 2022 14:40:20 +0100 Subject: [PATCH] feat: added shuffle button in playlist and album section --- .../shared/playlist_shuffle_button.dart | 20 ++++++++++++++++++ .../track_table/track_collection_view.dart | 4 ++++ lib/pages/album/album.dart | 21 +++++++++++++++++++ lib/pages/playlist/playlist.dart | 18 ++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 lib/components/shared/playlist_shuffle_button.dart diff --git a/lib/components/shared/playlist_shuffle_button.dart b/lib/components/shared/playlist_shuffle_button.dart new file mode 100644 index 000000000..d7de549e3 --- /dev/null +++ b/lib/components/shared/playlist_shuffle_button.dart @@ -0,0 +1,20 @@ +import 'package:flutter/material.dart'; +import 'package:platform_ui/platform_ui.dart'; + +class PlaylistShuffleButton extends StatelessWidget { + final onPressed; + + const PlaylistShuffleButton({ + Key? key, + this.onPressed, + }); + + @override + Widget build(BuildContext context) { + return PlatformIconButton( + tooltip: "Shuffle", + icon: const Icon(Icons.shuffle), + onPressed: this.onPressed, + ); + } +} diff --git a/lib/components/shared/track_table/track_collection_view.dart b/lib/components/shared/track_table/track_collection_view.dart index d85acc605..1c355d7cd 100644 --- a/lib/components/shared/track_table/track_collection_view.dart +++ b/lib/components/shared/track_table/track_collection_view.dart @@ -26,6 +26,7 @@ class TrackCollectionView extends HookConsumerWidget { final void Function([Track? currentTrack]) onPlay; final void Function() onShare; final Widget? heartBtn; + final Widget? shuffleButton; final AlbumSimple? album; final bool showShare; @@ -43,6 +44,7 @@ class TrackCollectionView extends HookConsumerWidget { required this.onShare, required this.routePath, this.heartBtn, + this.shuffleButton, this.album, this.description, this.showShare = true, @@ -68,6 +70,8 @@ class TrackCollectionView extends HookConsumerWidget { onPressed: onShare, ), if (heartBtn != null) heartBtn!, + if (shuffleButton != null) shuffleButton!, + // play playlist Container( diff --git a/lib/pages/album/album.dart b/lib/pages/album/album.dart index f85a4e053..e3d1bb7bb 100644 --- a/lib/pages/album/album.dart +++ b/lib/pages/album/album.dart @@ -14,6 +14,7 @@ import 'package:spotube/utils/type_conversion_utils.dart'; import 'package:spotube/models/current_playlist.dart'; import 'package:spotube/provider/playback_provider.dart'; import 'package:spotube/provider/spotify_provider.dart'; +import 'package:spotube/components/shared/playlist_shuffle_button.dart'; class AlbumPage extends HookConsumerWidget { final AlbumSimple album; @@ -112,6 +113,26 @@ class AlbumPage extends HookConsumerWidget { ); }, heartBtn: AlbumHeartButton(album: album), + shuffleButton: PlaylistShuffleButton(onPressed: () { + var albumTracks = tracksSnapshot.data! + .map((track) => + TypeConversionUtils.simpleTrack_X_Track(track, album)) + .toList(); + // Shuffle the tracks (create a copy of playlist) + var tracks = [...albumTracks]; + tracks.shuffle(); + + // If playback is playing a track then pause it + if (playback.isPlaying) { + playback.pause(); + } + + // Play the shuffled playlist + playPlaylist( playback, + tracks, + ref, + ); + }), ); } } diff --git a/lib/pages/playlist/playlist.dart b/lib/pages/playlist/playlist.dart index 3cafe3a5f..c5989d206 100644 --- a/lib/pages/playlist/playlist.dart +++ b/lib/pages/playlist/playlist.dart @@ -16,6 +16,7 @@ import 'package:spotube/services/queries/queries.dart'; import 'package:spotube/utils/service_utils.dart'; import 'package:spotube/utils/type_conversion_utils.dart'; +import 'package:spotube/components/shared/playlist_shuffle_button.dart'; class PlaylistView extends HookConsumerWidget { final logger = getLogger(PlaylistView); @@ -126,6 +127,23 @@ class PlaylistView extends HookConsumerWidget { }); }, heartBtn: PlaylistHeartButton(playlist: playlist), + shuffleButton: PlaylistShuffleButton(onPressed: () { + // Shuffle the tracks (create a copy of playlist) + var tracks = [...?tracksSnapshot.data]; + tracks.shuffle(); + + // If playback is playing a track then pause it + if (playback.isPlaying) { + playback.pause(); + } + + // Play the shuffled playlist + playPlaylist( playback, + tracks, + ref, + currentTrack: null, + ); + }), ); } }