diff --git a/lib/screens/libraries/tabs/library_recommended_tab.dart b/lib/screens/libraries/tabs/library_recommended_tab.dart index a87a1e32..eedc5acb 100644 --- a/lib/screens/libraries/tabs/library_recommended_tab.dart +++ b/lib/screens/libraries/tabs/library_recommended_tab.dart @@ -53,55 +53,29 @@ class _LibraryRecommendedTabState extends BaseLibraryTabState t.libraries.tabs.recommended; + /// Detects Continue Watching hubs by hubIdentifier. + /// Section-specific CW hubs use identifiers like "movie.inprogress.1". + static bool _isContinueWatchingHub(PlexHub hub) { + final hubId = hub.hubIdentifier?.toLowerCase() ?? ''; + return hubId.contains('inprogress'); + } + @override Future> loadData() async { // Clear hub keys before loading new hubs to prevent stale references _hubKeys.clear(); - // Use server-specific client for this library final client = getClientForLibrary(); + final hubs = await client.getLibraryHubs(widget.library.key, limit: 12); - // Load both continue watching items and regular hubs in parallel - final results = await Future.wait([ - client.getOnDeckForLibrary(widget.library.key), - client.getLibraryHubs(widget.library.key, limit: 12), - ]); - - final continueWatchingItems = results.first as List; - final hubs = results[1] as List; - - // Filter out any existing Continue Watching hubs since we're adding our own - final filteredHubs = hubs.where((hub) { - final title = hub.title.toLowerCase(); - final hubId = hub.hubIdentifier?.toLowerCase() ?? ''; - return !title.contains('continue watching') && - !title.contains('on deck') && - !hubId.contains('ondeck') && - !hubId.contains('continue'); - }).toList(); - - final finalHubs = []; - - // Add Continue Watching as the first hub if there are items - if (continueWatchingItems.isNotEmpty) { - final continueWatchingHub = PlexHub( - hubKey: 'library_continue_watching_${widget.library.key}', - title: t.discover.continueWatching, - type: 'mixed', - hubIdentifier: '_library_continue_watching_', - size: continueWatchingItems.length, - more: false, - items: continueWatchingItems, - serverId: widget.library.serverId, - serverName: widget.library.serverName, - ); - finalHubs.add(continueWatchingHub); + // Move Continue Watching hub to the front if present + final cwIndex = hubs.indexWhere(_isContinueWatchingHub); + if (cwIndex > 0) { + final cwHub = hubs.removeAt(cwIndex); + hubs.insert(0, cwHub); } - // Add the filtered regular hubs - finalHubs.addAll(filteredHubs); - - return finalHubs; + return hubs; } /// Ensure we have enough GlobalKeys for all hubs @@ -163,7 +137,7 @@ class _LibraryRecommendedTabState extends BaseLibraryTabState _processOnDeckResponse(response.data as Map, sid, sname)); } - /// Get on deck items filtered by a specific library section - Future> getOnDeckForLibrary(String sectionId) async { - final allOnDeck = await getOnDeck(); - - // Filter items to only include those from the specified library section - return allOnDeck.where((item) { - return item.librarySectionID == int.tryParse(sectionId); - }).toList(); - } - /// Get children of a metadata item (e.g., seasons for a show, episodes for a season) /// Uses cache when offline or as fallback on network error Future> getChildren(String ratingKey) async {