Revert "Use QML WorkerScript for loading and updating models"
This reverts commit d8fd1ff3dd
.
This commit is contained in:
parent
ab4dfc611b
commit
4a0b3f1b12
6 changed files with 33 additions and 126 deletions
|
@ -62,10 +62,6 @@ ListModel {
|
|||
});
|
||||
}
|
||||
|
||||
property var worker: ModelWorkerScript {
|
||||
id: modelWorker
|
||||
}
|
||||
|
||||
function forEachEpisode(callback) {
|
||||
// Go from bottom up (= chronological order)
|
||||
for (var i=count-1; i>=0; i--) {
|
||||
|
@ -126,12 +122,11 @@ ListModel {
|
|||
|
||||
ready = false;
|
||||
py.call('main.load_episodes', [podcast_id, query], function (episodes) {
|
||||
modelWorker.updateModelFrom(episodeListModel, episodes, function () {
|
||||
episodeListModel.ready = true;
|
||||
if (callback !== undefined) {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
Util.updateModelFrom(episodeListModel, episodes);
|
||||
episodeListModel.ready = true;
|
||||
if (callback !== undefined) {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,11 +26,11 @@ Connections {
|
|||
target: py
|
||||
|
||||
onDownloadProgress: {
|
||||
episodeListModel.worker.updateModelWith(episodeListModel, 'id', episode_id,
|
||||
Util.updateModelWith(episodeListModel, 'id', episode_id,
|
||||
{'progress': progress});
|
||||
}
|
||||
onPlaybackProgress: {
|
||||
episodeListModel.worker.updateModelWith(episodeListModel, 'id', episode_id,
|
||||
Util.updateModelWith(episodeListModel, 'id', episode_id,
|
||||
{'playbackProgress': progress});
|
||||
}
|
||||
onUpdatedEpisode: {
|
||||
|
|
|
@ -25,13 +25,9 @@ import 'util.js' as Util
|
|||
ListModel {
|
||||
id: podcastListModel
|
||||
|
||||
property var worker: ModelWorkerScript {
|
||||
id: modelWorker
|
||||
}
|
||||
|
||||
function reload() {
|
||||
py.call('main.load_podcasts', [], function (podcasts) {
|
||||
modelWorker.updateModelFrom(podcastListModel, podcasts);
|
||||
Util.updateModelFrom(podcastListModel, podcasts);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,67 +0,0 @@
|
|||
|
||||
/**
|
||||
*
|
||||
* gPodder QML UI Reference Implementation
|
||||
* Copyright (c) 2015, Thomas Perl <m@thp.io>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
* PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
import QtQuick 2.0
|
||||
|
||||
|
||||
WorkerScript {
|
||||
source: 'modelworker.js'
|
||||
|
||||
property int callbacks_seq: 1
|
||||
property var callbacks: ({})
|
||||
|
||||
function refCallback(callback) {
|
||||
var id = callbacks_seq++;
|
||||
callbacks[id] = callback;
|
||||
return id;
|
||||
}
|
||||
|
||||
function unrefCallback(callback) {
|
||||
var result = callbacks[callback];
|
||||
delete callbacks[callback];
|
||||
return result;
|
||||
}
|
||||
|
||||
function updateModelFrom(model, data, callback) {
|
||||
sendMessage({
|
||||
action: 'updateModelFrom',
|
||||
model: model,
|
||||
data: data,
|
||||
callback: refCallback(callback),
|
||||
});
|
||||
}
|
||||
|
||||
function updateModelWith(model, key, value, update, callback) {
|
||||
sendMessage({
|
||||
action: 'updateModelWith',
|
||||
model: model,
|
||||
key: key,
|
||||
value: value,
|
||||
update: update,
|
||||
callback: refCallback(callback),
|
||||
});
|
||||
}
|
||||
|
||||
onMessage: {
|
||||
if (messageObject.callback !== undefined) {
|
||||
unrefCallback(messageObject.callback)();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
function updateModelFrom(model, data) {
|
||||
// TODO: This is very naive at the moment, we should do proper remove(),
|
||||
// move(), set() and insert() calls, so that the UI can animate changes.
|
||||
for (var i=0; i<data.length; i++) {
|
||||
if (model.count < i) {
|
||||
model.append(data[i]);
|
||||
} else {
|
||||
model.set(i, data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
while (model.count > data.length) {
|
||||
model.remove(model.count-1);
|
||||
}
|
||||
|
||||
model.sync();
|
||||
}
|
||||
|
||||
function updateModelWith(model, key, value, update) {
|
||||
for (var row=0; row<model.count; row++) {
|
||||
var current = model.get(row);
|
||||
if (current[key] == value) {
|
||||
for (var key in update) {
|
||||
model.setProperty(row, key, update[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
model.sync();
|
||||
}
|
||||
|
||||
WorkerScript.onMessage = function (msg) {
|
||||
if (msg.action === 'updateModelFrom') {
|
||||
updateModelFrom(msg.model, msg.data);
|
||||
WorkerScript.sendMessage({callback: msg.callback});
|
||||
} else if (msg.action === 'updateModelWith') {
|
||||
updateModelWith(msg.model, msg.key, msg.value, msg.update);
|
||||
WorkerScript.sendMessage({callback: msg.callback});
|
||||
} else {
|
||||
console.log('Unknown action: ' + msg.action);
|
||||
}
|
||||
}
|
|
@ -18,6 +18,31 @@
|
|||
*
|
||||
*/
|
||||
|
||||
function updateModelFrom(model, data) {
|
||||
for (var i=0; i<data.length; i++) {
|
||||
if (model.count < i) {
|
||||
model.append(data[i]);
|
||||
} else {
|
||||
model.set(i, data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
while (model.count > data.length) {
|
||||
model.remove(model.count-1);
|
||||
}
|
||||
}
|
||||
|
||||
function updateModelWith(model, key, value, update) {
|
||||
for (var row=0; row<model.count; row++) {
|
||||
var current = model.get(row);
|
||||
if (current[key] == value) {
|
||||
for (var key in update) {
|
||||
model.setProperty(row, key, update[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function formatDuration(duration) {
|
||||
if (duration !== 0 && !duration) {
|
||||
return ''
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue