diff --git a/apps/sleeplogalarm/lib.js b/apps/sleeplogalarm/lib.js index d7375f79a..d5fb87a65 100644 --- a/apps/sleeplogalarm/lib.js +++ b/apps/sleeplogalarm/lib.js @@ -19,11 +19,6 @@ function getNextAlarm(allAlarms, from, to, withId) { }).sort((a, b) => a.tTo - b.tTo)[0] || {}; } -// calculate a time from its date -function dateToTime(date) { - return ((date.getHours() * 60 + date.getMinutes()) * 60 + date.getSeconds()) * 1000; -} - exports = { // function to read settings with defaults getSettings: function() { @@ -68,7 +63,7 @@ exports = { // get settings from widget, now and calculate time of now var settings = WIDGETS.sleeplogalarm; var now = new Date(); - var tNow = dateToTime(now); + var tNow = ((now.getHours() * 60 + now.getMinutes()) * 60 + now.getSeconds()) * 1000; // execute trigger function if inside the alarm range if (tNow >= settings.time - settings.earlier * 6E4 && @@ -78,32 +73,25 @@ exports = { // trigger function trigger: function(now, tNow) { - // define settings + // read settings var settings = this.getSettings(); - // calculate then date - var then = new Date(now + settings.earlier * 6E4); - // read all alarms var allAlarms = sched.getAlarms(); // find first active alarm - var alarm = firstActiveAlarm(allAlarms); + var alarm = getNextAlarm(sched.getAlarms(), settings.from * 36E5, settings.to * 36E5, settings.disableOnAlarm); // return if no alarm is found if (!alarm) return; - // disable early triggered alarm if set and now and then on the same day - if (settings.disableOnAlarm && now.getDate() === then.getDate()) { - // add indexes to find alarm to temporary disable - allAlarms = allAlarms.map((a, idx) => { - a.idx = idx; - return a; - }); - // get index of first active alarm - var idx = firstActiveAlarm(allAlarms).idx; - // set this alarms last to then - allAlarms[idx].last = then.getDate(); + // get date of the alarm + var aDate = new Date(now + alarm.tTo).getDate(); + + // 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; // remove added indexes allAlarms = allAlarms.map(a => { delete a.idx; @@ -116,7 +104,7 @@ exports = { id: "sleeplog", appid: "sleeplog", on: true, - t: (((now.getHours() * 60 + now.getMinutes()) * 60 + now.getSeconds()) * 1000), + t: tNow, dow: 127, msg: settings.msg + (settings.msgAsPrefix ? alarm.msg || "" : ""), vibrate: settings.vibrate || alarm.vibrate, diff --git a/apps/sleeplogalarm/settings.js b/apps/sleeplogalarm/settings.js index f6787f3bd..2447a20aa 100644 --- a/apps/sleeplogalarm/settings.js +++ b/apps/sleeplogalarm/settings.js @@ -1,26 +1,10 @@ (function(back) { - // define settings filename - var filename = "sleeplogalarm.settings.json"; - - // define settings - var settings = Object.assign({ - enabled: true, - hide: false, - drawRange: true, - color: g.theme.dark ? 65504 : 31, // yellow or blue - from: 4, // 0400 - to: 8, // 0800 - earlier: 30, - disableOnAlarm: false, // !!! not available if alarm is at the next day - msg: "...\n", - msgAsPrefix: true, - vibrate: "..", - as: true - }, require("Storage").readJSON(filename, true) || {}); + // read settings + var settings = require("sleeplogalarm").getSettings(); // write change to storage function writeSetting() { - require("Storage").writeJSON(filename, settings); + require("Storage").writeJSON("sleeplogalarm.settings.json", settings); } // show widget menu diff --git a/apps/sleeplogalarm/widget.js b/apps/sleeplogalarm/widget.js index d0e60b488..ee5e06fcb 100644 --- a/apps/sleeplogalarm/widget.js +++ b/apps/sleeplogalarm/widget.js @@ -1,5 +1,7 @@ // check if enabled in settings if ((require("Storage").readJSON("sleeplogalarm.settings.json", true) || {enabled: true}).enabled) { + // read settings + var settings = require("sleeplogalarm").getSettings(); // insert neccessary settings into widget WIDGETS.sleeplogalarm = { @@ -11,18 +13,22 @@ if ((require("Storage").readJSON("sleeplogalarm.settings.json", true) || {enable earlier: settings.earlier, draw: function () { // draw zzz - g.reset().setColor(this.color).drawImage(atob("BwoBD8SSSP4EEEDg"), this.x + 1, this.y); - // draw time of alarm if enabled - if (this.drawTime && this.time) { - // directly include Font4x5Numeric - g.setFontCustom(atob("CAZMA/H4PgvXoK1+DhPg7W4P1uCEPg/X4O1+AA=="), 46, atob("AgQEAgQEBAQEBAQE"), 5).setFontAlign(1, 1); - g.drawString(0|(this.time / 36E5), this.x + this.width + 1, this.y + 12); - g.drawString(0|((this.time / 36E5)%1 * 60), this.x + this.width + 1, this.y + 23); - } + g.reset().setColor(settings.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); }, + drawTime: () => {}, reload: require("sleeplogalarm").widReload() }; + // add function to draw the time of alarm if enabled + if (this.drawTime) 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 + 12); + g.drawString(0|((time / 36E5)%1 * 60), this.x + this.width + 1, this.y + 23); + }; + // load widget WIDGETS.sleeplogalarm.reload(); } \ No newline at end of file