-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.ts
More file actions
196 lines (163 loc) · 4.47 KB
/
script.ts
File metadata and controls
196 lines (163 loc) · 4.47 KB
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
import {
config,
Script,
ListWidget,
Color,
Font,
LinearGradient,
Alert,
Request,
WidgetStack,
} from "nios-scriptable";
// ANY CONFIGS YOU WISH TO ADD
const SHOW_TEXT = true;
const SIMULATE_ERROR = false;
// YOUR MAIN FUNCTION
async function mainScript() {
const displayer = myInfo();
if (config.runsInWidget) {
let widget = displayer.has_error
? await createErrorWidget(displayer as BadTestInfo)
: await createWidget(displayer as TestInfo);
Script.setWidget(widget);
} else {
const options = ["Small", "Medium", "Large", "Cancel"];
let resp = await presentAlert("Preview Widget", options, false);
if (resp == options.length - 1) return;
let size = options[resp];
let widget = displayer.has_error
? await createErrorWidget(displayer as BadTestInfo)
: await createWidget(displayer as TestInfo, size.toLowerCase());
switch (size) {
case "Small":
await widget.presentSmall();
break;
case "Medium":
await widget.presentMedium();
break;
case "Large":
await widget.presentLarge();
break;
}
}
Script.complete();
}
await mainScript();
// YOUR CUSTOM CODE FOR THE SCRIPT
interface TestInfo {
has_error: boolean;
imgUrl: string;
url: string;
text: string;
}
interface BadTestInfo {
has_error: boolean;
err_msg: string;
}
function myInfo() {
const urlRedirect = "https://scriptable.app/";
const imageUrl = "https://scriptable.app/assets/appicon.png";
var title = "Sample text using Scriptable!";
const simulateError = SIMULATE_ERROR;
if (simulateError) {
return {
has_error: true,
err_msg: "This text is displayed because there was an error",
} as BadTestInfo;
} else {
return {
has_error: false,
imgUrl: imageUrl,
url: urlRedirect,
text: title,
} as TestInfo;
}
}
// Useful short function to add text quickly to a stack
function addText(
container: WidgetStack,
text: string,
align: string,
size: number
) {
const txt = container.addText(text);
switch (align) {
default:
txt.leftAlignText();
break;
case "center":
txt.centerAlignText();
break;
case "right":
txt.rightAlignText();
break;
}
txt.font = Font.systemFont(size);
txt.shadowRadius = 3;
txt.textColor = Color.white();
txt.shadowColor = Color.black();
}
// Linear gradent for readability for text at the bottom of widget
function newLinearGradient(hexcolors: string[], locations: number[]) {
let gradient = new LinearGradient();
gradient.locations = locations;
gradient.colors = hexcolors.map((color) => new Color(color));
return gradient;
}
// Display an alert
async function presentAlert(prompt: string, items: string[], asSheet: boolean) {
let alert = new Alert();
alert.message = prompt;
for (const item of items) {
alert.addAction(item);
}
let resp = asSheet ? await alert.presentSheet() : await alert.presentAlert();
return resp;
}
// CREATE YOUR WIDGETS
async function createWidget(
data: TestInfo,
widgetFamily: string | null = null
) {
widgetFamily = widgetFamily || config.widgetFamily;
const padd = widgetFamily == "large" ? 12 : 10;
const fontSize = widgetFamily == "large" ? 14 : 10;
const widget = new ListWidget();
const req = new Request(data.imgUrl);
const img = await req.loadImage();
const refreshRate = 5; // min
var refreshDate = Date.now() + 1000 * 60 * refreshRate;
widget.refreshAfterDate = new Date(refreshDate);
widget.url = data.url;
widget.setPadding(padd, padd, padd, padd);
widget.backgroundImage = img;
if (SHOW_TEXT) {
// add gradient with a semi-transparent
// dark section at the bottom. this helps
// with the readability of the status line
widget.backgroundGradient = newLinearGradient(
["#ffffff00", "#ffffff00", "#00000088"],
[0, 0.8, 1]
);
// top spacer to push the bottom stack down
widget.addSpacer();
// horizontal stack to hold the status line
const stats = widget.addStack();
stats.layoutHorizontally();
stats.centerAlignContent();
stats.spacing = 3;
addText(stats, data.text, "left", fontSize);
stats.addSpacer();
}
return widget;
}
// Just in case. It is not needed
async function createErrorWidget(data: BadTestInfo) {
const widget = new ListWidget();
widget.addSpacer();
const text = widget.addText(data.err_msg);
text.textColor = Color.white();
text.centerAlignText();
widget.addSpacer();
return widget;
}