Skip to content

Commit

Permalink
msglist: Handle updated events in MessageListView (zulip#118).
Browse files Browse the repository at this point in the history
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 zulip#118.
  • Loading branch information
amy-at-braid committed Jul 24, 2023
1 parent 43236f8 commit 24f324e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
43 changes: 43 additions & 0 deletions lib/model/message_list.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import 'package:drift/drift.dart';
import 'package:flutter/foundation.dart';

import '../api/model/events.dart';
import '../api/model/model.dart';
import '../api/route/messages.dart';
import 'content.dart';
import 'narrow.dart';
import 'store.dart';
import 'package:zulip/log.dart';

/// A view-model for a message list.
///
Expand Down Expand Up @@ -86,6 +89,46 @@ class MessageListView extends ChangeNotifier {
notifyListeners();
}

Message applyChangesToMessage(UpdateMessageEvent event, Message oldMessage) {
Map<String, dynamic> 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);
}

void maybeUpdateMessage(UpdateMessageEvent event) {
int? oldIdx;

for (int i=0; i<messages.length; i++) {
Message m = messages[i];

if (m.id == event.messageId) {
oldIdx = i;
break;
}

}

if (oldIdx == null) {
debugLog("Didn't find anything :(");
return;
}

Message oldMessage = messages[oldIdx];
Message newMessage = applyChangesToMessage(event, oldMessage);

//TODO
messages.replaceRange(oldIdx, oldIdx+1, [newMessage]);
debugLog("Replacing!");

//wtf
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
Expand Down
4 changes: 3 additions & 1 deletion lib/model/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 24f324e

Please sign in to comment.