From d3e3f14afd8f551691f4158d66425216c315b11a Mon Sep 17 00:00:00 2001 From: Angela Mazzurco Date: Mon, 5 Jun 2017 16:06:59 +0200 Subject: [PATCH] Implement chat cache on GXS delegate view --- retroshare-qml-app/src/ChatCache.qml | 28 ++++++- .../src/GxsIdentityDelegate.qml | 75 +++++++++++++++++-- 2 files changed, 94 insertions(+), 9 deletions(-) diff --git a/retroshare-qml-app/src/ChatCache.qml b/retroshare-qml-app/src/ChatCache.qml index 5abb0c26c..9ee673c11 100644 --- a/retroshare-qml-app/src/ChatCache.qml +++ b/retroshare-qml-app/src/ChatCache.qml @@ -11,8 +11,12 @@ QtObject { property QtObject lastMessageCache: QtObject { + id: lastMessageCache property var lastMessageList: ({}) + signal lastMessageChanged() + + function updateLastMessageCache (chatId, chatModel){ console.log("updateLastMessageCache (chatId, chatModel)", chatId) @@ -27,14 +31,34 @@ QtObject { function updateLastMessage (chatId, chatModel){ console.log("updateLastMessage (chatId, chatModel)") - var lastMessage = getLastMessageFromChat (chatModel) + var lastMessage = findChatLastMessage (chatModel) lastMessageList[chatId] = lastMessage + } - function getLastMessageFromChat (chatModel){ + function findChatLastMessage (chatModel){ var messagesData = JSON.parse(chatModel).data return messagesData.slice(-1)[0] } + + + function getChatIdFromGxs (gxs){ + for (var key in lastMessageList) { + if ( lastMessageList[key].author_id === gxs ) { + return key + } + } + return undefined + } + + function getChatLastMessage (chatId){ + if (lastMessageList[chatId]) { + return lastMessageList[chatId] + } + + return undefined + + } } } diff --git a/retroshare-qml-app/src/GxsIdentityDelegate.qml b/retroshare-qml-app/src/GxsIdentityDelegate.qml index 9b9a12316..d47b69cbe 100644 --- a/retroshare-qml-app/src/GxsIdentityDelegate.qml +++ b/retroshare-qml-app/src/GxsIdentityDelegate.qml @@ -18,6 +18,7 @@ import QtQuick 2.7 import QtQuick.Controls 2.0 +import "." //Needed for ChatCache singleton Item { @@ -25,6 +26,18 @@ Item height: 50 width: parent.width + // property var lastMessageData: getLastMessage() + + property var chatId: undefined + property var lastMessageData: "" + property var authorName: lastMessageData.author_name + +// JSONListModel +// { +// id: lastMessageData +// query: "$.*" +// } + Rectangle { anchors.fill: parent @@ -61,7 +74,6 @@ Item anchors.fill: parent color: "transparent" anchors.margins: 5 -// anchors.leftMargin: 5 ColorHash @@ -81,16 +93,32 @@ Item } } - Text - { - id: nickText - color: model.own ? "blue" : "black" - text: model.name + Column { + + height: parent.height anchors.left: colorHash.right anchors.leftMargin: 5 - anchors.verticalCenter: parent.verticalCenter + + Text + { + id: nickText + color: model.own ? "blue" : "black" + text: model.name + font.bold: true + } + + Text + { + id: lastMessage + color: model.own ? "blue" : "black" + text: (lastMessageData.msg) ? lastMessageData.msg : "" + font.italic: true + } + } + + Row { anchors.right: parent.right @@ -133,6 +161,39 @@ Item } } } +// signal lastMessageChanged(var chatI, var newLastMessage) +// lastMessageChanged(chatId, lastMessage) +// Connections { +// target: ChatCache.lastMessageCache +// onLastMessageChanged: { +// if (!chatId) { +// getChatIdFromGXS() +// } + +// if (chatId && chatId === chatI){ +// delegateRoot.lastMessageData = newLastMessage +// console.log("@@@@@@@@@@@2 Last message changed ",chatI ,newLastMessage.msg, chatId ) +// } +// } + +// } + + Component.onCompleted: { + if (!chatId){ + chatId = getChatIdFromGXS() + } + if (chatId) { + lastMessageData = getChatLastMessage(chatId) + } + } + + function getChatLastMessage (chatId){ + return ChatCache.lastMessageCache.getChatLastMessage(chatId) + } + + function getChatIdFromGXS (){ + return ChatCache.lastMessageCache.getChatIdFromGxs(model.gxs_id) + } function showDetails() {