Skip to content

Commit

Permalink
Add more song-like types in download system. Make default for unrecog…
Browse files Browse the repository at this point in the history
…nised types be a collection with no children instead of throwing an error.
  • Loading branch information
Komodo5197 committed May 24, 2024
1 parent cec5b3d commit 0972093
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 31 deletions.
70 changes: 46 additions & 24 deletions lib/models/finamp_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ class FinampSettings {
this.showArtistChipImage = _showArtistChipImage,
this.trackOfflineFavorites = _trackOfflineFavoritesDefault,
this.showProgressOnNowPlayingBar = _showProgressOnNowPlayingBarDefault,
this.startInstantMixForIndividualTracks = _startInstantMixForIndividualTracksDefault,
this.startInstantMixForIndividualTracks =
_startInstantMixForIndividualTracksDefault,
});

@HiveField(0, defaultValue: _isOfflineDefault)
Expand Down Expand Up @@ -821,22 +822,21 @@ class DownloadStub {
case DownloadItemType.collection:
return baseItem != null &&
BaseItemDtoType.fromItem(baseItem!) == baseItemType &&
baseItemType != BaseItemDtoType.song &&
baseItemType != BaseItemDtoType.unknown;
baseItemType.downloadType == DownloadItemType.collection &&
baseItemType != BaseItemDtoType.noItem;
case DownloadItemType.song:
return baseItemType == BaseItemDtoType.song &&
return baseItemType.downloadType == DownloadItemType.song &&
baseItem != null &&
BaseItemDtoType.fromItem(baseItem!) == baseItemType;
case DownloadItemType.image:
return baseItem != null;
case DownloadItemType.finampCollection:
// TODO create an enum or somthing for this if more custom collections happen
return baseItem == null &&
baseItemType == BaseItemDtoType.unknown &&
baseItemType == BaseItemDtoType.noItem &&
finampCollection != null;
case DownloadItemType.anchor:
return baseItem == null &&
baseItemType == BaseItemDtoType.unknown &&
baseItemType == BaseItemDtoType.noItem &&
id == "Anchor";
}
}
Expand Down Expand Up @@ -873,7 +873,7 @@ class DownloadStub {
jsonItem: null,
type: type,
name: name ?? "Unlocalized $id",
baseItemType: BaseItemDtoType.unknown);
baseItemType: BaseItemDtoType.noItem);
}

factory DownloadStub.fromFinampCollection(FinampCollection collection) {
Expand All @@ -891,7 +891,7 @@ class DownloadStub {
jsonItem: jsonEncode(collection.toJson()),
type: DownloadItemType.finampCollection,
name: name ?? "Unlocalized Finamp Collection $id",
baseItemType: BaseItemDtoType.unknown);
baseItemType: BaseItemDtoType.noItem);
}

