diff --git a/documentation/channels.md b/documentation/channels.md index d9dd58f7..a8ce6641 100644 --- a/documentation/channels.md +++ b/documentation/channels.md @@ -31,7 +31,7 @@ QuickSwitchBar tab 1 (middle) from any main screen. - Unread badge (if messages are unread) - Drag handle (when manual sort is active) - **"+" FAB** to add a new channel -- **Overflow menu**: Disconnect, Manage Communities, Settings +- **Overflow menu**: Disconnect, Bookmarks, Manage Communities, Settings If no channels exist, an empty state with an "Add Public Channel" shortcut is shown. If a search produces no results, a separate "no results" empty state with a search-off icon is shown. @@ -79,6 +79,7 @@ Tap a channel card to open the channel chat screen. - Tap the title or landscape icon to select a region; see [Regions](regions.md) - Overflow menu (⋮) → **Notifications**: choose all messages, mentions only or muted for this channel. The row shows the current mode - Overflow menu (⋮) → **Edit channel** and **Delete channel**: the same sheet and confirmation as a long-press in the channel list +- Overflow menu (⋮) → **Show bookmarks** toggles the channel's bookmarked-message filter; the bookmark icon is filled while active and outlined while inactive. - Overflow menu (⋮) → **Chat background**: a color or picture, with a fade slider, behind this channel only. See [Chat backgrounds](themes.md#chat-backgrounds). Holiday greetings in new messages play a short animation, as in [direct chat](chat-and-messaging.md#message-list) ### Message Display @@ -87,6 +88,7 @@ Tap a channel card to open the channel chat screen. - **Incoming messages**: Colored avatar with sender's initial (or first emoji if name starts with one; color is deterministic from sender name hash), sender name in primary color, message bubble - **Outgoing messages**: Primary container color bubble with a small status icon: pending (clock), sent (checkmark), or failed (red error circle) - Automatic older-message loading on scroll-to-top +- Long-press a message and choose **Bookmark** to mark it with a yellow icon. Bookmarks are stored separately from channel history and remain available after a channel is deleted. - Jump-to-bottom button when scrolled up - **Pinch-to-zoom**: Two-finger zoom (0.8x–1.8x) and double-tap to reset text size - **Message tracing mode** (when enabled in App Settings): Each bubble additionally shows path prefix bytes (`via XX,YY,...`), a timestamp, and a repeat count icon diff --git a/documentation/chat-and-messaging.md b/documentation/chat-and-messaging.md index cd2af697..33d7b2d8 100644 --- a/documentation/chat-and-messaging.md +++ b/documentation/chat-and-messaging.md @@ -29,6 +29,7 @@ From the Contacts screen, tap any Chat-type contact to open the ChatScreen. - **Holiday animations**: A new message with a holiday greeting ("Happy Halloween", "Merry Christmas", and so on, in any app language) plays a short animation. Turn off in App Settings → Appearance; see [Holiday message animations](themes.md#holiday-message-animations) - **Outgoing messages**: Right-aligned, primary color background. **Failed messages** change to a red-toned error container background - **Incoming messages**: Left-aligned, grey background with a colored avatar (initial letter or first emoji of sender name; color is deterministic from a hash of the sender name) +- Long-press a message and choose **Bookmark** to show a yellow bookmark on the bubble. **Show bookmarks** in the chat menu toggles a filter for bookmarked messages; use **Bookmarks** in the Channels screen menu to browse saved messages, including those from deleted contacts or channels. From the bookmarks list, copy a message with its copy button. Links remain tappable, and supported URL-image previews are shown when **Load images from links** is enabled in App Settings. - Bubble width capped at 72% of screen width - Hyperlinks rendered as tappable green underlined text - Message text follows the system font size (Android/iOS accessibility text size); pinch-to-zoom scales on top of it diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 0c1e668f..eb4f8f3e 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -12,6 +12,7 @@ import 'package:flutter_blue_plus_platform_interface/flutter_blue_plus_platform_ import '../utils/advert_signature.dart'; import '../models/channel.dart'; import '../models/channel_message.dart'; +import '../models/bookmarked_message.dart'; import '../models/companion_radio_stats.dart'; import '../models/contact.dart'; import '../models/message_filter.dart'; @@ -50,6 +51,7 @@ import '../services/notification_reply.dart'; import '../services/notification_service.dart'; import 'meshcore_connector_usb.dart'; import 'meshcore_connector_tcp.dart'; +import '../storage/bookmark_store.dart'; import '../storage/channel_message_store.dart'; import '../storage/channel_order_store.dart'; import '../storage/channel_settings_store.dart'; @@ -514,6 +516,7 @@ class MeshCoreConnector extends ChangeNotifier { // batched here; see [CoalescedWrites]. final CoalescedWrites _coalescedWrites = CoalescedWrites(); final MessageStore _messageStore = MessageStore(); + final BookmarkStore _bookmarkStore = BookmarkStore(); final ChannelOrderStore _channelOrderStore = ChannelOrderStore(); final ChannelSettingsStore _channelSettingsStore = ChannelSettingsStore(); final ChannelRegionStore _channelRegionStore = ChannelRegionStore(); @@ -961,6 +964,66 @@ class MeshCoreConnector extends ChangeNotifier { return _conversations[contact.publicKeyHex] ?? []; } + Future> loadBookmarkedMessages() => + _bookmarkStore.loadBookmarks(); + + Future setBookmarkedMessage( + BookmarkedMessage bookmark, { + required bool isBookmarked, + }) async { + if (_bookmarkStore.publicKeyHex.isEmpty) return; + if (isBookmarked) { + await _bookmarkStore.saveBookmark(bookmark); + } else { + await _bookmarkStore.removeBookmark(bookmark); + } + + if (bookmark.sourceType == BookmarkSourceType.contact) { + final messages = _conversations[bookmark.sourceId]; + final index = messages?.indexWhere( + (message) => message.messageId == bookmark.messageId, + ); + if (messages != null && index != null && index >= 0) { + messages[index] = messages[index].copyWith(isBookmarked: isBookmarked); + await _coalescedWrites.flush(); + await _messageStore.saveMessages(bookmark.sourceId, messages); + } + await _messageStore.updateBookmarkFlag( + bookmark.sourceId, + bookmark.messageId, + isBookmarked, + ); + } else { + final mappedSlot = _slotForChannelId(bookmark.sourceId); + final slot = + mappedSlot ?? + (bookmark.sourceId.startsWith('slot:') + ? int.tryParse(bookmark.sourceId.substring(5)) + : null); + if (slot != null) { + final canonicalSlot = _canonicalSlot(slot); + final messages = _channelMessages[canonicalSlot]; + final index = messages?.indexWhere( + (message) => message.messageId == bookmark.messageId, + ); + if (messages != null && index != null && index >= 0) { + messages[index] = messages[index].copyWith( + isBookmarked: isBookmarked, + ); + await _saveChannelMessages(canonicalSlot, messages); + } + } + if (!bookmark.sourceId.startsWith('slot:')) { + await _channelMessageStore.updateBookmarkFlag( + bookmark.sourceId, + bookmark.messageId, + isBookmarked, + ); + } + } + notifyListeners(); + } + Future deleteMessage(Message message) async { final contactKeyHex = message.senderKeyHex; final messages = _conversations[contactKeyHex]; @@ -9609,6 +9672,7 @@ class MeshCoreConnector extends ChangeNotifier { unawaited(_unreadStore.flush()); _channelMessageStore.setPublicKeyHex = keyHex; _messageStore.setPublicKeyHex = keyHex; + _bookmarkStore.setPublicKeyHex = keyHex; _channelOrderStore.setPublicKeyHex = keyHex; _channelSettingsStore.setPublicKeyHex = keyHex; _channelRegionStore.setPublicKeyHex = keyHex; diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index cda9324f..b6492f08 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -1128,6 +1128,14 @@ }, "chat_messageCopied": "Message copied", "chat_messageDeleted": "Message deleted", + "chat_bookmark": "Bookmark", + "chat_removeBookmark": "Remove bookmark", + "chat_showBookmarks": "Show bookmarks", + "bookmarks_title": "Bookmarks", + "bookmarks_empty": "No bookmarked messages", + "bookmarks_channel": "Channel", + "bookmarks_contact": "Contact", + "bookmarks_remove": "Remove bookmark", "chat_selectMessages": "Select messages", "chat_selectedCount": "{count} selected", "@chat_selectedCount": { diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart index bdd028de..e2305161 100644 --- a/lib/l10n/app_localizations.dart +++ b/lib/l10n/app_localizations.dart @@ -4039,6 +4039,54 @@ abstract class AppLocalizations { /// **'Message deleted'** String get chat_messageDeleted; + /// No description provided for @chat_bookmark. + /// + /// In en, this message translates to: + /// **'Bookmark'** + String get chat_bookmark; + + /// No description provided for @chat_removeBookmark. + /// + /// In en, this message translates to: + /// **'Remove bookmark'** + String get chat_removeBookmark; + + /// No description provided for @chat_showBookmarks. + /// + /// In en, this message translates to: + /// **'Show bookmarks'** + String get chat_showBookmarks; + + /// No description provided for @bookmarks_title. + /// + /// In en, this message translates to: + /// **'Bookmarks'** + String get bookmarks_title; + + /// No description provided for @bookmarks_empty. + /// + /// In en, this message translates to: + /// **'No bookmarked messages'** + String get bookmarks_empty; + + /// No description provided for @bookmarks_channel. + /// + /// In en, this message translates to: + /// **'Channel'** + String get bookmarks_channel; + + /// No description provided for @bookmarks_contact. + /// + /// In en, this message translates to: + /// **'Contact'** + String get bookmarks_contact; + + /// No description provided for @bookmarks_remove. + /// + /// In en, this message translates to: + /// **'Remove bookmark'** + String get bookmarks_remove; + /// No description provided for @chat_selectMessages. /// /// In en, this message translates to: diff --git a/lib/l10n/app_localizations_bg.dart b/lib/l10n/app_localizations_bg.dart index f45c475d..5a0ef5a5 100644 --- a/lib/l10n/app_localizations_bg.dart +++ b/lib/l10n/app_localizations_bg.dart @@ -2280,6 +2280,30 @@ class AppLocalizationsBg extends AppLocalizations { @override String get chat_messageDeleted => 'Съобщението е изтрито'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => 'Избор на съобщения'; diff --git a/lib/l10n/app_localizations_cs.dart b/lib/l10n/app_localizations_cs.dart index b015322c..5d519369 100644 --- a/lib/l10n/app_localizations_cs.dart +++ b/lib/l10n/app_localizations_cs.dart @@ -2264,6 +2264,30 @@ class AppLocalizationsCs extends AppLocalizations { @override String get chat_messageDeleted => 'Zpráva smazána'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => 'Vybrat zprávy'; diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dart index c8d2226d..75746713 100644 --- a/lib/l10n/app_localizations_de.dart +++ b/lib/l10n/app_localizations_de.dart @@ -2284,6 +2284,30 @@ class AppLocalizationsDe extends AppLocalizations { @override String get chat_messageDeleted => 'Nachricht gelöscht'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => 'Nachrichten auswählen'; diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart index 880a8a4b..a43c26ca 100644 --- a/lib/l10n/app_localizations_en.dart +++ b/lib/l10n/app_localizations_en.dart @@ -2239,6 +2239,30 @@ class AppLocalizationsEn extends AppLocalizations { @override String get chat_messageDeleted => 'Message deleted'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => 'Select messages'; diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dart index c14c8fcf..86c1d0a3 100644 --- a/lib/l10n/app_localizations_es.dart +++ b/lib/l10n/app_localizations_es.dart @@ -2274,6 +2274,30 @@ class AppLocalizationsEs extends AppLocalizations { @override String get chat_messageDeleted => 'Mensaje borrado'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => 'Seleccionar mensajes'; diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dart index 61effc54..a422f85b 100644 --- a/lib/l10n/app_localizations_fr.dart +++ b/lib/l10n/app_localizations_fr.dart @@ -2279,6 +2279,30 @@ class AppLocalizationsFr extends AppLocalizations { @override String get chat_messageDeleted => 'Message supprimé'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => 'Sélectionner des messages'; diff --git a/lib/l10n/app_localizations_hu.dart b/lib/l10n/app_localizations_hu.dart index b99d6fe9..8654fc3f 100644 --- a/lib/l10n/app_localizations_hu.dart +++ b/lib/l10n/app_localizations_hu.dart @@ -2272,6 +2272,30 @@ class AppLocalizationsHu extends AppLocalizations { @override String get chat_messageDeleted => 'Üzenet törölve'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => 'Üzenetek kijelölése'; diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dart index 058c8a27..46679b46 100644 --- a/lib/l10n/app_localizations_it.dart +++ b/lib/l10n/app_localizations_it.dart @@ -2276,6 +2276,30 @@ class AppLocalizationsIt extends AppLocalizations { @override String get chat_messageDeleted => 'Messaggio eliminato'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => 'Seleziona messaggi'; diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dart index 51064d0a..bc6a07bd 100644 --- a/lib/l10n/app_localizations_ja.dart +++ b/lib/l10n/app_localizations_ja.dart @@ -2166,6 +2166,30 @@ class AppLocalizationsJa extends AppLocalizations { @override String get chat_messageDeleted => 'メッセージは削除されました'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => 'メッセージを選択'; diff --git a/lib/l10n/app_localizations_ko.dart b/lib/l10n/app_localizations_ko.dart index 9544d808..28b89d94 100644 --- a/lib/l10n/app_localizations_ko.dart +++ b/lib/l10n/app_localizations_ko.dart @@ -2170,6 +2170,30 @@ class AppLocalizationsKo extends AppLocalizations { @override String get chat_messageDeleted => '메시지가 삭제되었습니다.'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => '메시지 선택'; diff --git a/lib/l10n/app_localizations_nl.dart b/lib/l10n/app_localizations_nl.dart index 565e79a4..00d393e9 100644 --- a/lib/l10n/app_localizations_nl.dart +++ b/lib/l10n/app_localizations_nl.dart @@ -2263,6 +2263,30 @@ class AppLocalizationsNl extends AppLocalizations { @override String get chat_messageDeleted => 'Bericht verwijderd'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => 'Berichten selecteren'; diff --git a/lib/l10n/app_localizations_pl.dart b/lib/l10n/app_localizations_pl.dart index 5f5c9b00..c48c7ec6 100644 --- a/lib/l10n/app_localizations_pl.dart +++ b/lib/l10n/app_localizations_pl.dart @@ -2292,6 +2292,30 @@ class AppLocalizationsPl extends AppLocalizations { @override String get chat_messageDeleted => 'Wiadomość usunięta'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => 'Wybierz wiadomości'; diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dart index 0c0749b6..8b7a13f8 100644 --- a/lib/l10n/app_localizations_pt.dart +++ b/lib/l10n/app_localizations_pt.dart @@ -2273,6 +2273,30 @@ class AppLocalizationsPt extends AppLocalizations { @override String get chat_messageDeleted => 'Mensagem excluída'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => 'Selecionar mensagens'; diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dart index 5a6119ee..7f05ecb9 100644 --- a/lib/l10n/app_localizations_ru.dart +++ b/lib/l10n/app_localizations_ru.dart @@ -2273,6 +2273,30 @@ class AppLocalizationsRu extends AppLocalizations { @override String get chat_messageDeleted => 'Сообщение удалено'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => 'Выбрать сообщения'; diff --git a/lib/l10n/app_localizations_sk.dart b/lib/l10n/app_localizations_sk.dart index 109fdbd5..03d408f2 100644 --- a/lib/l10n/app_localizations_sk.dart +++ b/lib/l10n/app_localizations_sk.dart @@ -2266,6 +2266,30 @@ class AppLocalizationsSk extends AppLocalizations { @override String get chat_messageDeleted => 'Správa odstránená'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => 'Vybrať správy'; diff --git a/lib/l10n/app_localizations_sl.dart b/lib/l10n/app_localizations_sl.dart index aedc64ea..4b2ee5b8 100644 --- a/lib/l10n/app_localizations_sl.dart +++ b/lib/l10n/app_localizations_sl.dart @@ -2266,6 +2266,30 @@ class AppLocalizationsSl extends AppLocalizations { @override String get chat_messageDeleted => 'Sporočilo izbrisano'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => 'Izberi sporočila'; diff --git a/lib/l10n/app_localizations_sv.dart b/lib/l10n/app_localizations_sv.dart index 50dc42c8..b2d6ae13 100644 --- a/lib/l10n/app_localizations_sv.dart +++ b/lib/l10n/app_localizations_sv.dart @@ -2252,6 +2252,30 @@ class AppLocalizationsSv extends AppLocalizations { @override String get chat_messageDeleted => 'Meddelandet raderat'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => 'Markera meddelanden'; diff --git a/lib/l10n/app_localizations_uk.dart b/lib/l10n/app_localizations_uk.dart index a656a790..6e0de8ef 100644 --- a/lib/l10n/app_localizations_uk.dart +++ b/lib/l10n/app_localizations_uk.dart @@ -2269,6 +2269,30 @@ class AppLocalizationsUk extends AppLocalizations { @override String get chat_messageDeleted => 'Повідомлення видалено'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => 'Вибрати повідомлення'; diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart index a1661572..2cd91f07 100644 --- a/lib/l10n/app_localizations_zh.dart +++ b/lib/l10n/app_localizations_zh.dart @@ -2138,6 +2138,30 @@ class AppLocalizationsZh extends AppLocalizations { @override String get chat_messageDeleted => '消息已删除'; + @override + String get chat_bookmark => 'Bookmark'; + + @override + String get chat_removeBookmark => 'Remove bookmark'; + + @override + String get chat_showBookmarks => 'Show bookmarks'; + + @override + String get bookmarks_title => 'Bookmarks'; + + @override + String get bookmarks_empty => 'No bookmarked messages'; + + @override + String get bookmarks_channel => 'Channel'; + + @override + String get bookmarks_contact => 'Contact'; + + @override + String get bookmarks_remove => 'Remove bookmark'; + @override String get chat_selectMessages => '选择消息'; diff --git a/lib/models/bookmarked_message.dart b/lib/models/bookmarked_message.dart new file mode 100644 index 00000000..a078625c --- /dev/null +++ b/lib/models/bookmarked_message.dart @@ -0,0 +1,110 @@ +import 'package:flutter/foundation.dart'; + +import 'channel_message.dart'; +import 'message.dart'; + +enum BookmarkSourceType { contact, channel } + +@immutable +class BookmarkedMessage { + final BookmarkSourceType sourceType; + final String sourceId; + final String sourceName; + final String messageId; + final String senderName; + final String text; + final String? translatedText; + final DateTime timestamp; + final DateTime receivedAt; + final bool isOutgoing; + + const BookmarkedMessage({ + required this.sourceType, + required this.sourceId, + required this.sourceName, + required this.messageId, + required this.senderName, + required this.text, + required this.timestamp, + required this.receivedAt, + required this.isOutgoing, + this.translatedText, + }); + + factory BookmarkedMessage.fromContact({ + required Message message, + required String contactKey, + required String contactName, + required String senderName, + }) => BookmarkedMessage( + sourceType: BookmarkSourceType.contact, + sourceId: contactKey, + sourceName: contactName, + messageId: message.messageId, + senderName: senderName, + text: message.text, + translatedText: message.translatedText, + timestamp: message.timestamp, + receivedAt: message.receivedAt, + isOutgoing: message.isOutgoing, + ); + + factory BookmarkedMessage.fromChannel({ + required ChannelMessage message, + required String channelId, + required String channelName, + required String senderName, + }) => BookmarkedMessage( + sourceType: BookmarkSourceType.channel, + sourceId: channelId, + sourceName: channelName, + messageId: message.messageId, + senderName: senderName, + text: message.text, + translatedText: message.translatedText, + timestamp: message.timestamp, + receivedAt: message.receivedAt, + isOutgoing: message.isOutgoing, + ); + + String get storageId => storageIdFor(sourceType, sourceId, messageId); + + static String storageIdFor( + BookmarkSourceType sourceType, + String sourceId, + String messageId, + ) => '${sourceType.name}:$sourceId:$messageId'; + + Map toJson() => { + 'sourceType': sourceType.name, + 'sourceId': sourceId, + 'sourceName': sourceName, + 'messageId': messageId, + 'senderName': senderName, + 'text': text, + 'translatedText': translatedText, + 'timestamp': timestamp.millisecondsSinceEpoch, + 'receivedAt': receivedAt.millisecondsSinceEpoch, + 'isOutgoing': isOutgoing, + }; + + factory BookmarkedMessage.fromJson(Map json) { + final type = BookmarkSourceType.values.firstWhere( + (value) => value.name == json['sourceType'], + ); + return BookmarkedMessage( + sourceType: type, + sourceId: json['sourceId'] as String, + sourceName: json['sourceName'] as String, + messageId: json['messageId'] as String, + senderName: json['senderName'] as String, + text: json['text'] as String, + translatedText: json['translatedText'] as String?, + timestamp: DateTime.fromMillisecondsSinceEpoch(json['timestamp'] as int), + receivedAt: DateTime.fromMillisecondsSinceEpoch( + json['receivedAt'] as int? ?? json['timestamp'] as int, + ), + isOutgoing: json['isOutgoing'] as bool? ?? false, + ); + } +} diff --git a/lib/models/channel_message.dart b/lib/models/channel_message.dart index 4b66df3d..f3460138 100644 --- a/lib/models/channel_message.dart +++ b/lib/models/channel_message.dart @@ -43,6 +43,7 @@ class ChannelMessage { /// to the bottom; [timestamp] stays the sender's identity time. final DateTime receivedAt; final bool isOutgoing; + final bool isBookmarked; final ChannelMessageStatus status; final List repeats; final int repeatCount; @@ -77,6 +78,7 @@ class ChannelMessage { required this.timestamp, DateTime? receivedAt, required this.isOutgoing, + this.isBookmarked = false, this.status = ChannelMessageStatus.pending, this.repeats = const [], this.repeatCount = 0, @@ -117,6 +119,7 @@ class ChannelMessage { ChannelMessage copyWith({ ChannelMessageStatus? status, + bool? isBookmarked, List? repeats, int? repeatCount, int? pathLength, @@ -159,6 +162,7 @@ class ChannelMessage { timestamp: timestamp, receivedAt: receivedAt, isOutgoing: isOutgoing, + isBookmarked: isBookmarked ?? this.isBookmarked, status: status ?? this.status, repeats: repeats ?? this.repeats, repeatCount: repeatCount ?? this.repeatCount, diff --git a/lib/models/message.dart b/lib/models/message.dart index b518b61b..d180b33c 100644 --- a/lib/models/message.dart +++ b/lib/models/message.dart @@ -34,6 +34,7 @@ class Message { /// to the bottom; [timestamp] stays the sender's identity time. final DateTime receivedAt; final bool isOutgoing; + final bool isBookmarked; final bool isCli; final MessageStatus status; final String? originalText; @@ -62,6 +63,7 @@ class Message { required this.timestamp, DateTime? receivedAt, required this.isOutgoing, + this.isBookmarked = false, this.isCli = false, this.status = MessageStatus.pending, String? messageId, @@ -108,6 +110,7 @@ class Message { int? pathLength, Uint8List? pathBytes, bool? isCli, + bool? isBookmarked, Object? originalText = _unset, Object? translatedText = _unset, Object? translatedLanguageCode = _unset, @@ -123,6 +126,7 @@ class Message { timestamp: timestamp, receivedAt: receivedAt, isOutgoing: isOutgoing, + isBookmarked: isBookmarked ?? this.isBookmarked, isCli: isCli ?? this.isCli, status: status ?? this.status, messageId: messageId, diff --git a/lib/screens/bookmarks_screen.dart b/lib/screens/bookmarks_screen.dart new file mode 100644 index 00000000..827d0d5f --- /dev/null +++ b/lib/screens/bookmarks_screen.dart @@ -0,0 +1,178 @@ +import 'package:flutter/material.dart'; +import 'package:intl/intl.dart'; +import 'package:provider/provider.dart'; + +import '../connector/meshcore_connector.dart'; +import '../helpers/message_url_image_helper.dart'; +import '../l10n/l10n.dart'; +import '../models/bookmarked_message.dart'; +import '../services/app_settings_service.dart'; +import '../widgets/chat_send_pipeline.dart'; +import '../widgets/empty_state.dart'; +import '../widgets/translated_message_content.dart'; + +class BookmarksScreen extends StatefulWidget { + const BookmarksScreen({super.key}); + + @override + State createState() => _BookmarksScreenState(); +} + +class _BookmarksScreenState extends State { + late Future> _bookmarks; + + @override + void initState() { + super.initState(); + _bookmarks = context.read().loadBookmarkedMessages(); + } + + void _reload() { + setState(() { + _bookmarks = context.read().loadBookmarkedMessages(); + }); + } + + Future _remove(BookmarkedMessage bookmark) async { + await context.read().setBookmarkedMessage( + bookmark, + isBookmarked: false, + ); + if (mounted) _reload(); + } + + @override + Widget build(BuildContext context) { + final l10n = context.l10n; + final publicIpfsGateway = context + .watch() + .settings + .publicIpfsGateway; + final dateFormat = DateFormat.yMd( + Localizations.localeOf(context).toString(), + ).add_Hm(); + return Scaffold( + appBar: AppBar(title: Text(l10n.bookmarks_title), centerTitle: true), + body: FutureBuilder>( + future: _bookmarks, + builder: (context, snapshot) { + if (snapshot.connectionState != ConnectionState.done) { + return const Center(child: CircularProgressIndicator()); + } + final bookmarks = snapshot.data ?? const []; + if (bookmarks.isEmpty) { + return EmptyState( + icon: Icons.bookmark_border, + title: l10n.bookmarks_empty, + ); + } + bookmarks.sort((a, b) => b.receivedAt.compareTo(a.receivedAt)); + return ListView.builder( + padding: const EdgeInsets.all(12), + itemCount: bookmarks.length, + itemBuilder: (context, index) { + final bookmark = bookmarks[index]; + final sourceType = + bookmark.sourceType == BookmarkSourceType.channel + ? l10n.bookmarks_channel + : l10n.bookmarks_contact; + final time = dateFormat.format(bookmark.receivedAt.toLocal()); + final translation = bookmark.translatedText?.trim(); + final displayText = translation?.isNotEmpty == true + ? translation! + : bookmark.text; + final textStyle = + Theme.of(context).textTheme.bodyMedium ?? const TextStyle(); + final textContent = TranslatedMessageContent( + displayText: displayText, + originalText: displayText == bookmark.text + ? null + : bookmark.text, + style: textStyle, + originalStyle: textStyle.copyWith(fontStyle: FontStyle.italic), + ); + return Card( + margin: const EdgeInsets.only(bottom: 10), + child: Padding( + padding: const EdgeInsets.fromLTRB(16, 12, 8, 12), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Padding( + padding: EdgeInsets.only(top: 2, right: 8), + child: Icon( + Icons.bookmark, + size: 20, + color: Color(0xFFFFE54A), + ), + ), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + '$sourceType · ${bookmark.sourceName}', + style: Theme.of(context).textTheme.titleSmall, + ), + const SizedBox(height: 2), + Text( + '${bookmark.senderName} · $time', + style: Theme.of(context).textTheme.bodySmall, + ), + ], + ), + ), + IconButton( + tooltip: l10n.common_copy, + onPressed: () => + copyChatMessageText(context, bookmark.text), + icon: const Icon(Icons.copy), + ), + IconButton( + tooltip: l10n.bookmarks_remove, + onPressed: () => _remove(bookmark), + icon: const Icon(Icons.bookmark_remove_outlined), + ), + ], + ), + const SizedBox(height: 8), + if (MessageUrlImageHelper.hasPotentialImageUrl( + bookmark.text, + )) + MessageUrlImageFutureBuilder( + messageId: bookmark.messageId, + text: bookmark.text, + publicGateway: publicIpfsGateway, + builder: (context, snapshot) { + final imageUrl = snapshot.data; + if (imageUrl == null) return textContent; + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Padding( + padding: const EdgeInsets.only(bottom: 8), + child: MessageUrlImagePreview( + imageUrl: imageUrl, + ), + ), + textContent, + ], + ); + }, + ) + else + textContent, + ], + ), + ), + ); + }, + ); + }, + ), + ); + } +} diff --git a/lib/screens/channel_chat_screen.dart b/lib/screens/channel_chat_screen.dart index fc276315..45cdd711 100644 --- a/lib/screens/channel_chat_screen.dart +++ b/lib/screens/channel_chat_screen.dart @@ -31,6 +31,7 @@ import '../helpers/snack_bar_builder.dart'; import '../l10n/l10n.dart'; import '../models/channel.dart'; import '../models/channel_message.dart'; +import '../models/bookmarked_message.dart'; import '../models/image_codec_support.dart' show aeicRatePointForUi; import '../models/message_filter.dart'; import '../services/app_settings_service.dart' show AppSettingsService; @@ -165,6 +166,7 @@ class _ChannelChatScreenState extends State { /// Message ids picked for a bulk action. Non-empty means selection mode: /// taps toggle messages and the app bar shows the bulk actions. final Set _selectedIds = {}; + bool _showBookmarkedOnly = false; bool get _selecting => _selectedIds.isNotEmpty; @override @@ -574,6 +576,11 @@ class _ChannelChatScreenState extends State { widget.channel, onSaved: _onChannelEdited, ); + case 'showBookmarks': + setState(() { + _showBookmarkedOnly = !_showBookmarkedOnly; + _selectedIds.clear(); + }); case 'background': showChatBackgroundSheet( context, @@ -629,6 +636,14 @@ class _ChannelChatScreenState extends State { enabled: !readOnly, child: _menuRow(Icons.settings, context.l10n.channels_editChannel), ), + PopupMenuItem( + value: 'showBookmarks', + child: _menuRow( + _showBookmarkedOnly ? Icons.bookmark : Icons.bookmark_border, + context.l10n.chat_showBookmarks, + color: _showBookmarkedOnly ? const Color(0xFFFFE54A) : null, + ), + ), PopupMenuItem( value: 'background', child: _menuRow(Icons.wallpaper, context.l10n.chatBackground_title), @@ -856,13 +871,22 @@ class _ChannelChatScreenState extends State { final layout = _transcriptLayoutFor( context, messages, + showBookmarkedOnly: _showBookmarkedOnly, ); - // Fewer entries than messages means the filter hid or - // collapsed some; make sure older history stays reachable. - final hidesMessages = layout.length < messages.length; + // Fewer entries than messages means the message filter + // hid or collapsed some; bookmark filtering is deliberate. + final hidesMessages = + !_showBookmarkedOnly && + layout.length < messages.length; if (hidesMessages) _scheduleHiddenHistoryBackfill(); if (layout.length == 0) { + if (_showBookmarkedOnly) { + return EmptyState( + icon: Icons.bookmark_border, + title: context.l10n.bookmarks_empty, + ); + } final canLoadOlder = hidesMessages && !_olderHistoryExhausted; return EmptyState( @@ -1167,6 +1191,7 @@ class _ChannelChatScreenState extends State { : null, child: MessageBubbleFrame( isOutgoing: isOutgoing, + isBookmarked: message.isBookmarked || _showBookmarkedOnly, compact: gifId != null, maxWidth: constraints.maxWidth, child: Column( @@ -2187,8 +2212,9 @@ class _ChannelChatScreenState extends State { /// `State`'s) — `watch` asserts on that. _TranscriptLayout _transcriptLayoutFor( BuildContext context, - List messages, - ) { + List messages, { + bool showBookmarkedOnly = false, + }) { ReceivedImageStore? store; try { store = context.watch(); @@ -2217,10 +2243,17 @@ class _ChannelChatScreenState extends State { displayMode, filterActive, _revealVersion, + showBookmarkedOnly, _filteredMessagesSignature(messages), + Object.hashAll( + messages + .where((message) => message.isBookmarked) + .map((message) => message.messageId), + ), ); final cached = _transcriptLayout; - if (cached != null && + if (!showBookmarkedOnly && + cached != null && cached.matches( messages, _imageStoreVersion, @@ -2229,11 +2262,14 @@ class _ChannelChatScreenState extends State { )) { return cached; } - final images = store == null + final displayedMessages = showBookmarkedOnly + ? messages.where((message) => message.isBookmarked).toList() + : messages; + final images = store == null || showBookmarkedOnly ? const [] : store.entriesForChannel(widget.channel.storageId); final layout = _TranscriptLayout.build( - messages: messages, + messages: displayedMessages, images: images, imageVersion: _imageStoreVersion, channelIndex: widget.channel.index, @@ -2994,6 +3030,38 @@ class _ChannelChatScreenState extends State { _markAsUnread(message); }, ), + ListTile( + leading: Icon( + message.isBookmarked + ? Icons.bookmark_remove_outlined + : Icons.bookmark_add_outlined, + ), + title: Text( + message.isBookmarked + ? context.l10n.chat_removeBookmark + : context.l10n.chat_bookmark, + ), + onTap: () { + Navigator.pop(sheetContext); + final bookmark = BookmarkedMessage.fromChannel( + message: message, + channelId: + widget.channel.storageId ?? + 'slot:${widget.channel.index}', + channelName: widget.channel.name, + senderName: message.isOutgoing + ? (context.read().selfName ?? + context.l10n.chat_me) + : message.senderName, + ); + unawaited( + context.read().setBookmarkedMessage( + bookmark, + isBookmarked: !message.isBookmarked, + ), + ); + }, + ), if (showFilterActions) ...[ const Divider(height: 1), if (message.isFiltered) @@ -3094,9 +3162,10 @@ class _ChannelChatScreenState extends State { final filterActive = _transcriptLayout?.filterActive ?? true; return [ for (final m in messages) - if (!(filterActive && - m.isFiltered && - !_revealedFilteredIds.contains(m.messageId))) + if ((!_showBookmarkedOnly || m.isBookmarked) && + !(filterActive && + m.isFiltered && + !_revealedFilteredIds.contains(m.messageId))) m, ]; } diff --git a/lib/screens/channels_screen.dart b/lib/screens/channels_screen.dart index 333923e2..6f247343 100644 --- a/lib/screens/channels_screen.dart +++ b/lib/screens/channels_screen.dart @@ -34,6 +34,7 @@ import '../helpers/contact_share_message.dart'; import '../helpers/gif_helper.dart'; import '../helpers/snack_bar_builder.dart'; import '../helpers/utf8_length_limiter.dart'; +import 'bookmarks_screen.dart'; import 'channel_chat_screen.dart'; import 'community_qr_scanner_screen.dart'; import 'contacts_screen.dart'; @@ -128,6 +129,16 @@ class _ChannelsScreenState extends State { // context, which is deactivated by then. itemBuilder: (menuContext) => [ connectionMenuItem(context, menuContext), + PopupMenuItem( + child: MeshMenuItemRow( + icon: Icons.bookmark_border, + label: menuContext.l10n.bookmarks_title, + ), + onTap: () => Navigator.push( + context, + MaterialPageRoute(builder: (_) => const BookmarksScreen()), + ), + ), PopupMenuItem( child: MeshMenuItemRow( icon: Icons.groups, diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart index 57df7069..ad509b82 100644 --- a/lib/screens/chat_screen.dart +++ b/lib/screens/chat_screen.dart @@ -28,6 +28,7 @@ import '../helpers/marker_message.dart'; import '../helpers/message_search.dart'; import '../widgets/chat_search_results.dart'; import '../models/channel_message.dart'; +import '../models/bookmarked_message.dart'; import '../models/contact.dart'; import '../l10n/contact_localization.dart'; import '../models/message.dart'; @@ -96,6 +97,7 @@ class _ChatScreenState extends State { final _textFieldFocusNode = FocusNode(); final GlobalKey _unreadScrollKey = GlobalKey(); bool _isLoadingOlder = false; + bool _showBookmarkedOnly = false; MeshCoreConnector? _connector; Message? _pendingUnreadScrollTarget; String? _unreadDividerMessageId; @@ -484,6 +486,10 @@ class _ChatScreenState extends State { _showContactInfo(context); case 'settings': _showContactSettings(context); + case 'showBookmarks': + setState( + () => _showBookmarkedOnly = !_showBookmarkedOnly, + ); case 'background': showChatBackgroundSheet( context, @@ -551,6 +557,18 @@ class _ChatScreenState extends State { label: context.l10n.contact_settings, ), ), + PopupMenuItem( + value: 'showBookmarks', + child: MeshMenuItemRow( + icon: _showBookmarkedOnly + ? Icons.bookmark + : Icons.bookmark_border, + label: context.l10n.chat_showBookmarks, + color: _showBookmarkedOnly + ? const Color(0xFFFFE54A) + : null, + ), + ), PopupMenuItem( value: 'background', child: MeshMenuItemRow( @@ -631,6 +649,7 @@ class _ChatScreenState extends State { } Widget _buildEmptyState() { + if (_showBookmarkedOnly) return _buildBookmarkEmptyState(); return EmptyState( icon: Icons.chat_bubble_outline, title: context.l10n.chat_noMessages, @@ -640,12 +659,23 @@ class _ChatScreenState extends State { ); } + Widget _buildBookmarkEmptyState() => EmptyState( + icon: Icons.bookmark_border, + title: context.l10n.bookmarks_empty, + ); + Widget _buildMessageList( List messages, MeshCoreConnector connector, ) { + final visibleMessages = _showBookmarkedOnly + ? messages.where((message) => message.isBookmarked).toList() + : messages; + if (_showBookmarkedOnly && visibleMessages.isEmpty) { + return _buildBookmarkEmptyState(); + } // Reverse messages so newest appear at bottom with reverse: true - final reversedMessages = messages.reversed.toList(); + final reversedMessages = visibleMessages.reversed.toList(); final itemCount = reversedMessages.length + (_isLoadingOlder ? 1 : 0); // Auto-scroll to bottom if user is already at bottom @@ -713,6 +743,7 @@ class _ChatScreenState extends State { : contact.name, sourceId: widget.contact.publicKeyHex, textScale: textScale, + isBookmarked: message.isBookmarked || _showBookmarkedOnly, onLongPress: () => _showMessageActions(message, contact), onRetryReaction: (msg, emoji) => _sendReaction(msg, contact, emoji), @@ -1457,6 +1488,36 @@ class _ChatScreenState extends State { _markAsUnread(message); }, ), + ListTile( + leading: Icon( + message.isBookmarked + ? Icons.bookmark_remove_outlined + : Icons.bookmark_add_outlined, + ), + title: Text( + message.isBookmarked + ? context.l10n.chat_removeBookmark + : context.l10n.chat_bookmark, + ), + onTap: () { + Navigator.pop(sheetContext); + final bookmark = BookmarkedMessage.fromContact( + message: message, + contactKey: widget.contact.publicKeyHex, + contactName: widget.contact.name, + senderName: message.isOutgoing + ? (context.read().selfName ?? + context.l10n.chat_me) + : contact.name, + ); + unawaited( + context.read().setBookmarkedMessage( + bookmark, + isBookmarked: !message.isBookmarked, + ), + ); + }, + ), if (message.isOutgoing && message.status == MessageStatus.failed) ListTile( enabled: !readOnly, @@ -1567,12 +1628,14 @@ class _MessageBubble extends StatelessWidget { final void Function(Message message, String emoji)? onRetryReaction; final double textScale; final String sourceId; + final bool isBookmarked; const _MessageBubble({ required this.message, required this.senderName, required this.sourceId, required this.textScale, + required this.isBookmarked, this.onLongPress, this.onRetryReaction, }); @@ -1664,6 +1727,7 @@ class _MessageBubble extends StatelessWidget { child: MessageBubbleFrame( isOutgoing: isOutgoing, isFailed: isFailed, + isBookmarked: isBookmarked, compact: gifId != null, maxWidth: constraints.maxWidth, child: Column( diff --git a/lib/storage/bookmark_store.dart b/lib/storage/bookmark_store.dart new file mode 100644 index 00000000..3b4ca251 --- /dev/null +++ b/lib/storage/bookmark_store.dart @@ -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 _pendingWrites = Future.value(); + + Future> loadBookmarks() async { + final writes = _pendingWrites; + await writes; + return _readBookmarks(keyFor); + } + + Future saveBookmark(BookmarkedMessage bookmark) { + if (publicKeyHex.isEmpty) return Future.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 removeBookmark(BookmarkedMessage bookmark) { + if (publicKeyHex.isEmpty) return Future.value(); + final key = keyFor; + return _enqueue(() async { + final bookmarks = await _readBookmarks(key); + bookmarks.removeWhere( + (existing) => existing.storageId == bookmark.storageId, + ); + await _writeBookmarks(key, bookmarks); + }); + } + + Future _enqueue(Future Function() operation) { + final next = _pendingWrites.then((_) => operation()); + _pendingWrites = next.then( + (_) {}, + onError: (Object _, StackTrace _) {}, + ); + return next; + } + + Future> _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 = []; + for (final value in decoded) { + if (value is! Map) 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 _writeBookmarks( + String key, + List bookmarks, + ) async { + if (!await PrefsManager.instance.setString( + key, + jsonEncode(bookmarks.map((bookmark) => bookmark.toJson()).toList()), + )) { + throw StateError('Could not save bookmarks'); + } + } +} diff --git a/lib/storage/channel_message_store.dart b/lib/storage/channel_message_store.dart index ca1efeb2..5383b59a 100644 --- a/lib/storage/channel_message_store.dart +++ b/lib/storage/channel_message_store.dart @@ -123,6 +123,16 @@ class ChannelMessageStore { return messages; } + Future updateBookmarkFlag( + String channelId, + String messageId, + bool isBookmarked, + ) => _archive.updateBookmarkFlag( + _keyForChannel(channelId), + messageId, + isBookmarked, + ); + Future 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, diff --git a/lib/storage/history_archive.dart b/lib/storage/history_archive.dart index 46ae678b..07587360 100644 --- a/lib/storage/history_archive.dart +++ b/lib/storage/history_archive.dart @@ -121,6 +121,51 @@ class HistoryArchive { (_archives[HistoryStore.instance] ??= {})[key] = _ArchiveState(count); } + Future 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 || + 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 = []; + 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 entries) { final state = _stateFor(HistoryStore.instance, key); diff --git a/lib/storage/message_store.dart b/lib/storage/message_store.dart index ba2bb2c2..fec500d1 100644 --- a/lib/storage/message_store.dart +++ b/lib/storage/message_store.dart @@ -175,6 +175,19 @@ class MessageStore { return messages; } + Future updateBookmarkFlag( + String contactKeyHex, + String messageId, + bool isBookmarked, + ) { + if (publicKeyHex.isEmpty) return Future.value(); + return _archive.updateBookmarkFlag( + '$keyFor$contactKeyHex', + messageId, + isBookmarked, + ); + } + Future clearMessages(String contactKeyHex) { if (publicKeyHex.isEmpty) return Future.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?, diff --git a/lib/widgets/message_bubble_frame.dart b/lib/widgets/message_bubble_frame.dart index 0822ad96..646eb344 100644 --- a/lib/widgets/message_bubble_frame.dart +++ b/lib/widgets/message_bubble_frame.dart @@ -93,6 +93,7 @@ Color senderNameColor(String name) { class MessageBubbleFrame extends StatelessWidget { final bool isOutgoing; final bool isFailed; + final bool isBookmarked; /// Tighter padding for media (GIF) content. final bool compact; @@ -105,6 +106,7 @@ class MessageBubbleFrame extends StatelessWidget { required this.maxWidth, required this.child, this.isFailed = false, + this.isBookmarked = false, this.compact = false, }); @@ -115,17 +117,35 @@ class MessageBubbleFrame extends StatelessWidget { isOutgoing: isOutgoing, isFailed: isFailed, ); - return Container( - padding: compact - ? const EdgeInsets.all(4) - : const EdgeInsets.symmetric(horizontal: 12, vertical: 8), - constraints: BoxConstraints(maxWidth: maxWidth * 0.72), - decoration: BoxDecoration( - color: colors.background, - borderRadius: messageBubbleRadius(isOutgoing: isOutgoing), - border: Border.all(color: colors.border, width: 1), - ), - child: child, + final contentPadding = compact + ? const EdgeInsets.all(4) + : const EdgeInsets.symmetric(horizontal: 12, vertical: 8); + return Stack( + clipBehavior: Clip.none, + children: [ + Container( + constraints: BoxConstraints(maxWidth: maxWidth * 0.72), + decoration: BoxDecoration( + color: colors.background, + borderRadius: messageBubbleRadius(isOutgoing: isOutgoing), + border: Border.all(color: colors.border, width: 1), + ), + child: Padding( + padding: isBookmarked + ? contentPadding.add( + EdgeInsets.only(top: compact ? 18 : 14, right: 14), + ) + : contentPadding, + child: child, + ), + ), + if (isBookmarked) + const Positioned( + top: -2, + right: 12, + child: Icon(Icons.bookmark, size: 22, color: Color(0xFFFFE54A)), + ), + ], ); } } diff --git a/test/storage/bookmark_store_test.dart b/test/storage/bookmark_store_test.dart new file mode 100644 index 00000000..3160d51e --- /dev/null +++ b/test/storage/bookmark_store_test.dart @@ -0,0 +1,87 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:meshcore_open/models/bookmarked_message.dart'; +import 'package:meshcore_open/models/channel_message.dart'; +import 'package:meshcore_open/storage/bookmark_store.dart'; +import 'package:meshcore_open/storage/channel_message_store.dart'; +import 'package:meshcore_open/storage/prefs_manager.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +const _deviceKey = 'abcdef012345'; +const _channelId = 'channel-psk-id'; + +BookmarkedMessage _bookmark( + String id, { + BookmarkSourceType type = BookmarkSourceType.contact, +}) => BookmarkedMessage( + sourceType: type, + sourceId: type == BookmarkSourceType.channel ? _channelId : 'contact-key', + sourceName: type == BookmarkSourceType.channel ? 'Channel' : 'Contact', + messageId: id, + senderName: 'Alice', + text: 'saved text $id', + timestamp: DateTime.fromMillisecondsSinceEpoch(1000), + receivedAt: DateTime.fromMillisecondsSinceEpoch(2000), + isOutgoing: false, +); + +Future _init() async { + SharedPreferences.setMockInitialValues({}); + PrefsManager.reset(); + await PrefsManager.initialize(); +} + +void main() { + test('bookmarks persist independently of channel history', () async { + await _init(); + final bookmarks = BookmarkStore()..setPublicKeyHex = _deviceKey; + final channelHistory = ChannelMessageStore()..setPublicKeyHex = _deviceKey; + await bookmarks.saveBookmark( + _bookmark('message-1', type: BookmarkSourceType.channel), + ); + await channelHistory.saveChannelMessages(_channelId, [ + ChannelMessage( + senderName: 'Alice', + text: 'saved text message-1', + timestamp: DateTime.fromMillisecondsSinceEpoch(1000), + isOutgoing: false, + messageId: 'message-1', + ), + ]); + + await channelHistory.clearChannelMessages(_channelId); + + final loaded = await bookmarks.loadBookmarks(); + expect(loaded, hasLength(1)); + expect(loaded.single.text, 'saved text message-1'); + expect(loaded.single.sourceName, 'Channel'); + }); + + test('bookmarks are isolated by radio and can be removed', () async { + await _init(); + final store = BookmarkStore()..setPublicKeyHex = _deviceKey; + final bookmark = _bookmark('message-1'); + await store.saveBookmark(bookmark); + + store.setPublicKeyHex = '123456789abc'; + expect(await store.loadBookmarks(), isEmpty); + + store.setPublicKeyHex = _deviceKey; + await store.removeBookmark(bookmark); + expect(await store.loadBookmarks(), isEmpty); + }); + + test('queued saves retain bookmarks added together', () async { + await _init(); + final store = BookmarkStore()..setPublicKeyHex = _deviceKey; + + await Future.wait([ + store.saveBookmark(_bookmark('one')), + store.saveBookmark(_bookmark('two')), + ]); + + expect( + (await store.loadBookmarks()).map((bookmark) => bookmark.messageId), + ['one', 'two'], + ); + }); +} diff --git a/test/storage/message_store_archive_test.dart b/test/storage/message_store_archive_test.dart index f1026300..31316a3e 100644 --- a/test/storage/message_store_archive_test.dart +++ b/test/storage/message_store_archive_test.dart @@ -191,6 +191,19 @@ void main() { expect(reloaded.single.text, 'hi'); expect(reloaded.single.reactionStatuses, {'😀': MessageStatus.values[1]}); }); + + test('bookmark flags update in archived contact history', () async { + await _init(); + final store = _messageStore(); + await store.saveMessages(_contact, [_dm(0)]); + store.setArchivedCount(_contact, 1); + + await store.updateBookmarkFlag(_contact, 'id0', true); + expect((await store.loadMessages(_contact)).single.isBookmarked, isTrue); + + await store.updateBookmarkFlag(_contact, 'id0', false); + expect((await store.loadMessages(_contact)).single.isBookmarked, isFalse); + }); }); group('ChannelMessageStore window persistence', () { @@ -224,6 +237,25 @@ void main() { final reloaded = await store.loadChannelMessages(_channelId); expect(reloaded.map((m) => m.text), ['c3']); }); + + test('bookmark flags update in archived channel history', () async { + await _init(); + final store = ChannelMessageStore()..setPublicKeyHex = _device; + await store.saveChannelMessages(_channelId, [_ch(0)]); + store.setArchivedCount(_channelId, 1); + + await store.updateBookmarkFlag(_channelId, 'cid0', true); + expect( + (await store.loadChannelMessages(_channelId)).single.isBookmarked, + isTrue, + ); + + await store.updateBookmarkFlag(_channelId, 'cid0', false); + expect( + (await store.loadChannelMessages(_channelId)).single.isBookmarked, + isFalse, + ); + }); }); group('Smaz.tryDecodePrefixed', () { diff --git a/untranslated.json b/untranslated.json index 9e26dfee..31216ce7 100644 --- a/untranslated.json +++ b/untranslated.json @@ -1 +1,210 @@ -{} \ No newline at end of file +{ + "bg": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ], + + "cs": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ], + + "de": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ], + + "es": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ], + + "fr": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ], + + "hu": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ], + + "it": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ], + + "ja": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ], + + "ko": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ], + + "nl": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ], + + "pl": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ], + + "pt": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ], + + "pt_PT": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ], + + "ru": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ], + + "sk": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ], + + "sl": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ], + + "sv": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ], + + "uk": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ], + + "zh": [ + "chat_bookmark", + "chat_removeBookmark", + "chat_showBookmarks", + "bookmarks_title", + "bookmarks_empty", + "bookmarks_channel", + "bookmarks_contact", + "bookmarks_remove" + ] +}