This commit is contained in:
Sam Matthews
2025-11-04 17:09:32 +00:00
parent feb9254aea
commit 1cab790570
+72 -18
View File
@@ -346,7 +346,7 @@ class PlexClient {
queryParameters: {'X-Plex-Container-Size': limit, 'includeGuids': 1},
);
final allItems = _extractMetadataList(response);
// Filter out music content (artists, albums, tracks)
return allItems.where((item) {
final type = item.type.toLowerCase();
@@ -362,7 +362,7 @@ class PlexClient {
final allItems = (container['Metadata'] as List)
.map((json) => PlexMetadata.fromJsonWithImages(json))
.toList();
// Filter out music content (artists, albums, tracks)
return allItems.where((item) {
final type = item.type.toLowerCase();
@@ -446,7 +446,10 @@ class PlexClient {
/// Get detailed media info including chapters and tracks
/// [mediaIndex] specifies which Media item to use (defaults to 0 - first version)
Future<PlexMediaInfo?> getMediaInfo(String ratingKey, {int mediaIndex = 0}) async {
Future<PlexMediaInfo?> getMediaInfo(
String ratingKey, {
int mediaIndex = 0,
}) async {
final response = await _dio.get('/library/metadata/$ratingKey');
final metadataJson = _getFirstMetadataJson(response);
@@ -550,7 +553,9 @@ class PlexClient {
(metadataJson['Media'] as List).isNotEmpty) {
final mediaList = metadataJson['Media'] as List;
return mediaList
.map((media) => PlexMediaVersion.fromJson(media as Map<String, dynamic>))
.map(
(media) => PlexMediaVersion.fromJson(media as Map<String, dynamic>),
)
.toList();
}
@@ -705,24 +710,75 @@ class PlexClient {
final container = _getMediaContainer(response);
if (container != null && container['Sort'] != null) {
return (container['Sort'] as List)
final sorts = (container['Sort'] as List)
.map((json) => PlexSort.fromJson(json as Map<String, dynamic>))
.toList();
// For TV show libraries, add sort by latest episode air date if not already present
// Note: Plex uses different field names - we need to check what's actually available
final libraryType = container['librarySectionType'] as String?;
if (libraryType == 'show') {
// Try to find existing sort options that might work
final hasEpisodeAirSort = sorts.any(
(s) =>
s.key == 'episode.originallyAvailableAt' ||
s.key == 'lastRatedAt' ||
s.title.toLowerCase().contains('episode') &&
s.title.toLowerCase().contains('air'),
);
if (!hasEpisodeAirSort) {
// Add custom sort - Plex typically uses 'episode.originallyAvailableAt' or 'lastRatedAt'
// for sorting shows by their latest episode's air date
sorts.add(
PlexSort(
key: 'episode.originallyAvailableAt',
descKey: 'episode.originallyAvailableAt:desc',
title: 'Latest Episode Air Date',
defaultDirection: 'desc',
),
);
}
}
return sorts;
}
// Fallback: return common sort options if API doesn't provide them
return [
PlexSort(
key: 'titleSort',
title: 'Title',
defaultDirection: 'asc',
),
// Get library type to determine which sorts to include
final librariesResponse = await _dio.get('/library/sections');
final libraries = _extractDirectoryList(
librariesResponse,
PlexLibrary.fromJson,
);
final library = libraries.firstWhere(
(lib) => lib.key == sectionId,
orElse: () => libraries.first,
);
final fallbackSorts = <PlexSort>[
PlexSort(key: 'titleSort', title: 'Title', defaultDirection: 'asc'),
PlexSort(
key: 'addedAt',
descKey: 'addedAt:desc',
title: 'Date Added',
defaultDirection: 'desc',
),
];
// Add "Latest Episode Air Date" only for TV show libraries
if (library.type.toLowerCase() == 'show') {
fallbackSorts.add(
PlexSort(
key: 'episode.originallyAvailableAt',
descKey: 'episode.originallyAvailableAt:desc',
title: 'Latest Episode Air Date',
defaultDirection: 'desc',
),
);
}
fallbackSorts.addAll([
PlexSort(
key: 'originallyAvailableAt',
descKey: 'originallyAvailableAt:desc',
@@ -735,16 +791,14 @@ class PlexClient {
title: 'Rating',
defaultDirection: 'desc',
),
];
]);
return fallbackSorts;
} catch (e) {
appLogger.e('Failed to get library sorts: $e');
// Return fallback sort options on error
// Return minimal fallback sort options on error
return [
PlexSort(
key: 'titleSort',
title: 'Title',
defaultDirection: 'asc',
),
PlexSort(key: 'titleSort', title: 'Title', defaultDirection: 'asc'),
PlexSort(
key: 'addedAt',
descKey: 'addedAt:desc',