Refactored
parent
fd8785fe5b
commit
3022a9acf6
|
|
@ -4,14 +4,23 @@ const storage = require('Storage');
|
||||||
const widget_utils = require('widget_utils');
|
const widget_utils = require('widget_utils');
|
||||||
const global_settings = storage.readJSON("setting.json", true) || {};
|
const global_settings = storage.readJSON("setting.json", true) || {};
|
||||||
const LOCATION_FILE = "mylocation.json";
|
const LOCATION_FILE = "mylocation.json";
|
||||||
const h = g.getHeight();
|
|
||||||
const w = g.getWidth();
|
|
||||||
let location;
|
let location;
|
||||||
let cachedSunTimes = null;
|
let cachedSunTimes = null;
|
||||||
let lastSunCalcDate = null;
|
let lastSunCalcDate = null;
|
||||||
var prevVals = {};
|
var valsArrs = {};
|
||||||
let drawTimeout;
|
let drawTimeout;
|
||||||
|
|
||||||
|
const h = g.getHeight();
|
||||||
|
const w = g.getWidth();
|
||||||
|
const fontSize = 13;
|
||||||
|
const lineHeight = 16;
|
||||||
|
const buttonHeight = 12;
|
||||||
|
const buttonX = 78;
|
||||||
|
const buttonY = 3;
|
||||||
|
const headerHeight = 16;
|
||||||
|
const usableHeight = h - headerHeight;
|
||||||
|
const maxLines = Math.floor(usableHeight / lineHeight);
|
||||||
|
|
||||||
var settings = {
|
var settings = {
|
||||||
hr_12: global_settings["12hour"] !== undefined ? global_settings["12hour"] : false,
|
hr_12: global_settings["12hour"] !== undefined ? global_settings["12hour"] : false,
|
||||||
dark_mode: g.theme.dark
|
dark_mode: g.theme.dark
|
||||||
|
|
@ -26,22 +35,6 @@ let clrs = {
|
||||||
brackets: g.theme.fg,
|
brackets: g.theme.fg,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
let jsonText;
|
|
||||||
let lines = [];
|
|
||||||
let fontSize = 13;
|
|
||||||
const lineHeight = 16;
|
|
||||||
|
|
||||||
const buttonHeight = 12;
|
|
||||||
const buttonX = 78;
|
|
||||||
const buttonY = 3;
|
|
||||||
|
|
||||||
let keysDrawn = false;
|
|
||||||
const headerHeight = 16;
|
|
||||||
const usableHeight = h - headerHeight;
|
|
||||||
const maxLines = Math.floor(usableHeight / lineHeight);
|
|
||||||
var numWidth = 0;
|
|
||||||
|
|
||||||
// requires the myLocation app
|
// requires the myLocation app
|
||||||
let loadLocation = function() {
|
let loadLocation = function() {
|
||||||
location = require("Storage").readJSON(LOCATION_FILE, 1) || {};
|
location = require("Storage").readJSON(LOCATION_FILE, 1) || {};
|
||||||
|
|
@ -110,7 +103,17 @@ let getVal = function(now, loc) {
|
||||||
return vals;
|
return vals;
|
||||||
};
|
};
|
||||||
|
|
||||||
let loadJson = function() {
|
|
||||||
|
let getKeyValRegex = function(line) {
|
||||||
|
return line.trim().match(/^"([^"]+)":\s*(.+)$/);
|
||||||
|
};
|
||||||
|
|
||||||
|
let getIndentRegex = function(line) {
|
||||||
|
const indentMatch = line.match(/^(\s*)/);
|
||||||
|
return indentMatch ? indentMatch[1] : "";
|
||||||
|
};
|
||||||
|
|
||||||
|
let getJsonLine = function() {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const vals = getVal(now, location);
|
const vals = getVal(now, location);
|
||||||
//vals.steps = null; // For testing; uncomment to see the steps not appear
|
//vals.steps = null; // For testing; uncomment to see the steps not appear
|
||||||
|
|
@ -135,9 +138,9 @@ let loadJson = function() {
|
||||||
set: vals.set,
|
set: vals.set,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
jsonText = JSON.stringify(raw, null, 2);
|
let jsonText = JSON.stringify(raw, null, 2);
|
||||||
lines = jsonText.split("\n");
|
return jsonText.split("\n");
|
||||||
};
|
};
|
||||||
|
|
||||||
let draw = function() {
|
let draw = function() {
|
||||||
g.clear();
|
g.clear();
|
||||||
|
|
@ -153,91 +156,101 @@ let draw = function() {
|
||||||
g.setFont("Vector", buttonHeight);
|
g.setFont("Vector", buttonHeight);
|
||||||
g.drawString("X", buttonX, buttonY);
|
g.drawString("X", buttonX, buttonY);
|
||||||
g.setFont("Vector", fontSize);
|
g.setFont("Vector", fontSize);
|
||||||
|
|
||||||
|
var lines = getJsonLine();
|
||||||
|
var numWidth = 0;
|
||||||
|
|
||||||
|
// Draw numbers first to find out their max width
|
||||||
for (let i = 0; i < maxLines; i++) {
|
for (let i = 0; i < maxLines; i++) {
|
||||||
const y = headerHeight + i * lineHeight;
|
const y = headerHeight + i * lineHeight;
|
||||||
const lineNumberStr = (i + 1).toString().padStart(2, " ") + " ";
|
const lineNumberStr = (i + 1).toString().padStart(2, " ") + " ";
|
||||||
g.drawString(lineNumberStr, 0, y);
|
g.drawString(lineNumberStr, 0, y);
|
||||||
numWidth = Math.max(numWidth, g.stringWidth(lineNumberStr));
|
numWidth = Math.max(numWidth, g.stringWidth(lineNumberStr));
|
||||||
}
|
}
|
||||||
|
|
||||||
redrawValues();
|
|
||||||
};
|
|
||||||
|
|
||||||
let redraw = function() {
|
|
||||||
g.setFont("Vector", fontSize);
|
|
||||||
for (let i = 0; i < maxLines; i++) {
|
for (let i = 0; i < maxLines; i++) {
|
||||||
const lineIndex = i;
|
|
||||||
const line = lines[lineIndex];
|
|
||||||
if (!line) continue;
|
|
||||||
const y = headerHeight + i * lineHeight;
|
const y = headerHeight + i * lineHeight;
|
||||||
|
const line = lines[i];
|
||||||
|
if (!line) continue;
|
||||||
|
|
||||||
const indentMatch = line.match(/^(\s*)/);
|
let kvMatch = getKeyValRegex(line);
|
||||||
const indent = indentMatch ? indentMatch[1] : "";
|
|
||||||
|
|
||||||
const kvMatch = line.trim().match(/^"([^"]+)":\s*(.+)$/);
|
|
||||||
if (kvMatch) {
|
if (kvMatch) {
|
||||||
const key = kvMatch[1];
|
const key = kvMatch[1];
|
||||||
let value = kvMatch[2];
|
let value = kvMatch[2];
|
||||||
|
|
||||||
let valPrev = (prevVals[key] && prevVals[key].text) || null;
|
|
||||||
if (valPrev === value) continue;
|
|
||||||
|
|
||||||
if (!keysDrawn) {
|
|
||||||
prevVals[key] = {};
|
|
||||||
// Key
|
|
||||||
g.setColor(clrs.keys);
|
|
||||||
const keyText = indent + `"${key}"`;
|
|
||||||
g.drawString(keyText, numWidth, y);
|
|
||||||
const keyWidth = g.stringWidth(keyText);
|
|
||||||
let valueX = numWidth + keyWidth;
|
|
||||||
|
|
||||||
g.setColor(clrs.brackets);
|
// Key
|
||||||
const colonText = ": ";
|
g.setColor(clrs.keys);
|
||||||
g.drawString(colonText, valueX, y);
|
const indent = getIndentRegex(line);
|
||||||
valueX += g.stringWidth(colonText);
|
const keyText = indent + `"${key}"`;
|
||||||
prevVals[key].x = valueX;
|
g.drawString(keyText, numWidth, y);
|
||||||
prevVals[key].y = y;
|
const keyWidth = g.stringWidth(keyText);
|
||||||
}
|
let x = numWidth + keyWidth;
|
||||||
prevVals[key].text = value;
|
|
||||||
let pos = prevVals[key];
|
|
||||||
|
|
||||||
// Clear prev values
|
g.setColor(clrs.brackets);
|
||||||
g.setColor(clrs.bg);
|
const colonText = ": ";
|
||||||
g.fillRect(pos.x, pos.y, w, pos.y + lineHeight);
|
g.drawString(colonText, x, y);
|
||||||
|
x += g.stringWidth(colonText);
|
||||||
|
|
||||||
// Value color
|
// Value color
|
||||||
const endComma = value.endsWith(',');
|
const endComma = value.endsWith(',');
|
||||||
|
valsArrs[key] = {text:value, x:x, y:y, endComma:endComma};
|
||||||
if (endComma) value = value.slice(0, -1);
|
if (endComma) value = value.slice(0, -1);
|
||||||
if (value.startsWith('"')) {
|
if (value.startsWith('"')) {
|
||||||
g.setColor(clrs.strings);
|
valsArrs[key].color = clrs.strings;
|
||||||
} else if (value.startsWith('{') || value.startsWith('}')) {
|
} else if (value.startsWith('{') || value.startsWith('}')) {
|
||||||
g.setColor(clrs.brackets);
|
valsArrs[key].color = clrs.brackets;
|
||||||
} else {
|
} else {
|
||||||
g.setColor(clrs.ints);
|
valsArrs[key].color = clrs.ints;
|
||||||
}
|
}
|
||||||
g.drawString(value, pos.x, pos.y);
|
g.setColor(valsArrs[key].color);
|
||||||
|
g.drawString(value, x, y);
|
||||||
if (endComma){
|
if (endComma){
|
||||||
g.setColor(clrs.brackets);
|
g.setColor(clrs.brackets);
|
||||||
g.drawString(',', pos.x + g.stringWidth(value), pos.y);
|
g.drawString(',', x + g.stringWidth(value), y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (!keysDrawn) {
|
else {
|
||||||
g.setColor(clrs.brackets);
|
g.setColor(clrs.brackets);
|
||||||
g.drawString(line, numWidth, y);
|
g.drawString(line, numWidth, y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Redraws only values that changed
|
||||||
|
let redraw = function() {
|
||||||
|
g.setFontAlign(-1, -1);
|
||||||
|
g.setFont("Vector", fontSize);
|
||||||
|
var lines = getJsonLine();
|
||||||
|
|
||||||
|
for (let i = 0; i < lines.length; i++) {
|
||||||
|
let kvMatch = getKeyValRegex(lines[i]);
|
||||||
|
if (!kvMatch) continue;
|
||||||
|
const key = kvMatch[1];
|
||||||
|
let value = kvMatch[2];
|
||||||
|
if (!(key in valsArrs)) continue;
|
||||||
|
let valsArr = valsArrs[key];
|
||||||
|
if (value === valsArr.text) continue; // No need to update
|
||||||
|
valsArrs[key].text = value;
|
||||||
|
|
||||||
|
// Clear prev values
|
||||||
|
g.setColor(clrs.bg);
|
||||||
|
g.fillRect(valsArr.x, valsArr.y, w, valsArr.y + lineHeight);
|
||||||
|
|
||||||
|
g.setColor(valsArr.color);
|
||||||
|
g.drawString(value, valsArr.x, valsArr.y);
|
||||||
|
if (valsArr.endComma){
|
||||||
|
g.setColor(clrs.brackets);
|
||||||
|
g.drawString(',', valsArr.Banglex + g.stringWidth(value), valsArr.y);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
keysDrawn = true;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let redrawValues = function() {
|
let redrawValues = function() {
|
||||||
loadJson();
|
|
||||||
redraw();
|
redraw();
|
||||||
if (drawTimeout) clearTimeout(drawTimeout);
|
if (drawTimeout) clearTimeout(drawTimeout);
|
||||||
drawTimeout = setTimeout(function() {
|
drawTimeout = setTimeout(function() {
|
||||||
drawTimeout = undefined;
|
drawTimeout = undefined;
|
||||||
redrawValues();
|
redrawValues();
|
||||||
}, 60000 - (Date.now() % 60000));
|
}, 600 - (Date.now() % 600));
|
||||||
};
|
};
|
||||||
|
|
||||||
Bangle.on('touch', (zone, e) => {
|
Bangle.on('touch', (zone, e) => {
|
||||||
|
|
@ -258,4 +271,5 @@ loadLocation();
|
||||||
Bangle.loadWidgets();
|
Bangle.loadWidgets();
|
||||||
widget_utils.hide();
|
widget_utils.hide();
|
||||||
draw();
|
draw();
|
||||||
|
redrawValues(); // To set the timeout
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue