Add timer capability to alarms app

pull/837/head^2
Gordon Williams 2021-10-06 09:47:37 +01:00
parent f78c9b7c22
commit ce358e4b55
3 changed files with 78 additions and 10 deletions

View File

@ -199,11 +199,11 @@
"sortorder" : -2 "sortorder" : -2
}, },
{ "id": "alarm", { "id": "alarm",
"name": "Default Alarm", "name": "Default Alarm & Timer",
"shortName":"Alarms", "shortName":"Alarms",
"icon": "app.png", "icon": "app.png",
"version":"0.12", "version":"0.13",
"description": "Set and respond to alarms", "description": "Set and respond to alarms and timers",
"tags": "tool,alarm,widget,b2", "tags": "tool,alarm,widget,b2",
"storage": [ "storage": [
{"name":"alarm.app.js","url":"app.js"}, {"name":"alarm.app.js","url":"app.js"},

View File

@ -19,7 +19,7 @@ function showAlarm(alarm) {
if (alarm.msg) if (alarm.msg)
msg += "\n"+alarm.msg; msg += "\n"+alarm.msg;
E.showPrompt(msg,{ E.showPrompt(msg,{
title:"ALARM!", title:alarm.timer ? "TIMER!" : "ALARM!",
buttons : {"Sleep":true,"Ok":false} // default is sleep so it'll come back in 10 mins buttons : {"Sleep":true,"Ok":false} // default is sleep so it'll come back in 10 mins
}).then(function(sleep) { }).then(function(sleep) {
buzzCount = 0; buzzCount = 0;

View File

@ -9,6 +9,7 @@ var alarms = require("Storage").readJSON("alarm.json",1)||[];
last : 0, // last day of the month we alarmed on - so we don't alarm twice in one day! last : 0, // last day of the month we alarmed on - so we don't alarm twice in one day!
rp : true, // repeat rp : true, // repeat
as : false, // auto snooze as : false, // auto snooze
timer : 5, // OPTIONAL - if set, this is a timer and it's the time in minutes
} }
];*/ ];*/
@ -18,6 +19,12 @@ function formatTime(t) {
return hrs+":"+("0"+mins).substr(-2); return hrs+":"+("0"+mins).substr(-2);
} }
function formatMins(t) {
mins = (0|t)%60;
hrs = 0|(t/60);
return hrs+":"+("0"+mins).substr(-2);
}
function getCurrentHr() { function getCurrentHr() {
var time = new Date(); var time = new Date();
return time.getHours()+(time.getMinutes()/60)+(time.getSeconds()/3600); return time.getHours()+(time.getMinutes()/60)+(time.getSeconds()/3600);
@ -25,14 +32,20 @@ function getCurrentHr() {
function showMainMenu() { function showMainMenu() {
const menu = { const menu = {
'': { 'title': 'Alarms' }, '': { 'title': 'Alarm/Timer' },
'New Alarm': ()=>editAlarm(-1) 'New Alarm': ()=>editAlarm(-1),
'New Timer': ()=>editTimer(-1)
}; };
alarms.forEach((alarm,idx)=>{ alarms.forEach((alarm,idx)=>{
txt = (alarm.on?"on ":"off ")+formatTime(alarm.hr); if (alarm.timer) {
txt = "TIMER "+(alarm.on?"on ":"off ")+formatMins(alarm.timer);
} else {
txt = "ALARM "+(alarm.on?"on ":"off ")+formatTime(alarm.hr);
if (alarm.rp) txt += " (repeat)"; if (alarm.rp) txt += " (repeat)";
}
menu[txt] = function() { menu[txt] = function() {
editAlarm(idx); if (alarm.timer) editTimer(idx);
else editAlarm(idx);
}; };
}); });
menu['< Back'] = ()=>{load();}; menu['< Back'] = ()=>{load();};
@ -55,7 +68,7 @@ function editAlarm(alarmIndex) {
as = a.as; as = a.as;
} }
const menu = { const menu = {
'': { 'title': 'Alarms' }, '': { 'title': 'Alarm' },
'Hours': { 'Hours': {
value: hrs, value: hrs,
onchange: function(v){if (v<0)v=23;if (v>23)v=0;hrs=v;this.value=v;} // no arrow fn -> preserve 'this' onchange: function(v){if (v<0)v=23;if (v>23)v=0;hrs=v;this.value=v;} // no arrow fn -> preserve 'this'
@ -109,4 +122,59 @@ function editAlarm(alarmIndex) {
return E.showMenu(menu); return E.showMenu(menu);
} }
function editTimer(alarmIndex) {
var newAlarm = alarmIndex<0;
var hrs = 0;
var mins = 5;
var en = true;
if (!newAlarm) {
var a = alarms[alarmIndex];
mins = (0|a.timer)%60;
hrs = 0|(a.timer/60);
en = a.on;
}
const menu = {
'': { 'title': 'Timer' },
'Hours': {
value: hrs,
onchange: function(v){if (v<0)v=23;if (v>23)v=0;hrs=v;this.value=v;} // no arrow fn -> preserve 'this'
},
'Minutes': {
value: mins,
onchange: function(v){if (v<0)v=59;if (v>59)v=0;mins=v;this.value=v;} // no arrow fn -> preserve 'this'
},
'Enabled': {
value: en,
format: v=>v?"On":"Off",
onchange: v=>en=v
}
};
function getTimer() {
var d = new Date(Date.now() + ((hrs*60)+mins)*60000);
var hr = d.getHours() + (d.getMinutes()/60) + (d.getSeconds()/3600);
// Save alarm
return {
on : en,
timer : (hrs*60)+mins,
hr : hr,
rp : false, as: false
};
}
menu["> Save"] = function() {
if (newAlarm) alarms.push(getTimer());
else alarms[alarmIndex] = getTimer();
require("Storage").write("alarm.json",JSON.stringify(alarms));
showMainMenu();
};
if (!newAlarm) {
menu["> Delete"] = function() {
alarms.splice(alarmIndex,1);
require("Storage").write("alarm.json",JSON.stringify(alarms));
showMainMenu();
};
}
menu['< Back'] = showMainMenu;
return E.showMenu(menu);
}
showMainMenu(); showMainMenu();