From c9668af2aeb3d797b165b967e2ea9e62efa9fa95 Mon Sep 17 00:00:00 2001 From: edde746 <86283021+edde746@users.noreply.github.com> Date: Sat, 14 Mar 2026 18:56:20 +0100 Subject: [PATCH] feat: sort home screen hubs by user's library order Closes #696 --- lib/screens/discover_screen.dart | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lib/screens/discover_screen.dart b/lib/screens/discover_screen.dart index b09a6484..7cd255f2 100644 --- a/lib/screens/discover_screen.dart +++ b/lib/screens/discover_screen.dart @@ -18,6 +18,7 @@ import '../utils/content_utils.dart'; import '../models/plex_hub.dart'; import '../providers/multi_server_provider.dart'; import '../providers/hidden_libraries_provider.dart'; +import '../providers/libraries_provider.dart'; import '../providers/playback_state_provider.dart'; import 'profile/user_avatar_widget.dart'; import '../widgets/hub_section.dart'; @@ -522,6 +523,25 @@ class _DiscoverScreenState extends State !title.contains('on deck'); }).toList(); + // Sort hubs by the user's library order + final libraryOrder = context.read().libraries; + if (libraryOrder.isNotEmpty) { + final orderMap = {}; + for (var i = 0; i < libraryOrder.length; i++) { + orderMap[libraryOrder[i].globalKey] = i; + } + filteredHubs.sort((a, b) { + final aKey = _hubLibraryGlobalKey(a); + final bKey = _hubLibraryGlobalKey(b); + final aIndex = aKey != null ? orderMap[aKey] : null; + final bIndex = bKey != null ? orderMap[bKey] : null; + if (aIndex == null && bIndex == null) return 0; + if (aIndex == null) return 1; + if (bIndex == null) return -1; + return aIndex.compareTo(bIndex); + }); + } + appLogger.d('Received ${onDeck.length} on deck items and ${filteredHubs.length} global hubs from all servers'); if (!mounted) return; setState(() { @@ -541,6 +561,15 @@ class _DiscoverScreenState extends State } } + /// Resolve the library globalKey for a hub (for sorting by library order). + String? _hubLibraryGlobalKey(PlexHub hub) { + final serverId = hub.serverId; + if (serverId == null) return null; + final sectionId = hub.librarySectionID ?? hub.items.firstOrNull?.librarySectionID; + if (sectionId == null) return null; + return buildGlobalKey(serverId, sectionId.toString()); + } + /// Refresh only the Continue Watching section in the background /// This is called when returning to the home screen to avoid blocking UI Future _refreshContinueWatching() async {