gbridge: improve settings handling

No need to keep them in memory after reload() is done
pull/462/head
Richard de Boer 2020-05-29 10:37:44 +02:00
parent fa692c4d8e
commit 855186f7c9
2 changed files with 26 additions and 22 deletions

View File

@ -2,17 +2,20 @@
function gb(j) {
Bluetooth.println(JSON.stringify(j));
}
const storage = require('Storage');
let settings = storage.readJSON("gbridge.json", true) || {};
if (!("showIcon" in settings)) {
settings.showIcon = true;
function settings() {
let settings = require('Storage').readJSON("gbridge.json", true) || {};
if (!("showIcon" in settings)) {
settings.showIcon = true;
}
return settings
}
function updateSettings() {
storage.write('gbridge.json', settings);
function updateSetting(setting, value) {
let settings = require('Storage').readJSON("gbridge.json", true) || {};
settings[setting] = value
require('Storage').write('gbridge.json', settings);
}
function toggleIcon() {
settings.showIcon = !settings.showIcon;
updateSettings();
function setIcon(visible) {
updateSetting('showIcon', visible);
// need to re-layout widgets
WIDGETS["gbridgew"].reload();
g.clear();
@ -22,9 +25,9 @@
"" : { "title" : "Gadgetbridge" },
"Connected" : { value : NRF.getSecurityStatus().connected?"Yes":"No" },
"Show Icon" : {
value: settings.showIcon,
value: settings().showIcon,
format: v => v?"Yes":"No",
onchange: toggleIcon
onchange: setIcon
},
"Find Phone" : function() { E.showMenu(findPhone); },
"< Back" : back,

View File

@ -1,7 +1,4 @@
(() => {
const storage = require('Storage');
let settings;
const state = {
music: "stop",
@ -14,6 +11,13 @@
scrollPos: 0
};
function settings() {
let settings = require('Storage').readJSON("gbridge.json", true) || {};
if (!("showIcon" in settings)) {
settings.showIcon = true;
}
return settings
}
function gbSend(message) {
Bluetooth.println("");
@ -183,7 +187,6 @@
});
function draw() {
if (!settings.showIcon) return;
g.setColor(-1);
if (NRF.getSecurityStatus().connected)
g.drawImage(require("heatshrink").decompress(atob("i0WwgHExAABCIwJCBYwJEBYkIBQ2ACgvzCwoECx/z/AKDD4WD+YLBEIYKCx//+cvnAKCBwU/mc4/8/HYv//Ev+Y4EEAePn43DBQkzn4rCEIoABBIwKHO4cjmczK42I6mqlqEEBQeIBQaDED4IgDUhi6KaBbmIA==")), this.x + 1, this.y + 1);
@ -197,18 +200,16 @@
}
function reload() {
settings = storage.readJSON('gbridge.json', 1) || {};
if (!("showIcon" in settings)) {
settings.showIcon = true;
}
if (settings.showIcon) {
NRF.removeListener("connect", changedConnectionState);
NRF.removeListener("disconnect", changedConnectionState);
if (settings().showIcon) {
WIDGETS["gbridgew"].width = 24;
WIDGETS["gbridgew"].draw = draw;
NRF.on("connect", changedConnectionState);
NRF.on("disconnect", changedConnectionState);
} else {
WIDGETS["gbridgew"].width = 0;
NRF.removeListener("connect", changedConnectionState);
NRF.removeListener("disconnect", changedConnectionState);
WIDGETS["gbridgew"].draw = ()=>{};
}
}