Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Update open book and podcast pages live when things change on the server

Barnabasf017931152cc
diff --git a/lib/screens/admin_missing_items_screen.dart b/lib/screens/admin_missing_items_screen.dart
index 8513746c..8ad4354a 100644
--- a/lib/screens/admin_missing_items_screen.dart
+++ b/lib/screens/admin_missing_items_screen.dart
@@ -1,8 +1,10 @@
+import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:cached_network_image/cached_network_image.dart';
import '../l10n/app_localizations.dart';
import '../providers/auth_provider.dart';
+import '../services/socket_service.dart';
import '../widgets/absorb_page_header.dart';
/// Admin cleanup screen: lists the items a library has flagged as "issues" -
@@ -33,12 +35,32 @@ class _AdminMissingItemsScreenState extends State<AdminMissingItemsScreen> {
void initState() {
super.initState();
_load();
+ SocketService().addItemsChangedListener(_onItemsChanged);
+ }
+
+ @override
+ void dispose() {
+ SocketService().removeItemsChangedListener(_onItemsChanged);
+ _liveRefreshDebounce?.cancel();
+ super.dispose();
+ }
+
+ // A running scan can flag or clear items while this list is open.
+ Timer? _liveRefreshDebounce;
+ void _onItemsChanged() {
+ if (!mounted) return;
+ _liveRefreshDebounce?.cancel();
+ _liveRefreshDebounce = Timer(const Duration(seconds: 2), () {
+ if (mounted && _deleting.isEmpty) _load();
+ });
}
Future<void> _load() async {
final api = context.read<AuthProvider>().apiService;
if (api == null) return;
- setState(() => _loading = true);
+ // Full-screen spinner only before the first result; live refreshes and
+ // pull-to-refresh swap the list in place.
+ if (_items.isEmpty) setState(() => _loading = true);
final items = await api.getIssueItems(_libraryId);
if (!mounted) return;
setState(() {
diff --git a/lib/screens/admin_podcasts_screen.dart b/lib/screens/admin_podcasts_screen.dart
index 5b23a6aa..d3ecac71 100644
--- a/lib/screens/admin_podcasts_screen.dart
+++ b/lib/screens/admin_podcasts_screen.dart
@@ -1,8 +1,10 @@
+import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:provider/provider.dart';
import '../providers/auth_provider.dart';
+import '../services/socket_service.dart';
import 'podcast_edit_screen.dart';
import '../widgets/absorb_page_header.dart';
import '../widgets/html_description.dart';
@@ -952,6 +954,18 @@ class _PodcastDetailScreenState extends State<_PodcastDetailScreen> with SingleT
_reloadItem(); // Load full item with episodes
_loadFeed(); // Pre-load feed so it's ready when user switches tabs
_pollDownloadQueue(); // Check for any in-progress downloads
+ SocketService().addItemUpdatedListener(_onSocketItemUpdated);
+ }
+
+ // Live-refresh when this show changes on the server (episode downloads
+ // finishing, metadata edits from the web UI).
+ Timer? _liveRefreshDebounce;
+ void _onSocketItemUpdated(Map<String, dynamic> data) {
+ if (!mounted || data['id'] != _podcastId) return;
+ _liveRefreshDebounce?.cancel();
+ _liveRefreshDebounce = Timer(const Duration(milliseconds: 800), () {
+ if (mounted) _reloadItem();
+ });
}
int _lastTabIndex = 0;
@@ -972,7 +986,14 @@ class _PodcastDetailScreenState extends State<_PodcastDetailScreen> with SingleT
}
@override
- void dispose() { _pollingQueue = false; _tabCtrl.removeListener(_onTabChanged); _tabCtrl.dispose(); super.dispose(); }
+ void dispose() {
+ SocketService().removeItemUpdatedListener(_onSocketItemUpdated);
+ _liveRefreshDebounce?.cancel();
+ _pollingQueue = false;
+ _tabCtrl.removeListener(_onTabChanged);
+ _tabCtrl.dispose();
+ super.dispose();
+ }
Future<void> _removeShow() async {
final l = AppLocalizations.of(context)!;
diff --git a/lib/services/socket_service.dart b/lib/services/socket_service.dart
index 55f24413..2cbe45a6 100644
--- a/lib/services/socket_service.dart
+++ b/lib/services/socket_service.dart
@@ -115,6 +115,31 @@ class SocketService {
}
}
+ // Per-item update fan-out carrying the item JSON, so an open detail view can
+ // live-refresh when its item changes on the server (web UI edits, scans,
+ // finished episode downloads). Bulk events fan out one call per item.
+ final List<void Function(Map<String, dynamic>)> _itemUpdatedListeners = [];
+
+ void addItemUpdatedListener(void Function(Map<String, dynamic>) fn) {
+ if (!_itemUpdatedListeners.contains(fn)) _itemUpdatedListeners.add(fn);
+ }
+ void removeItemUpdatedListener(void Function(Map<String, dynamic>) fn) =>
+ _itemUpdatedListeners.remove(fn);
+
+ void _emitItemUpdated(dynamic data) {
+ if (data is Map<String, dynamic>) {
+ for (final fn in List.of(_itemUpdatedListeners)) {
+ fn(data);
+ }
+ } else if (data is List) {
+ for (final item in data.whereType<Map<String, dynamic>>()) {
+ for (final fn in List.of(_itemUpdatedListeners)) {
+ fn(item);
+ }
+ }
+ }
+ }
+
/// Called when ereader devices change. Server emits this both for the
/// per-user update (always) and admin-wide updates (only to admins).
/// Payload shape: { ereaderDevices: [...] } already filtered for this user.
@@ -169,11 +194,13 @@ class SocketService {
_socket!.on('item_added', (data) {
debugPrint('[Socket] Item added');
if (data is Map<String, dynamic>) onItemUpdated?.call(data);
+ _emitItemUpdated(data);
_emitItemsChanged();
});
_socket!.on('item_updated', (data) {
debugPrint('[Socket] Item updated');
if (data is Map<String, dynamic>) onItemUpdated?.call(data);
+ _emitItemUpdated(data);
_emitItemsChanged();
});
_socket!.on('item_removed', (data) {
@@ -182,12 +209,14 @@ class SocketService {
_emitItemsChanged();
});
// Bulk events the scanner emits in chunks while a scan runs
- _socket!.on('items_updated', (_) {
+ _socket!.on('items_updated', (data) {
debugPrint('[Socket] Items updated (bulk)');
+ _emitItemUpdated(data);
_emitItemsChanged();
});
- _socket!.on('items_added', (_) {
+ _socket!.on('items_added', (data) {
debugPrint('[Socket] Items added (bulk)');
+ _emitItemUpdated(data);
_emitItemsChanged();
});
@@ -352,18 +381,26 @@ class SocketService {
_socket!.on('item_added', (data) {
if (data is Map<String, dynamic>) onItemUpdated?.call(data);
+ _emitItemUpdated(data);
_emitItemsChanged();
});
_socket!.on('item_updated', (data) {
if (data is Map<String, dynamic>) onItemUpdated?.call(data);
+ _emitItemUpdated(data);
_emitItemsChanged();
});
_socket!.on('item_removed', (data) {
if (data is Map<String, dynamic>) onItemRemoved?.call(data);
_emitItemsChanged();
});
- _socket!.on('items_updated', (_) => _emitItemsChanged());
- _socket!.on('items_added', (_) => _emitItemsChanged());
+ _socket!.on('items_updated', (data) {
+ _emitItemUpdated(data);
+ _emitItemsChanged();
+ });
+ _socket!.on('items_added', (data) {
+ _emitItemUpdated(data);
+ _emitItemsChanged();
+ });
_socket!.on('series_added', (_) => onSeriesUpdated?.call());
_socket!.on('series_updated', (_) => onSeriesUpdated?.call());
diff --git a/lib/widgets/book_detail_sheet.dart b/lib/widgets/book_detail_sheet.dart
index e34c91b9..4a212a1e 100644
--- a/lib/widgets/book_detail_sheet.dart
+++ b/lib/widgets/book_detail_sheet.dart
@@ -1,3 +1,4 @@
+import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:ui' as ui;
@@ -28,6 +29,7 @@ import '../services/download_service.dart';
import '../services/ebook_cache.dart';
import '../services/progress_sync_service.dart';
import '../services/metadata_override_service.dart';
+import '../services/socket_service.dart';
import '../services/scoped_prefs.dart';
import '../main.dart' show rootNavigatorKey, colorSourceNotifier, useColorEverywhereNotifier, manualSeedNotifier, manualColorScheme;
import '../screens/app_shell.dart';
@@ -130,6 +132,7 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {
}
_loadItem();
_loadBookmarks();
+ SocketService().addItemUpdatedListener(_onSocketItemUpdated);
PlayerSettings.getRectangleCovers().then((v) { if (mounted) setState(() => _squareCovers = !v); });
PlayerSettings.getShowGoodreadsButton().then((v) { if (mounted) setState(() => _showGoodreads = v); });
ScopedPrefs.getStringList('saved_ebooks').then((list) {
@@ -139,6 +142,24 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {
});
}
+ @override
+ void dispose() {
+ SocketService().removeItemUpdatedListener(_onSocketItemUpdated);
+ _liveRefreshDebounce?.cancel();
+ super.dispose();
+ }
+
+ // Live-refresh when this item changes on the server (web UI edit, scan,
+ // match) so an open sheet shows the new cover/metadata without reopening.
+ Timer? _liveRefreshDebounce;
+ void _onSocketItemUpdated(Map<String, dynamic> data) {
+ if (!mounted || data['id'] != widget.itemId) return;
+ _liveRefreshDebounce?.cancel();
+ _liveRefreshDebounce = Timer(const Duration(milliseconds: 800), () {
+ if (mounted) _loadItem();
+ });
+ }
+
Future<void> _loadBookmarks() async {
final bm = await BookmarkService().getBookmarks(widget.itemId, sort: 'position');
if (mounted) setState(() => _bookmarks = bm);
diff --git a/lib/widgets/episode_list_sheet.dart b/lib/widgets/episode_list_sheet.dart
index caa20fe4..881f6a40 100644
--- a/lib/widgets/episode_list_sheet.dart
+++ b/lib/widgets/episode_list_sheet.dart
@@ -1,3 +1,4 @@
+import 'dart:async';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
@@ -12,6 +13,7 @@ import '../services/audio_player_service.dart';
import '../services/download_service.dart';
import '../services/chromecast_service.dart';
import '../providers/auth_provider.dart';
+import '../services/socket_service.dart';
import 'card_buttons.dart';
import 'html_description.dart';
import 'stackable_sheet.dart';
@@ -99,6 +101,35 @@ class _EpisodeListSheetState extends State<EpisodeListSheet> {
_loadEpisodes();
_loadAutoDownloadState();
_loadNewEpisodePosition();
+ SocketService().addItemUpdatedListener(_onSocketItemUpdated);
+ }
+
+ @override
+ void dispose() {
+ SocketService().removeItemUpdatedListener(_onSocketItemUpdated);
+ _liveRefreshDebounce?.cancel();
+ super.dispose();
+ }
+
+ // Live-refresh the episode list when this show changes on the server, e.g.
+ // an episode download finishing or a new episode arriving via auto-download.
+ // Always fetches fresh - _loadEpisodes would early-return on the stale
+ // episode list already embedded in the sheet's item snapshot.
+ Timer? _liveRefreshDebounce;
+ void _onSocketItemUpdated(Map<String, dynamic> data) {
+ if (!mounted || data['id'] != _itemId) return;
+ _liveRefreshDebounce?.cancel();
+ _liveRefreshDebounce = Timer(const Duration(milliseconds: 800), () async {
+ if (!mounted) return;
+ final lib = context.read<LibraryProvider>();
+ final api = context.read<AuthProvider>().apiService;
+ if (api == null || lib.isOffline) return;
+ final fullItem = await api.getLibraryItem(_itemId);
+ if (fullItem == null || !mounted) return;
+ final media = fullItem['media'] as Map<String, dynamic>? ?? {};
+ final episodes = media['episodes'] as List<dynamic>? ?? [];
+ setState(() => _episodes = _sortEpisodes(episodes));
+ });
}
void _loadNewEpisodePosition() async {