From 8befef4d282bc19d502c8e556631de932f4c8d8e Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 4 Aug 2021 11:57:06 -0700 Subject: [PATCH] Add initial draft of fresh-tile based replying --- src/domain/session/room/ComposerViewModel.js | 42 ++++++++++++++++--- src/domain/session/room/RoomViewModel.js | 12 +++++- .../room/timeline/tiles/BaseMessageTile.js | 2 +- 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/domain/session/room/ComposerViewModel.js b/src/domain/session/room/ComposerViewModel.js index 3ae11872..6aa22b62 100644 --- a/src/domain/session/room/ComposerViewModel.js +++ b/src/domain/session/room/ComposerViewModel.js @@ -6,14 +6,46 @@ export class ComposerViewModel extends ViewModel { this._roomVM = roomVM; this._isEmpty = true; this._replyVM = null; + this._replySub = null; + this._replyingToId = null; } - setReplyingTo(tile) { - const changed = this._replyVM !== tile; - this._replyVM = tile; - if (changed) { - this.emitChange("replyViewModel"); + setReplyingTo(id) { + if (this._replyingToId === id) { + return; } + this._replyingToId = id; + // Dispose of event subscription + if (this._replySub) { + this._replySub(); + this.untrack(this._replySub); + } + // replyVM may not be created yet even if subscribed. + if (this._replyVM) { + this._replyVM.dispose(); + } + // Early return if we don't have an ID to reply to. + if (!id) { + this._replyVM = null; + this._replySub = null; + this.emitChange("replyViewModel"); + return; + } + const observable = this._roomVM._observeEvent(id); + const entry = observable.get(); + if (entry) { + this._replyVM = this._roomVM._createTile(entry); + } + this._replySub = observable.subscribe(entry => { + if (!this._replyVM) { + this._replyVM = this._roomVM._createTile(entry); + } else { + this._replyVM.updateEntry(entry); + } + this.emitChange("replyViewModel"); + }); + this.track(this._replySub); + this.emitChange("replyViewModel"); } clearReplyingTo() { diff --git a/src/domain/session/room/RoomViewModel.js b/src/domain/session/room/RoomViewModel.js index d36713bf..7b1347c8 100644 --- a/src/domain/session/room/RoomViewModel.js +++ b/src/domain/session/room/RoomViewModel.js @@ -306,11 +306,19 @@ export class RoomViewModel extends ViewModel { this.navigation.applyPath(path); } - startReply(entry) { + startReply(id) { if (!this._room.isArchived) { - this._composerVM.setReplyingTo(entry); + this._composerVM.setReplyingTo(id); } } + + _createTile(entry) { + return this._tilesCreator(entry); + } + + _observeEvent(eventId) { + return this._room.observeEvent(eventId); + } } function imageToInfo(image) { diff --git a/src/domain/session/room/timeline/tiles/BaseMessageTile.js b/src/domain/session/room/timeline/tiles/BaseMessageTile.js index eef83cb6..d78b85e4 100644 --- a/src/domain/session/room/timeline/tiles/BaseMessageTile.js +++ b/src/domain/session/room/timeline/tiles/BaseMessageTile.js @@ -107,7 +107,7 @@ export class BaseMessageTile extends SimpleTile { } startReply() { - this._roomVM.startReply(this); + this._roomVM.startReply(this._entry.id); } reply(msgtype, body, log = null) {