Skip to content

Commit

Permalink
Add shuffle button
Browse files Browse the repository at this point in the history
  • Loading branch information
BobcatNoah committed Oct 1, 2023
1 parent f5796c7 commit 141f029
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 8 deletions.
12 changes: 4 additions & 8 deletions lib/components/ArtistScreen/artist_play_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,26 @@ class _ArtistPlayButtonState extends State<ArtistPlayButton> {
if (isOffline) {
final downloadsHelper = GetIt.instance<DownloadsHelper>();

final List<BaseItemDto>songs = [];
final List<BaseItemDto>artistsSongs = [];

for (DownloadedSong item in downloadsHelper.downloadedItems) {
if (item.song.albumArtist == widget.artist.name) {
songs.add(item.song);
artistsSongs.add(item.song);
}
}

// We have to sort by hand in offline mode because a downloadedParent for artists hasn't been implemented
Map<String, List<BaseItemDto>> groupedSongs = {};
for (BaseItemDto song in songs) {
for (BaseItemDto song in artistsSongs) {
groupedSongs.putIfAbsent((song.albumId ?? 'unknown'), () => []);
groupedSongs[song.albumId]!.add(song);
}

final List<BaseItemDto> sortedSongs = [];
groupedSongs.forEach((album, albumSongs) {
albumSongs.sort((a, b) => (a.indexNumber ?? 0).compareTo(b.indexNumber ?? 0));
});

final List<BaseItemDto> sortedSongs = [];
groupedSongs.values.forEach((albumSongs) {
sortedSongs.addAll(albumSongs);
});


return IconButton(
onPressed: () async {
Expand Down
92 changes: 92 additions & 0 deletions lib/components/ArtistScreen/artist_shuffle_button.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:hive/hive.dart';


import '../../models/jellyfin_models.dart';
import '../../models/finamp_models.dart';
import '../../services/jellyfin_api_helper.dart';
import '../../services/audio_service_helper.dart';
import '../../services/finamp_settings_helper.dart';
import '../../services/downloads_helper.dart';

class ArtistShuffleButton extends StatefulWidget {
const ArtistShuffleButton({
Key? key,
required this.artist,
}) : super(key: key);

final BaseItemDto artist;


@override
State<ArtistShuffleButton> createState() => _ArtistShuffleButtonState();
}

class _ArtistShuffleButtonState extends State<ArtistShuffleButton> {
static const _disabledButton = IconButton(
onPressed: null,
icon: Icon(Icons.play_arrow)
);
Future<List<BaseItemDto>?>? artistShuffleButtonFuture;

final _jellyfinApiHelper = GetIt.instance<JellyfinApiHelper>();
final _audioServiceHelper = GetIt.instance<AudioServiceHelper>();


@override
Widget build(BuildContext context) {
return ValueListenableBuilder<Box<FinampSettings>>(
valueListenable: FinampSettingsHelper.finampSettingsListener,
builder: (context, box, _) {
final isOffline = box.get("FinampSettings")?.isOffline ?? false;

if (isOffline) {
final downloadsHelper = GetIt.instance<DownloadsHelper>();

final List<BaseItemDto>artistsSongs = [];

for (DownloadedSong item in downloadsHelper.downloadedItems) {
if (item.song.albumArtist == widget.artist.name) {
artistsSongs.add(item.song);
}
}

return IconButton(
onPressed: () async {
await _audioServiceHelper
.replaceQueueWithItem(itemList: artistsSongs, shuffle: true);
},
icon: const Icon(Icons.shuffle),
);
} else {
artistShuffleButtonFuture ??= _jellyfinApiHelper.getItems(
parentItem: widget.artist,
includeItemTypes: "Audio",
sortBy: 'PremiereDate,Album,SortName',
isGenres: false,
);

return FutureBuilder<List<BaseItemDto>?>(
future: artistShuffleButtonFuture,
builder: (context, snapshot) {
if (snapshot.hasData){
final List<BaseItemDto> items = snapshot.data!;

return IconButton(
onPressed: () async {
await _audioServiceHelper
.replaceQueueWithItem(itemList: items, shuffle: true);
},
icon: const Icon(Icons.shuffle),
);
} else {
return _disabledButton;
}
},
);
}
},
);
}
}
2 changes: 2 additions & 0 deletions lib/screens/artist_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import '../components/MusicScreen/music_screen_tab_view.dart';
import '../components/now_playing_bar.dart';
import '../components/favourite_button.dart';
import '../components/ArtistScreen/artist_play_button.dart';
import '../components/ArtistScreen/artist_shuffle_button.dart';

class ArtistScreen extends StatelessWidget {
const ArtistScreen({
Expand All @@ -30,6 +31,7 @@ class ArtistScreen extends StatelessWidget {
actions: [
// this screen is also used for genres, which can't be favorited
if (artist.type != "MusicGenre") ArtistPlayButton(artist: artist),
if (artist.type != "MusicGenre") ArtistShuffleButton(artist: artist),
if (artist.type != "MusicGenre") FavoriteButton(item: artist),
ArtistDownloadButton(artist: artist)
],
Expand Down

0 comments on commit 141f029

Please sign in to comment.