-
Notifications
You must be signed in to change notification settings - Fork 0
/
MottoWidget.js
156 lines (132 loc) · 4.02 KB
/
MottoWidget.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-blue; icon-glyph: terminal;
/******************************************************************************
* Constants and Configurations
*****************************************************************************/
// Cache keys and default location
const CACHE_KEY_LAST_UPDATED = 'last_updated';
const CACHE_KEY_LOCATION = 'location';
const DEFAULT_LOCATION = { latitude: 0, longitude: 0 };
// Font name and size
const FONT_NAME = '微软雅黑';
const FONT_SIZE = 10;
// Colors
const COLORS = {
bg0: '#29323c',
bg1: '#1c1c1c',
personalCalendar: '#5BD2F0',
workCalendar: '#9D90FF',
weather: '#FDFD97',
location: '#FEB144',
period: '#FF6663',
deviceStats: '#7AE7B9',
};
// TODO: PLEASE SET THESE VALUES
const NAME = 'TODO';
const WORK_CALENDAR_NAME = 'TODO';
const PERSONAL_CALENDAR_NAME = 'TODO';
const PERIOD_CALENDAR_NAME = 'TODO';
const PERIOD_EVENT_NAME = 'TODO';
/******************************************************************************
* Initial Setups
*****************************************************************************/
/**
* Convenience function to add days to a Date.
*
* @param {*} days The number of days to add
*/
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
// Import and setup Cache
const Cache = importModule('Cache');
const cache = new Cache('mottoWidget');
// Fetch data and create widget
const data = await fetchData();
const widget = createWidget(data);
Script.setWidget(widget);
Script.complete();
/******************************************************************************
* Main Functions (Widget and Data-Fetching)
*****************************************************************************/
/**
* Main widget function.
*
* @param {} data The data for the widget to display
*/
function createWidget(data) {
console.log(`Creating widget with data: ${JSON.stringify(data)}`);
const widget = new ListWidget();
const bgColor = new LinearGradient();
bgColor.colors = [new Color(COLORS.bg0), new Color(COLORS.bg1)];
bgColor.locations = [0.0, 1.0];
widget.backgroundGradient = bgColor;
widget.setPadding(10, 10, 15, 10);
const stack = widget.addStack();
stack.layoutVertically();
stack.spacing = 4;
stack.size = new Size(320, 0);
const periodLine = stack.addText(`严于律己,宽以待人`);
periodLine.textColor = new Color(COLORS.period);
periodLine.font = new Font(FONT_NAME, 30);
return widget;
}
/**
* Fetch pieces of data for the widget.
*/
async function fetchData() {
// Get last data update time (and set)
const lastUpdated = await getLastUpdated();
cache.write(CACHE_KEY_LAST_UPDATED, new Date().getTime());
return {
device: {
battery: Math.round(Device.batteryLevel() * 100),
brightness: Math.round(Device.screenBrightness() * 100),
},
lastUpdated,
};
}
//-------------------------------------
// Misc. Helper Functions
//-------------------------------------
/**
* Make a REST request and return the response
*
* @param {*} key Cache key
* @param {*} url URL to make the request to
* @param {*} headers Headers for the request
*/
async function fetchJson(key, url, headers) {
const cached = await cache.read(key, 5);
if (cached) {
return cached;
}
try {
console.log(`Fetching url: ${url}`);
const req = new Request(url);
req.headers = headers;
const resp = await req.loadJSON();
cache.write(key, resp);
return resp;
} catch (error) {
try {
return cache.read(key, 5);
} catch (error) {
console.log(`Couldn't fetch ${url}`);
}
}
}
/**
* Get the last updated timestamp from the Cache.
*/
async function getLastUpdated() {
let cachedLastUpdated = await cache.read(CACHE_KEY_LAST_UPDATED);
if (!cachedLastUpdated) {
cachedLastUpdated = new Date().getTime();
cache.write(CACHE_KEY_LAST_UPDATED, cachedLastUpdated);
}
return cachedLastUpdated;
}