From cef797efb86f405cd73d39c08557b75bf5a0cc44 Mon Sep 17 00:00:00 2001 From: Mark Washeim Date: Fri, 16 Dec 2022 19:20:07 +0100 Subject: [PATCH] this version, brings a check on current List memeber to stop duplication It also fixes a spacing bug in the login and some small issues. --- harbour-tooterb.pro.user | 4 +- qml/lib/Worker.js | 79 +++++++++++++++++------ qml/pages/LoginPage.qml | 11 +--- qml/pages/components/MyList.qml | 107 ++++++++++++++++++++++++++++++-- rpm/harbour-tooterb.spec | 2 +- 5 files changed, 167 insertions(+), 36 deletions(-) diff --git a/harbour-tooterb.pro.user b/harbour-tooterb.pro.user index 4e5e8fc..3d4772a 100644 --- a/harbour-tooterb.pro.user +++ b/harbour-tooterb.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -112,7 +112,7 @@ true QtProjectManager.QMakeBuildStep - true + false diff --git a/qml/lib/Worker.js b/qml/lib/Worker.js index 19b9ddd..9ffe04b 100644 --- a/qml/lib/Worker.js +++ b/qml/lib/Worker.js @@ -2,13 +2,22 @@ Qt.include("Mastodon.js") var loadImages = true; +// used to dedupe on append/insert +var knownIds = []; WorkerScript.onMessage = function(msg) { +/* console.log("Action > " + msg.action) console.log("Model > " + msg.model) console.log("Mode > " + msg.mode) console.log("Conf > " + JSON.stringify(msg.conf)) console.log("Params > " + JSON.stringify(msg.params)) +*/ + if (msg.params[1]) { + // console.log(JSON.stringify(msg.params[1]["data"])) + knownIds = msg.params[1]["data"] + //console.log(knownIds[0]) + } /** order notifications in ASC order */ function orderNotifications(items){ @@ -45,7 +54,7 @@ WorkerScript.onMessage = function(msg) { } else { for (var i in data) { if (data.hasOwnProperty(i)) { - console.log(JSON.stringify(data[i])) + //console.log(JSON.stringify(data[i])) WorkerScript.sendMessage({ 'action': msg.action, 'success': true, key: i, "data": data[i]}) } } @@ -54,22 +63,28 @@ WorkerScript.onMessage = function(msg) { return; } - API.get(msg.action, msg.params, function(data) { + API.get(msg.action, msg.params, function(data) { var items = []; + + //console.log(msg.ids) + for (var i in data) { var item; if (data.hasOwnProperty(i)) { if(msg.action === "accounts/search") { item = parseAccounts([], "", data[i]); - console.log(JSON.stringify(data[i])) + //console.log(JSON.stringify(data[i])) + console.log("has own data") + items.push(item) } else if(msg.action === "notifications") { // notification console.log("Get notification list") - console.log(JSON.stringify(data[i])) + //console.log(JSON.stringify(data[i])) item = parseNotification(data[i]); - items.push(item) + items.push(item); + } else if(msg.action.indexOf("statuses") >-1 && msg.action.indexOf("context") >-1 && i === "ancestors") { // status ancestors toots - conversation @@ -79,8 +94,9 @@ WorkerScript.onMessage = function(msg) { item['id'] = item['status_id']; if (typeof item['attachments'] === "undefined") item['attachments'] = []; - items.push(item) - console.log(JSON.stringify(data[i][j])) + // don't permit doubles + items.push(item); + //console.log(JSON.stringify(data[i][j])) } addDataToModel (msg.model, "prepend", items); items = []; @@ -94,17 +110,18 @@ WorkerScript.onMessage = function(msg) { item['id'] = item['status_id']; if (typeof item['attachments'] === "undefined") item['attachments'] = []; - items.push(item) - console.log(JSON.stringify(data[i][j])) + items.push(item); + //console.log(JSON.stringify(data[i][j])) } addDataToModel (msg.model, "append", items); items = []; } else if (data[i].hasOwnProperty("content")){ - //console.log("Get Toot") + console.log("Get Toot") item = parseToot(data[i]); item['id'] = item['status_id'] - items.push(item) + items.push(item); + } else { WorkerScript.sendMessage({ 'action': msg.action, 'success': true, key: i, "data": data[i] }) @@ -112,8 +129,11 @@ WorkerScript.onMessage = function(msg) { } } - if(msg.model && items.length) + console.log("Get em all?") + if(msg.model && items.length) { addDataToModel(msg.model, msg.mode, items) + WorkerScript.sendMessage({ 'updatedAll': true}) + } /*if(msg.action === "notifications") orderNotifications(items)*/ }); @@ -121,19 +141,40 @@ WorkerScript.onMessage = function(msg) { //WorkerScript.sendMessage({ 'notifyNewItems': length - i }) function addDataToModel (model, mode, items) { + var length = items.length; + var i + var inti = 0 console.log("Fetched > " +length) if (mode === "append") { - model.append(items) - } else if (mode === "prepend") { - for(var i = length-1; i >= 0 ; i--) { - model.insert(0,items[i]) + for(i = length-1; i >= 0 ; i--) { + if ( knownIds.indexOf( items[i]["id"]) === -1) { + model.append(items[i]) + inti++ + } } - } - model.sync() -} + // don't sync unless we have updates + if (inti > 0 ) model.sync() + } else if (mode === "prepend") { + for(i = length-1; i >= 0 ; i--) { + if ( knownIds.indexOf( items[i]["id"]) === -1) { + inti++ + model.insert(0,items[i]) + } + } + if (inti > 0 ) model.sync() + } +} +function findDuplicate(arr,val) { + for(var i=0; i < arr.length; i++){ + if( arr.indexOf(val) === -1 ) { + return true; + } + } + return false; +} /** Function: Get Account Data */ function parseAccounts(collection, prefix, data) { diff --git a/qml/pages/LoginPage.qml b/qml/pages/LoginPage.qml index 8526528..4df27d5 100644 --- a/qml/pages/LoginPage.qml +++ b/qml/pages/LoginPage.qml @@ -135,23 +135,18 @@ Page { case "embed:contentOrientationChanged": break case "webview:action": - if ( data.topic != lon ) { - //webview.runJavaScript("return latlon('" + lat + "','" + lon + "')"); - //if (debug) console.debug(data.topic) - //if (debug) console.debug(data.also) - //if (debug) console.debug(data.src) - } break } } visible: false //opacity: 0 - anchors { + anchors.fill: parent + /*{ top: parent.top left: parent.left right: parent.right bottom: parent.bottom - } + }*/ onLoadingChanged: { if(debug) console.log('loading changed: ' + url) diff --git a/qml/pages/components/MyList.qml b/qml/pages/components/MyList.qml index f1885a8..fd0dcab 100644 --- a/qml/pages/components/MyList.qml +++ b/qml/pages/components/MyList.qml @@ -7,11 +7,12 @@ import "." SilicaListView { id: myList - property bool debug: false + property bool debug: true property string type property string title property string description property ListModel mdl: [] + property ListModel tempMdl: [] property variant params: [] property var locale: Qt.locale() property bool autoLoadMore: true @@ -21,6 +22,8 @@ SilicaListView { property variant vars property variant conf property bool notifier: false + property bool deduping: false + property variant uniqueIds: [] model: mdl @@ -116,7 +119,10 @@ SilicaListView { } onCountChanged: { + + //deDouble() loadStarted = false + /*contentY = scrollOffset console.log("CountChanged!")*/ } @@ -131,7 +137,7 @@ SilicaListView { anchors.bottomMargin: Theme.paddingLarge visible: false onClicked: { - loadData("append") + if (!loadStarted && !deduping) loadData("append") } } @@ -152,8 +158,10 @@ SilicaListView { scrollOffset = contentY } if(contentY+height > footerItem.y && !loadStarted && autoLoadMore) { - loadData("append") - loadStarted = true + if (! deduping) { + loadData("append") + loadStarted = true + } } } @@ -163,28 +171,110 @@ SilicaListView { id: worker source: "../../lib/Worker.js" onMessage: { + //if (debug) console.log("worker says") + //if (debug) console.log(JSON.stringify(messageObject)) if (messageObject.error){ if (debug) console.log(JSON.stringify(messageObject)) } if (messageObject.fireNotification && notifier){ Logic.notifier(messageObject.data) } + // temporary debugging measure + // should be resolved within loadData() + if (messageObject.updatedAll){ + //if (model.count > 30) deDouble() + } } } Component.onCompleted: { loadData("prepend") + if (debug) console.log("MyList completed") } Timer { triggeredOnStart: false; interval: 5*60*1000; running: true; repeat: true onTriggered: { if(debug) console.log(title + ' ' +Date().toString()) - loadData("prepend") + // let's avoid pre and appending at the same time! + if ( ! loadStarted && ! deduping ) loadData("prepend") } } + /* + * utility called on updates to model to remove remove Duplicates: + * the dupes are probably a result of improper syncing of the models + * this is temporary and can probaly be removed because of the + * loadData method passing in to the WorkerScript + */ + function deDouble(){ + + deduping = true + var ids = [] + var uniqueItems = [] + var i + var j + var seenIt = 0 + + if (debug) console.log(model.count) + + for(i = 0 ; i < model.count ; i++) { + ids.push(model.get(i).id) + uniqueItems = removeDuplicates(ids) + //if (debug) console.log(model.get(i).id) + } + + if (debug) console.log(uniqueItems.length) + + if ( uniqueItems.length < model.count) { + if (debug) console.log(model.count) + for(j = 0; j <= uniqueItems.length - 1 ; j++) { + seenIt = 0 + for(i = 0 ; i < model.count - 1 ; i++) { + if (model.get(i).id === uniqueItems[j]){ + seenIt = seenIt+1 + if (seenIt > 1) { + if (debug) console.log(uniqueItems[j] + " - " + seenIt) + + // model.remove(i,1) // (model.get(i)) + seenIt = seenIt-1 + } + } + } + } + } + + deduping = false + } + /* utility function because this version of qt doesn't support modern javascript + * + */ + function removeDuplicates(arr) { + var unique = []; + for(var i=0; i < arr.length; i++){ + if(unique.indexOf(arr[i]) === -1) { + unique.push(arr[i]); + } + } + return unique; + } + + /* Principle load function, uses websocket's worker.js + * + */ + function loadData(mode) { + + if (debug) console.log('loadData called') + // since the worker adds Duplicates + // we pass in current ids in the model + // and skip those on insert append in the worker + for(var i = 0 ; i < model.count ; i++) { + uniqueIds.push(model.get(i).id) + //if (debug) console.log(model.get(i).id) + } + uniqueIds = removeDuplicates(uniqueIds) + var p = [] if (params.length) { for(var i = 0; i