Starting the work

This commit is contained in:
Zach Fox 2018-05-15 11:48:33 -07:00
parent e15a800639
commit ccab2dd641
2 changed files with 207 additions and 90 deletions

View file

@ -18,27 +18,24 @@ import QtQuick.Controls 2.2
import "../../../styles-uit"
import "../../../controls-uit" as HifiControlsUit
import "../../../controls" as HifiControls
import "../../../models" as HifiModels
Item {
HifiConstants { id: hifi; }
id: root;
property bool initialHistoryReceived: false;
property bool historyRequestPending: true;
property bool noMoreHistoryData: false;
property bool initialResultReceived: false;
property int pendingCount: 0;
property int currentHistoryPage: 1;
property var pagesAlreadyAdded: new Array();
onVisibleChanged: {
if (visible) {
transactionHistoryModel.clear();
Commerce.balance();
initialHistoryReceived = false;
root.currentHistoryPage = 1;
root.noMoreHistoryData = false;
root.historyRequestPending = true;
Commerce.history(root.currentHistoryPage);
transactionHistoryModel.initialResultReceived = false;
transactionHistoryModel.nextPageToRetrieve = 1;
transactionHistoryModel.noMoreDataToRetrieve = false;
transactionHistoryModel.requestPending = true;
Commerce.history(transactionHistoryModel.nextPageToRetrieve);
Commerce.getAvailableUpdates();
} else {
refreshTimer.stop();
@ -53,84 +50,16 @@ Item {
}
onHistoryResult : {
root.initialHistoryReceived = true;
root.historyRequestPending = false;
transactionHistoryModel.processResult(result.status, result.data.history);
if (result.status === 'success') {
var currentPage = parseInt(result.current_page);
if (result.data.history.length === 0) {
root.noMoreHistoryData = true;
console.log("No more data to retrieve from Commerce.history() endpoint.")
} else if (root.currentHistoryPage === 1) {
var sameItemCount = 0;
tempTransactionHistoryModel.clear();
tempTransactionHistoryModel.append(result.data.history);
for (var i = 0; i < tempTransactionHistoryModel.count; i++) {
if (!transactionHistoryModel.get(i)) {
sameItemCount = -1;
break;
} else if (tempTransactionHistoryModel.get(i).transaction_type === transactionHistoryModel.get(i).transaction_type &&
tempTransactionHistoryModel.get(i).text === transactionHistoryModel.get(i).text) {
sameItemCount++;
}
}
if (sameItemCount !== tempTransactionHistoryModel.count) {
transactionHistoryModel.clear();
for (var i = 0; i < tempTransactionHistoryModel.count; i++) {
transactionHistoryModel.append(tempTransactionHistoryModel.get(i));
}
calculatePendingAndInvalidated();
}
} else {
if (root.pagesAlreadyAdded.indexOf(currentPage) !== -1) {
console.log("Page " + currentPage + " of history has already been added to the list.");
} else {
// First, add the history result to a temporary model
tempTransactionHistoryModel.clear();
tempTransactionHistoryModel.append(result.data.history);
// Make a note that we've already added this page to the model...
root.pagesAlreadyAdded.push(currentPage);
var insertionIndex = 0;
// If there's nothing in the model right now, we don't need to modify insertionIndex.
if (transactionHistoryModel.count !== 0) {
var currentIteratorPage;
// Search through the whole transactionHistoryModel and look for the insertion point.
// The insertion point is found when the result page from the server is less than
// the page that the current item came from, OR when we've reached the end of the whole model.
for (var i = 0; i < transactionHistoryModel.count; i++) {
currentIteratorPage = transactionHistoryModel.get(i).resultIsFromPage;
if (currentPage < currentIteratorPage) {
insertionIndex = i;
break;
} else if (i === transactionHistoryModel.count - 1) {
insertionIndex = i + 1;
break;
}
}
}
// Go through the results we just got back from the server, setting the "resultIsFromPage"
// property of those results and adding them to the main model.
for (var i = 0; i < tempTransactionHistoryModel.count; i++) {
tempTransactionHistoryModel.setProperty(i, "resultIsFromPage", currentPage);
transactionHistoryModel.insert(i + insertionIndex, tempTransactionHistoryModel.get(i))
}
calculatePendingAndInvalidated();
}
}
if (!transactionHistoryModel.noMoreDataToRetrieve) {
calculatePendingAndInvalidated();
}
// Only auto-refresh if the user hasn't scrolled
// and there is more data to grab
if (transactionHistory.atYBeginning && !root.noMoreHistoryData) {
if (transactionHistory.atYBeginning && !transactionHistoryModel.noMoreDataToRetrieve) {
refreshTimer.start();
}
}
@ -235,7 +164,7 @@ Item {
onTriggered: {
if (transactionHistory.atYBeginning) {
console.log("Refreshing 1st Page of Recent Activity...");
root.historyRequestPending = true;
transactionHistoryModel.requestPending = true;
Commerce.balance();
Commerce.history(1);
}
@ -302,7 +231,7 @@ Item {
ListModel {
id: tempTransactionHistoryModel;
}
ListModel {
HifiModels.PSFListModel {
id: transactionHistoryModel;
}
Item {
@ -313,7 +242,7 @@ Item {
anchors.right: parent.right;
Item {
visible: transactionHistoryModel.count === 0 && root.initialHistoryReceived;
visible: transactionHistoryModel.count === 0 && transactionHistoryModel.initialResultReceived;
anchors.centerIn: parent;
width: parent.width - 12;
height: parent.height;
@ -462,11 +391,11 @@ Item {
onAtYEndChanged: {
if (transactionHistory.atYEnd) {
console.log("User scrolled to the bottom of 'Recent Activity'.");
if (!root.historyRequestPending && !root.noMoreHistoryData) {
if (!transactionHistoryModel.requestPending && !transactionHistoryModel.noMoreDataToRetrieve) {
// Grab next page of results and append to model
root.historyRequestPending = true;
Commerce.history(++root.currentHistoryPage);
console.log("Fetching Page " + root.currentHistoryPage + " of Recent Activity...");
transactionHistoryModel.requestPending = true;
Commerce.history(++transactionHistoryModel.nextPageToRetrieve);
console.log("Fetching Page " + transactionHistoryModel.nextPageToRetrieve + " of Recent Activity...");
}
}
}

View file

@ -0,0 +1,188 @@
//
// PSFListModel.qml
// qml/hifi/commerce/common
//
// PSFListModel
// "PSF" stands for:
// - Paged
// - Sortable
// - Filterable
//
// Created by Zach Fox on 2018-05-15
// Copyright 2018 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
import QtQuick 2.7
ListModel {
id: root;
property string sortColumnName: "";
property bool isSortingDescending: true;
property bool valuesAreNumerical: false;
property bool initialResultReceived: false;
property bool requestPending: false;
property bool noMoreDataToRetrieve: false;
property int nextPageToRetrieve: 1;
property var pagesAlreadyAdded: new Array();
ListModel {
id: tempModel;
}
function processResult(status, retrievedResult) {
root.initialResultReceived = true;
root.requestPending = false;
if (status === 'success') {
var currentPage = parseInt(result.current_page);
if (retrievedResult.length === 0) {
root.noMoreDataToRetrieve = true;
console.log("No more data to retrieve from backend endpoint.")
} else if (root.nextPageToRetrieve === 1) {
var sameItemCount = 0;
tempModel.clear();
tempModel.append(retrievedResult);
for (var i = 0; i < tempModel.count; i++) {
if (!root.get(i)) {
sameItemCount = -1;
break;
}
// Gotta think of another way to determine if the data we just got is the same
// as the data that we already have in the model.
/* else if (tempModel.get(i).transaction_type === root.get(i).transaction_type &&
tempModel.get(i).text === root.get(i).text) {
sameItemCount++;
}*/
}
if (sameItemCount !== tempModel.count) {
root.clear();
for (var i = 0; i < tempModel.count; i++) {
root.append(tempModel.get(i));
}
}
} else {
if (root.pagesAlreadyAdded.indexOf(currentPage) !== -1) {
console.log("Page " + currentPage + " of paginated data has already been added to the list.");
} else {
// First, add the result to a temporary model
tempModel.clear();
tempModel.append(retrievedResult);
// Make a note that we've already added this page to the model...
root.pagesAlreadyAdded.push(currentPage);
var insertionIndex = 0;
// If there's nothing in the model right now, we don't need to modify insertionIndex.
if (root.count !== 0) {
var currentIteratorPage;
// Search through the whole model and look for the insertion point.
// The insertion point is found when the result page from the server is less than
// the page that the current item came from, OR when we've reached the end of the whole model.
for (var i = 0; i < root.count; i++) {
currentIteratorPage = root.get(i).resultIsFromPage;
if (currentPage < currentIteratorPage) {
insertionIndex = i;
break;
} else if (i === root.count - 1) {
insertionIndex = i + 1;
break;
}
}
}
// Go through the results we just got back from the server, setting the "resultIsFromPage"
// property of those results and adding them to the main model.
// NOTE that this wouldn't be necessary if we did this step on the server.
for (var i = 0; i < tempModel.count; i++) {
tempModel.setProperty(i, "resultIsFromPage", currentPage);
root.insert(i + insertionIndex, tempModel.get(i))
}
}
}
}
}
function swap(a, b) {
if (a < b) {
move(a, b, 1);
move(b - 1, a, 1);
} else if (a > b) {
move(b, a, 1);
move(a - 1, b, 1);
}
}
function partition(begin, end, pivot) {
if (valuesAreNumerical) {
var piv = get(pivot)[sortColumnName];
swap(pivot, end - 1);
var store = begin;
var i;
for (i = begin; i < end - 1; ++i) {
var currentElement = get(i)[sortColumnName];
if (isSortingDescending) {
if (currentElement > piv) {
swap(store, i);
++store;
}
} else {
if (currentElement < piv) {
swap(store, i);
++store;
}
}
}
swap(end - 1, store);
return store;
} else {
var piv = get(pivot)[sortColumnName].toLowerCase();
swap(pivot, end - 1);
var store = begin;
var i;
for (i = begin; i < end - 1; ++i) {
var currentElement = get(i)[sortColumnName].toLowerCase();
if (isSortingDescending) {
if (currentElement > piv) {
swap(store, i);
++store;
}
} else {
if (currentElement < piv) {
swap(store, i);
++store;
}
}
}
swap(end - 1, store);
return store;
}
}
function qsort(begin, end) {
if (end - 1 > begin) {
var pivot = begin + Math.floor(Math.random() * (end - begin));
pivot = partition(begin, end, pivot);
qsort(begin, pivot);
qsort(pivot + 1, end);
}
}
function quickSort() {
qsort(0, count)
}
}