Skip to content

Commit

Permalink
Matched sorting behaviour to the one that is in the jellyfin web clie…
Browse files Browse the repository at this point in the history
…nt. (#232)

* Matched sorting behaviour to the one that is in the jellyfin web client.
WebClient behaviour:
Albums - https://github.com/jellyfin/jellyfin-web/blob/master/src/controllers/music/musicalbums.js#L243

Songs - https://github.com/jellyfin/jellyfin-web/blob/master/src/controllers/music/songs.js#L157

* Upgraded Dart sdk requirement to be more than 2.17.0 so that enhanced enums can be implemented.
Also updated SortBy enum to enhanced enums.

* Updated SortOrder, ContentGridViewCrossAxisCountType, and TabContentType to be enhanced enums

* Removed humanReadableName property and replaced it with an overridden toString method provided by the enhanced enums.
  • Loading branch information
faizzzzz authored May 15, 2022
1 parent a127e2a commit 5db4d65
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import '../../services/FinampSettingsHelper.dart';

enum ContentGridViewCrossAxisCountType {
portrait,
landscape,
}
landscape;

extension ContentGridViewCrossAxisCountTypeExtension
on ContentGridViewCrossAxisCountType {
/// Human-readable version of the [ContentGridViewCrossAxisCountType]. For
/// example, toString() on [ContentGridViewCrossAxisCountType.portrait],
/// toString() would return "ContentGridViewCrossAxisCountType.portrait". With
/// this function, the same input would return "Portrait".
String get humanReadableName => _humanReadableName(this);
@override
String toString() => _humanReadableName(this);

String _humanReadableName(
ContentGridViewCrossAxisCountType contentGridViewCrossAxisCountType) {
Expand Down Expand Up @@ -63,9 +61,9 @@ class _ContentGridViewCrossAxisCountListTileState
@override
Widget build(BuildContext context) {
return ListTile(
title: Text("${widget.type.humanReadableName} Grid Cross-Axis Count"),
title: Text("${widget.type.toString()} Grid Cross-Axis Count"),
subtitle: Text(
"Amount of grid tiles to use per-row when ${widget.type.humanReadableName.toLowerCase()}."),
"Amount of grid tiles to use per-row when ${widget.type.toString().toLowerCase()}."),
trailing: SizedBox(
width: 50 * MediaQuery.of(context).textScaleFactor,
child: TextField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ContentViewTypeDropdownListTile extends StatelessWidget {
value: box.get("FinampSettings")?.contentViewType,
items: ContentViewType.values
.map((e) => DropdownMenuItem<ContentViewType>(
child: Text(e.humanReadableName),
child: Text(e.toString()),
value: e,
))
.toList(),
Expand Down
8 changes: 4 additions & 4 deletions lib/components/MusicScreen/MusicScreenTabView.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ class _MusicScreenTabViewState extends State<MusicScreenTabView>
// artist view. If it's null, sort by "SortName". If it isn't null, check
// if the parentItem is a MusicArtist. If it is, sort by year. Otherwise,
// sort by SortName. If widget.sortBy is set, it is used instead.
sortBy: widget.sortBy?.jellyfinName == null
sortBy: widget.sortBy?.jellyfinName(widget.tabContentType) == null
? widget.tabContentType == TabContentType.songs
? "Album,SortName"
: widget.parentItem == null
? "SortName"
: widget.parentItem?.type == "MusicArtist"
? "ProductionYear"
: "SortName"
: widget.sortBy?.jellyfinName,
sortOrder: widget.sortOrder?.humanReadableName ??
SortOrder.ascending.humanReadableName,
: widget.sortBy?.jellyfinName(widget.tabContentType),
sortOrder: widget.sortOrder?.toString() ??
SortOrder.ascending.toString(),
searchTerm: widget.searchTerm,
// If this is the genres tab, tell getItems to get genres.
isGenres: widget.tabContentType == TabContentType.genres,
Expand Down
14 changes: 7 additions & 7 deletions lib/components/MusicScreen/SortByMenuButton.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SortByMenuButton extends StatelessWidget {
itemBuilder: (context) => <PopupMenuEntry<SortBy>>[
PopupMenuItem(
child: Text(
SortBy.sortName.humanReadableName,
SortBy.sortName.toString(),
style: TextStyle(
color:
FinampSettingsHelper.finampSettings.sortBy == SortBy.sortName
Expand All @@ -26,7 +26,7 @@ class SortByMenuButton extends StatelessWidget {
),
PopupMenuItem(
child: Text(
SortBy.albumArtist.humanReadableName,
SortBy.albumArtist.toString(),
style: TextStyle(
color: FinampSettingsHelper.finampSettings.sortBy ==
SortBy.albumArtist
Expand All @@ -38,7 +38,7 @@ class SortByMenuButton extends StatelessWidget {
),
PopupMenuItem(
child: Text(
SortBy.communityRating.humanReadableName,
SortBy.communityRating.toString(),
style: TextStyle(
color: FinampSettingsHelper.finampSettings.sortBy ==
SortBy.communityRating
Expand All @@ -50,7 +50,7 @@ class SortByMenuButton extends StatelessWidget {
),
PopupMenuItem(
child: Text(
SortBy.criticRating.humanReadableName,
SortBy.criticRating.toString(),
style: TextStyle(
color: FinampSettingsHelper.finampSettings.sortBy ==
SortBy.criticRating
Expand All @@ -62,7 +62,7 @@ class SortByMenuButton extends StatelessWidget {
),
PopupMenuItem(
child: Text(
SortBy.dateCreated.humanReadableName,
SortBy.dateCreated.toString(),
style: TextStyle(
color: FinampSettingsHelper.finampSettings.sortBy ==
SortBy.dateCreated
Expand All @@ -74,7 +74,7 @@ class SortByMenuButton extends StatelessWidget {
),
PopupMenuItem(
child: Text(
SortBy.premiereDate.humanReadableName,
SortBy.premiereDate.toString(),
style: TextStyle(
color: FinampSettingsHelper.finampSettings.sortBy ==
SortBy.premiereDate
Expand All @@ -86,7 +86,7 @@ class SortByMenuButton extends StatelessWidget {
),
PopupMenuItem(
child: Text(
SortBy.random.humanReadableName,
SortBy.random.toString(),
style: TextStyle(
color: FinampSettingsHelper.finampSettings.sortBy == SortBy.random
? Theme.of(context).colorScheme.secondary
Expand Down
2 changes: 1 addition & 1 deletion lib/components/TabsSettingsScreen/HideTabToggle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class HideTabToggle extends StatelessWidget {
valueListenable: FinampSettingsHelper.finampSettingsListener,
builder: (_, box, __) {
return SwitchListTile(
title: Text('Show ' + tabContentType.humanReadableName),
title: Text('Show ' + tabContentType.toString()),
// This should never be null, but it gets set to true if it is.
value: FinampSettingsHelper
.finampSettings.showTabs[this.tabContentType] ??
Expand Down
16 changes: 6 additions & 10 deletions lib/models/FinampModels.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:io';

import 'package:hive/hive.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:logging/logging.dart';
Expand Down Expand Up @@ -366,14 +364,13 @@ enum TabContentType {
genres,

@HiveField(4)
songs,
}
songs;

extension TabContentTypeExtension on TabContentType {
/// Human-readable version of the [TabContentType]. For example, toString() on
/// [TabContentType.songs], toString() would return "TabContentType.songs".
/// With this function, the same input would return "Songs".
String get humanReadableName => _humanReadableName(this);
@override
String toString() => _humanReadableName(this);

String _humanReadableName(TabContentType tabContentType) {
switch (tabContentType) {
Expand All @@ -397,14 +394,13 @@ enum ContentViewType {
list,

@HiveField(1)
grid,
}
grid;

extension ContentViewTypeExtension on ContentViewType {
/// Human-readable version of this enum. I've written longer descriptions on
/// enums like [TabContentType], and I can't be bothered to copy and paste it
/// again.
String get humanReadableName => _humanReadableName(this);
@override
String toString() => _humanReadableName(this);

String _humanReadableName(ContentViewType contentViewType) {
switch (contentViewType) {
Expand Down
98 changes: 89 additions & 9 deletions lib/models/JellyfinModels.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
///
/// These classes should be correct with Jellyfin 10.7.5
import 'package:finamp/models/FinampModels.dart';
import 'package:hive/hive.dart';
import 'package:json_annotation/json_annotation.dart';

Expand Down Expand Up @@ -3307,17 +3308,25 @@ enum SortBy {
revenue,

@HiveField(14)
runtime,
}
runtime;

extension SortByExtension on SortBy {
/// Human-readable version of the [SortBy]. For example, toString() on
/// [SortBy.album], toString() would return "SortBy.album". With this
/// function, the same input would return "Album".
String get humanReadableName => _humanReadableName(this);
@override
String toString() => _humanReadableName(this);

/// Name used by Jellyfin in API requests.
String get jellyfinName => _jellyfinName(this);
String jellyfinName(TabContentType contentType){
switch (contentType){
case TabContentType.albums:
return _jellyfinNameMusicAlbums(this);
case TabContentType.songs:
return _jellyfinNameSongs(this);
default:
return _jellyfinName(this);
}
}

String _humanReadableName(SortBy sortBy) {
switch (sortBy) {
Expand Down Expand Up @@ -3388,6 +3397,78 @@ extension SortByExtension on SortBy {
return "Runtime";
}
}

String _jellyfinNameMusicAlbums(SortBy sortBy){
switch (sortBy) {
case SortBy.album:
return "Album";
case SortBy.albumArtist:
return "AlbumArtist,SortName";
case SortBy.artist:
return "Artist";
case SortBy.budget:
return "Budget";
case SortBy.communityRating:
return "CommunityRating,SortName";
case SortBy.criticRating:
return "CriticRating,SortName";
case SortBy.dateCreated:
return "DateCreated,SortName";
case SortBy.datePlayed:
return "DatePlayed";
case SortBy.playCount:
return "PlayCount";
case SortBy.premiereDate:
return "PremiereDate";
case SortBy.productionYear:
return "ProductionYear,PremiereDate,SortName";
case SortBy.sortName:
return "SortName";
case SortBy.random:
return "Random,SortName";
case SortBy.revenue:
return "Revenue";
case SortBy.runtime:
return "Runtime";
}
}

String _jellyfinNameSongs(SortBy sortBy) {
switch (sortBy) {
case SortBy.album:
return "Album,SortName";
case SortBy.albumArtist:
return "AlbumArtist,Album,SortName";
case SortBy.artist:
return "Artist,Album,SortName";
case SortBy.budget:
return "Budget";
case SortBy.communityRating:
return "CommunityRating";
case SortBy.criticRating:
return "CriticRating";
case SortBy.dateCreated:
return "DateCreated,SortName";
case SortBy.datePlayed:
return "DatePlayed,SortName";
case SortBy.playCount:
return "PlayCount,SortName";
case SortBy.premiereDate:
return "PremiereDate,AlbumArtist,Album,SortName";
case SortBy.productionYear:
return "ProductionYear";
case SortBy.sortName:
return "Name";
case SortBy.random:
return "Random";
case SortBy.revenue:
return "Revenue";
case SortBy.runtime:
return "Runtime,AlbumArtist,Album,SortName";
}
}


}

/// Enum for sort orders.
Expand All @@ -3397,16 +3478,15 @@ enum SortOrder {
ascending,

@HiveField(1)
descending,
}
descending;

extension SortOrderExtension on SortOrder {
/// Human-readable version of the [SortOrder]. For example, toString() on
/// [SortOrder.ascending], toString() would return "SortOrder.ascending". With
/// this function, the same input would return "Ascending". This function is
/// also used in Jellyfin API calls, but another function will be needed when
/// localisation support is added.
String get humanReadableName => _humanReadableName(this);
@override
String toString() => _humanReadableName(this);

String _humanReadableName(SortOrder sortOrder) {
switch (sortOrder) {
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/MusicScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class _MusicScreenState extends State<MusicScreen>
tabs: finampSettings.showTabs.entries
.where((element) => element.value)
.map((e) =>
Tab(text: e.key.humanReadableName.toUpperCase()))
Tab(text: e.key.toString().toUpperCase()))
.toList(),
isScrollable: true,
),
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 0.6.1+17

environment:
sdk: ">=2.13.0 <3.0.0"
sdk: ">=2.17.0 <3.0.0"

dependencies:
flutter:
Expand Down

0 comments on commit 5db4d65

Please sign in to comment.