A cross platform Audiobookshelf client for Android and iOS
Dart
Fix bookmark sync: server-authoritative, socket-driven updates
Nathan Poulsonfd21704a026a
diff --git a/lib/providers/_lp_core.dart b/lib/providers/_lp_core.dartindex 4f4da03f..d1c728d0 100644--- a/lib/providers/_lp_core.dart+++ b/lib/providers/_lp_core.dart@@ -734,6 +734,28 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {}void _onRemoteUserUpdated(Map<String, dynamic> data) {+ // Sync bookmarks from user_updated event+ final bookmarks = data['bookmarks'] as List<dynamic>?;+ if (bookmarks != null && _api != null) {+ // Group server bookmarks by libraryItemId+ final byItem = <String, List<Map<String, dynamic>>>{};+ for (final b in bookmarks) {+ if (b is Map<String, dynamic>) {+ final id = b['libraryItemId'] as String? ?? '';+ if (id.isNotEmpty) byItem.putIfAbsent(id, () => []).add(b);+ }+ }+ // Sync each item that has bookmarks, plus the currently playing item+ final player = AudioPlayerService();+ final currentId = player.currentItemId;+ final idsToSync = <String>{...byItem.keys};+ if (currentId != null) idsToSync.add(currentId);+ for (final itemId in idsToSync) {+ BookmarkService().syncBookmarks(itemId, _api!,+ preloadedServerBookmarks: byItem[itemId] ?? []);+ }+ }+final progressList = data['mediaProgress'] as List<dynamic>?;if (progressList != null) {final player = AudioPlayerService();diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dartindex c69cd39e..525c49e4 100644--- a/lib/providers/library_provider.dart+++ b/lib/providers/library_provider.dart@@ -12,6 +12,7 @@ import '../services/progress_sync_service.dart';import '../services/download_service.dart';import '../services/android_auto_service.dart';import '../services/chromecast_service.dart';+import '../services/bookmark_service.dart';import '../services/socket_service.dart';import '../main.dart' show scaffoldMessengerKey;diff --git a/lib/services/bookmark_service.dart b/lib/services/bookmark_service.dartindex 31e9dc91..ef77637d 100644--- a/lib/services/bookmark_service.dart+++ b/lib/services/bookmark_service.dart@@ -82,6 +82,8 @@ class BookmarkService {static const int _maxBookmarksPerBook = 100;static const _keyPrefix = 'bookmarks_';+ // Track bookmarks not yet pushed to server (created offline)+ final Set<String> _unpushed = {}; // "itemId::position" keys/// Get all bookmarks for a book.Future<List<Bookmark>> getBookmarks(String itemId, {String sort = 'newest'}) async {@@ -132,8 +134,12 @@ class BookmarkService {debugPrint('[Bookmarks] Added "${bookmark.title}" at ${bookmark.formattedPosition}');// Push to server+ final unpushedKey = '$itemId::${positionSeconds.toStringAsFixed(1)}';if (api != null) {- await api.createBookmark(itemId, time: positionSeconds, title: bookmark.serverTitle);+ final ok = await api.createBookmark(itemId, time: positionSeconds, title: bookmark.serverTitle);+ if (!ok) _unpushed.add(unpushedKey);+ } else {+ _unpushed.add(unpushedKey);}return bookmark;@@ -243,16 +249,33 @@ class BookmarkService {}}- // Find local bookmarks not on server - push them+ // Remove local bookmarks that no longer exist on server,+ // but push any that were created offline and haven't been synced yet.final refreshedLocal = await getBookmarks(itemId);+ final kept = <Bookmark>[];+ bool changed = false;for (final lb in refreshedLocal) {final serverMatch = serverBookmarks.where((sb) =>posMatch((sb['time'] as num?)?.toDouble() ?? 0, lb.positionSeconds)).firstOrNull;- if (serverMatch == null) {- await api.createBookmark(itemId, time: lb.positionSeconds, title: lb.serverTitle);- debugPrint('[Bookmarks] Pushed to server: "${lb.serverTitle}" at ${lb.formattedPosition}');+ if (serverMatch != null) {+ kept.add(lb);+ } else {+ final unpushedKey = '$itemId::${lb.positionSeconds.toStringAsFixed(1)}';+ if (_unpushed.contains(unpushedKey)) {+ // Created offline, push now+ await api.createBookmark(itemId, time: lb.positionSeconds, title: lb.serverTitle);+ _unpushed.remove(unpushedKey);+ debugPrint('[Bookmarks] Pushed offline bookmark: "${lb.title}" at ${lb.formattedPosition}');+ kept.add(lb);+ } else {+ debugPrint('[Bookmarks] Removed locally (deleted on server): "${lb.title}" at ${lb.formattedPosition}');+ changed = true;+ }}}+ if (changed) {+ await _saveAll(itemId, kept);+ }} catch (e) {debugPrint('[Bookmarks] Sync error: $e');}