Skip to content

Commit

Permalink
Configure amount of songs played with shuffle all
Browse files Browse the repository at this point in the history
  • Loading branch information
jmshrv committed Jul 26, 2021
1 parent 6fe209b commit 4ce3185
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:flutter/material.dart';

import '../../services/FinampSettingsHelper.dart';

class SongShuffleItemCountEditor extends StatefulWidget {
const SongShuffleItemCountEditor({Key? key}) : super(key: key);

@override
_SongShuffleItemCountEditorState createState() =>
_SongShuffleItemCountEditorState();
}

class _SongShuffleItemCountEditorState
extends State<SongShuffleItemCountEditor> {
final _controller = TextEditingController(
text:
FinampSettingsHelper.finampSettings.songShuffleItemCount.toString());

@override
Widget build(BuildContext context) {
return ListTile(
title: Text("Song Shuffle Song Count"),
subtitle: Text(
"Amount of songs to load when using the shuffle all songs button. Has no effect in offline mode."),
trailing: SizedBox(
width: 50 * MediaQuery.of(context).textScaleFactor,
child: TextField(
controller: _controller,
textAlign: TextAlign.center,
keyboardType: TextInputType.number,
onChanged: (value) {
final valueInt = int.tryParse(value);

if (valueInt != null) {
FinampSettingsHelper.setSongShuffleItemCount(valueInt);
}
},
),
),
);
}
}
38 changes: 23 additions & 15 deletions lib/models/FinampModels.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,26 @@ class FinampUser {
BaseItemDto? get currentView => views[currentViewId];
}

// These consts are so that we can easily keep the same default for
// FinampSettings's constructor and Hive's defaultValue.
const _songShuffleItemCountDefault = 250;

@HiveType(typeId: 28)
class FinampSettings {
FinampSettings({
this.isOffline = false,
this.shouldTranscode = false,
this.transcodeBitrate = 320000,
// downloadLocations is required since the other values can be created with
// default values. create() is used to return a FinampSettings with
// downloadLocations.
required this.downloadLocations,
this.androidStopForegroundOnPause = true,
required this.showTabs,
this.isFavourite = false,
this.sortBy = SortBy.sortName,
this.sortOrder = SortOrder.ascending,
});
FinampSettings(
{this.isOffline = false,
this.shouldTranscode = false,
this.transcodeBitrate = 320000,
// downloadLocations is required since the other values can be created with
// default values. create() is used to return a FinampSettings with
// downloadLocations.
required this.downloadLocations,
this.androidStopForegroundOnPause = true,
required this.showTabs,
this.isFavourite = false,
this.sortBy = SortBy.sortName,
this.sortOrder = SortOrder.ascending,
this.songShuffleItemCount = _songShuffleItemCountDefault});

@HiveField(0)
bool isOffline;
Expand All @@ -62,7 +66,7 @@ class FinampSettings {
@HiveField(3)
List<DownloadLocation> downloadLocations;
@HiveField(4)
late bool androidStopForegroundOnPause;
bool androidStopForegroundOnPause;
@HiveField(5)
Map<TabContentType, bool> showTabs;

Expand All @@ -79,6 +83,10 @@ class FinampSettings {
@HiveField(8)
SortOrder sortOrder;

/// Amount of songs to get when shuffling songs.
@HiveField(9, defaultValue: _songShuffleItemCountDefault)
int songShuffleItemCount;

static Future<FinampSettings> create() async {
Directory internalSongDir = await getInternalSongDir();
return FinampSettings(
Expand Down
7 changes: 5 additions & 2 deletions lib/models/FinampModels.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion lib/screens/AudioServiceSettingsScreen.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';

import '../components/AudioServiceSettingsScreen/StopForegroundSelector.dart';
import '../components/AudioServiceSettingsScreen/SongShuffleItemCountEditor.dart';

class AudioServiceSettingsScreen extends StatelessWidget {
const AudioServiceSettingsScreen({Key? key}) : super(key: key);
Expand All @@ -13,7 +14,10 @@ class AudioServiceSettingsScreen extends StatelessWidget {
),
body: Scrollbar(
child: ListView(
children: [StopForegroundSelector()],
children: [
StopForegroundSelector(),
SongShuffleItemCountEditor(),
],
),
),
);
Expand Down
2 changes: 2 additions & 0 deletions lib/services/AudioServiceHelper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ class AudioServiceHelper {
parentItem: _jellyfinApiData.currentUser!.currentView,
includeItemTypes: "Audio",
filters: isFavourite ? "IsFavorite" : null,
limit: FinampSettingsHelper.finampSettings.songShuffleItemCount,
sortBy: "Random",
);
}

Expand Down
7 changes: 7 additions & 0 deletions lib/services/FinampSettingsHelper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,11 @@ class FinampSettingsHelper {
Hive.box<FinampSettings>("FinampSettings")
.put("FinampSettings", finampSettingsTemp);
}

static void setSongShuffleItemCount(int songShuffleItemCount) {
FinampSettings finampSettingsTemp = finampSettings;
finampSettingsTemp.songShuffleItemCount = songShuffleItemCount;
Hive.box<FinampSettings>("FinampSettings")
.put("FinampSettings", finampSettingsTemp);
}
}

0 comments on commit 4ce3185

Please sign in to comment.