-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathContext.js
More file actions
285 lines (246 loc) · 6.07 KB
/
Context.js
File metadata and controls
285 lines (246 loc) · 6.07 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
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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: teal; icon-glyph: magic;
// Example match value: 'Home,Weekday,Fri,am,Morning,09'
// The first match is used
const RULES = [
//==================
{title: 'Woken Up',
match: 'Home,.*,.*,.*,Early,.*',
shortcuts: [
'Phone In Bed',
'Phone Normal',
'Overcast Play',
'Tea'
],
perspectives: [
'Review',
'Morning H'
]},
//==================
{title: 'Home Morning',
match: 'Home,Weekday,.*,.*,Morning,.*',
shortcuts: [
'Phone Normal',
'Overcast Play'
],
perspectives: [
'Morning H',
'Today H'
]},
//==================
{title: 'Home Evening',
match: 'Home,.*,.*,.*,Evening,.*',
shortcuts: [
'Phone Normal',
'Overcast Play'
],
perspectives: [
'Evening H',
'Evening']},
//==================
{title: 'Home Late',
match: 'Home,.*,.*,.*,(Late|Night),.*',
shortcuts: [
'Phone In Bed',
'Phone At Night',
'Overcast Play'
]},
//==================
{title: 'Home Weekend',
match: 'Home,Weekend,.*,.*,.*,.*',
shortcuts: [
'Phone Normal',
'Overcast Play'
],
perspectives: [
'Today H',
'Out',
'Out Today']},
//==================
{title: 'Out',
match: 'Out,.*,.*,.*,.*,.*',
shortcuts: [
'Parked',
'I Am Here',
'Phone Low Power'
],
perspectives: [
'Out',
'Out Today']},
//==================
{title: 'Driving',
match: 'Driving,.*,.*,.*,.*,.*',
shortcuts: [
'Parked',
'Go To Work',
'Go Home'
],
perspectives: [
'Out',
'Out Today']},
//==================
// Default rule
{title: 'Default',
match: '.*',
shortcuts: [
'Phone At Night',
'Phone In Bed',
'Phone Normal'
]}
];
const GEO_CONTEXTS = {
'BS7' : 'Home'
};
const DEFAULT_GEO_CONTEXT = "Out";
const DAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const DAY_TYPE = ['Weekend', 'Weekday', 'Weekday', 'Weekday', 'Weekday', 'Weekday', 'Weekend'];
const HOUR_TYPE = {
'00':'Late',
'01':'Night',
'02':'Night',
'03':'Night',
'04':'Night',
'05':'Early',
'06':'Early',
'07':'Early',
'08':'Early',
'09':'Morning',
'10':'Morning',
'11':'Morning',
'12':'Lunchtime',
'13':'Afternoon',
'14':'Afternoon',
'15':'Afternoon',
'16':'Afternoon',
'17':'Afternoon',
'18':'Evening',
'19':'Evening',
'20':'Evening',
'21':'Evening',
'22':'Late',
'23':'Late'
};
async function getShortPostcode() {
console.log('Getting Location...');
let location = await Location.current();
console.log('Getting Address...');
let address = await Location.reverseGeocode(location.latitude, location.longitude);
let postcode = address[0].postalCode;
let shortPostcode = postcode.split(' ')[0];
return shortPostcode;
}
function setContext(context) {
let fm = FileManager.iCloud();
let docs = fm.documentsDirectory();
fm.writeString(docs + '.context.txt', context);
}
async function getContext(useGeo) {
let fm = FileManager.iCloud();
let docs = fm.documentsDirectory();
let filePath = docs + '.context.txt';
let place = null;
if (useGeo || !fm.fileExists(filePath)) {
let shortPostcode = await getShortPostcode();
place = GEO_CONTEXTS[shortPostcode];
if (!place) {
place = DEFAULT_GEO_CONTEXT;
}
setContext(place);
} else {
place = fm.readString(filePath);
}
return place;
}
function getTime() {
let date = new Date();
let day = DAYS[date.getDay()];
let dayType = DAY_TYPE[date.getDay()];
let hour = '' + date.getHours();
hour = hour.length === 1 ? '0' + hour : hour;
let hourType = HOUR_TYPE[hour];
let amPm = hour < 12 ? 'am' : 'pm';
return dayType + ',' + day + ',' + amPm + ',' + hourType + ',' + hour;
}
function getRule(key) {
let rule = null;
for (let i = 0; i < RULES.length; i++) {
let match = key.match(RULES[i].match);
if (match) {
rule = RULES[i];
break;
}
}
return rule;
}
async function getShortcutsKey(useGeo) {
let context = await getContext(useGeo);
let time = getTime();
let key = context + ',' + time;
return key;
}
function createLauncherRow(text, fn) {
let row = new UITableRow();
row.dismissOnSelect = false;
let cell = row.addText(text);
row.onSelect = fn;
return row;
}
function getInput(argName) {
let params = URLScheme.allParameters();
let value = params[argName];
if (value) {
return decodeURIComponent(params[argName]);
} else {
return null;
}
}
function createTitleRow(text, subText) {
let titleRow = new UITableRow();
titleRow.addText(text, subText);
titleRow.isHeader = true;
titleRow.dismissOnSelect = false;
return titleRow;
}
async function ui(rule, key) {
let table = new UITable();
table.addRow(createTitleRow(rule.title, key));
let shortcuts = rule.shortcuts;
if (shortcuts && shortcuts.length > 0) {
table.addRow(createTitleRow('Shortcuts'));
for (let i = 0; i < shortcuts.length; i++) {
let shortcut = shortcuts[i];
let row = createLauncherRow(shortcut, (selIndex) => {
let url = new CallbackURL('shortcuts://run-shortcut');
url.addParameter('name', shortcut);
url.open();
});
table.addRow(row);
}
}
let perspectives = rule.perspectives;
if (perspectives && perspectives.length > 0) {
table.addRow(createTitleRow('Perspectives'));
for (let i = 0; i < perspectives.length; i++) {
let perspective = perspectives[i];
let row = createLauncherRow(perspective, (selIndex) => {
let url = 'omnifocus:///perspective/' + encodeURI(perspective);
Safari.open(url);
});
table.addRow(row);
}
}
table.present();
}
// Load url parameters
let useGeo = getInput('geo') === 'true';
let context = getInput('context');
let key = null;
if (context && context.length > 0 && !useGeo) {
setContext(context);
key = await getShortcutsKey();
} else {
key = await getShortcutsKey(useGeo);
}
let rule = getRule(key);
ui(rule, key);