Create avatar cache and identity details cache

This commit is contained in:
Angela Mazzurco 2017-07-09 15:40:37 +02:00
parent 36708efa8c
commit 53eadfdc19
2 changed files with 55 additions and 17 deletions

View File

@ -101,6 +101,8 @@ QtObject
id: contactsCache id: contactsCache
property var contactsList property var contactsList
property var own property var own
property var identityDetails: ({})
function getContactFromGxsId (gxsId) function getContactFromGxsId (gxsId)
{ {
@ -109,10 +111,29 @@ QtObject
{ {
if (contactsList[i].gxs_id == gxsId) return contactsList[i] if (contactsList[i].gxs_id == gxsId) return contactsList[i]
} }
} }
function getIdentityDetails (gxsId)
{
if (identityDetails[gxsId]) return identityDetails[gxsId]
return ""
}
function setIdentityDetails (jData)
{
identityDetails[jData.gxs_id] = jData
}
function getIdentityAvatar (gxsId)
{
if (identityDetails[gxsId] && identityDetails[gxsId].avatar !== undefined)
{
return identityDetails[gxsId].avatar
}
return ""
}
} }
} }

View File

@ -45,16 +45,25 @@ Item
if (gxs_id) if (gxs_id)
{ {
default_image = false default_image = false
rsApi.request( var hasAvatarCached = hasAvatar (ChatCache.contactsCache.getIdentityAvatar(gxs_id))
"/identity/get_identity_details", if ( !hasAvatarCached )
JSON.stringify({ gxs_id: compRoot.gxs_id }), {
function(par) rsApi.request(
{ "/identity/get_identity_details",
var jData = JSON.parse(par.response).data JSON.stringify({ gxs_id: compRoot.gxs_id }),
setDetails(jData) function(par)
if(!compRoot.has_avatar && {
compRoot.avatarAttemptCnt < 3) getDetails() var jData = JSON.parse(par.response).data
}) ChatCache.contactsCache.setIdentityDetails(jData)
setDetails(jData)
if(!compRoot.has_avatar &&
compRoot.avatarAttemptCnt < 3) getDetails()
})
}
else
{
setDetails(ChatCache.contactsCache.getIdentityDetails(gxs_id))
}
} }
else else
{ {
@ -65,14 +74,23 @@ Item
} }
function setDetails(data) function setDetails(data)
{ {
compRoot.has_avatar = data.avatar.length > 0 compRoot.has_avatar = hasAvatar (data.avatar)
if(compRoot.has_avatar) if(compRoot.has_avatar)
{ {
contactAvatar.source = setImage(data.avatar)
"data:image/png;base64," + data.avatar
} }
} }
function hasAvatar (avatar)
{
return avatar.length > 0
}
function setImage (source)
{
contactAvatar.source =
"data:image/png;base64," + source
}
function showDetails() function showDetails()
{ {
console.log("showDetails() ", gxs_id) console.log("showDetails() ", gxs_id)
@ -80,7 +98,6 @@ Item
stackView.push( stackView.push(
"qrc:/ContactDetails.qml", "qrc:/ContactDetails.qml",
{md: ChatCache.contactsCache.getContactFromGxsId(gxs_id)}) {md: ChatCache.contactsCache.getContactFromGxsId(gxs_id)})
} }
Component.onCompleted: if(visible && (!has_avatar || default_image ) ) getDetails() Component.onCompleted: if(visible && (!has_avatar || default_image ) ) getDetails()