refactor(Chat): move send message logic out of status chat input

`StatusChatInput` ideally shouldn't rely on chatsModel and other global
objects at all. Also, when using the component in different places, it can cause
accidental sending of message when testing the component (because all the logic is already
wired up)
This commit is contained in:
Pascal Precht 2020-12-07 15:59:24 +01:00 committed by Pascal Precht
parent 0293f3eac1
commit 9745205302
2 changed files with 29 additions and 19 deletions

View File

@ -268,6 +268,21 @@ StackLayout {
onStickerSelected: { onStickerSelected: {
chatsModel.stickers.send(hashId, packId) chatsModel.stickers.send(hashId, packId)
} }
onSendMessage: {
if (chatInput.fileUrls.length > 0){
chatsModel.sendImage(chatInput.fileUrls[0]);
}
var msg = chatsModel.plainText(Emoji.deparse(chatInput.textInput.text))
if (msg.length > 0){
msg = chatInput.interpretMessage(msg)
chatsModel.sendMessage(msg, chatInput.isReply ? SelectedMessage.messageId : "", Utils.isOnlyEmoji(msg) ? Constants.emojiType : Constants.messageType);
chatInput.textInput.text = "";
if(event) event.accepted = true
chatInput.messageSound.stop()
Qt.callLater(chatInput.messageSound.play);
}
}
} }
} }
} }

View File

@ -16,6 +16,7 @@ Rectangle {
signal sendTransactionCommandButtonClicked() signal sendTransactionCommandButtonClicked()
signal receiveTransactionCommandButtonClicked() signal receiveTransactionCommandButtonClicked()
signal stickerSelected(string hashId, string packId) signal stickerSelected(string hashId, string packId)
signal sendMessage(var event)
property bool emojiEvent: false; property bool emojiEvent: false;
property bool paste: false; property bool paste: false;
@ -37,6 +38,9 @@ Rectangle {
property alias textInput: messageInputField property alias textInput: messageInputField
property bool isStatusUpdateInput: chatType === Constants.chatTypeStatusUpdate property bool isStatusUpdateInput: chatType === Constants.chatTypeStatusUpdate
property var fileUrls: []
property alias messageSound: sendMessageSound
height: { height: {
if (extendedArea.visible) { if (extendedArea.visible) {
return messageInput.height + extendedArea.height + Style.current.bigPadding return messageInput.height + extendedArea.height + Style.current.bigPadding
@ -71,7 +75,7 @@ Rectangle {
messageInputField.insert(start, text.replace(/\n/g, "<br/>")); messageInputField.insert(start, text.replace(/\n/g, "<br/>"));
} }
function interpretMessage(msg) { property var interpretMessage: function (msg) {
if (msg.startsWith("/shrug")) { if (msg.startsWith("/shrug")) {
return msg.replace("/shrug", "") + " ¯\\\\\\_(ツ)\\_/¯" return msg.replace("/shrug", "") + " ¯\\\\\\_(ツ)\\_/¯"
} }
@ -98,8 +102,12 @@ Rectangle {
event.accepted = true; event.accepted = true;
return return
} }
if (control.isStatusUpdateInput) {
return // Status update require the send button to be clicked
}
if (messageInputField.length < messageLimit) { if (messageInputField.length < messageLimit) {
sendMsg(event); control.sendMessage(event)
control.hideExtendedArea();
return; return;
} }
if(event) event.accepted = true if(event) event.accepted = true
@ -334,25 +342,10 @@ Rectangle {
return true; return true;
} }
function sendMsg(event){
if(control.isImage){
chatsModel.sendImage(imageArea.imageSource);
}
var msg = chatsModel.plainText(Emoji.deparse(messageInputField.text))
if(msg.length > 0){
msg = interpretMessage(msg)
chatsModel.sendMessage(msg, control.isReply ? SelectedMessage.messageId : "", Utils.isOnlyEmoji(msg) ? Constants.emojiType : Constants.messageType);
messageInputField.text = "";
if(event) event.accepted = true
sendMessageSound.stop()
Qt.callLater(sendMessageSound.play);
}
control.hideExtendedArea();
}
function hideExtendedArea() { function hideExtendedArea() {
isImage = false; isImage = false;
isReply = false; isReply = false;
control.fileUrls = []
imageArea.imageSource = ""; imageArea.imageSource = "";
replyArea.userName = "" replyArea.userName = ""
replyArea.identicon = "" replyArea.identicon = ""
@ -362,7 +355,8 @@ Rectangle {
function showImageArea(imagePath) { function showImageArea(imagePath) {
isImage = true; isImage = true;
isReply = false; isReply = false;
imageArea.imageSource = imageDialog.fileUrls[0] control.fileUrls = imageDialog.fileUrls
imageArea.imageSource = control.fileUrls[0]
} }
function showReplyArea(userName, message, identicon) { function showReplyArea(userName, message, identicon) {
@ -582,6 +576,7 @@ Rectangle {
anchors.topMargin: control.isStatusUpdateInput ? 0 : Style.current.halfPadding anchors.topMargin: control.isStatusUpdateInput ? 0 : Style.current.halfPadding
visible: isImage visible: isImage
onImageRemoved: { onImageRemoved: {
control.fileUrls = []
isImage = false isImage = false
} }
} }