forked from 0x20/tab-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.qml
434 lines (379 loc) · 11.8 KB
/
main.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import QtQml 2.2
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3
import QuickPromise 1.0
import com.thequux.tab 1.0
import "controls"
import "http.js" as Http
Window {
id: application
visible: true
width: 640
height: 480
title: qsTr("Hello World")
property int fontSize: 16
property string fontFace: fontFaceInput.text
property int buttonWidth: 123
property int buttonHeight: (Math.sqrt(5) - 1) / 2 * buttonWidth
property string backend_url: Qt.application.arguments[1] || "http://localhost:4903"
property bool layoutDebug: false
Timer {
interval: 3600000 // hourly
repeat: true
running: true
onTriggered: {
var date = new Date();
if (date.getDay() == 0 && date.getMonth() == 3) {
application.fontFace = "Comic Sans MS"
} else {
application.fontFace = fontFaceInput.text
}
}
}
function layoutColor(color) {
if (layoutDebug) {
return color;
} else {
return "black";
}
}
StackLayout {
width: 1024
height: 768
id: mainStack
anchors.centerIn: parent
state: "loading"
MainView {
id: mainView
onSwitchToTransfer: {
mainStack.state = "transfer"
}
onSwitchToStats: {
mainStack.state = "stats"
}
onPurchaseCommand: {
application.handlePurchase(member, bill)
}
onDepositCommand: {
performTxn(member + " deposited € " + (amount / 100).toFixed(2),
"deposit", {
member: member,
amount: (amount / 100).toFixed(2),
});
}
}
TransferView {
id: transferView
onAccepted: {
// from, to, amount
performTxn(from + " gave " + to + " " + formatCurrency(amount),
"xfer", {
payer: from,
payee: to,
amount: (amount / 100).toFixed(2),
});
mainStack.state = "main"
reset()
}
onCanceled: {
mainStack.state = "main"
reset()
}
}
StatsView {
id: statsView
onCanceled: mainStack.state = "main"
}
LoadingView {
id: loadingView
Timer {
id: reloadTimer
interval: 5000
}
}
states: [
State {
name: "main"
PropertyChanges {
target: mainStack
currentIndex: 0
}
},
State {
name: "transfer"
PropertyChanges {
target: mainStack
currentIndex: 1
}
},
State {
name: "stats"
PropertyChanges {
target: mainStack
currentIndex: 2
}
},
State {
name: "loading"
PropertyChanges {
target: mainStack
currentIndex: 3
}
PropertyChanges {
target: loadingView
progress: 0
message: "Loading"
}
}
]
}
RowLayout {
anchors.top: mainStack.bottom
anchors.left: mainStack.left
anchors.right: parent.right
spacing: 10
TqButton {
text: "text-"
onClicked: { fontSize -= 1 }
}
TqButton {
text: "text+"
onClicked: { fontSize += 1 }
}
Text {
text: "font size: " + fontSize
font.pixelSize: fontSize
}
TqButton {
text: "Toggle layout debug"
onClicked: layoutDebug = !layoutDebug
}
TextInput {
id: fontFaceInput
text: "Roboto"
}
}
Component.onCompleted: {
loadAllData()
}
MemberModel {
id: members
property int dataGeneration: 0
onDataChanged: {
dataGeneration++
}
}
ListModel {
id: productModel
property int dataGeneration: 0
onDataChanged: {
dataGeneration += 1
}
function loadFromJson(json) {
var items = [];
for (var key in json) {
var product = json[key]
items.push({
internal_name: key,
name: product.name,
cost: Math.round((product.price - 0) * 100),
category: product.category,
sort_key: product.sort_key,
})
}
items.sort(function (a,b) { return a.sort_key.localeCompare(b.sort_key)});
clear()
for (var i = 0; i < items.length; i++) {
append(items[i]);
}
}
}
ListModel {
id: logModel
function log(message) {
logPending(message).confirm()
}
function logPending(message) {
var i = count;
var timestamp = new Date()
append({
message: message,
status: "pending",
timestamp: timestamp,
})
return {
confirm: function(newMessage) {
if (newMessage) {
set(i, {
message: newMessage,
status: "confirmed",
timestamp: timestamp,
});
} else {
setProperty(i, "status", "confirmed");
}
},
fail: function() {
setProperty(i, "status", "failed")
},
}
}
}
ListModel {
id: tallyModel
function addItem(currency, name, cost, qty) {
qty = qty || 1;
var found = false;
for (var i = 0; i < count; i++) {
var item = get(i);
if (item.name === name) {
setProperty(i, "count", item.count + qty);
found = true;
break;
}
}
if (!found) {
append({
currency: currency,
name: name,
cost: cost,
count: qty,
});
}
}
function adjustQuantity(currency, qty) {
var index = currency;
var item;
if (currency instanceof String) {
var found = false;
for (var i = 0; i < count; i++) {
item = get(i);
if (item.currency === currency) {
index = i;
found = true;
break;
}
}
if (!found) {
return false;
}
} else if (index < 0 || index >= count) {
return false;
} else {
item = get(index);
}
if (item.count + qty <= 0) {
remove(index);
} else {
setProperty(index, "count", item.count + qty);
}
return true;
}
property int total: {
var total = 0
for (var i = 0; i < count; i++) {
var item = get(i)
total = total + item.cost * item.count
}
return total
}
property int itemCount: {
var total = 0
for (var i = 0; i < count; i++) {
var item = get(i)
total = total + item.count
}
total
}
}
function formatCurrency(cents) {
var locale = Qt.locale("nl_BE");
//var locale = Qt.locale("en_US");
return "€ " + (cents / 100).toFixed(2)
}
function handlePurchase(name, bill) {
var totalCost = 0
var totalItems = 0
var req = {
member: name,
products: {},
};
var displayName = members.get(name).name;
for (var i = 0; i < bill.length; i++) {
var item = bill[i]
req.products[item.currency] =
(req.products[item.currency] || 0) + item.count;
totalCost += item.cost * item.count
totalItems += item.count
}
performTxn(displayName + " bought " + totalItems + " item" +
(totalItems > 1 ? "s" : "") + " for " + formatCurrency(totalCost),
"buy", req);
tallyModel.clear()
}
function performTxn(logMsg, type, req) {
var logEntry = logModel.logPending(logMsg);
Http.post(backend_url + "/api/v1/txn/" + type, req).then(function(xhr) {
var json = JSON.parse(xhr.responseText);
for (var member_name in json.members) {
var member = json.members[member_name];
members.applyDelta(member_name, Math.round(member.balance * 100), member.items)
}
logEntry.confirm(json.message);
}, function(xhr) {
// rejected
logEntry.fail();
logModel.log("Failed to commit transaction: " + xhr.status + " " + xhr.statusText).fail();
});
}
function loadAllData(refresh) {
mainStack.state = "loading";
var loadLevel = 0;
var maxLoad = 3;
function incrLoadLevel() {
loadLevel += 1;
loadingView.progress = loadLevel / maxLoad;
}
function doReload() {
var accounts = Http.get(backend_url + "/api/v1/accounts").then(function(xhr) {
var json = JSON.parse(xhr.responseText)
members.loadFromJson(json)
incrLoadLevel();
return true;
});
var products = Http.get(backend_url + "/api/v1/products").then(function(xhr) {
console.log("here");
var json = JSON.parse(xhr.responseText)
console.log("Adding items " + JSON.stringify(json));
productModel.loadFromJson(json)
incrLoadLevel();
return true;
});
return Q.all([accounts, products]);
}
var start_url;
if (refresh) {
start_url = application.backend_url + "/api/v1/admin/update";
} else {
start_url = application.backend_url + "/api/v1/ping";
}
function doStart() {
loadingView.message = "Waiting for server";
return Http.get(start_url).then(function() {
loadingView.message = "Loading...";
incrLoadLevel();
return doReload()
}).then(null, function() {
loadingView.message = "Failed";
return new Q.Promise(function(resolve, reject) {
console.log("here");
Q.setTimeout(function() {resolve(true)}, 5000);
}).then(doStart);
});
}
doStart().then(function() {
mainStack.state = "main";
});
}
}