mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-14 12:02:29 -04:00
Improve QML app Contacts view performances
Simplified sorting getting rid of complicated DelegateModel Offload sorting work to another thread via WorkerScript Get rid of polling and use token system instead
This commit is contained in:
parent
7d9e89e3d2
commit
8e03fab8da
4 changed files with 166 additions and 154 deletions
95
retroshare-qml-app/src/qml/ContactSort.js
Normal file
95
retroshare-qml-app/src/qml/ContactSort.js
Normal file
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* RetroShare Android QML App
|
||||
* Copyright (C) 2017 Gioacchino Mazzurco <gio@eigenlab.org>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
function strcmp(left, right)
|
||||
{ return ( left < right ? -1 : ( left > right ? 1:0 ) ) }
|
||||
|
||||
var unreadMessages = {}
|
||||
var contactsData = {}
|
||||
|
||||
function cntcmp(left, right, searchText)
|
||||
{
|
||||
if(typeof searchText !== 'undefined' && searchText.length > 0)
|
||||
{
|
||||
var mtc = searchText.toLowerCase()
|
||||
var lfn = left.name.toLowerCase()
|
||||
var rgn = right.name.toLowerCase()
|
||||
var lfml = lfn.indexOf(mtc)
|
||||
var rgml = rgn.indexOf(mtc)
|
||||
if ( lfml !== rgml )
|
||||
{
|
||||
lfml = lfml >= 0 ? lfml : Number.MAX_VALUE
|
||||
rgml = rgml >= 0 ? rgml : Number.MAX_VALUE
|
||||
return lfml - rgml
|
||||
}
|
||||
}
|
||||
|
||||
var lfun = left.hasOwnProperty("unread_count") ? left.unread_count : 0
|
||||
var rgun = right.hasOwnProperty("unread_count") ? right.unread_count : 0
|
||||
if( lfun !== rgun ) return rgun - lfun
|
||||
if(left.name !== right.name) return strcmp(left.name, right.name)
|
||||
return strcmp(left.gxs_id, right.gxs_id)
|
||||
}
|
||||
|
||||
function mergeContactsUnread()
|
||||
{
|
||||
var jsonData = contactsData.data
|
||||
var dataLen = jsonData.length
|
||||
for ( var i=0; i<dataLen; ++i)
|
||||
{
|
||||
var el = jsonData[i]
|
||||
if(unreadMessages.hasOwnProperty(el.gxs_id))
|
||||
el['unread_count'] = unreadMessages[el.gxs_id]
|
||||
}
|
||||
}
|
||||
|
||||
function parseUnread(responseStr)
|
||||
{
|
||||
var jsonData = JSON.parse(responseStr).data
|
||||
var dataLen = jsonData.length
|
||||
unreadMessages = {}
|
||||
for ( var i=0; i<dataLen; ++i)
|
||||
{
|
||||
var el = jsonData[i]
|
||||
if(el.is_distant_chat_id)
|
||||
unreadMessages[el.remote_author_id] = el.unread_count
|
||||
}
|
||||
|
||||
mergeContactsUnread()
|
||||
}
|
||||
|
||||
function parseContacts(responseStr)
|
||||
{
|
||||
contactsData = JSON.parse(responseStr)
|
||||
mergeContactsUnread()
|
||||
}
|
||||
|
||||
WorkerScript.onMessage = function(message)
|
||||
{
|
||||
var sortFn = cntcmp
|
||||
message.action = message.hasOwnProperty("action") ? message.action : "rSort"
|
||||
|
||||
if(message.action === "refreshContacts") parseContacts(message.response)
|
||||
else if(message.action === "refreshUnread") parseUnread(message.response)
|
||||
else if(message.action === "searchContact")
|
||||
sortFn = function cmp(l,r) { return cntcmp(l,r, message.sexp) }
|
||||
|
||||
contactsData.data.sort(sortFn)
|
||||
|
||||
WorkerScript.sendMessage(contactsData)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue