Skip to content

Commit

Permalink
feat: added shuffle button in playlist and album section
Browse files Browse the repository at this point in the history
  • Loading branch information
tiaxter committed Dec 8, 2022
1 parent bd18f19 commit 1fad95f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/components/shared/playlist_shuffle_button.dart
Original file line number Diff line number Diff line change
@@ -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,
);
}
}
4 changes: 4 additions & 0 deletions lib/components/shared/track_table/track_collection_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class TrackCollectionView<T> extends HookConsumerWidget {
final void Function([Track? currentTrack]) onPlay;
final void Function() onShare;
final Widget? heartBtn;
final Widget? shuffleButton;
final AlbumSimple? album;

final bool showShare;
Expand All @@ -43,6 +44,7 @@ class TrackCollectionView<T> extends HookConsumerWidget {
required this.onShare,
required this.routePath,
this.heartBtn,
this.shuffleButton,
this.album,
this.description,
this.showShare = true,
Expand All @@ -68,6 +70,8 @@ class TrackCollectionView<T> extends HookConsumerWidget {
onPressed: onShare,
),
if (heartBtn != null) heartBtn!,
if (shuffleButton != null) shuffleButton!,


// play playlist
Container(
Expand Down
21 changes: 21 additions & 0 deletions lib/pages/album/album.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
);
}),
);
}
}
18 changes: 18 additions & 0 deletions lib/pages/playlist/playlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
);
}),
);
}
}

0 comments on commit 1fad95f

Please sign in to comment.