-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwidget.js
153 lines (141 loc) · 4.76 KB
/
widget.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
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
// DUCO Widget for iOS © MIT licensed
// Version 1.1
// Naphob ([email protected])
const ducoUser = "YOUR USERNAME" //input your duino coin username here
const colors = {
gradientBackground: {
primary: {
start : "ff4112",
end: "ffb412"
},
medium: {
start : "4A00E0",
end: "8E2DE2"
},
premium: {
start : "232526",
end: "414345"
}
},
text: {
primary: "ffffff"
}
}
const gradientStart = new Point(0.75, 1)
const gradientEnd = new Point(0.05, 0)
const api = await userData()
const widget = await createWidget(api)
if (config.runsInWidget) {
// The script runs inside a widget, so we pass our instance of ListWidget to be shown inside the widget on the Home Screen.
Script.setWidget(widget)
} else {
// The script runs inside the app, so we preview the widget.
//widget.presentMedium()
widget.presentMedium()
}
// Calling Script.complete() signals to Scriptable that the script have finished running.
// This can speed up the execution, in particular when running the script from Shortcuts or using Siri.
Script.complete()
async function createWidget(api) {
let appIcon = await loadAppIcon()
const title = "Duino Coin"
let widget = new ListWidget()
// Add background gradient
let gradient = new LinearGradient()
gradient.locations = [0, 1]
gradient.colors = await setBackgroundColor(api.balance)
gradient.startPoint = gradientStart
gradient.endPoint = gradientEnd
widget.backgroundGradient = gradient
// Show title
let titleStack = widget.addStack()
let titleElement = titleStack.addText(title)
titleElement.textColor = new Color(colors.text.primary)
titleElement.textOpacity = 0.8
titleElement.font = Font.mediumSystemFont(14)
widget.addSpacer(8)
// Show DUCO balance
let userStack = widget.addStack()
userStack.centerAlignContent()
let nameElement = userStack.addText(api.username + "'s balance")
nameElement.textColor = new Color(colors.text.primary)
nameElement.font = Font.systemFont(18)
// Show veriftied badge
if (api.verified === "yes") {
userStack.addSpacer(5)
let tmpSF = SFSymbol.named('checkmark.circle.fill')
let wImg = userStack.addImage(tmpSF.image)
wImg.tintColor = new Color(colors.text.primary)
wImg.imageSize = new Size(14, 14)
}
widget.addSpacer(2)
let balanceElement = widget.addText(api.balance.toFixed(2).toString() + " ᕲ")
balanceElement.textColor = new Color(colors.text.primary)
balanceElement.font = Font.boldSystemFont(22)
widget.addSpacer(1)
let profitElement = widget.addText("≈ $" + api.balanceInUSD.toFixed(2).toString())
profitElement.textColor = new Color(colors.text.primary)
profitElement.textOpacity = 0.6
profitElement.font = Font.systemFont(14)
widget.addSpacer(2)
// Show miners
let bottomStack = widget.addStack()
let minerElement = bottomStack.addText("Miners: " + api.miner)
minerElement.textColor = new Color(colors.text.primary)
minerElement.textOpacity = 0.8
minerElement.font = Font.systemFont(16)
minerElement.leftAlignText()
// Show DUCO icon
bottomStack.addSpacer(null)
let appIconElement = bottomStack.addImage(appIcon)
appIconElement.imageSize = new Size(25, 25)
appIconElement.rightAlignImage()
return widget
}
async function userData() {
let docs = await loadDocs()
let username = docs["result"]["balance"]["username"]
let balance = docs["result"]["balance"]["balance"]
let miner = docs["result"]["miners"].length
let verified = docs["result"]["balance"]["verified"]
let prices = await loadPrice()
let price = prices["Duco price"]
let result = {
username: username,
balance: balance,
miner: miner,
verified: verified,
balanceInUSD: balance * price
}
return result
}
async function loadDocs() {
let url = "https://server.duinocoin.com/users/" + ducoUser
let req = new Request(url)
return await req.loadJSON()
}
async function loadPrice() {
let url = "https://server.duinocoin.com/api.json"
let req = new Request(url)
return await req.loadJSON()
}
async function loadAppIcon() {
let url = "https://github.com/revoxhere/duino-coin/raw/master/Resources/duco.png"
let req = new Request(url)
return req.loadImage()
}
async function setBackgroundColor(balance) {
if (balance <= 1000) {
let backgroundGradient = [new Color(colors.gradientBackground.primary.start),
new Color(colors.gradientBackground.primary.end)]
return backgroundGradient
} else if (balance > 1000 && balance <= 10000) {
let backgroundGradient = [new Color(colors.gradientBackground.medium.start),
new Color(colors.gradientBackground.medium.end)]
return backgroundGradient
} else {
let backgroundGradient = [new Color(colors.gradientBackground.premium.start),
new Color(colors.gradientBackground.premium.end)]
return backgroundGradient
}
}