[sleeplogalarm] Correct filtering improve settings

pull/2323/head
storm64 2022-11-28 00:08:21 +01:00
parent f687ea0027
commit beab5405c7
6 changed files with 69 additions and 70 deletions

View File

@ -1,3 +1,4 @@
0.01: New App!
0.02: Add "from Consec."-setting
0.03: Correct how to ignore last triggered alarm
0.03: Correct how to ignore last triggered alarm
0.04: Make "disable alarm" possible on next day; correct alarm filtering; improve settings

View File

@ -17,8 +17,7 @@ This widget searches for active alarms and raises an own alarm event up to the d
- __msg as prefix__ | use the customized message as prefix to the original message or replace it comlpetely if disabled
__on__ / _off_
- __disable alarm__ | if enabled the original alarm will be disabled
_on_ / __off__
This feature does not work for alarms on the next day!
_on_ / __off__
- __auto snooze__ | auto snooze option for the earlier alarm
__on__ / _off_
- __Filter Alarm__ submenu

View File

@ -9,18 +9,23 @@ function getNextAlarm(allAlarms, fo, withId) {
});
// return next active alarms in range, filter for
// active && not timer && not own alarm &&
// after from && before to && includes msg &&
// lastDate not today || after lastTime
return allAlarms.filter(
// after from && before to && includes msg
var ret = allAlarms.filter(
a => a.on && !a.timer && a.id !== "sleeplog" &&
a.t >= fo.from && a.t < fo.to && (!fo.msg || a.msg.includes(fo.msg)) &&
fo.lastDate !== new Date().getDate() || a.t > fo.lastTime
a.t >= fo.from && a.t < fo.to && (!fo.msg || a.msg.includes(fo.msg))
).map(a => { // add time to alarm
a.tTo = sched.getTimeToAlarm(a);
return a;
}).filter(a => a.tTo // filter non active alarms
// sort to get next alarm first
).sort((a, b) => a.tTo - b.tTo)[0] || {};
).sort((a, b) => a.tTo - b.tTo);
// prevent triggering for an already triggered alarm again if available
if (fo.lastDate) {
var toLast = fo.lastDate - new Date().valueOf() + 1000;
if (toLast > 0) ret = ret.filter(a => a.tTo > toLast);
}
// return first entry
return ret[0] || {};
}
exports = {
@ -35,12 +40,16 @@ exports = {
msgAsPrefix: true,
disableOnAlarm: false, // !!! not available if alarm is at the next day
as: true,
filter_from: 3,
filter_to: 12,
filter_msg: "",
wid_hide: false,
wid_time: true,
wid_color: g.theme.dark ? 65504 : 31, // yellow or blue
filter: {
from: 3 * 36E5,
to: 12 * 36E5,
msg: ""
},
wid: {
hide: false,
time: true,
color: g.theme.dark ? 65504 : 31 // yellow or blue
}
}, require("Storage").readJSON("sleeplogalarm.settings.json", true) || {});
},
@ -53,13 +62,7 @@ exports = {
var settings = exports.getSettings();
// set the alarm time
this.time = getNextAlarm(sched.getAlarms(), {
from: settings.filter_from * 36E5,
to: settings.filter_to * 36E5,
msg: settings.filter_msg,
lastTime: settings.lastTime,
lastDate: settings.lastDate
}).t;
this.time = getNextAlarm(sched.getAlarms(), settings.filter).t;
// abort if no alarm time could be found inside range
if (!this.time) return;
@ -90,13 +93,7 @@ exports = {
var allAlarms = sched.getAlarms();
// find first active alarm
var alarm = getNextAlarm(sched.getAlarms(), {
from: settings.filter_from * 36E5,
to: settings.filter_to * 36E5,
msg: settings.filter_msg,
lastTime: settings.lastTime,
lastDate: settings.lastDate
}, settings.disableOnAlarm);
var alarm = getNextAlarm(sched.getAlarms(), settings.filter, settings.disableOnAlarm);
// return if no alarm is found
if (!alarm) return;
@ -105,12 +102,12 @@ exports = {
var now = new Date();
// get date of the alarm
var aDate = new Date(now + alarm.tTo).getDate();
var aDate = new Date(now + alarm.tTo);
// disable earlier triggered alarm if set and on the same day
if (settings.disableOnAlarm && now.getDate() === aDate) {
// set alarms last to today
allAlarms[alarm.idx].last = aDate;
// disable earlier triggered alarm if set
if (settings.disableOnAlarm) {
// set alarms last to the day it would trigger
allAlarms[alarm.idx].last = aDate.getDate();
// remove added indexes
allAlarms = allAlarms.map(a => {
delete a.idx;
@ -131,9 +128,8 @@ exports = {
del: true
});
// save time of alarm and this day to prevent triggering for the same alarm again
settings.lastTime = alarm.t;
settings.lastDate = now.getDate();
// save date of the alarm to prevent triggering for the same alarm again
settings.filter.lastDate = aDate.valueOf();
require("Storage").writeJSON("sleeplogalarm.settings.json", settings);
// write changes

View File

@ -2,7 +2,7 @@
"id":"sleeplogalarm",
"name":"Sleep Log Alarm",
"shortName": "SleepLogAlarm",
"version": "0.03",
"version": "0.04",
"description": "Enhance your morning and let your alarms wake you up when you are in light sleep.",
"icon": "app.png",
"type": "widget",

View File

@ -8,19 +8,16 @@
}
// read input from keyboard
function readInput(setting, retPos, cb) {
setTimeout((setting, retPos, cb) => {
function readInput(v, cb) {
// setTimeout required to load after menu refresh
setTimeout((v, cb) => {
if (require("Storage").read("textinput")) {
g.clear();
require("textinput").input({text: settings[setting]}).then(result => {
settings[setting] = result;
writeSetting();
cb(retPos);
});
require("textinput").input({text: v}).then(v => cb(v));
} else {
E.showAlert(/*LANG*/"No keyboard app installed").then(() => cb(retPos));
E.showAlert(/*LANG*/"No keyboard app installed").then(() => cb());
}
}, 0, setting, retPos, cb);
}, 0, v, cb);
}
// show widget menu
@ -32,36 +29,39 @@
},
/*LANG*/"< Back": () => showMain(8),
/*LANG*/"time from": {
value: settings.filter_from,
step: 0.25,
value: settings.filter.from / 6E4,
step: 10,
min: 0,
max: 24,
max: 1440,
wrap: true,
noList: true,
format: v => (0|v) + ":" + ("" + (0|(v%1 * 60))).padStart(2, "0"),
format: v => (0|v/60) + ":" + ("" + (v%60)).padStart(2, "0"),
onchange: v => {
settings.filter_from = v;
settings.filter.from = v * 6E4;
writeSetting();
}
},
/*LANG*/"time to": {
value: settings.filter_to,
step: 0.25,
value: settings.filter.to / 6E4,
step: 10,
min: 0,
max: 24,
max: 1440,
wrap: true,
noList: true,
format: v => (0|v) + ":" + ("" + (0|(v%1 * 60))).padStart(2, "0"),
format: v => (0|v/60) + ":" + ("" + (v%60)).padStart(2, "0"),
onchange: v => {
settings.filter_to = v;
settings.filter.to = v * 6E4;
writeSetting();
}
},
/*LANG*/"msg includes": {
value: settings.filter_msg,
value: settings.filter.msg,
format: v => !v ? "" : v.length > 6 ? v.substring(0, 6)+"..." : v,
// setTimeout required to load after menu refresh
onchange: () => readInput("filter_msg", 3, showFilterMenu)
onchange: v => readInput(v, v => {
settings.filter.msg = v;
writeSetting();
showFilterMenu(3);
})
}
};
var menu = E.showMenu(filterMenu);
@ -80,27 +80,27 @@
},
/*LANG*/"< Back": () => showMain(9),
/*LANG*/"hide": {
value: settings.wid_hide,
value: settings.wid.hide,
onchange: v => {
settings.wid_hide = v;
settings.wid.hide = v;
writeSetting();
}
},
/*LANG*/"show time": {
value: settings.wid_time,
value: settings.wid.time,
onchange: v => {
settings.wid_time = v;
settings.wid.time = v;
writeSetting();
}
},
/*LANG*/"color": {
value: colVal.indexOf(settings.wid_color),
value: colVal.indexOf(settings.wid.color),
min: 0,
max: colVal.length -1,
wrap: true,
format: v => colName[v],
onchange: v => {
settings.wid_color = colVal[v];
settings.wid.color = colVal[v];
writeSetting();
}
}
@ -147,8 +147,11 @@
/*LANG*/"msg": {
value: settings.msg,
format: v => !v ? "" : v.length > 6 ? v.substring(0, 6)+"..." : v,
// setTimeout required to load after menu refresh
onchange: () => readInput("msg", 4, showMain)
onchange: v => readInput(v, v => {
settings.msg = v;
writeSetting();
showMenu(4);
})
},
/*LANG*/"msg as prefix": {
value: settings.msgAsPrefix,

View File

@ -11,7 +11,7 @@ if ((require("Storage").readJSON("sleeplogalarm.settings.json", true) || {enable
earlier: settings.earlier,
draw: function () {
// draw zzz
g.reset().setColor(settings.wid_color).drawImage(atob("BwoBD8SSSP4EEEDg"), this.x + 1, this.y);
g.reset().setColor(settings.wid.color).drawImage(atob("BwoBD8SSSP4EEEDg"), this.x + 1, this.y);
// call function to draw the time of alarm if a alarm is found
if (this.time) this.drawTime(this.time + 1);
},
@ -20,7 +20,7 @@ if ((require("Storage").readJSON("sleeplogalarm.settings.json", true) || {enable
};
// add function to draw the time of alarm if enabled
if (settings.wid_time) WIDGETS.sleeplogalarm.drawTime = function(time) {
if (settings.wid.time) WIDGETS.sleeplogalarm.drawTime = function(time) {
// directly include Font4x5Numeric
g.setFontCustom(atob("CAZMA/H4PgvXoK1+DhPg7W4P1uCEPg/X4O1+AA=="), 46, atob("AgQEAgQEBAQEBAQE"), 5).setFontAlign(1, 1);
g.drawString(0|(time / 36E5), this.x + this.width + 1, this.y + 17);