ChatCache

Move the functionality to cache the contents of a chat bar from the room directly and to a new ChatCache object. This works pretty much the same with a few extra check and balances, this also made it easy to put a test suite around the functionality so I did. The current functionality should be identical to what exists.

This is in prep for threads which will require managing even more caches if we create one per thread.
This commit is contained in:
James Graham
2023-10-15 12:55:56 +00:00
parent f5417a6227
commit a57744891a
22 changed files with 775 additions and 410 deletions

View File

@@ -39,6 +39,7 @@ QQC2.Control {
* @brief The current room that user is viewing.
*/
required property NeoChatRoom currentRoom
onCurrentRoomChanged: _private.chatBarCache = currentRoom.mainCache
/**
* @brief The QQC2.TextArea object.
@@ -62,7 +63,7 @@ QQC2.Control {
property bool isBusy: root.currentRoom && root.currentRoom.hasFileUploading
// Matrix does not allow sending attachments in replies
visible: root.currentRoom.chatBoxReplyId.length === 0 && root.currentRoom.chatBoxAttachmentPath.length === 0
visible: _private.chatBarCache.isReplying && _private.chatBarCache.attachmentPath.length === 0
icon.name: "mail-attachment"
text: i18n("Attach an image or file")
displayHint: Kirigami.DisplayHint.IconOnly
@@ -76,7 +77,7 @@ QQC2.Control {
if (!path) {
return;
}
root.currentRoom.chatBoxAttachmentPath = path;
_private.chatBarCache.attachmentPath = path;
})
fileDialog.open()
}
@@ -169,7 +170,7 @@ QQC2.Control {
leftPadding: LayoutMirroring.enabled ? actionsRow.width : Kirigami.Units.largeSpacing
rightPadding: LayoutMirroring.enabled ? Kirigami.Units.largeSpacing : actionsRow.width + x * 2 + Kirigami.Units.largeSpacing * 2
placeholderText: root.currentRoom.usesEncryption ? i18n("Send an encrypted message…") : root.currentRoom.chatBoxAttachmentPath.length > 0 ? i18n("Set an attachment caption...") : i18n("Send a message…")
placeholderText: root.currentRoom.usesEncryption ? i18n("Send an encrypted message…") : _private.chatBarCache.attachmentPath.length > 0 ? i18n("Set an attachment caption...") : i18n("Send a message…")
verticalAlignment: TextEdit.AlignVCenter
wrapMode: Text.Wrap
@@ -189,7 +190,7 @@ QQC2.Control {
root.currentRoom.sendTypingNotification(textExists)
textExists ? repeatTimer.start() : repeatTimer.stop()
}
root.currentRoom.chatBoxText = text
_private.chatBarCache.text = text
}
onCursorRectangleChanged: chatBarScrollView.ensureVisible(cursorRectangle)
onSelectedTextChanged: {
@@ -285,25 +286,25 @@ QQC2.Control {
anchors.rightMargin: root.width > chatBarSizeHelper.currentWidth ? 0 : (chatBarScrollView.QQC2.ScrollBar.vertical.visible ? Kirigami.Units.largeSpacing * 3.5 : Kirigami.Units.largeSpacing)
active: visible
visible: root.currentRoom.chatBoxReplyId.length > 0 || root.currentRoom.chatBoxAttachmentPath.length > 0
sourceComponent: root.currentRoom.chatBoxReplyId.length > 0 ? replyPane : attachmentPane
visible: _private.chatBarCache.isReplying || _private.chatBarCache.attachmentPath.length > 0
sourceComponent: _private.chatBarCache.isReplying ? replyPane : attachmentPane
}
Component {
id: replyPane
ReplyPane {
userName: root.currentRoom.chatBoxReplyUser.displayName
userColor: root.currentRoom.chatBoxReplyUser.color
userAvatar: root.currentRoom.chatBoxReplyUser.avatarSource
text: root.currentRoom.chatBoxReplyMessage
userName: _private.chatBarCache.relationUser.displayName
userColor: _private.chatBarCache.relationUser.color
userAvatar: _private.chatBarCache.relationUser.avatarSource
text: _private.chatBarCache.relationMessage
}
}
Component {
id: attachmentPane
AttachmentPane {
attachmentPath: root.currentRoom.chatBoxAttachmentPath
attachmentPath: _private.chatBarCache.attachmentPath
onAttachmentCancelled: {
root.currentRoom.chatBoxAttachmentPath = "";
_private.chatBarCache.attachmentPath = "";
root.forceActiveFocus()
}
}
@@ -349,14 +350,14 @@ QQC2.Control {
anchors.right: parent.right
anchors.rightMargin: (root.width - chatBarSizeHelper.currentWidth) / 2 + Kirigami.Units.largeSpacing + (chatBarScrollView.QQC2.ScrollBar.vertical.visible && !(root.width > chatBarSizeHelper.currentWidth) ? Kirigami.Units.largeSpacing * 2.5 : 0)
visible: root.currentRoom.chatBoxReplyId.length > 0
visible: _private.chatBarCache.isReplying
display: QQC2.AbstractButton.IconOnly
action: Kirigami.Action {
text: i18nc("@action:button", "Cancel reply")
icon.name: "dialog-close"
onTriggered: {
root.currentRoom.chatBoxReplyId = "";
root.currentRoom.chatBoxAttachmentPath = "";
_private.chatBarCache.replyId = "";
_private.chatBarCache.attachmentPath = "";
root.forceActiveFocus()
}
}
@@ -472,16 +473,16 @@ QQC2.Control {
if (localPath.length === 0) {
return false;
}
root.currentRoom.chatBoxAttachmentPath = localPath;
_private.chatBarCache.attachmentPath = localPath;
return true;
}
function postMessage() {
actionsHandler.handleNewMessage();
actionsHandler.handleMessageEvent(_private.chatBarCache);
repeatTimer.stop()
root.currentRoom.markAllMessagesAsRead();
textField.clear();
root.currentRoom.chatBoxReplyId = "";
_private.chatBarCache.replyId = "";
messageSent()
}
@@ -558,7 +559,7 @@ QQC2.Control {
if (!path) {
return;
}
root.currentRoom.chatBoxAttachmentPath = path;
_private.chatBarCache.attachmentPath = path;
})
fileDialog.open()
@@ -581,7 +582,7 @@ QQC2.Control {
if (!Clipboard.saveImage(localPath)) {
return;
}
root.currentRoom.chatBoxAttachmentPath = localPath;
_private.chatBarCache.attachmentPath = localPath;
attachDialog.close();
}
}
@@ -595,4 +596,10 @@ QQC2.Control {
parentWindow: Window.window
}
}
QtObject {
id: _private
property ChatBarCache chatBarCache
onChatBarCacheChanged: documentHandler.chatBarCache = chatBarCache
}
}

View File

@@ -59,8 +59,8 @@ MessageDelegateContextMenu {
text: i18n("Reply")
icon.name: "mail-replied-symbolic"
onTriggered: {
currentRoom.chatBoxReplyId = eventId
currentRoom.chatBoxEditId = ""
currentRoom.mainCache.replyId = eventId;
currentRoom.editCache.editId = "";
RoomManager.requestFullScreenClose()
}
},

View File

@@ -93,8 +93,8 @@ Loader {
text: i18n("Edit")
icon.name: "document-edit"
onTriggered: {
currentRoom.chatBoxEditId = eventId;
currentRoom.chatBoxReplyId = "";
currentRoom.editCache.editId = eventId;
currentRoom.mainCache.replyId = "";
}
visible: author.isLocalUser && (root.delegateType === DelegateType.Emote || root.delegateType === DelegateType.Message)
},
@@ -102,8 +102,8 @@ Loader {
text: i18n("Reply")
icon.name: "mail-replied-symbolic"
onTriggered: {
currentRoom.chatBoxReplyId = eventId;
currentRoom.chatBoxEditId = "";
currentRoom.mainCache.replyId = eventId;
currentRoom.editCache.editId = "";
}
},
Kirigami.Action {

View File

@@ -13,7 +13,10 @@ QQC2.TextArea {
id: root
required property NeoChatRoom room
onRoomChanged: room.chatBoxEditIdChanged.connect(updateEditText)
onRoomChanged: {
_private.chatBarCache = room.editCache
_private.chatBarCache.relationIdChanged.connect(_private.updateEditText)
}
property string messageId
@@ -26,7 +29,7 @@ QQC2.TextArea {
wrapMode: Text.Wrap
onTextChanged: {
room.editText = text
_private.chatBarCache.text = text
}
Keys.onEnterPressed: {
@@ -88,7 +91,7 @@ QQC2.TextArea {
text: i18nc("@action:button", "Cancel edit")
icon.name: "dialog-close"
onTriggered: {
room.chatBoxEditId = "";
_private.chatBarCache.editId = "";
}
shortcut: "Escape"
}
@@ -134,16 +137,22 @@ QQC2.TextArea {
}
function postEdit() {
actionsHandler.handleEdit();
actionsHandler.handleMessageEvent(_private.chatBarCache);
root.clear();
room.chatBoxEditId = "";
_private.chatBarCache.editId = "";
}
function updateEditText() {
if (room.chatBoxEditId == messageId && room.chatBoxEditMessage.length > 0) {
root.text = room.chatBoxEditMessage
forceActiveFocus();
root.cursorPosition = root.length;
QtObject {
id: _private
property ChatBarCache chatBarCache
onChatBarCacheChanged: documentHandler.chatBarCache = chatBarCache
function updateEditText() {
if (chatBarCache.isEditing && chatBarCache.relationMessage.length > 0) {
root.text = chatBarCache.relationMessage
root.forceActiveFocus();
root.cursorPosition = root.length;
}
}
}
}

View File

@@ -43,7 +43,7 @@ MessageDelegate {
RichLabel {
id: label
Layout.fillWidth: true
visible: currentRoom.chatBoxEditId !== root.eventId
visible: currentRoom.editCache.editId !== root.eventId
isReply: root.isReply
@@ -59,7 +59,7 @@ MessageDelegate {
Layout.fillWidth: true
Layout.minimumHeight: item ? item.minimumHeight : -1
Layout.preferredWidth: item ? item.preferredWidth : -1
visible: currentRoom.chatBoxEditId === root.eventId
visible: currentRoom.editCache.editId === root.eventId
active: visible
sourceComponent: MessageEditComponent {
room: currentRoom

View File

@@ -182,7 +182,7 @@ QQC2.ScrollView {
DropArea {
id: dropAreaFile
anchors.fill: parent
onDropped: root.currentRoom.chatBoxAttachmentPath = drop.urls[0]
onDropped: root.currentRoom.mainCache.attachmentPath = drop.urls[0]
;
enabled: !Controller.isFlatpak
}
@@ -250,12 +250,13 @@ QQC2.ScrollView {
}
}
onEditClicked: {
root.currentRoom.chatBoxEditId = delegate.eventId;
root.currentRoom.chatBoxReplyId = "";
root.currentRoom.editCache.editId = delegate.eventId;
root.currentRoom.mainCache.replyId = "";
}
onReplyClicked: {
root.currentRoom.chatBoxReplyId = delegate.eventId;
root.currentRoom.chatBoxEditId = "";
root.currentRoom.mainCache.replyId = delegate.eventId;
root.currentRoom.editCache.editId = "";
root.focusChatBox();
}
}