Save Bookmark with Bookmark overview in channel menu, and bookmark-filter in channel and contact.

This commit is contained in:
ericz committed 2026-11-13 23:06:17 +01:00
1 parent c0702806de
commit f32300797e
39 files changed
+1556 -25

No files matched your search

+92
View File
@@ -0,0 +1,92 @@
import 'dart:convert';
import '../models/bookmarked_message.dart';
import '../utils/app_logger.dart';
import 'prefs_manager.dart';
class BookmarkStore {
static const String _keyPrefix = 'message_bookmarks_';
String publicKeyHex = '';
set setPublicKeyHex(String value) =>
publicKeyHex = value.length > 10 ? value.substring(0, 10) : '';
String get keyFor => '$_keyPrefix$publicKeyHex';
Future<void> _pendingWrites = Future<void>.value();
Future<List<BookmarkedMessage>> loadBookmarks() async {
final writes = _pendingWrites;
await writes;
return _readBookmarks(keyFor);
}
Future<void> saveBookmark(BookmarkedMessage bookmark) {
if (publicKeyHex.isEmpty) return Future<void>.value();
final key = keyFor;
return _enqueue(() async {
final bookmarks = await _readBookmarks(key);
bookmarks.removeWhere(
(existing) => existing.storageId == bookmark.storageId,
);
bookmarks.add(bookmark);
await _writeBookmarks(key, bookmarks);
});
}
Future<void> removeBookmark(BookmarkedMessage bookmark) {
if (publicKeyHex.isEmpty) return Future<void>.value();
final key = keyFor;
return _enqueue(() async {
final bookmarks = await _readBookmarks(key);
bookmarks.removeWhere(
(existing) => existing.storageId == bookmark.storageId,
);
await _writeBookmarks(key, bookmarks);
});
}
Future<void> _enqueue(Future<void> Function() operation) {
final next = _pendingWrites.then((_) => operation());
_pendingWrites = next.then<void>(
(_) {},
onError: (Object _, StackTrace _) {},
);
return next;
}
Future<List<BookmarkedMessage>> _readBookmarks(String key) async {
if (key == _keyPrefix) return [];
final raw = PrefsManager.instance.getString(key);
if (raw == null || raw.isEmpty) return [];
try {
final decoded = jsonDecode(raw);
if (decoded is! List) return [];
final bookmarks = <BookmarkedMessage>[];
for (final value in decoded) {
if (value is! Map<String, dynamic>) continue;
try {
bookmarks.add(BookmarkedMessage.fromJson(value));
} on Object catch (error) {
appLogger.warn('Skipping malformed bookmark: $error');
}
}
return bookmarks;
} on FormatException catch (error) {
appLogger.warn('Stored bookmarks are unreadable: $error');
return [];
}
}
Future<void> _writeBookmarks(
String key,
List<BookmarkedMessage> bookmarks,
) async {
if (!await PrefsManager.instance.setString(
key,
jsonEncode(bookmarks.map((bookmark) => bookmark.toJson()).toList()),
)) {
throw StateError('Could not save bookmarks');
}
}
}
+12
View File
@@ -123,6 +123,16 @@ class ChannelMessageStore {
return messages;
}
Future<void> updateBookmarkFlag(
String channelId,
String messageId,
bool isBookmarked,
) => _archive.updateBookmarkFlag(
_keyForChannel(channelId),
messageId,
isBookmarked,
);
Future<void> clearChannelMessages(String channelId) =>
_archive.clear(HistoryStore.instance, _keyForChannel(channelId));
@@ -149,6 +159,7 @@ class ChannelMessageStore {
'timestamp': msg.timestamp.millisecondsSinceEpoch,
'receivedAt': msg.receivedAt.millisecondsSinceEpoch,
'isOutgoing': msg.isOutgoing,
'isBookmarked': msg.isBookmarked,
'status': msg.status.index,
'channelIndex': msg.channelIndex,
'region': msg.region,
@@ -226,6 +237,7 @@ class ChannelMessageStore {
timestamp: DateTime.fromMillisecondsSinceEpoch(json['timestamp'] as int),
receivedAt: legacyAwareReceivedAt(json),
isOutgoing: json['isOutgoing'] as bool? ?? false,
isBookmarked: json['isBookmarked'] as bool? ?? false,
status: _statusAt(json['status']) ?? ChannelMessageStatus.failed,
repeatCount: (json['repeatCount'] as int?) ?? 0,
pathLength: decodedPathLength,
+45
View File
@@ -121,6 +121,51 @@ class HistoryArchive {
(_archives[HistoryStore.instance] ??= {})[key] = _ArchiveState(count);
}
Future<void> updateBookmarkFlag(
String key,
String messageId,
bool isBookmarked,
) {
final store = HistoryStore.instance;
final state = _stateFor(store, key);
return _serialize(store, key, () async {
String? update(String raw) {
try {
final decoded = jsonDecode(raw);
if (decoded is! Map<String, dynamic> ||
mergeKey(decoded) != 'id:$messageId') {
return null;
}
decoded['isBookmarked'] = isBookmarked;
return jsonEncode(decoded);
} on FormatException {
return null;
}
}
for (var i = 0; i < state.pending.length; i++) {
final entry = state.pending[i];
final replacement = update(entry.$2);
if (replacement != null) state.pending[i] = (entry.$1, replacement);
}
final entries = await store.readEntries(key);
var changed = false;
final updated = <String>[];
for (final entry in entries) {
final replacement = update(entry);
updated.add(replacement ?? entry);
changed = changed || replacement != null;
}
if (changed &&
!await store.setString(
key,
jsonEncode(updated.map(jsonDecode).toList()),
)) {
throw StateError('Could not update message bookmark flag');
}
});
}
/// Only entries trimmed since the last successful save need buffering.
void archive(String key, Iterable<String> entries) {
final state = _stateFor(HistoryStore.instance, key);
+15
View File
@@ -175,6 +175,19 @@ class MessageStore {
return messages;
}
Future<void> updateBookmarkFlag(
String contactKeyHex,
String messageId,
bool isBookmarked,
) {
if (publicKeyHex.isEmpty) return Future<void>.value();
return _archive.updateBookmarkFlag(
'$keyFor$contactKeyHex',
messageId,
isBookmarked,
);
}
Future<void> clearMessages(String contactKeyHex) {
if (publicKeyHex.isEmpty) return Future<void>.value();
return _archive.clear(HistoryStore.instance, '$keyFor$contactKeyHex');
@@ -187,6 +200,7 @@ class MessageStore {
'timestamp': msg.timestamp.millisecondsSinceEpoch,
'receivedAt': msg.receivedAt.millisecondsSinceEpoch,
'isOutgoing': msg.isOutgoing,
'isBookmarked': msg.isBookmarked,
'isCli': msg.isCli,
'status': msg.status.index,
'messageId': msg.messageId,
@@ -257,6 +271,7 @@ class MessageStore {
timestamp: DateTime.fromMillisecondsSinceEpoch(json['timestamp'] as int),
receivedAt: legacyAwareReceivedAt(json),
isOutgoing: json['isOutgoing'] as bool? ?? false,
isBookmarked: json['isBookmarked'] as bool? ?? false,
isCli: isCli,
status: _statusAt(json['status']) ?? MessageStatus.failed,
messageId: json['messageId'] as String?,