From bba32a35d0fc18c5e1eac218403492537edbd471 Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Fri, 4 Nov 2022 10:07:53 +0000 Subject: [PATCH] settings 0.54: If setting.json is corrupt, ensure it gets re-written --- apps/setting/ChangeLog | 1 + apps/setting/metadata.json | 2 +- apps/setting/settings.js | 26 ++++++++++++++------------ 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/apps/setting/ChangeLog b/apps/setting/ChangeLog index cd97e1dda0..55b61c46a4 100644 --- a/apps/setting/ChangeLog +++ b/apps/setting/ChangeLog @@ -58,3 +58,4 @@ 0.51: Add setting for configuring a launcher 0.52: Add option for left-handed users 0.53: Ensure that when clock is set, clockHasWidgets is set correctly too +0.54: If setting.json is corrupt, ensure it gets re-written diff --git a/apps/setting/metadata.json b/apps/setting/metadata.json index 47d0a309f1..08544cff6d 100644 --- a/apps/setting/metadata.json +++ b/apps/setting/metadata.json @@ -1,7 +1,7 @@ { "id": "setting", "name": "Settings", - "version": "0.53", + "version": "0.54", "description": "A menu for setting up Bangle.js", "icon": "settings.png", "tags": "tool,system", diff --git a/apps/setting/settings.js b/apps/setting/settings.js index d7bb060ea0..2350a89655 100644 --- a/apps/setting/settings.js +++ b/apps/setting/settings.js @@ -60,7 +60,9 @@ function resetSettings() { } settings = storage.readJSON('setting.json', 1); -if (!settings) resetSettings(); +if (("object" != typeof settings) || + ("object" != typeof settings.options)) + resetSettings(); const boolFormat = v => v ? /*LANG*/"On" : /*LANG*/"Off"; @@ -237,9 +239,9 @@ function showThemeMenu() { } }; - require("Storage").list(/^.*\.theme$/).forEach( + storage.list(/^.*\.theme$/).forEach( n => { - let newTheme = require("Storage").readJSON(n); + let newTheme = storage.readJSON(n); themesMenu[newTheme.name ? newTheme.name : n] = () => { upd({ fg:cl(newTheme.fg), bg:cl(newTheme.bg), @@ -567,11 +569,11 @@ function showUtilMenu() { }, /*LANG*/'Compact Storage': () => { E.showMessage(/*LANG*/"Compacting...\nTakes approx\n1 minute",{title:/*LANG*/"Storage"}); - require("Storage").compact(); + storage.compact(); showUtilMenu(); }, /*LANG*/'Rewrite Settings': () => { - require("Storage").write(".boot0","eval(require('Storage').read('bootupdate.js'));"); + storage.write(".boot0","eval(require('Storage').read('bootupdate.js'));"); load("setting.app.js"); }, /*LANG*/'Flatten Battery': () => { @@ -592,9 +594,9 @@ function showUtilMenu() { menu[/*LANG*/'Calibrate Battery'] = () => { E.showPrompt(/*LANG*/"Is the battery fully charged?",{title:/*LANG*/"Calibrate"}).then(ok => { if (ok) { - var s=require("Storage").readJSON("setting.json"); + var s=storage.readJSON("setting.json"); s.batFullVoltage = (analogRead(D3)+analogRead(D3)+analogRead(D3)+analogRead(D3))/4; - require("Storage").writeJSON("setting.json",s); + storage.writeJSON("setting.json",s); E.showAlert(/*LANG*/"Calibrated!").then(() => load("setting.app.js")); } else { E.showAlert(/*LANG*/"Please charge Bangle.js for 3 hours and try again").then(() => load("settings.app.js")); @@ -659,7 +661,7 @@ function makeConnectable() { }); } function showClockMenu() { - var clockApps = require("Storage").list(/\.info$/) + var clockApps = storage.list(/\.info$/) .map(app => {var a=storage.readJSON(app, 1);return (a&&a.type == "clock")?a:undefined}) .filter(app => app) // filter out any undefined apps .sort((a, b) => a.sortorder - b.sortorder); @@ -676,7 +678,7 @@ function showClockMenu() { } clockMenu[label] = () => { settings.clock = app.src; - settings.clockHasWidgets = require("Storage").read(app.src).includes("Bangle.loadWidgets"); + settings.clockHasWidgets = storage.read(app.src).includes("Bangle.loadWidgets"); updateSettings(); showMainMenu(); }; @@ -687,7 +689,7 @@ function showClockMenu() { return E.showMenu(clockMenu); } function showLauncherMenu() { - var launcherApps = require("Storage").list(/\.info$/) + var launcherApps = storage.list(/\.info$/) .map(app => {var a=storage.readJSON(app, 1);return (a&&a.type == "launch")?a:undefined}) .filter(app => app) // filter out any undefined apps .sort((a, b) => a.sortorder - b.sortorder); @@ -865,9 +867,9 @@ function showTouchscreenCalibration() { Bangle.setOptions({ touchX1: calib.x1, touchY1: calib.y1, touchX2: calib.x2, touchY2: calib.y2 }); - var s = require("Storage").readJSON("setting.json",1)||{}; + var s = storage.readJSON("setting.json",1)||{}; s.touch = calib; - require("Storage").writeJSON("setting.json",s); + storage.writeJSON("setting.json",s); g.setFont("6x8:2").setFontAlign(0,0).drawString("Calibrated!", g.getWidth()/2, g.getHeight()/2); // now load the main menu again setTimeout(showLCDMenu, 500);