Skip to content

Commit 215248f

Browse files
committed
Merge branch 'ticalc-travis-sched-refactor'
2 parents 916a5a9 + 736e4d2 commit 215248f

10 files changed

Lines changed: 140 additions & 213 deletions

File tree

apps/keytimer/ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
0.02: Submitted to the app loader
33
0.03: Rewrote to use scheduler library
44
0.04: Use theme colors
5+
0.05: Avoid need for custom alarm handler--now uses latest handler provided by sched lib

apps/keytimer/boot.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Bangle.on('alarmDismiss', alarm => {
2+
if (alarm.appid === 'keytimer') {
3+
console.debug('keytimer: alarmDismiss for keytimer alarm');
4+
let state = require('Storage').readJSON('keytimer.json');
5+
state.timeLeft = 0;
6+
require('Storage').writeJSON('keytimer.json', state);
7+
}
8+
});

apps/keytimer/common.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ exports.startTimer = function (time) {
2727
let timer = sched.newDefaultTimer();
2828

2929
timer.timer = time;
30-
common.state.timeLeft = time;
30+
exports.state.timeLeft = time;
3131
timer.del = true;
3232
timer.appid = 'keytimer';
33-
timer.js = "load('keytimer-ring.js')";
3433

3534
sched.setAlarm('keytimer', timer);
3635
sched.reload();

apps/keytimer/metadata.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "keytimer",
33
"name": "Keypad Timer",
4-
"version": "0.04",
4+
"version": "0.05",
55
"author": "bruceblore",
66
"description": "A timer with a keypad that runs in the background",
77
"icon": "icon.png",
@@ -17,6 +17,10 @@
1717
"name": "keytimer.app.js",
1818
"url": "app.js"
1919
},
20+
{
21+
"name": "keytimer.boot.js",
22+
"url": "boot.js"
23+
},
2024
{
2125
"name": "keytimer.img",
2226
"url": "icon.js",
@@ -26,10 +30,6 @@
2630
"name": "keytimer-com.js",
2731
"url": "common.js"
2832
},
29-
{
30-
"name": "keytimer-ring.js",
31-
"url": "ring.js"
32-
},
3333
{
3434
"name": "keytimer-keys.js",
3535
"url": "keypad.js"

apps/keytimer/ring.js

Lines changed: 0 additions & 95 deletions
This file was deleted.

apps/sched/ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@
4343
0.40: Prevent time-of-day alarms set via setAlarm() that have a time earlier than the current time triggering immediately after #4220 fix; schedule them for tomorrow by default to match original behavior
4444
0.41: Fix issue with snoozing alarms and timers beyond midnight causing the alarm/timer to go off immediately instead of snoozing
4545
0.42: Fix timer snooze 5m button adding 6 minutes instead of 5
46+
0.43: Introduce `snoozeAlarm` and `dismissAlarm` functions for easier alarm handling by custom JS
47+

apps/sched/lib.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,35 @@ function timeToMillis(time) {
22
return (time.getHours() * 3600000) + (time.getMinutes() * 60000) + (time.getSeconds() * 1000);
33
}
44

5+
function setNextRepeatDate(alarm) {
6+
let date = new Date(alarm.date);
7+
let rp = alarm.rp;
8+
if (rp===true) { // fallback in case rp is set wrong
9+
date.setDate(date.getDate() + 1);
10+
} else switch(rp.interval) { // rp is an object
11+
case "day":
12+
date.setDate(date.getDate() + rp.num);
13+
break;
14+
case "week":
15+
date.setDate(date.getDate() + (rp.num * 7));
16+
break;
17+
case "month":
18+
if (!alarm.od) alarm.od = date.getDate();
19+
date = new Date(date.getFullYear(), date.getMonth() + rp.num, alarm.od);
20+
if (date.getDate() != alarm.od) date.setDate(0);
21+
break;
22+
case "year":
23+
if (!alarm.od) alarm.od = date.getDate();
24+
date = new Date(date.getFullYear() + rp.num, date.getMonth(), alarm.od);
25+
if (date.getDate() != alarm.od) date.setDate(0);
26+
break;
27+
default:
28+
console.log(`sched: unknown repeat '${JSON.stringify(rp)}'`);
29+
break;
30+
}
31+
alarm.date = date.toLocalISOString().slice(0,10);
32+
}
33+
534
// Return an array of all alarms
635
exports.getAlarms = function() {
736
// we do this direct in clkinfo.js to avoid loading the library
@@ -77,6 +106,81 @@ exports.setAlarm = function(id, alarm) {
77106
return alarm;
78107
};
79108

109+
// Put an alarm on snooze for `snoozeTime` milliseconds (if already
110+
// snoozed, this will add to the existing snooze time).
111+
//
112+
// `alarms` is the list of alarms from getAlarms, and `alarm` is the
113+
// alarm object from that list to snooze.
114+
exports.snoozeAlarm = function(alarms, alarm, snoozeTime) {
115+
if (alarms.indexOf(alarm) < 0) {
116+
console.error('[sched] snoozeAlarm: Given alarm not in list of alarms');
117+
return 'error';
118+
}
119+
120+
if (alarm.ot === undefined) {
121+
alarm.ot = alarm.t;
122+
}
123+
let time = new Date();
124+
let currentTime = timeToMillis(time);
125+
alarm.t = currentTime + snoozeTime;
126+
alarm.t %= 86400000;
127+
128+
// This makes updateAlarm() recompute the last alarm date so
129+
// that it works correctly if we're snoozing beyond midnight
130+
delete alarm.last;
131+
132+
exports.updateAlarm(alarm);
133+
134+
// Save snoozed alarm (still a member of `alarms`) back to storage
135+
exports.setAlarms(alarms);
136+
Bangle.emit("alarmSnooze", alarm);
137+
return 'snoozed';
138+
};
139+
140+
// Stop and dismiss an expired alarm. This will cancel any snooze status
141+
// on the alarm, turn the alarm off (or delete the alarm if configured
142+
// to do so), or reschedule the alarm if it's set to repeat.
143+
//
144+
// Returns the string 'stopped', 'deleted', or 'rescheduled' to indicate
145+
// what action was taken.
146+
//
147+
// `alarms` is the list of alarms from getAlarms, and `alarm` is the
148+
// alarm object from that list to dismiss.
149+
exports.dismissAlarm = function(alarms, alarm) {
150+
const alarmIndex = alarms.indexOf(alarm);
151+
if (alarmIndex < 0) {
152+
console.error('[sched] dismissAlarm: Given alarm not in list of alarms');
153+
return 'error';
154+
}
155+
156+
const settings = exports.getSettings();
157+
let actionTaken = 'stopped';
158+
let del = alarm.del === undefined ? settings.defaultDeleteExpiredTimers : alarm.del;
159+
if (del) {
160+
alarms.splice(alarmIndex, 1);
161+
actionTaken = 'deleted';
162+
} else {
163+
if (alarm.date && alarm.rp) {
164+
setNextRepeatDate(alarm);
165+
actionTaken = 'rescheduled';
166+
} else if (!alarm.timer) {
167+
alarm.last = new Date().getDate();
168+
}
169+
if (alarm.ot !== undefined) {
170+
alarm.t = alarm.ot;
171+
delete alarm.ot;
172+
}
173+
if (!alarm.rp) {
174+
alarm.on = false;
175+
}
176+
}
177+
178+
// Save dismissed alarm (still a member of `alarms`) back to storage
179+
require("sched").setAlarms(alarms);
180+
Bangle.emit("alarmDismiss", alarm);
181+
return actionTaken;
182+
};
183+
80184
/// Get time until the given alarm (object). Return undefined if alarm not enabled, or if 86400000 or more, alarm could be *more* than a day in the future
81185
exports.getTimeToAlarm = function(alarm, time) {
82186
if (!alarm) return undefined;

apps/sched/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "sched",
33
"name": "Scheduler",
4-
"version": "0.42",
4+
"version": "0.43",
55
"author": "gfwilliams",
66
"description": "Scheduling library for alarms and timers",
77
"icon": "app.png",

apps/sched/sched.js

Lines changed: 3 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,12 @@ function formatMS(ms) {
1616
}
1717
}
1818

19-
function snoozeAlarm(alarm, snoozeTime) {
20-
if (alarm.ot === undefined) {
21-
alarm.ot = alarm.t;
22-
}
23-
let time = new Date();
24-
let currentTime = (time.getHours()*3600000)+(time.getMinutes()*60000)+(time.getSeconds()*1000);
25-
alarm.t = currentTime + snoozeTime;
26-
alarm.t %= 86400000;
27-
28-
// This makes updateAlarm() recompute the last alarm date so
29-
// that it works correctly if we're snoozing beyond midnight
30-
delete alarm.last;
31-
32-
require("sched").updateAlarm(alarm);
33-
Bangle.emit("alarmSnooze", alarm);
34-
35-
// The updated alarm is still a member of 'alarms'
36-
// so writing to array writes changes back directly
37-
require("sched").setAlarms(alarms);
38-
}
39-
4019
function showSnoozeMenu(alarm){
4120

4221
Bangle.buzz(40);
4322

4423
function onSnooze(snoozeTime) {
45-
snoozeAlarm(alarm, snoozeTime);
24+
require("sched").snoozeAlarm(alarms, alarm, snoozeTime);
4625
load();
4726
}
4827

@@ -66,7 +45,6 @@ function showSnoozeMenu(alarm){
6645
}
6746

6847
function showAlarm(alarm) {
69-
const alarmIndex = alarms.indexOf(alarm);
7048
const settings = require("sched").getSettings();
7149

7250
let message = "";
@@ -93,30 +71,9 @@ function showAlarm(alarm) {
9371
return;
9472
}
9573
if (sleep==1) {
96-
snoozeAlarm(alarm, settings.defaultSnoozeMillis);
74+
require("sched").snoozeAlarm(alarms, alarm, settings.defaultSnoozeMillis);
9775
} else { // sleep=2, stop the alarm
98-
let del = alarm.del === undefined ? settings.defaultDeleteExpiredTimers : alarm.del;
99-
if (del) {
100-
alarms.splice(alarmIndex, 1);
101-
} else {
102-
if (alarm.date && alarm.rp) {
103-
setNextRepeatDate(alarm);
104-
} else if (!alarm.timer) {
105-
alarm.last = new Date().getDate();
106-
}
107-
if (alarm.ot !== undefined) {
108-
alarm.t = alarm.ot;
109-
delete alarm.ot;
110-
}
111-
if (!alarm.rp) {
112-
alarm.on = false;
113-
}
114-
}
115-
Bangle.emit("alarmDismiss", alarm);
116-
117-
// The updated alarm is still a member of 'alarms'
118-
// so writing to array writes changes back directly
119-
require("sched").setAlarms(alarms);
76+
require("sched").dismissAlarm(alarms, alarm);
12077
}
12178

12279
load();
@@ -145,35 +102,6 @@ function showAlarm(alarm) {
145102
});
146103
}
147104

148-
function setNextRepeatDate(alarm) {
149-
let date = new Date(alarm.date);
150-
let rp = alarm.rp;
151-
if (rp===true) { // fallback in case rp is set wrong
152-
date.setDate(date.getDate() + 1);
153-
} else switch(rp.interval) { // rp is an object
154-
case "day":
155-
date.setDate(date.getDate() + rp.num);
156-
break;
157-
case "week":
158-
date.setDate(date.getDate() + (rp.num * 7));
159-
break;
160-
case "month":
161-
if (!alarm.od) alarm.od = date.getDate();
162-
date = new Date(date.getFullYear(), date.getMonth() + rp.num, alarm.od);
163-
if (date.getDate() != alarm.od) date.setDate(0);
164-
break;
165-
case "year":
166-
if (!alarm.od) alarm.od = date.getDate();
167-
date = new Date(date.getFullYear() + rp.num, date.getMonth(), alarm.od);
168-
if (date.getDate() != alarm.od) date.setDate(0);
169-
break;
170-
default:
171-
console.log(`sched: unknown repeat '${JSON.stringify(rp)}'`);
172-
break;
173-
}
174-
alarm.date = date.toLocalISOString().slice(0,10);
175-
}
176-
177105
if ((require("Storage").readJSON("setting.json", 1) || {}).quiet > 1)
178106
return;
179107

0 commit comments

Comments
 (0)