From 5d1798a4e663bf4539b71fd481697f8451a41dc7 Mon Sep 17 00:00:00 2001 From: Amy Dyer Date: Mon, 24 Jul 2023 11:18:10 -0400 Subject: [PATCH] msglist: Handle updated events in MessageListView (#118). Processes an UpdateMessageEvent and hands it off to the MessageListView to update, if the message is visible in the MessageListView. This completes the changes required for issue #118. --- lib/model/message_list.dart | 48 +++++++++++++++++++++++++++++++++++++ lib/model/store.dart | 4 +++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/lib/model/message_list.dart b/lib/model/message_list.dart index 952b00ebbd4..f3b88aaa6cb 100644 --- a/lib/model/message_list.dart +++ b/lib/model/message_list.dart @@ -1,5 +1,6 @@ import 'package:flutter/foundation.dart'; +import '../api/model/events.dart'; import '../api/model/model.dart'; import '../api/route/messages.dart'; import 'content.dart'; @@ -86,6 +87,53 @@ class MessageListView extends ChangeNotifier { notifyListeners(); } + Message applyChangesToMessage(UpdateMessageEvent event, Message oldMessage) { + Map oldMessageJson = oldMessage.toJson(); + + //TODO: What other fields should be edited by the update event? + oldMessageJson["content"] = event.renderedContent; + oldMessageJson["lastEditTimestamp"] = event.editTimestamp; + + return Message.fromJson(oldMessageJson); + } + + ///This is almost directly copied from package:collection/algorithms.dart. + ///The way that package was set up doesn't allow us to search + ///for a message ID among a bunch of message objects - this is a quick + ///modification of that method to work here for us. + int findMessageWithId(int messageId) { + var min = 0; + var max = messages.length; + + while (min < max) { + var mid = min + ((max - min) >> 1); + Message message = messages[mid]; + var comp = message.id.compareTo(messageId); + if (comp == 0) return mid; + if (comp < 0) { + min = mid + 1; + } else { + max = mid; + } + } + return -1; +} + + void maybeUpdateMessage(UpdateMessageEvent event) { + int oldIdx = findMessageWithId(event.messageId); + + if (oldIdx == -1) { + return; + } + + Message oldMessage = messages[oldIdx]; + Message newMessage = applyChangesToMessage(event, oldMessage); + + messages.replaceRange(oldIdx, oldIdx+1, [newMessage]); + contents.replaceRange(oldIdx, oldIdx+1, [parseContent(newMessage.content)]); + notifyListeners(); + } + /// Called when the app is reassembled during debugging, e.g. for hot reload. /// /// This will redo from scratch any computations we can, such as parsing diff --git a/lib/model/store.dart b/lib/model/store.dart index d9de7200207..f5121f3da82 100644 --- a/lib/model/store.dart +++ b/lib/model/store.dart @@ -272,7 +272,9 @@ class PerAccountStore extends ChangeNotifier { } } else if (event is UpdateMessageEvent) { assert(debugLog("server event: update_message ${event.messageId}")); - // TODO handle + for (final view in _messageListViews) { + view.maybeUpdateMessage(event); + } } else if (event is DeleteMessageEvent) { assert(debugLog("server event: delete_message ${event.messageIds}")); // TODO handle