fix(media): paginate large server lists
This commit is contained in:
@@ -2,8 +2,11 @@ import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:material_symbols_icons/symbols.dart';
|
||||
import '../media/library_query.dart';
|
||||
import '../media/media_backend.dart';
|
||||
import '../media/media_hub.dart';
|
||||
import '../media/media_item.dart';
|
||||
import '../media/media_server_client.dart';
|
||||
import '../media/media_sort.dart';
|
||||
import '../services/settings_service.dart';
|
||||
import '../widgets/settings_builder.dart';
|
||||
@@ -45,13 +48,20 @@ class HubDetailScreen extends StatefulWidget {
|
||||
|
||||
class _HubDetailScreenState extends State<HubDetailScreen>
|
||||
with Refreshable, GridFocusNodeMixin, FocusableDetailScreenMixin {
|
||||
static const int _pageSize = 200;
|
||||
|
||||
List<MediaItem> _items = [];
|
||||
List<MediaItem> _filteredItems = [];
|
||||
List<MediaSort> _sortOptions = [];
|
||||
MediaSort? _selectedSort;
|
||||
bool _isSortDescending = false;
|
||||
bool _isLoading = false;
|
||||
bool _isLoadingMore = false;
|
||||
String? _errorMessage;
|
||||
String? _continuationErrorMessage;
|
||||
int? _continuationOffset;
|
||||
int? _continuationTotal;
|
||||
int _loadGeneration = 0;
|
||||
|
||||
/// Key for getting a context below OverlaySheetHost
|
||||
final GlobalKey _overlayChildKey = GlobalKey();
|
||||
@@ -225,6 +235,7 @@ class _HubDetailScreenState extends State<HubDetailScreen>
|
||||
|
||||
Future<void> _loadMoreItems() async {
|
||||
if (_isLoading) return;
|
||||
final generation = ++_loadGeneration;
|
||||
|
||||
final serverId = widget.hub.serverId;
|
||||
if (widget.loadItems == null && serverId == null) {
|
||||
@@ -234,23 +245,33 @@ class _HubDetailScreenState extends State<HubDetailScreen>
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
_isLoadingMore = false;
|
||||
_errorMessage = null;
|
||||
_continuationErrorMessage = null;
|
||||
_continuationOffset = null;
|
||||
_continuationTotal = null;
|
||||
});
|
||||
|
||||
try {
|
||||
final loader = widget.loadItems;
|
||||
final client = serverId == null ? null : context.tryGetMediaClientForServer(serverId);
|
||||
var items = loader == null
|
||||
? (client == null ? const <MediaItem>[] : await client.fetchMoreHubItems(widget.hub.id))
|
||||
: await loader();
|
||||
|
||||
// Filter to specific library if this hub was split from a multi-library hub
|
||||
final sectionFilter = int.tryParse(widget.hub.libraryId ?? '');
|
||||
if (sectionFilter != null) {
|
||||
items = items.where((item) => int.tryParse(item.libraryId ?? '') == sectionFilter).toList();
|
||||
final List<MediaItem> items;
|
||||
int totalCount;
|
||||
int loadedCount;
|
||||
if (loader == null) {
|
||||
final page = client == null
|
||||
? const LibraryPage<MediaItem>(items: [], totalCount: 0)
|
||||
: await client.fetchMoreHubItemsPage(widget.hub.id, start: 0, size: _pageSize);
|
||||
items = _applySectionFilter(page.items);
|
||||
totalCount = page.totalCount;
|
||||
loadedCount = page.items.length;
|
||||
} else {
|
||||
items = _applySectionFilter(await loader());
|
||||
totalCount = items.length;
|
||||
loadedCount = items.length;
|
||||
}
|
||||
|
||||
if (!mounted) return;
|
||||
if (!mounted || generation != _loadGeneration) return;
|
||||
setState(() {
|
||||
_items = items;
|
||||
_filteredItems = items;
|
||||
@@ -258,6 +279,13 @@ class _HubDetailScreenState extends State<HubDetailScreen>
|
||||
});
|
||||
|
||||
_applySort();
|
||||
if (loader == null && client != null && loadedCount < totalCount) {
|
||||
if (client.backend == MediaBackend.plex) {
|
||||
unawaited(_loadFullHubContent(client, generation));
|
||||
} else {
|
||||
unawaited(_loadRemainingHubPages(client, generation, loadedCount, totalCount));
|
||||
}
|
||||
}
|
||||
|
||||
appLogger.d('Loaded ${items.length} items for hub: ${widget.hub.title}');
|
||||
} catch (e) {
|
||||
@@ -270,6 +298,106 @@ class _HubDetailScreenState extends State<HubDetailScreen>
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _loadFullHubContent(MediaServerClient client, int generation) async {
|
||||
if (mounted && generation == _loadGeneration) {
|
||||
setState(() {
|
||||
_isLoadingMore = true;
|
||||
_continuationErrorMessage = null;
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
final items = _applySectionFilter(await client.fetchMoreHubItems(widget.hub.id));
|
||||
if (!mounted || generation != _loadGeneration) return;
|
||||
if (items.isEmpty && _items.isNotEmpty) {
|
||||
throw StateError('Hub continuation returned no items');
|
||||
}
|
||||
setState(() {
|
||||
_items = items;
|
||||
_filteredItems = items;
|
||||
_isLoadingMore = false;
|
||||
_continuationErrorMessage = null;
|
||||
_continuationOffset = null;
|
||||
_continuationTotal = null;
|
||||
});
|
||||
_applySort();
|
||||
} catch (e, st) {
|
||||
appLogger.w('Failed to finish loading hub content', error: e, stackTrace: st);
|
||||
if (!mounted || generation != _loadGeneration) return;
|
||||
setState(() {
|
||||
_isLoadingMore = false;
|
||||
_continuationErrorMessage = t.messages.errorLoading(error: e.toString());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _loadRemainingHubPages(MediaServerClient client, int generation, int startOffset, int totalCount) async {
|
||||
var offset = startOffset;
|
||||
var total = totalCount;
|
||||
if (mounted && generation == _loadGeneration) {
|
||||
setState(() {
|
||||
_isLoadingMore = true;
|
||||
_continuationErrorMessage = null;
|
||||
_continuationOffset = offset;
|
||||
_continuationTotal = total;
|
||||
});
|
||||
}
|
||||
try {
|
||||
while (offset < total) {
|
||||
final page = await client.fetchMoreHubItemsPage(widget.hub.id, start: offset, size: _pageSize);
|
||||
if (!mounted || generation != _loadGeneration) return;
|
||||
if (page.items.isEmpty) break;
|
||||
final items = _applySectionFilter(page.items);
|
||||
setState(() {
|
||||
_items.addAll(items);
|
||||
_filteredItems = List.of(_items);
|
||||
});
|
||||
_applySort();
|
||||
offset += page.items.length;
|
||||
total = page.totalCount;
|
||||
_continuationOffset = offset;
|
||||
_continuationTotal = total;
|
||||
}
|
||||
if (!mounted || generation != _loadGeneration) return;
|
||||
setState(() {
|
||||
_isLoadingMore = false;
|
||||
_continuationErrorMessage = null;
|
||||
_continuationOffset = null;
|
||||
_continuationTotal = null;
|
||||
});
|
||||
} catch (e, st) {
|
||||
appLogger.w('Failed to finish loading hub content', error: e, stackTrace: st);
|
||||
if (!mounted || generation != _loadGeneration) return;
|
||||
setState(() {
|
||||
_isLoadingMore = false;
|
||||
_continuationErrorMessage = t.messages.errorLoading(error: e.toString());
|
||||
_continuationOffset = offset;
|
||||
_continuationTotal = total;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _retryHubContinuation() {
|
||||
final serverId = widget.hub.serverId;
|
||||
final client = serverId == null ? null : context.tryGetMediaClientForServer(serverId);
|
||||
if (client == null || _isLoadingMore) return;
|
||||
final generation = _loadGeneration;
|
||||
if (client.backend == MediaBackend.plex) {
|
||||
unawaited(_loadFullHubContent(client, generation));
|
||||
return;
|
||||
}
|
||||
final offset = _continuationOffset;
|
||||
final total = _continuationTotal;
|
||||
if (offset == null || total == null) return;
|
||||
unawaited(_loadRemainingHubPages(client, generation, offset, total));
|
||||
}
|
||||
|
||||
List<MediaItem> _applySectionFilter(List<MediaItem> items) {
|
||||
final sectionFilter = int.tryParse(widget.hub.libraryId ?? '');
|
||||
if (sectionFilter == null) return items;
|
||||
return items.where((item) => int.tryParse(item.libraryId ?? '') == sectionFilter).toList();
|
||||
}
|
||||
|
||||
Future<void> _handleItemRefresh(String ratingKey) async {
|
||||
final itemIndex = _items.indexWhere((item) => item.id == ratingKey);
|
||||
final filteredIndex = _filteredItems.indexWhere((item) => item.id == ratingKey);
|
||||
@@ -302,6 +430,27 @@ class _HubDetailScreenState extends State<HubDetailScreen>
|
||||
unawaited(_loadMoreItems());
|
||||
}
|
||||
|
||||
Widget _buildContinuationStatusSliver() {
|
||||
final error = _continuationErrorMessage;
|
||||
return SliverToBoxAdapter(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Center(
|
||||
child: error == null
|
||||
? const CircularProgressIndicator()
|
||||
: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(error, textAlign: TextAlign.center),
|
||||
const SizedBox(height: 8),
|
||||
TextButton(onPressed: _retryHubContinuation, child: Text(t.common.retry)),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void refresh() {
|
||||
_loadMoreItems();
|
||||
@@ -437,6 +586,8 @@ class _HubDetailScreenState extends State<HubDetailScreen>
|
||||
);
|
||||
},
|
||||
),
|
||||
if (_filteredItems.isNotEmpty && (_isLoadingMore || _continuationErrorMessage != null))
|
||||
_buildContinuationStatusSliver(),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user