-
Notifications
You must be signed in to change notification settings - Fork 1
/
bmw-leaselense.js
126 lines (96 loc) · 3.9 KB
/
bmw-leaselense.js
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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: blue; icon-glyph: taxi;
const api = importModule('bmw-leaselense/api');
const utils = importModule('bmw-leaselense/utils');
const configuration = importModule('bmw-leaselense/config');
if (config.runsInWidget) {
const size = config.widgetFamily;
const widget = await createWidget(size);
Script.setWidget(widget);
Script.complete();
} else {
// Choose a size for debugging
//const size = 'small';
const size = 'medium';
//const size = 'large'
const widget = await createWidget(size);
if (size == 'small') {
widget.presentSmall();
} else if (size == 'medium') {
widget.presentMedium();
} else {
widget.presentLarge();
}
Script.complete();
}
async function createWidget(size) {
const data = await api.fetchVehicleData();
const colors = configuration.colorConfig(data.remainingRange);
const widget = new ListWidget();
widget.setPadding(0, 15, 10, 15);
widget.backgroundColor = colors.bgColor;
if (size == 'small') {
// TODO: Show message that this size is currently not supported
} else if (size == 'medium') {
const car = await api.fetchVehicleImage();
const mileage = utils.computeMileageLevel(data.mileage);
const contentStack = widget.addStack();
contentStack.layoutHorizontally();
contentStack.addImage(car);
contentStack.addSpacer();
widget.addImage(utils.createProgressbar(mileage.total, mileage.current, mileage.withinLimit));
widget.addSpacer();
const mileageStack = widget.addStack();
mileageStack.layoutHorizontally();
// Display the expected cost if not within your limits
if (!mileage.withinLimit) {
const costStack = contentStack.addStack();
costStack.layoutVertically();
costStack.addSpacer();
// Need to replace _ with - in the locale string since js has a different view on it then iOS
const locale = Device.locale().replace('_', '-');
const cost = (Math.abs(mileage.difference) * configuration.COST_PER_KILOMETER).toLocaleString(locale, {
style: 'currency',
currency: configuration.CURRENCY,
});
const costText = costStack.addText(cost);
costText.font = Font.boldMonospacedSystemFont(35);
costText.minimumScaleFactor = 0.5;
costStack.addSpacer();
}
const milageIcon = mileageStack.addImage(await utils.getAsset('mileage.png'));
milageIcon.imageSize = new Size(15, 15);
milageIcon.tintColor = colors.textColor;
mileageStack.addSpacer(5);
const mileageText = mileageStack.addText(
`${mileage.current.toLocaleString()} / ${mileage.total.toLocaleString()} km`
);
mileageText.font = Font.boldMonospacedSystemFont(15);
mileageStack.addSpacer();
const sign = mileage.difference > 0 ? '+' : '-';
const diffText = mileageStack.addText(`${sign}${Math.abs(mileage.difference)} km`);
diffText.font = Font.boldMonospacedSystemFont(15);
const rangeStack = widget.addStack();
rangeStack.layoutHorizontally();
const rangeimg = rangeStack.addImage(await utils.getAsset('fuel.png'));
rangeimg.imageSize = new Size(15, 15);
rangeimg.tintColor = colors.rangeColor;
rangeStack.addSpacer(5);
const rangeText = rangeStack.addText(`${data.remainingRange} km`);
rangeText.font = Font.regularMonospacedSystemFont(12);
rangeText.textColor = colors.rangeColor;
widget.addSpacer();
const updateStack = widget.addStack();
updateStack.addSpacer();
const updateImg = SFSymbol.named('arrow.triangle.2.circlepath').image;
const updateIcon = updateStack.addImage(updateImg);
updateIcon.imageSize = new Size(11, 11);
updateIcon.tintColor = colors.textColor;
updateStack.addSpacer(3);
const updated = new Date(data.updated);
const updateText = updateStack.addText(`${updated.toLocaleString()}`);
updateText.font = Font.systemFont(9);
}
return widget;
}