Implement chat cache on GXS delegate view

This commit is contained in:
Angela Mazzurco 2017-06-05 16:06:59 +02:00
parent 549b76141a
commit d3e3f14afd
2 changed files with 94 additions and 9 deletions

View File

@ -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
}
}
}

View File

@ -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
}
}
Column {
height: parent.height
anchors.left: colorHash.right
anchors.leftMargin: 5
Text
{
id: nickText
color: model.own ? "blue" : "black"
text: model.name
anchors.left: colorHash.right
anchors.leftMargin: 5
anchors.verticalCenter: parent.verticalCenter
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()
{