1
0
Fork 0
BangleApps/apps/widtmr/widget.js

168 lines
4.4 KiB
JavaScript
Raw Normal View History

(() => {
2022-02-25 16:14:19 +00:00
let storage = require('Storage');
var settings;
var interval = 0; //used for the 1 second interval timer
var diff;
2022-02-25 16:14:19 +00:00
//Convert ms to time
function getTime(t) {
var milliseconds = parseInt((t % 1000) / 100),
seconds = Math.floor((t / 1000) % 60),
minutes = Math.floor((t / (1000 * 60)) % 60),
hours = Math.floor((t / (1000 * 60 * 60)) % 24);
return hours.toString().padStart(2,0) + ":" + minutes.toString().padStart(2,0) + ":" + seconds.toString().padStart(2,0);
}
//counts down, calculates and displays
function countDown() {
var now = new Date();
2022-02-25 16:14:19 +00:00
diff = settings.goal - now; //calculate difference
// time is up
2022-02-25 16:14:19 +00:00
if (settings.started && diff < 1000) {
Bangle.buzz(1500);
//write timer off to file
2022-02-25 16:14:19 +00:00
settings.started = false;
storage.writeJSON('widtmr.json', settings);
clearInterval(interval); //stop interval
interval = undefined;
}
// calculates width and redraws accordingly
2022-02-25 16:14:19 +00:00
WIDGETS["widtmr"].redraw();
}
2022-02-25 16:14:19 +00:00
function updateSettings(){
var now = new Date();
const goal = new Date(now.getFullYear(), now.getMonth(), now.getDate(),
now.getHours(), now.getMinutes() + settings.minutes, now.getSeconds());
settings.goal = goal.getTime();
settings.goal = goal.getTime();
storage.writeJSON('widtmr.json', settings);
WIDGETS["widtmr"].reload();
}
2022-02-25 16:14:19 +00:00
/*
* Add the widgets and functions for other apps
*/
WIDGETS["widtmr"]={area:"tl",width:0,draw:function() {
if (!this.width) {
return;
}
g.reset().setFontAlign(0,0).clearRect(this.x,this.y,this.x+this.width,this.y+23);
2022-02-25 16:14:19 +00:00
var scale;
var timeStr;
if (diff < 3600000) { //less than 1 hour left
width = 58;
scale = 2;
timeStr = getTime(diff).substring(3); // remove hour part 00:00:00 -> 00:00
} else { //one hour or more left
width = 48;
scale = 1;
timeStr = getTime(diff); //display hour 00:00:00 but small
}
2022-02-25 16:14:19 +00:00
// Font5x9Numeric7Seg - just build this in as it's tiny
g.setFontCustom(atob("AAAAAAAAAAIAAAQCAQAAAd0BgMBdwAAAAAAAdwAB0RiMRcAAAERiMRdwAcAQCAQdwAcERiMRBwAd0RiMRBwAAEAgEAdwAd0RiMRdwAcERiMRdwAFAAd0QiEQdwAdwRCIRBwAd0BgMBAAABwRCIRdwAd0RiMRAAAd0QiEQAAAAAAAAAA="), 32, atob("BgAAAAAAAAAAAAAAAAYCAAYGBgYGBgYGBgYCAAAAAAAABgYGBgYG"), 9 + (scale<<8));
g.drawString(timeStr, this.x+this.width/2, this.y+12);
2022-02-25 16:14:19 +00:00
}, redraw:function() {
var last = this.width;
2022-02-25 16:14:19 +00:00
if (!settings.started) {
this.width = 0;
} else {
this.width = (diff < 3600000) ? 58 : 48;
}
if (last != this.width) {
Bangle.drawWidgets();
} else {
this.draw();
}
}, reload:function() {
2022-02-25 16:14:19 +00:00
settings = storage.readJSON("widtmr.json",1)||{};
if (interval) {
clearInterval(interval);
}
interval = undefined;
2022-02-25 16:14:19 +00:00
// start countdown each second
2022-02-25 16:14:19 +00:00
if (settings.started) {
interval = setInterval(countDown, 1000);
}
// reset everything
countDown();
2022-02-25 16:14:19 +00:00
}, isStarted: function(){
settings = storage.readJSON("widtmr.json",1)||{started: false};
return settings.started;
}, setStarted: function(started){
settings.started=started;
updateSettings();
}, increaseTimer: function(m){
settings.minutes += m;
updateSettings();
}, decreaseTimer: function(m){
settings.minutes -= m;
if(settings.minutes <= 0){
settings.started=false;
settings.minutes=0;
}
updateSettings();
}, setTime: function(t){
settings.minutes = Math.max(0, t);
settings.started = t > 0;
updateSettings();
}, getRemainingMinutes: function(){
settings = storage.readJSON("widtmr.json",1)||{started: false};
if(!settings.started){
return -1;
}
var now = new Date();
var diff = settings.goal - now;
return Math.round(diff / (1000*60));
}, getRemainingTimeStr: function(){
settings = storage.readJSON("widtmr.json",1)||{started: false};
if(!settings.started){
return;
}
var now = new Date();
var diff = settings.goal - now;
var timeStr = getTime(diff);
if(diff < 3600000){
timeStr = timeStr.substring(3); // remove hour part 00:00:00 -> 00:00
}
return timeStr;
}, getRemainingTime: function(){
settings = storage.readJSON("widtmr.json",1)||{started: false};
if(!settings.started){
return;
}
var now = new Date();
var diff = settings.goal - now;
return diff;
2022-02-25 16:14:19 +00:00
}
};
// set width correctly, start countdown each second
2022-02-25 16:14:19 +00:00
WIDGETS["widtmr"].reload();
})();