checkpoint

This commit is contained in:
howard-stearns 2018-05-24 15:23:45 -07:00
parent 2f1ce2a2fc
commit 235971f8bf
7 changed files with 144 additions and 222 deletions

View file

@ -44,7 +44,7 @@ Item {
Item {
id: avatarImage;
visible: profileUrl !== "" && userName !== "";
visible: profilePicUrl !== "" && userName !== "";
// Size
anchors.verticalCenter: parent.verticalCenter;
anchors.left: parent.left;

View file

@ -19,6 +19,7 @@ import "../../../../styles-uit"
import "../../../../controls-uit" as HifiControlsUit
import "../../../../controls" as HifiControls
import "../" as HifiCommerceCommon
import "../../../models" as HifiModels
Item {
HifiConstants { id: hifi; }
@ -118,9 +119,7 @@ Item {
if (root.currentActiveView === 'chooseRecipientConnection') {
// Refresh connections model
connectionsLoading.visible = false;
connectionsLoading.visible = true;
sendSignalToParent({method: 'refreshConnections'});
connectionsModel.getFirstPage();
} else if (root.currentActiveView === 'sendAssetHome') {
Commerce.balance();
} else if (root.currentActiveView === 'chooseRecipientNearby') {
@ -392,11 +391,15 @@ Item {
hoverEnabled: true;
}
ListModel {
HifiModels.PSFListModel {
id: connectionsModel;
}
ListModel {
id: filteredConnectionsModel;
http: root.parent; // Misuse of "root" in this file!
endpoint: "/api/v1/users?per_page=400&filter=connections"; // FIXME per_page
processPage: function (data) {
console.log("HRS FIXME processPage", JSON.stringify(data));
return data.users;
//buildFilteredConnectionsModel();
};
}
Rectangle {
@ -495,6 +498,7 @@ Item {
AnimatedImage {
id: connectionsLoading;
visible: !connectionsModel.retrievedAtLeastOnePage;
source: "../../../../../icons/profilePicLoading.gif"
width: 120;
height: width;
@ -515,14 +519,14 @@ Item {
}
visible: !connectionsLoading.visible;
clip: true;
model: filteredConnectionsModel;
model: connectionsModel.model;
snapMode: ListView.SnapToItem;
// Anchors
anchors.fill: parent;
delegate: ConnectionItem {
isSelected: connectionsList.currentIndex === index;
userName: model.userName;
profilePicUrl: model.profileUrl;
userName: model.username;
profilePicUrl: model.images.thumbnail;
anchors.topMargin: 6;
anchors.bottomMargin: 6;
@ -1806,13 +1810,6 @@ Item {
// FUNCTION DEFINITIONS START
//
function updateConnections(connections) {
connectionsModel.clear();
connectionsModel.append(connections);
buildFilteredConnectionsModel();
connectionsLoading.visible = false;
}
function buildFilteredConnectionsModel() {
filteredConnectionsModel.clear();
for (var i = 0; i < connectionsModel.count; i++) {

View file

@ -768,11 +768,32 @@ Rectangle {
case 'updateSelectedRecipientUsername':
sendMoney.fromScript(message);
break;
case 'http.response':
handleHttpResponse(message);
break;
default:
console.log('Unrecognized message from wallet.js:', JSON.stringify(message));
}
}
signal sendToScript(var message);
property var httpCalls: ({});
property var httpCounter: 0;
function request(options, callback) {
console.debug('HRS FIXME Wallet request', JSON.stringify(options));
httpCalls[httpCounter] = callback;
var message = {method: 'http.request', params: options, id: httpCounter++, jsonrpc: "2.0"};
sendToScript(message);
}
function handleHttpResponse(message) {
var callback = httpCalls[message.id]; // FIXME: as different top level tablet apps gets loaded, the id repeats. We should drop old app callbacks without warning.
if (!callback) {
console.warn('No callback for', JSON.stringify(message));
return;
}
delete httpCalls[message.id];
console.log('HRS FIXME QML handling of', JSON.stringify(message));
callback(message.error, message.response);
}
// generateUUID() taken from:
// https://stackoverflow.com/a/8809472

View file

@ -24,12 +24,9 @@ Item {
HifiConstants { id: hifi; }
id: root;
property bool initialResultReceived: false;
property int pendingCount: 0;
onVisibleChanged: {
if (visible) {
transactionHistoryModel.clear();
Commerce.balance();
transactionHistoryModel.getFirstPage();
Commerce.getAvailableUpdates();
@ -46,7 +43,7 @@ Item {
}
onHistoryResult : {
transactionHistoryModel.pageRetrieved(result);
transactionHistoryModel.handlePage(null, result);
}
onAvailableUpdatesResult: {
@ -61,7 +58,7 @@ Item {
Connections {
target: GlobalServices
onMyUsernameChanged: {
transactionHistoryModel.clear();
transactionHistoryModel.resetModel();
usernameText.text = Account.username;
}
}
@ -150,8 +147,7 @@ Item {
if (transactionHistory.atYBeginning) {
console.log("Refreshing 1st Page of Recent Activity...");
Commerce.balance();
transactionHistoryModel.currentPageToRetrieve = 1;
transactionHistoryModel.getPage();
transactionHistoryModel.getFirstPage();
}
}
}
@ -218,22 +214,34 @@ Item {
listModelName: "transaction history";
itemsPerPage: 100;
getPage: function() {
transactionHistoryModel.requestPending = true;
getPage: function () {
console.log('HRS FIXME WalletHome getPage', transactionHistoryModel.currentPageToRetrieve, transactionHistoryModel.itemsPerPage);
Commerce.history(transactionHistoryModel.currentPageToRetrieve, transactionHistoryModel.itemsPerPage);
}
pageRetrieved: function(result) {
transactionHistoryModel.processResult(result.status, result.data.history);
if (!transactionHistoryModel.noMoreDataToRetrieve) {
calculatePendingAndInvalidated();
processPage: function (data) {
console.log('HRS FIXME WalletHome processPage', JSON.stringify(data));
var result, pending;
if (transactionHistoryModel.currentPageToRetrieve == 1) {
pending = {transaction_type: "pendingCount", count: 0};
result = [pending];
} else {
pending = transactionHistoryModel.get(0);
result = [];
}
data.history.forEach(function (item) {
if (item.status === 'pending') {
pending.count++;
} else {
result = result.concat(item);
}
});
// Only auto-refresh if the user hasn't scrolled
// and there is more data to grab
if (transactionHistory.atYBeginning && !transactionHistoryModel.noMoreDataToRetrieve) {
if (transactionHistory.atYBeginning && data.history.length && transactionHistoryModel.currentPageToRetrieve >= 0) {
refreshTimer.start();
}
return result;
}
}
Item {
@ -243,8 +251,8 @@ Item {
anchors.left: parent.left;
anchors.right: parent.right;
Item {
visible: transactionHistoryModel.count === 0 && transactionHistoryModel.initialResultReceived;
Item { // On empty history. We don't want to flash and then replace, so don't show until we know we're zero.
visible: transactionHistoryModel.count === 0 && transactionHistoryModel.currentPageToRetrieve < 0;
anchors.centerIn: parent;
width: parent.width - 12;
height: parent.height;
@ -316,10 +324,10 @@ Item {
model: transactionHistoryModel.model;
delegate: Item {
width: parent.width;
height: (model.transaction_type === "pendingCount" && root.pendingCount !== 0) ? 40 : ((model.status === "confirmed" || model.status === "invalidated") ? transactionText.height + 30 : 0);
height: (model.transaction_type === "pendingCount" && model.count !== 0) ? 40 : ((model.status === "confirmed" || model.status === "invalidated") ? transactionText.height + 30 : 0);
Item {
visible: model.transaction_type === "pendingCount" && root.pendingCount !== 0;
visible: model.transaction_type === "pendingCount" && model.count !== 0;
anchors.top: parent.top;
anchors.left: parent.left;
width: parent.width;
@ -328,7 +336,7 @@ Item {
AnonymousProRegular {
id: pendingCountText;
anchors.fill: parent;
text: root.pendingCount + ' Transaction' + (root.pendingCount > 1 ? 's' : '') + ' Pending';
text: model.count + ' Transaction' + (model.count > 1 ? 's' : '') + ' Pending';
size: 18;
color: hifi.colors.blueAccent;
verticalAlignment: Text.AlignVCenter;
@ -432,21 +440,6 @@ Item {
return year + '-' + month + '-' + day + '<br>' + drawnHour + ':' + min + amOrPm;
}
function calculatePendingAndInvalidated(startingPendingCount) {
var pendingCount = startingPendingCount ? startingPendingCount : 0;
for (var i = 0; i < transactionHistoryModel.count; i++) {
if (transactionHistoryModel.get(i).status === "pending") {
pendingCount++;
}
}
root.pendingCount = pendingCount;
if (pendingCount > 0) {
transactionHistoryModel.insert(0, {"transaction_type": "pendingCount"});
}
}
//
// Function Name: fromScript()
//

View file

@ -18,102 +18,96 @@
import QtQuick 2.7
Item {
id: root;
// Used when printing debug statements
property string listModelName: "";
property string listModelName: endpoint;
// Holds the value of the page that'll be retrieved the next time `getPage()` is called
property int currentPageToRetrieve: 1;
// If defined, the endpoint that `getPage()` will hit (as long as there isn't a custom `getPage()`
// that does something fancy)
// Parameters. Even if you override getPage, below, please set these for clarity and consistency, when applicable.
// E.g., your getPage function could refer to this sortKey, etc.
property string endpoint;
// If defined, the sort key used when calling the above endpoint.
// (as long as there isn't a custom `getPage()` that does something fancy)
property string sortKey;
// If defined, the search filter used when calling the above endpoint.
// (as long as there isn't a custom `getPage()` that does something fancy)
property string searchFilter;
// If defined, the tags filter used when calling the above endpoint.
// (as long as there isn't a custom `getPage()` that does something fancy)
property string tagsFilter;
// The number of items that'll be retrieved per page when calling `getPage()`
// (as long as there isn't a custom `getPage()` that does something fancy)
onEndpointChanged: getFirstPage();
onSortKeyChanged: getFirstPage();
onSearchFilterChanged: getFirstPage();
onTagsFilterChanged: getFirstPage();
property int itemsPerPage: 100;
// This function, by default, will retrieve data from the above-defined `endpoint` with the
// sort and filter data as set above. It can be custom-defined by this item's Parent.
property var getPage: function() {
// Put code here that calls the `endpoint` with the proper `sortKey`, `searchFilter`, and `tagsFilter`.
// Whatever code goes here should define the below `pageRetrieved()` as
// the callback for after the page is asynchronously retrieved.
// The parent of this Item can also define custom `getPage()` and `pageRetrieved()` functions.
// See `WalletHome.qml` as an example of a file that does this. `WalletHome.qml` must use that method because
// it hits an endpoint that must be authenticated via the Wallet.
console.log("default getPage()");
}
// This function, by default, will handle the data retrieved using `getPage()` above.
// It can be custom-defined by this item's Parent.
property var pageRetrieved: function() {
console.log("default pageRetrieved()");
}
// This function, by default, will get the _next_ page of data according to `getPage()` if there
// isn't a pending request and if there's more data to retrieve.
// It can be custom-defined by this item's Parent.
property var getNextPage: function() {
if (!root.requestPending && !root.noMoreDataToRetrieve) {
root.requestPending = true;
root.currentPageToRetrieve++;
root.getPage();
console.log("Fetching Page " + root.currentPageToRetrieve + " of " + root.listModelName + "...");
}
}
// A helper function used to get the first page from the server.
// It can be custom-defined by this item's Parent.
property var getFirstPage: function() {
root.initialResultReceived = false;
root.currentPageToRetrieve = 1;
root.noMoreDataToRetrieve = false;
root.getPage();
}
// State.
property int currentPageToRetrieve: 0; // 0 = before first page. -1 = we have them all. Otherwise 1-based page number.
property bool retrievedAtLeastOnePage: false;
// Resets both internal `ListModel`s and resets the page to retrieve to "1".
function resetModel() {
pagesAlreadyAdded = new Array();
tempModel.clear();
finalModel.clear();
root.currentPageToRetrieve = 1;
currentPageToRetrieve = 1;
retrievedAtLeastOnePage = false
}
onEndpointChanged: {
// Processing one page.
// Override to return one property of data, and/or to transform the elements. Must return an array of model elements.
property var processPage: function (data) { return data; }
// Check consistency and call processPage.
function handlePage(error, response) {
console.log("HRS FIXME got", endpoint, error, JSON.stringify(response));
function fail(message) {
console.warn("Warning", listModelName, JSON.stringify(message));
current_page_to_retrieve = -1;
requestPending = false;
}
if (error || (response.status !== 'success')) {
return fail(error || response.status);
}
if (!requestPending) {
return fail("No request in flight.");
}
requestPending = false;
if (response.current_page && response.current_page !== currentPageToRetrieve) { // Not all endpoints specify this property.
return fail("Mismatched page, expected:" + currentPageToRetrieve);
}
finalModel.append(processPage(response.data || response)); // FIXME keep index steady, and apply any post sort/filter
retrievedAtLeastOnePage = true;
}
// Override either http or getPage.
property var http: null; // An Item that has a request function.
property var getPage: function () { // Any override MUST call handlePage(), above, even if results empty.
if (!http) { return console.warn("Neither http nor getPage was set in", listModelName); }
var url = /^\//.test(endpoint) ? (Account.metaverseServerURL + endpoint) : endpoint;
// FIXME: handle sort and search parameters, and per_page and page parameters
console.log("HRS FIXME requesting", url);
http.request({uri: url}, handlePage);
}
// Start the show by retrieving data according to `getPage()`.
// It can be custom-defined by this item's Parent.
property var getFirstPage: function () {
resetModel();
root.getFirstPage();
requestPending = true;
getPage();
}
onSortKeyChanged: {
resetModel();
root.getFirstPage();
}
onSearchFilterChanged: {
resetModel();
root.getFirstPage();
}
onTagsFilterChanged: {
resetModel();
root.getFirstPage();
}
property bool initialResultReceived: false;
property bool requestPending: false;
property bool noMoreDataToRetrieve: false;
property var pagesAlreadyAdded: new Array();
property bool requestPending: false; // For de-bouncing getNextPage.
// This function, will get the _next_ page of data according to `getPage()`.
// It can be custom-defined by this item's Parent. Typical usage:
// ListView {
// id: theList
// model: thisPSFListModelId
// onAtYEndChanged: if (theList.atYEnd) { thisPSFListModelId.getNextPage(); }
// ...}
property var getNextPage: function () {
if (requestPending || currentPageToRetrieve < 0) {
return;
}
console.log("HRS FIXME Fetching Page " + currentPageToRetrieve + " of " + listModelName + "...");
currentPageToRetrieve++;
requestPending = true;
getPage();
}
// Redefining members and methods so that the parent of this Item
// can use PSFListModel as they would a regular ListModel
property alias model: finalModel;
@ -123,6 +117,8 @@ Item {
function remove(index) { return finalModel.remove(index); }
function setProperty(index, prop, value) { return finalModel.setProperty(index, prop, value); }
function move(from, to, n) { return finalModel.move(from, to, n); }
function insert(index, newElement) { finalModel.insert(index, newElement); }
function append(newElements) { finalModel.append(newElements); }
// Used while processing page data and sorting
ListModel {
@ -134,97 +130,6 @@ Item {
id: finalModel;
}
function processResult(status, retrievedResult) {
root.initialResultReceived = true;
root.requestPending = false;
if (status === 'success') {
var currentPage = parseInt(retrievedResult.current_page);
if (retrievedResult.length === 0) {
root.noMoreDataToRetrieve = true;
console.log("No more data to retrieve from backend endpoint.")
}
/*
See FIXME below...
else if (root.currentPageToRetrieve === 1) {
var sameItemCount = 0;
tempModel.clear();
tempModel.append(retrievedResult);
for (var i = 0; i < tempModel.count; i++) {
if (!finalModel.get(i)) {
sameItemCount = -1;
break;
}
// Gotta think of a generic 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 === finalModel.get(i).transaction_type &&
tempModel.get(i).text === finalModel.get(i).text) {
sameItemCount++;
}
}
if (sameItemCount !== tempModel.count) {
finalModel.clear();
for (var i = 0; i < tempModel.count; i++) {
finalModel.append(tempModel.get(i));
}
}
}
*/
else {
// FIXME! Reconsider this logic, because it means that auto-refreshing the first page of results
// (like we do in WalletHome for Recent Activity) _won't_ catch brand new data elements!
// See the commented code above for how I did this for WalletHome specifically.
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 (finalModel.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 < finalModel.count; i++) {
currentIteratorPage = finalModel.get(i).resultIsFromPage;
if (currentPage < currentIteratorPage) {
insertionIndex = i;
break;
} else if (i === finalModel.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 (or a similar step) on the server.
for (var i = 0; i < tempModel.count; i++) {
tempModel.setProperty(i, "resultIsFromPage", currentPage);
finalModel.insert(i + insertionIndex, tempModel.get(i))
}
}
}
return true;
} else {
console.log("Failed to get page result for " + root.listModelName);
}
return false;
}
// Used when sorting model data on the CLIENT
// Right now, there is no sorting done on the client for

View file

@ -511,6 +511,9 @@
case 'wallet_availableUpdatesReceived':
// NOP
break;
case 'http.request':
// Handled elsewhere, don't log.
break;
default:
print('Unrecognized message from QML:', JSON.stringify(message));
}

View file

@ -988,6 +988,9 @@ var selectionDisplay = null; // for gridTool.js to ignore
sendAssetParticleEffectUpdateTimer = Script.setInterval(updateSendAssetParticleEffect, SEND_ASSET_PARTICLE_TIMER_UPDATE);
}
break;
case 'http.request':
// Handled elsewhere, don't log.
break;
default:
print('Unrecognized message from Checkout.qml or Purchases.qml: ' + JSON.stringify(message));
}