/// The integer iD used as a database key by Isar
Expand Down Expand Up @@ -1231,28 +1231,52 @@ enum DownloadItemStatus {
/// The type of a BaseItemDto as determined from its type field.
/// Enumerated by Isar, do not modify order or delete existing entries
enum BaseItemDtoType {
unknown(null, true, null),
album("MusicAlbum", false, [song]),
artist("MusicArtist", true, [album, song]),
playlist("Playlist", true, [song]),
genre("MusicGenre", true, [album, song]),
song("Audio", false, []),
library("CollectionFolder", true, [album, song]),
folder("Folder", true, null),
musicVideo("MusicVideo", false, []);

const BaseItemDtoType(this.idString, this.expectChanges, this.childTypes);
noItem(null, true, null, null),
album("MusicAlbum", false, [song], DownloadItemType.collection),
artist("MusicArtist", true, [album, song], DownloadItemType.collection),
playlist("Playlist", true, [song], DownloadItemType.collection),
genre("MusicGenre", true, [album, song], DownloadItemType.collection),
song("Audio", false, [], DownloadItemType.song),
library("CollectionFolder", true, [album, song], DownloadItemType.collection),
folder("Folder", true, null, DownloadItemType.collection),
musicVideo("MusicVideo", false, [], DownloadItemType.song),
audioBook("AudioBook", false, [], DownloadItemType.song),
tvEpisode("Episode", false, [], DownloadItemType.song),
video("Video", false, [], DownloadItemType.song),
movie("Movie", false, [], DownloadItemType.song),
trailer("Trailer", false, [], DownloadItemType.song),
unknown(null, true, null, DownloadItemType.collection);

// All possible types in Jellyfin as of 10.9:
//"AggregateFolder" "Audio" "AudioBook" "BasePluginFolder" "Book" "BoxSet"
// "Channel" "ChannelFolderItem" "CollectionFolder" "Episode" "Folder" "Genre"
// "ManualPlaylistsFolder" "Movie" "LiveTvChannel" "LiveTvProgram" "MusicAlbum"
// "MusicArtist" "MusicGenre" "MusicVideo" "Person" "Photo" "PhotoAlbum" "Playlist"
// "PlaylistsFolder" "Program" "Recording" "Season" "Series" "Studio" "Trailer" "TvChannel"
// "TvProgram" "UserRootFolder" "UserView" "Video" "Year"

const BaseItemDtoType(
this.idString, this.expectChanges, this.childTypes, this.downloadType);

final String? idString;
final bool expectChanges;
final List<BaseItemDtoType>? childTypes;
final DownloadItemType? downloadType;

bool get expectChangesInChildren =>
childTypes?.any((x) => x.expectChanges) ?? true;

// BaseItemDto types that we handle like songs have been handled by returning
// the actual song type. This may be a bad ides?
static BaseItemDtoType fromItem(BaseItemDto item) {
switch (item.type) {
case "Audio":
case "AudioBook":
case "MusicVideo":
case "Episode":
case "Video":
case "Movie":
case "Trailer":
return song;
case "MusicAlbum":
return album;
Expand All @@ -1265,11 +1289,9 @@ enum BaseItemDtoType {
case "CollectionFolder":
return library;
case "Folder":
return song;
case "MusicVideo":
return song;
return folder;
default:
throw "Unknown baseItemDto type ${item.type}";
return unknown;
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/models/finamp_models.g.dart

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

4 changes: 2 additions & 2 deletions lib/models/jellyfin_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2206,7 +2206,7 @@ class BaseItemDto with RunTimeTickDuration {

/// Custom helper field to determine if the BaseItemDto was created in offline mode
bool? finampOffline;

/// Checks if the item has its own image (not inherited from a parent)
bool get hasOwnImage => imageTags?.containsKey("Primary") ?? false;

Expand Down Expand Up @@ -2302,7 +2302,7 @@ class BaseItemDto with RunTimeTickDuration {
}

DownloadItemType get downloadType =>
type! == "Audio" ? DownloadItemType.song : DownloadItemType.collection;
BaseItemDtoType.fromItem(this).downloadType!;
}

@JsonSerializable(
Expand Down
5 changes: 4 additions & 1 deletion lib/services/downloads_service_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,7 @@ class DownloadsSyncService {
String? fields;
String? sortOrder;
assert(parent.type == DownloadItemType.collection);
assert(parent.baseItemType.downloadType == DownloadItemType.collection);
switch (parent.baseItemType) {
case BaseItemDtoType.playlist || BaseItemDtoType.album:
childType = DownloadItemType.song;
Expand All @@ -1279,7 +1280,9 @@ class DownloadsSyncService {
childFilter = BaseItemDtoType.album;
fields = "${_jellyfinApiData.defaultFields},SortName";
case _:
throw StateError("Unknown collection type ${parent.baseItemType}");
_syncLogger.severe(
"Unknown collection type ${parent.baseItemType} for ${parent.name}");
return Future.value([]);
}
var item = parent.baseItem!;

Expand Down

0 comments on commit 0972093

Please sign in to comment.