From 909e68e7bb7be19066f955715910b691da9abc82 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 19 Apr 2020 14:29:01 +0200 Subject: [PATCH] Handles remove message events --- lib/helpers/conversations_helper.dart | 8 ++++++-- lib/helpers/events_helper.dart | 6 ++++++ lib/helpers/websocket_helper.dart | 6 ++++++ lib/lists/conversation_messages_list.dart | 3 +++ lib/ui/screens/conversation_screen.dart | 12 +++++++----- 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/helpers/conversations_helper.dart b/lib/helpers/conversations_helper.dart index 62f426d..626d2dc 100644 --- a/lib/helpers/conversations_helper.dart +++ b/lib/helpers/conversations_helper.dart @@ -333,6 +333,11 @@ class ConversationsHelper { await _conversationMessagesDatabaseHelper.insertOrUpdate(msg); } + /// Remove a message from the database + Future removeMessage(int msgID) async { + await _conversationMessagesDatabaseHelper.delete(msgID); + } + /// Update a message content Future updateMessage(int id, String newContent) async { final response = await APIRequest( @@ -355,8 +360,7 @@ class ConversationsHelper { if (response.code != 200) return false; - // Delete the message locally - return await _conversationMessagesDatabaseHelper.delete(id); + return true; } /// Register a conversation : ask the server to notify about updates to the diff --git a/lib/helpers/events_helper.dart b/lib/helpers/events_helper.dart index d486617..c389fff 100644 --- a/lib/helpers/events_helper.dart +++ b/lib/helpers/events_helper.dart @@ -60,6 +60,12 @@ class UpdatedConversationMessageEvent { UpdatedConversationMessageEvent(this.msg); } +/// Deleted conversation message +class DeletedConversationMessageEvent { + final ConversationMessage msg; + + DeletedConversationMessageEvent(this.msg); +} class EventsHelper { static EventBus _mgr = EventBus(); diff --git a/lib/helpers/websocket_helper.dart b/lib/helpers/websocket_helper.dart index da25a7d..b1b77e5 100644 --- a/lib/helpers/websocket_helper.dart +++ b/lib/helpers/websocket_helper.dart @@ -150,6 +150,12 @@ class WebSocketHelper { ConversationsHelper.apiToConversationMessage(msg.data))); break; + // Deleted a conversation message + case "deleted_conv_message": + EventsHelper.emit(DeletedConversationMessageEvent( + ConversationsHelper.apiToConversationMessage(msg.data))); + break; + default: throw Exception("Unknown message type: ${msg.title}"); } diff --git a/lib/lists/conversation_messages_list.dart b/lib/lists/conversation_messages_list.dart index 7a5aad8..b8713bd 100644 --- a/lib/lists/conversation_messages_list.dart +++ b/lib/lists/conversation_messages_list.dart @@ -54,4 +54,7 @@ class ConversationMessagesList extends ListBase { final index = this.indexWhere((t) => t.id == msg.id); if (index >= 0) this[index] = msg; } + + /// Remove a message from this list + void removeMsg(int id) => removeWhere((f) => f.id == id); } diff --git a/lib/ui/screens/conversation_screen.dart b/lib/ui/screens/conversation_screen.dart index e643b25..59daf89 100644 --- a/lib/ui/screens/conversation_screen.dart +++ b/lib/ui/screens/conversation_screen.dart @@ -98,6 +98,13 @@ class _ConversationScreenState extends SafeState { setState(() => _messages.replace(ev.msg)); } }); + + this.listen((ev) async { + if (ev.msg.conversationID == widget.conversationID) { + await _conversationsHelper.removeMessage(ev.msg.id); + setState(() => _messages.removeMsg(ev.msg.id)); + } + }); } /// Free resources when this conversation widget is no longer required @@ -450,10 +457,5 @@ class _ConversationScreenState extends SafeState { // Execute the request if (!await _conversationsHelper.deleteMessage(message.id)) showSimpleSnack(context, tr("Could not delete conversation message!")); - - // Remove the message from the list - setState(() { - _messages.remove(message); - }); } }