Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Keep playlists and the authors tab in sync with the server

Barnabas8d677e0620ef
diff --git a/lib/providers/_lp_core.dart b/lib/providers/_lp_core.dart
index 402cab00..6ee852ba 100644
--- a/lib/providers/_lp_core.dart
+++ b/lib/providers/_lp_core.dart
@@ -1171,6 +1171,10 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
loadCollections();
}
+ void _onRemotePlaylistUpdated() {
+ loadPlaylists();
+ }
+
void _onRemoteUserUpdated(Map<String, dynamic> data) {
// Sync bookmarks from user_updated event
final bookmarks = data['bookmarks'] as List<dynamic>?;
diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dart
index 851faaee..15510655 100644
--- a/lib/providers/library_provider.dart
+++ b/lib/providers/library_provider.dart
@@ -187,6 +187,7 @@ class LibraryProvider extends ChangeNotifier
socket.onItemRemoved = _onRemoteItemRemoved;
socket.onSeriesUpdated = _onRemoteSeriesUpdated;
socket.onCollectionUpdated = _onRemoteCollectionUpdated;
+ socket.onPlaylistUpdated = _onRemotePlaylistUpdated;
socket.onUserUpdated = _onRemoteUserUpdated;
socket.onReconnectFailed = _onSocketReconnectFailed;
socket.onEncodeFinished = _onEncodeFinished;
diff --git a/lib/screens/library_screen.dart b/lib/screens/library_screen.dart
index 1570421b..6bc13648 100644
--- a/lib/screens/library_screen.dart
+++ b/lib/screens/library_screen.dart
@@ -10,6 +10,7 @@ import '../providers/auth_provider.dart';
import '../providers/library_provider.dart';
import '../services/api_service.dart';
import '../services/book_search_index.dart';
+import '../services/socket_service.dart';
import '../services/download_service.dart';
import '../services/audio_player_service.dart';
import '../widgets/absorb_page_header.dart';
@@ -252,6 +253,28 @@ class LibraryScreenState extends State<LibraryScreen> with TickerProviderStateMi
if (mounted) AppShell.notifyScreenReady(1);
});
_loadRmabConfigured();
+ SocketService().addAuthorsChangedListener(_onAuthorsChanged);
+ }
+
+ // Live-refresh the authors tab when authors change on the server (edits,
+ // quick-match, books moving around). Silent swap - no spinner flag - so an
+ // open tab doesn't flash while it updates.
+ Timer? _authorsRefreshDebounce;
+ void _onAuthorsChanged() {
+ if (!mounted || !_authorsLoaded) return;
+ _authorsRefreshDebounce?.cancel();
+ _authorsRefreshDebounce = Timer(const Duration(seconds: 2), () async {
+ if (!mounted || !_authorsLoaded) return;
+ final lib = context.read<LibraryProvider>();
+ final api = context.read<AuthProvider>().apiService;
+ if (api == null || lib.selectedLibraryId == null || lib.isOffline) return;
+ final authors = await api.getLibraryAuthors(lib.selectedLibraryId!);
+ if (!mounted) return;
+ setState(() {
+ _authors = authors;
+ _sortAuthors();
+ });
+ });
}
Future<void> _loadRmabConfigured() async {
@@ -587,6 +610,8 @@ class LibraryScreenState extends State<LibraryScreen> with TickerProviderStateMi
@override
void dispose() {
+ SocketService().removeAuthorsChangedListener(_onAuthorsChanged);
+ _authorsRefreshDebounce?.cancel();
_debounce?.cancel();
_searchController.dispose();
_focusNode.dispose();
diff --git a/lib/services/socket_service.dart b/lib/services/socket_service.dart
index 8329ff23..8954995b 100644
--- a/lib/services/socket_service.dart
+++ b/lib/services/socket_service.dart
@@ -46,6 +46,9 @@ class SocketService {
/// Called when a collection changes.
void Function()? onCollectionUpdated;
+ /// Called when one of the user's playlists changes.
+ void Function()? onPlaylistUpdated;
+
/// Called when the current user's data changes on the server.
void Function(Map<String, dynamic> data)? onUserUpdated;
@@ -156,6 +159,22 @@ class SocketService {
}
}
+ // Author change fan-out (added/updated/removed) so the authors tab can
+ // live-refresh after quick-match or edits.
+ final List<VoidCallback> _authorsChangedListeners = [];
+
+ void addAuthorsChangedListener(VoidCallback fn) {
+ if (!_authorsChangedListeners.contains(fn)) _authorsChangedListeners.add(fn);
+ }
+ void removeAuthorsChangedListener(VoidCallback fn) =>
+ _authorsChangedListeners.remove(fn);
+
+ void _emitAuthorsChanged() {
+ for (final fn in List.of(_authorsChangedListeners)) {
+ fn();
+ }
+ }
+
/// 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.
@@ -267,6 +286,25 @@ class SocketService {
onCollectionUpdated?.call();
});
+ // Playlist changes (server emits these per-user)
+ _socket!.on('playlist_added', (_) {
+ debugPrint('[Socket] Playlist added');
+ onPlaylistUpdated?.call();
+ });
+ _socket!.on('playlist_updated', (_) {
+ debugPrint('[Socket] Playlist updated');
+ onPlaylistUpdated?.call();
+ });
+ _socket!.on('playlist_removed', (_) {
+ debugPrint('[Socket] Playlist removed');
+ onPlaylistUpdated?.call();
+ });
+
+ // Author changes
+ _socket!.on('author_added', (_) => _emitAuthorsChanged());
+ _socket!.on('author_updated', (_) => _emitAuthorsChanged());
+ _socket!.on('author_removed', (_) => _emitAuthorsChanged());
+
// Current user updated
_socket!.on('user_updated', (data) {
debugPrint('[Socket] User updated');
@@ -337,6 +375,7 @@ class SocketService {
onItemRemoved = null;
onSeriesUpdated = null;
onCollectionUpdated = null;
+ onPlaylistUpdated = null;
onUserUpdated = null;
onReconnectFailed = null;
onEncodeFinished = null;
@@ -432,6 +471,14 @@ class SocketService {
_socket!.on('collection_updated', (_) => onCollectionUpdated?.call());
_socket!.on('collection_removed', (_) => onCollectionUpdated?.call());
+ _socket!.on('playlist_added', (_) => onPlaylistUpdated?.call());
+ _socket!.on('playlist_updated', (_) => onPlaylistUpdated?.call());
+ _socket!.on('playlist_removed', (_) => onPlaylistUpdated?.call());
+
+ _socket!.on('author_added', (_) => _emitAuthorsChanged());
+ _socket!.on('author_updated', (_) => _emitAuthorsChanged());
+ _socket!.on('author_removed', (_) => _emitAuthorsChanged());
+
_socket!.on('user_updated', (data) {
if (data is Map<String, dynamic>) onUserUpdated?.call(data);
});