A cross platform Audiobookshelf client for Android and iOS
Dart
Add real-time socket events for library, series, collection, and user changes
Nathan Poulson1433b45e3ccc
diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dartindex cfef6d51..c274abfc 100644--- a/lib/providers/library_provider.dart+++ b/lib/providers/library_provider.dart@@ -452,9 +452,16 @@ class LibraryProvider extends ChangeNotifier {ProgressSyncService().flushPendingSync(api: _api!);DownloadService().enrichMetadata(_api!);}- // Connect Socket.IO for online presence+ // Connect Socket.IO for online presence + cross-device syncif (auth.serverUrl != null && auth.token != null) {- SocketService().connect(auth.serverUrl!, auth.token!);+ final socket = SocketService();+ socket.onProgressUpdated = _onRemoteProgressUpdated;+ socket.onItemUpdated = _onRemoteItemUpdated;+ socket.onItemRemoved = _onRemoteItemRemoved;+ socket.onSeriesUpdated = _onRemoteSeriesUpdated;+ socket.onCollectionUpdated = _onRemoteCollectionUpdated;+ socket.onUserUpdated = _onRemoteUserUpdated;+ socket.connect(auth.serverUrl!, auth.token!);}debugPrint('[Library] Calling loadLibraries()');loadLibraries();@@ -572,6 +579,60 @@ class LibraryProvider extends ChangeNotifier {if (_deviceHasConnectivity && !_manualOffline) _startServerPingTimer();}+ /// Handle a progress update pushed from the server (cross-device sync).+ void _onRemoteProgressUpdated(Map<String, dynamic> mp) {+ final itemId = mp['libraryItemId'] as String?;+ final episodeId = mp['episodeId'] as String?;+ if (itemId == null) return;+ final key = episodeId != null ? '$itemId-$episodeId' : itemId;+ _progressMap[key] = mp;+ _localProgressOverrides.remove(key);+ _resetItems.remove(key);+ notifyListeners();+ }++ /// Handle library item added or updated from socket.+ void _onRemoteItemUpdated(Map<String, dynamic> data) {+ // Refresh the personalized view so new/updated items appear+ loadPersonalizedView(force: true);+ }++ /// Handle library item removed from socket.+ void _onRemoteItemRemoved(Map<String, dynamic> data) {+ loadPersonalizedView(force: true);+ }++ /// Handle series changes from socket.+ void _onRemoteSeriesUpdated() {+ loadPersonalizedView(force: true);+ loadSeries();+ }++ /// Handle collection changes from socket.+ void _onRemoteCollectionUpdated() {+ loadPersonalizedView(force: true);+ }++ /// Handle current user updated from socket.+ void _onRemoteUserUpdated(Map<String, dynamic> data) {+ // Rebuild progress map from updated user data+ final progressList = data['mediaProgress'] as List<dynamic>?;+ if (progressList != null) {+ for (final mp in progressList) {+ if (mp is Map<String, dynamic>) {+ final itemId = mp['libraryItemId'] as String?;+ final episodeId = mp['episodeId'] as String?;+ if (itemId != null) {+ final key = episodeId != null ? '$itemId-$episodeId' : itemId;+ _progressMap[key] = mp;+ _localProgressOverrides.remove(key);+ }+ }+ }+ notifyListeners();+ }+ }+void _buildProgressMap(AuthProvider auth) {_progressMap = {};final userJson = auth.userJson;diff --git a/lib/services/socket_service.dart b/lib/services/socket_service.dartindex cafdc00b..e6b71066 100644--- a/lib/services/socket_service.dart+++ b/lib/services/socket_service.dart@@ -9,6 +9,24 @@ class SocketService {IO.Socket? _socket;String? _token;+ /// Called when the server pushes a progress update (cross-device sync).+ void Function(Map<String, dynamic> progress)? onProgressUpdated;++ /// Called when a library item is added, updated, or removed.+ void Function(Map<String, dynamic> data)? onItemUpdated;++ /// Called when a library item is removed.+ void Function(Map<String, dynamic> data)? onItemRemoved;++ /// Called when series data changes.+ void Function()? onSeriesUpdated;++ /// Called when a collection changes.+ void Function()? onCollectionUpdated;++ /// Called when the current user's data changes on the server.+ void Function(Map<String, dynamic> data)? onUserUpdated;+void connect(String serverUrl, String token) {if (_socket != null) disconnect();@@ -35,6 +53,65 @@ class SocketService {disconnect();});+ // Cross-device progress sync+ _socket!.on('user_item_progress_updated', (data) {+ if (data is Map<String, dynamic>) {+ final patch = data['data'] as Map<String, dynamic>?;+ if (patch != null) {+ debugPrint('[Socket] Progress updated for ${patch['libraryItemId']}');+ onProgressUpdated?.call(patch);+ }+ }+ });++ // Library item changes+ _socket!.on('item_added', (data) {+ debugPrint('[Socket] Item added');+ if (data is Map<String, dynamic>) onItemUpdated?.call(data);+ });+ _socket!.on('item_updated', (data) {+ debugPrint('[Socket] Item updated');+ if (data is Map<String, dynamic>) onItemUpdated?.call(data);+ });+ _socket!.on('item_removed', (data) {+ debugPrint('[Socket] Item removed');+ if (data is Map<String, dynamic>) onItemRemoved?.call(data);+ });++ // Series changes+ _socket!.on('series_added', (_) {+ debugPrint('[Socket] Series added');+ onSeriesUpdated?.call();+ });+ _socket!.on('series_updated', (_) {+ debugPrint('[Socket] Series updated');+ onSeriesUpdated?.call();+ });+ _socket!.on('series_removed', (_) {+ debugPrint('[Socket] Series removed');+ onSeriesUpdated?.call();+ });++ // Collection changes+ _socket!.on('collection_added', (_) {+ debugPrint('[Socket] Collection added');+ onCollectionUpdated?.call();+ });+ _socket!.on('collection_updated', (_) {+ debugPrint('[Socket] Collection updated');+ onCollectionUpdated?.call();+ });+ _socket!.on('collection_removed', (_) {+ debugPrint('[Socket] Collection removed');+ onCollectionUpdated?.call();+ });++ // Current user updated+ _socket!.on('user_updated', (data) {+ debugPrint('[Socket] User updated');+ if (data is Map<String, dynamic>) onUserUpdated?.call(data);+ });+_socket!.onDisconnect((_) {debugPrint('[Socket] Disconnected');});@@ -53,5 +130,11 @@ class SocketService {_socket?.dispose();_socket = null;_token = null;+ onProgressUpdated = null;+ onItemUpdated = null;+ onItemRemoved = null;+ onSeriesUpdated = null;+ onCollectionUpdated = null;+ onUserUpdated = null;}}