1
0
Fork 0
BangleApps/apps/fuzzyw/fuzzyw.app.js

76 lines
2.0 KiB
JavaScript
Raw Normal View History

2022-03-31 09:17:05 +00:00
// adapted from https://github.com/hallettj/Fuzzy-Text-International/
2022-03-31 11:58:17 +00:00
const fuzzy_strings = require("Storage").readJSON("fuzzy_strings.json");
2022-03-31 11:56:53 +00:00
const SETTINGS_FILE = "fuzzyw.settings.json";
2022-04-04 12:10:51 +00:00
let settings = require("Storage").readJSON(SETTINGS_FILE,1)|| {'language': 'System', 'alignment':'Centre'};
2022-03-31 12:04:30 +00:00
if (settings.language == 'System') {
settings.language = require('locale').name;
}
2022-03-31 11:56:53 +00:00
let fuzzy_string = fuzzy_strings[settings.language];
2022-04-06 11:49:59 +00:00
let timeout = 2.5*60;
let drawTimeout;
function queueDraw(seconds) {
let millisecs = seconds * 1000;
if (drawTimeout) clearTimeout(drawTimeout);
drawTimeout = setTimeout(function() {
drawTimeout = undefined;
draw();
}, millisecs - (Date.now() % millisecs));
}
2022-03-31 11:56:53 +00:00
const h = g.getHeight();
const w = g.getWidth();
2022-04-04 11:49:52 +00:00
let align_mode = 0;
let align_pos = w/2;
if (settings.alignment =='Left') {
align_mode = -1;
align_pos = 0;
} else if (settings.alignment == 'Right') {
align_mode = 1;
align_pos = w;
}
2022-03-31 11:56:53 +00:00
function getTimeString(date) {
2022-04-06 12:17:03 +00:00
let segment = Math.round((date.getMinutes()*60 + date.getSeconds() + 1)/300);
2022-03-31 12:04:30 +00:00
let hour = date.getHours() + Math.floor(segment/12);
f_string = fuzzy_string.minutes[segment % 12];
if (f_string.includes('$1')) {
f_string = f_string.replace('$1', fuzzy_string.hours[(hour) % 24]);
} else {
f_string = f_string.replace('$2', fuzzy_string.hours[(hour + 1) % 24]);
}
return f_string;
2022-03-31 11:56:53 +00:00
}
function draw() {
2022-03-31 12:04:30 +00:00
let time_string = getTimeString(new Date()).replace('*', '');
// print(time_string);
g.setFont('Vector', (h-24*2)/fuzzy_string.text_scale);
2022-04-04 11:49:52 +00:00
g.setFontAlign(align_mode, 0);
2022-03-31 12:04:30 +00:00
g.clearRect(0, 24, w, h-24);
g.setColor(g.theme.fg);
2022-04-04 11:49:52 +00:00
g.drawString(g.wrapString(time_string, w).join("\n"), align_pos, h/2);
2022-04-06 11:49:59 +00:00
queueDraw(timeout);
2022-03-31 11:56:53 +00:00
}
g.clear();
draw();
// Stop updates when LCD is off, restart when on
Bangle.on('lcdPower',on=>{
2022-03-31 12:04:30 +00:00
if (on) {
2022-04-06 11:54:17 +00:00
draw(); // draw immediately, queue redraw
} else { // stop draw timer
if (drawTimeout) clearTimeout(drawTimeout);
drawTimeout = undefined;
2022-03-31 12:04:30 +00:00
}
2022-03-31 11:56:53 +00:00
});
Bangle.setUI('clock');
Bangle.loadWidgets();
Bangle.drawWidgets();