Merge pull request #2781 from bobrippling/oneshot-alarms

multitimer: allow one-shot alarms (delete after)
pull/2861/head
Gordon Williams 2023-07-03 10:45:45 +01:00 committed by GitHub
commit e61eb4e88c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 135 additions and 165 deletions

View File

@ -1,3 +1,4 @@
0.01: Initial version
0.02: Update for time_utils module
0.03: Use default Bangle formatter for booleans
0.04: Remove copied sched alarm.js & import newer features (oneshot alarms)

View File

@ -2,6 +2,7 @@
With this app, you can set timers and chronographs (stopwatches) and watch them count down/up in real time. You can also set alarms - swipe left or right to switch between the three functions.
"Hard mode" is also available for timers and alarms. It will double the number of buzz counts and you will have to swipe the screen five to eight times correctly - make a mistake, and you will need to start over.
"Delete after expiration" can be set on a timer/alarm to have it delete itself once it's sounded (the same as the alarm app).
## WARNING
* Editing timers in another app (such as the default Alarm app) is not recommended. Editing alarms should not be a problem (in theory).

View File

@ -1,6 +1,4 @@
//sched.js, modified
// Chances are boot0.js got run already and scheduled *another*
// 'load(sched.js)' - so let's remove it first!
// called by getActiveAlarms(...)[0].js
if (Bangle.SCHED) {
clearInterval(Bangle.SCHED);
delete Bangle.SCHED;
@ -69,56 +67,9 @@ function startHM() {
hardMode(0, Math.abs(E.hwRand()%4)+5);
}
function showAlarm(alarm) {
const settings = require("sched").getSettings();
let msg = "";
if (alarm.timer) msg += require("time_utils").formatTime(alarm.timer);
if (alarm.msg) {
msg += "\n"+alarm.msg;
}
else msg = atob("ACQswgD//33vRcGHIQAAABVVVAAAAAAAABVVVAAAAAAAABVVVAAAAAAAABVVVAAAAAAAABVVVAAAAAAAABVVVAAAAAAAAAP/wAAAAAAAAAP/wAAAAAAAAAqqoAPAAAAAAqqqqoP8AAAAKqqqqqv/AAACqqqqqqq/wAAKqqqlWqqvwAAqqqqlVaqrAACqqqqlVVqqAAKqqqqlVVaqgAKqaqqlVVWqgAqpWqqlVVVqoAqlWqqlVVVaoCqlV6qlVVVaqCqVVfqlVVVWqCqVVf6lVVVWqKpVVX/lVVVVqqpVVV/+VVVVqqpVVV//lVVVqqpVVVfr1VVVqqpVVVfr1VVVqqpVVVb/lVVVqqpVVVW+VVVVqqpVVVVVVVVVqiqVVVVVVVVWqCqVVVVVVVVWqCqlVVVVVVVaqAqlVVVVVVVaoAqpVVVVVVVqoAKqVVVVVVWqgAKqlVVVVVaqgACqpVVVVVqqAAAqqlVVVaqoAAAKqqVVWqqgAAACqqqqqqqAAAAAKqqqqqgAAAAAAqqqqoAAAAAAAAqqoAAAAA==")+" "+msg;
Bangle.loadWidgets();
Bangle.drawWidgets();
let buzzCount = settings.buzzCount;
if (alarm.data.hm && alarm.data.hm == true) {
//hard mode extends auto-snooze time
buzzCount = buzzCount * 3;
startHM();
}
else {
E.showPrompt(msg,{
title: "TIMER!",
buttons : {"Snooze":true,"Ok":false} // default is sleep so it'll come back in 10 mins
}).then(function(sleep) {
buzzCount = 0;
if (sleep) {
if(alarm.ot===undefined) alarm.ot = alarm.t;
alarm.t += settings.defaultSnoozeMillis;
} else {
if (!alarm.timer) alarm.last = (new Date()).getDate();
if (alarm.ot!==undefined) {
alarm.t = alarm.ot;
delete alarm.ot;
}
if (!alarm.rp) alarm.on = false;
}
//reset timer value
if (alarm.timer) alarm.timer = alarm.data.ot;
// alarm is still a member of 'alarms', so writing to array writes changes back directly
require("sched").setAlarms(alarms);
load();
});
}
function buzz() {
if (settings.unlockAtBuzz) {
Bangle.setLocked(false);
}
const settings = require("sched").getSettings();
let buzzCount = 3 * settings.buzzCount;
require("buzz").pattern(alarm.vibrate === undefined ? "::" : alarm.vibrate).then(() => {
if (buzzCount--) {
@ -130,19 +81,9 @@ function showAlarm(alarm) {
});
}
if ((require("Storage").readJSON("setting.json", 1) || {}).quiet > 1)
return;
buzz();
}
// Check for alarms
let alarms = require("sched").getAlarms();
let active = require("sched").getActiveAlarms(alarms);
if (active.length) {
// if there's an alarm, show it
showAlarm(active[0]);
} else {
// otherwise just go back to default app
setTimeout(load, 100);
}
let alarm = active[0];
// active[0] is a HM alarm (otherwise we'd have triggered sched.js instead of this file)
startHM();
buzz();

View File

@ -56,6 +56,14 @@ function clearInt() {
timerInt2 = [];
}
function setHM(alarm, on) {
if (on)
alarm.js = "(require('Storage').read('multitimer.alarm.js') !== undefined) ? load('multitimer.alarm.js') : load('sched.js')";
else
delete alarm.js;
alarm.data.hm = on;
}
function drawTimers() {
layer = 0;
var timers = require("sched").getAlarms().filter(a => a.timer && a.appid == "multitimer");
@ -228,8 +236,7 @@ function editTimer(idx, a) {
else a = timers[idx];
}
if (!a.data) {
a.data = {};
a.data.hm = false;
a.data = { hm: false };
}
var t = decodeTime(a.timer);
@ -267,7 +274,10 @@ function editTimer(idx, a) {
},
"Enabled": {
value: a.on,
onchange: v => a.on = v
onchange: v => {
delete a.last;
a.on = v;
}
},
"Hours": {
value: t.hrs, min: 0, max: 23, wrap: true,
@ -292,9 +302,13 @@ function editTimer(idx, a) {
},
"Hard Mode": {
value: a.data.hm,
onchange: v => a.data.hm = v
onchange: v => setHM(a, v),
},
"Vibrate": require("buzz_menu").pattern(a.vibrate, v => a.vibrate = v),
"Delete After Expiration": {
value: !!a.del,
onchange: v => a.del = v
},
"Msg": {
value: !a.msg ? "" : a.msg.length > 6 ? a.msg.substring(0, 6)+"..." : a.msg,
//menu glitch? setTimeout required here
@ -556,8 +570,7 @@ function editAlarm(idx, a) {
else a = require("sched").newDefaultAlarm();
}
if (!a.data) {
a.data = {};
a.data.hm = false;
a.data = { hm: false };
}
var t = decodeTime(a.t);
@ -582,8 +595,6 @@ function editAlarm(idx, a) {
var menu = {
"": { "title": "Alarm" },
"< Back": () => {
if (a.data.hm == true) a.js = "(require('Storage').read('multitimer.alarm.js') !== undefined) ? load('multitimer.alarm.js') : load('sched.js')";
if (a.data.hm == false && a.js) delete a.js;
if (idx >= 0) alarms[alarmIdx[idx]] = a;
else alarms.push(a);
require("sched").setAlarms(alarms);
@ -592,7 +603,10 @@ function editAlarm(idx, a) {
},
"Enabled": {
value: a.on,
onchange: v => a.on = v
onchange: v => {
delete a.last;
a.on = v;
}
},
"Hours": {
value: t.hrs, min: 0, max: 23, wrap: true,
@ -618,9 +632,13 @@ function editAlarm(idx, a) {
},
"Hard Mode": {
value: a.data.hm,
onchange: v => a.data.hm = v
onchange: v => setHM(a, v),
},
"Vibrate": require("buzz_menu").pattern(a.vibrate, v => a.vibrate = v),
"Delete After Expiration": {
value: !!a.del,
onchange: v => a.del = v
},
"Auto Snooze": {
value: a.as,
onchange: v => a.as = v

8
apps/multitimer/boot.js Normal file
View File

@ -0,0 +1,8 @@
{
const resetTimer = alarm => {
if (alarm.timer) alarm.timer = alarm.data.ot;
};
Bangle.on("alarmSnooze", resetTimer);
Bangle.on("alarmDismiss", resetTimer);
}

View File

@ -1,7 +1,7 @@
{
"id": "multitimer",
"name": "Multi Timer",
"version": "0.03",
"version": "0.04",
"description": "Set timers and chronographs (stopwatches) and watch them count down in real time. Pause, create, edit, and delete timers and chronos, and add custom labels/messages. Also sets alarms.",
"icon": "app.png",
"screenshots": [
@ -14,6 +14,7 @@
"readme": "README.md",
"storage": [
{"name":"multitimer.app.js","url":"app.js"},
{"name":"multitimer.boot.js","url":"boot.js"},
{"name":"multitimer.alarm.js","url":"alarm.js"},
{"name":"multitimer.img","url":"app-icon.js","evaluate":true}
],