-
Notifications
You must be signed in to change notification settings - Fork 146
/
Env.js
591 lines (504 loc) · 16.5 KB
/
Env.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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/**
* Author: GideonSenku
* Github: https://github.com/GideonSenku
*/
var locationData, sunData
const currentDate = new Date()
const request = new Request('')
const files = FileManager.iCloud()
const dict = files.documentsDirectory()
files.isDirectory(`${dict}/Env`) ? `` : files.createDirectory(`${dict}/Env`)
const defaultHeaders = {
"Accept": "*/*",
"Content-Type": "application/json"
}
const textFormat = {
defaultText: { size: 14, color: "ffffff", font: "regular" },
battery: { size: 10, color: "", font: "bold" },
title: { size: 16, color: "", font: "semibold" },
SFMono: { size: 12, color: "ffffff", font: "SF Mono" }
}
/**
* @description GET,返回String数据
* @param {*} param0 request信息
* @param {*} callback 回调返回response和JSON对象
*/
const get = async ({ url, headers = {} }, callback = () => {} ) => {
request.url = url
request.method = 'GET'
request.headers = {
...headers,
...defaultHeaders
}
const data = await request.loadJSON()
callback(request.response, data)
return data
}
/**
* @description GET,返回String数据
* @param {*} param0 request信息
* @param {*} callback 回调返回response和String对象
*/
const getStr = async ({ url, headers = {} }, callback = () => {} ) => {
request.url = url
request.method = 'GET'
request.headers = {
...headers,
...defaultHeaders
}
const data = await request.loadString()
callback(request.response, data)
return data
}
/**
* @description POST,返回String数据
* @param {*} param0 request信息
* @param {*} callback 回调返回response和String
*/
const post = async ({ url, body, headers = {} }, callback = () => {} ) => {
request.url = url
request.body = body
request.method = 'POST'
request.headers = {
...defaultHeaders,
...headers
}
const data = await request.loadString()
callback(request.response, data)
return data
}
/**
* @description POST,返回JSON数据
* @param {*} param0 request信息
* @param {*} callback 回调返回response和JSON
*/
const _post = async ({ url, body, headers = {} }, callback = () => {} ) => {
request.url = url
request.body = body
request.method = 'POST'
request.headers = {
...defaultHeaders,
...headers
}
const data = await request.loadJSON()
callback(request.response, data)
return data
}
/**
* @description 下载文件
* @param {*} param0
*/
const getFile = async ({moduleName, url}) => {
log(`开始下载文件: 🌝 ${moduleName}`)
const header = `// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-gray; icon-glyph: file-code;\n`;
const content = await getStr({url})
const fileHeader = content.includes('icon-color') ? `` : header
write(`${moduleName}`, `${fileHeader}${content}`)
log(`文件下载完成: 🌚 ${moduleName}`)
}
/**
*
* @description 导入模块,不存在即下载模块,也可传入forceDownload: true 强制更新模块
* @param {*} param0
*/
const require = ({
moduleName,
url = '',
forceDownload = false
}) => {
if (isFileExists(moduleName) && !forceDownload) {
log(`导入模块: 🪐${moduleName}`)
return importModule(moduleName)
} else {
getFile({ moduleName, url })
log(`导入模块: 🪐${moduleName}`)
return importModule(moduleName)
}
}
/**
*
* @description 将数据写入文件
* @param {*} fileName 要写入的文件名,默认JS文件,可选其他,加上文件名后缀即可
* @param {*} content 要写入的文件内容
*/
const write = (fileName, content) => {
let file = initFile(fileName)
const filePath = `${dict}/${file}`
FileManager.iCloud().writeString(filePath, content)
return true
}
/**
*
* @description 判断文件是否存在
* @param {*} fileName
*/
const isFileExists = (fileName) => {
let file = initFile(fileName)
return FileManager.iCloud().fileExists(`${dict}/${file}`)
}
const initFile = (fileName) => {
const hasSuffix = fileName.lastIndexOf('.') + 1
return !hasSuffix ? `${fileName}.js` : fileName
}
/**
*
* @description 读取文件内容
* @param {*} fileName 要读取的文件名,默认JS文件,可选其他,加上文件名后缀即可
* @return 返回文件内容,字符串形式
*/
const read = (fileName) => {
const file = initFile(fileName)
return FileManager.iCloud().readString(`${dict}/${file}`)
}
/**
*
* @description 提示框
* @param {*} title 提示框标题
* @param {*} message 提示框内容
* @param {*} btnMes 提示框按钮标题,默认Cancel
*/
const msg = (title, message, btnMes = 'Cancel') => {
if (!config.runsInWidget) {
const alert = new Alert()
alert.title = title
alert.message = message
alert.addAction(btnMes)
alert.present()
}
}
const setdata = (Val, Key) => {
Keychain.set(Val, Key)
return true
}
const getdata = (Key) => {
return Keychain.get(Key)
}
const hasdata = (Key) => {
return Keychain.contains(Key)
}
const rmdata = (Key) => {
Keychain.remove(Key)
return true
}
// Presents an alert where the user can enter a value in a text field.
// Returns the entered value.
const input = async(title, message, placeholder, value = null) => {
if (!config.runsInWidget) {
let alert = new Alert()
alert.title = title
alert.message = message
alert.addTextField(placeholder, value)
alert.addAction("OK")
alert.addCancelAction("Cancel")
let idx = await alert.present()
if (idx != -1) {
return alert.textFieldValue(0)
} else {
throw new Error("Cancelled entering value")
}
}
}
/**
*
* 示例:$.time('yyyy-MM-dd qq HH:mm:ss.S')
* :$.time('yyyyMMddHHmmssS')
* y:年 M:月 d:日 q:季 H:时 m:分 s:秒 S:毫秒
* 其中y可选0-4位占位符、S可选0-1位占位符,其余可选0-2位占位符
* @param {*} fmt 格式化参数
* @param {*} ts 时间戳 13位
*/
const time = (fmt, ts = null) => {
const date = ts ? new Date(ts) : new Date()
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'H+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds(),
'q+': Math.floor((date.getMonth() + 3) / 3),
'S': date.getMilliseconds()
}
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
for (let k in o)
if (new RegExp('(' + k + ')').test(fmt))
fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
return fmt
}
/**
* @description create wiget
* @param {*} title required
* @param {*} texts required
* @param {*} preview option
*/
const createWidget = async({ title, texts = { },spacing = 5, preview = '' }) => {
let w = new ListWidget()
w.spacing = spacing
let gradient = new LinearGradient()
let gradientSettings = await setupGradient()
gradient.colors = gradientSettings.color()
gradient.locations = gradientSettings.position()
w.backgroundGradient = gradient
texts['battery'] ? battery(w, title) : provideText(title, w, textFormat.title)
for (const text in texts) {
if (text != 'battery' && text != 'updateTime' && texts.hasOwnProperty(text) && texts[text]) {
const element = texts[text]
provideText(element, w, textFormat.SFMono)
}
}
texts['updateTime'] ? provideText(`[更新] ${time('MM-dd HH:mm')}`, w, textFormat.SFMono) : ``
widgetPreview = preview ? preview: 'small'
if(widgetPreview == "small") { w.presentSmall() }
else if (widgetPreview == "medium") { w.presentMedium() }
else if (widgetPreview == "large") { w.presentLarge() }
return w
}
/**
* @description Provide a font based on the input.
* @param {*} fontName
* @param {*} fontSize
*/
const provideFont = (fontName, fontSize) => {
const fontGenerator = {
"ultralight": function() { return Font.ultraLightSystemFont(fontSize) },
"light": function() { return Font.lightSystemFont(fontSize) },
"regular": function() { return Font.regularSystemFont(fontSize) },
"medium": function() { return Font.mediumSystemFont(fontSize) },
"semibold": function() { return Font.semiboldSystemFont(fontSize) },
"bold": function() { return Font.boldSystemFont(fontSize) },
"heavy": function() { return Font.heavySystemFont(fontSize) },
"black": function() { return Font.blackSystemFont(fontSize) },
"italic": function() { return Font.italicSystemFont(fontSize) }
}
const systemFont = fontGenerator[fontName]
if (systemFont) { return systemFont() }
return new Font(fontName, fontSize)
}
/**
* @description Add formatted text to a container.
* @param {*} string
* @param {*} container widget container
* @param {*} format Object: size, color, font
*/
const provideText = (string, container, format) => {
let url
if (typeof string !== 'string') {
url = string.url
string = string.text
}
const stackItem = container.addStack()
if (url) {
stackItem.url = url
}
const textItem = stackItem.addText(string)
const textFont = format.font || textFormat.defaultText.font
const textSize = format.size || textFormat.defaultText.size
const textColor = format.color || textFormat.defaultText.color
textItem.font = provideFont(textFont, textSize)
textItem.textColor = new Color(textColor)
return stackItem
}
// Set up the gradient for the widget background.
const setupGradient = async() => {
// Requirements: sunrise
if (!sunData) { await setupSunrise() }
let gradient = {
dawn: {
color() { return [new Color("142C52"), new Color("1B416F"), new Color("62668B")] },
position() { return [0, 0.5, 1] },
},
sunrise: {
color() { return [new Color("274875"), new Color("766f8d"), new Color("f0b35e")] },
position() { return [0, 0.8, 1.5] },
},
midday: {
color() { return [new Color("3a8cc1"), new Color("90c0df")] },
position() { return [0, 1] },
},
noon: {
color() { return [new Color("b2d0e1"), new Color("80B5DB"), new Color("3a8cc1")] },
position() { return [-0.2, 0.2, 1.5] },
},
sunset: {
color() { return [new Color("32327A"), new Color("662E55"), new Color("7C2F43")] },
position() { return [0.1, 0.9, 1.2] },
},
twilight: {
color() { return [new Color("021033"), new Color("16296b"), new Color("414791")] },
position() { return [0, 0.5, 1] },
},
night: {
color() { return [new Color("16296b"), new Color("021033"), new Color("021033"), new Color("113245")] },
position() { return [-0.5, 0.2, 0.5, 1] },
},
}
const sunrise = sunData.sunrise
const sunset = sunData.sunset
const utcTime = currentDate.getTime()
function closeTo(time,mins) {
return Math.abs(utcTime - time) < (mins * 60000)
}
// Use sunrise or sunset if we're within 30min of it.
if (closeTo(sunrise,15)) { return gradient.sunrise }
if (closeTo(sunset,15)) { return gradient.sunset }
// In the 30min before/after, use dawn/twilight.
if (closeTo(sunrise,45) && utcTime < sunrise) { return gradient.dawn }
if (closeTo(sunset,45) && utcTime > sunset) { return gradient.twilight }
// Otherwise, if it's night, return night.
if (isNight(currentDate)) { return gradient.night }
// If it's around noon, the sun is high in the sky.
if (currentDate.getHours() == 12) { return gradient.noon }
// Otherwise, return the "typical" theme.
return gradient.midday
}
// Set up the sunData object.
const setupSunrise = async () => {
// Requirements: location
if (!locationData) { await setupLocation() }
// Set up the sunrise/sunset cache.
const sunCachePath = files.joinPath(dict, "Env/Env-sun")
const sunCacheExists = files.fileExists(sunCachePath)
const sunCacheDate = sunCacheExists ? files.modificationDate(sunCachePath) : 0
var sunDataRaw
// If cache exists and it was created today, use cached data.
if (sunCacheExists && sameDay(currentDate, sunCacheDate)) {
const sunCache = files.readString(sunCachePath)
sunDataRaw = JSON.parse(sunCache)
// Otherwise, use the API to get sunrise and sunset times.
} else {
const sunReq = "https://api.sunrise-sunset.org/json?lat=" + locationData.latitude + "&lng=" + locationData.longitude + "&formatted=0&date=" + currentDate.getFullYear() + "-" + (currentDate.getMonth()+1) + "-" + currentDate.getDate()
sunDataRaw = await new Request(sunReq).loadJSON()
files.writeString(sunCachePath, JSON.stringify(sunDataRaw))
}
// Store the timing values.
sunData = {}
sunData.sunrise = new Date(sunDataRaw.results.sunrise).getTime()
sunData.sunset = new Date(sunDataRaw.results.sunset).getTime()
}
const setupLocation = async (lockLocation = true) => {
locationData = {}
const locationPath = files.joinPath(dict, "Env/Env-location")
// If our location is unlocked or cache doesn't exist, ask iOS for location.
var readLocationFromFile = false
if (!lockLocation || !files.fileExists(locationPath)) {
try {
const location = await Location.current()
locationData.latitude = location.latitude
locationData.longitude = location.longitude
files.writeString(locationPath, location.latitude + "," + location.longitude)
} catch(e) {
// If we fail in unlocked mode, read it from the cache.
if (!lockLocation) { readLocationFromFile = true }
// We can't recover if we fail on first run in locked mode.
else { return }
}
}
// If our location is locked or we need to read from file, do it.
if (lockLocation || readLocationFromFile) {
const locationStr = files.readString(locationPath).split(",")
locationData.latitude = locationStr[0]
locationData.longitude = locationStr[1]
}
return locationData
}
// Determines if the provided date is at night.
const isNight = (dateInput) => {
const timeValue = dateInput.getTime()
return (timeValue < sunData.sunrise) || (timeValue > sunData.sunset)
}
// Determines if two dates occur on the same day
const sameDay = (d1, d2) => {
return d1.getFullYear() === d2.getFullYear() &&
d1.getMonth() === d2.getMonth() &&
d1.getDate() === d2.getDate()
}
/**
* @description 返回电池百分比
*/
const renderBattery = () => {
const batteryLevel = Device.batteryLevel()
const batteryAscii = `${Math.round(batteryLevel * 100)}%`
return batteryAscii
}
// Add a battery element to the widget; consisting of a battery icon and percentage.
function battery(column,title) {
const batteryLevel = Device.batteryLevel()
// Set up the battery level item
let batteryStack = column.addStack()
provideText(title, batteryStack, textFormat.title)
batteryStack.centerAlignContent()
batteryStack.addSpacer()
let batteryIcon = batteryStack.addImage(provideBatteryIcon())
batteryIcon.imageSize = new Size(20,20)
// Change the battery icon to red if battery level is <= 20 to match system behavior
if ( Math.round(batteryLevel * 100) > 20 || Device.isCharging() ) {
batteryIcon.tintColor = Color.white()
} else {
batteryIcon.tintColor = Color.red()
}
// Display the battery status
let batteryInfo = provideText(' '+renderBattery(), batteryStack, textFormat.battery)
}
// Provide a battery SFSymbol with accurate level drawn on top of it.
function provideBatteryIcon() {
if (Device.isCharging()) { return SFSymbol.named("battery.100.bolt").image }
// Set the size of the battery icon.
const batteryWidth = 87
const batteryHeight = 41
// Start our draw context.
let draw = new DrawContext()
draw.opaque = false
draw.respectScreenScale = true
draw.size = new Size(batteryWidth, batteryHeight)
// Draw the battery.
draw.drawImageInRect(SFSymbol.named("battery.0").image, new Rect(0, 0, batteryWidth, batteryHeight))
// Match the battery level values to the SFSymbol.
const x = batteryWidth*0.1525
const y = batteryHeight*0.247
const width = batteryWidth*0.602
const height = batteryHeight*0.505
// Prevent unreadable icons.
let level = Device.batteryLevel()
if (level < 0.05) { level = 0.05 }
// Determine the width and radius of the battery level.
const current = width * level
let radius = height/6.5
// When it gets low, adjust the radius to match.
if (current < (radius * 2)) { radius = current / 2 }
// Make the path for the battery level.
let barPath = new Path()
barPath.addRoundedRect(new Rect(x, y, current, height), radius, radius)
draw.addPath(barPath)
draw.setFillColor(Color.black())
draw.fillPath()
return draw.getImage()
}
const logErr = (e, messsage) => {
console.error(e)
}
module.exports = {
dict,
get,
getStr,
post,
_post,
getFile,
require,
write,
isFileExists,
initFile,
read,
setdata,
getdata,
hasdata,
rmdata,
msg,
input,
time,
createWidget,
provideText,
setupLocation,
renderBattery,
logErr
}