Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Show renamed and newly added books correctly in search without restarting the app

Barnabas99c37d33c05a
diff --git a/lib/services/book_search_index.dart b/lib/services/book_search_index.dart
index 50b19c7c..7df95382 100644
--- a/lib/services/book_search_index.dart
+++ b/lib/services/book_search_index.dart
@@ -1,6 +1,7 @@
import 'package:flutter/foundation.dart';
import '../utils/fuzzy_search.dart';
import 'api_service.dart';
+import 'socket_service.dart';
/// A scored search result: the raw library item plus whether it matched on the
/// title (so callers can keep a "Books" section to title hits).
@@ -23,15 +24,44 @@ class _Indexed {
/// In-memory, per-library index of all book items, used for lenient client-side
/// search (out-of-order words, punctuation, typos) that the strict server
-/// `/search` endpoint can't do. Built once per library per session.
+/// `/search` endpoint can't do. Built once per library per session, then kept
+/// current by patching single entries from socket item events (edits, adds,
+/// removals) - the payloads carry the full item, so no refetch is needed.
class BookSearchIndex {
- BookSearchIndex._();
+ BookSearchIndex._() {
+ SocketService()
+ ..addItemUpdatedListener(_onItemUpdated)
+ ..addItemRemovedListener(_onItemRemoved);
+ }
static final BookSearchIndex instance = BookSearchIndex._();
factory BookSearchIndex() => instance;
final Map<String, List<_Indexed>> _cache = {};
final Map<String, Future<void>> _inFlight = {};
+ void _onItemUpdated(Map<String, dynamic> item) {
+ final libId = item['libraryId'] as String?;
+ final id = item['id'] as String?;
+ if (libId == null || id == null) return;
+ final idx = _cache[libId];
+ if (idx == null) return; // library not indexed yet - built fresh on demand
+ final entry = _indexItem(item);
+ final i = idx.indexWhere((e) => e.item['id'] == id);
+ if (i >= 0) {
+ idx[i] = entry;
+ } else {
+ idx.add(entry);
+ }
+ }
+
+ void _onItemRemoved(Map<String, dynamic> data) {
+ final id = data['id'] as String?;
+ if (id == null) return;
+ for (final idx in _cache.values) {
+ idx.removeWhere((e) => e.item['id'] == id);
+ }
+ }
+
bool isReady(String libraryId) => _cache.containsKey(libraryId);
/// Drop everything (call on logout / account switch so a reused libraryId
diff --git a/lib/services/socket_service.dart b/lib/services/socket_service.dart
index 2cbe45a6..8329ff23 100644
--- a/lib/services/socket_service.dart
+++ b/lib/services/socket_service.dart
@@ -140,6 +140,22 @@ class SocketService {
}
}
+ // Removal fan-out with payload ({id, ...}), alongside the single-slot
+ // onItemRemoved owned by the library provider.
+ final List<void Function(Map<String, dynamic>)> _itemRemovedListeners = [];
+
+ void addItemRemovedListener(void Function(Map<String, dynamic>) fn) {
+ if (!_itemRemovedListeners.contains(fn)) _itemRemovedListeners.add(fn);
+ }
+ void removeItemRemovedListener(void Function(Map<String, dynamic>) fn) =>
+ _itemRemovedListeners.remove(fn);
+
+ void _emitItemRemoved(Map<String, dynamic> data) {
+ for (final fn in List.of(_itemRemovedListeners)) {
+ fn(data);
+ }
+ }
+
/// 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.
@@ -205,7 +221,10 @@ class SocketService {
});
_socket!.on('item_removed', (data) {
debugPrint('[Socket] Item removed');
- if (data is Map<String, dynamic>) onItemRemoved?.call(data);
+ if (data is Map<String, dynamic>) {
+ onItemRemoved?.call(data);
+ _emitItemRemoved(data);
+ }
_emitItemsChanged();
});
// Bulk events the scanner emits in chunks while a scan runs
@@ -390,7 +409,10 @@ class SocketService {
_emitItemsChanged();
});
_socket!.on('item_removed', (data) {
- if (data is Map<String, dynamic>) onItemRemoved?.call(data);
+ if (data is Map<String, dynamic>) {
+ onItemRemoved?.call(data);
+ _emitItemRemoved(data);
+ }
_emitItemsChanged();
});
_socket!.on('items_updated', (data) {