From a1e03d322978191c4165aaa898548bcde7c0dfd3 Mon Sep 17 00:00:00 2001 From: storm64 Date: Wed, 14 Sep 2022 21:53:04 +0200 Subject: [PATCH] [widbt_notify] Simplify code, add some options --- apps/widbt_notify/ChangeLog | 3 +- apps/widbt_notify/metadata.json | 4 +- apps/widbt_notify/settings.js | 90 +++++++--------- apps/widbt_notify/widget.js | 175 ++++++++++++++------------------ 4 files changed, 119 insertions(+), 153 deletions(-) diff --git a/apps/widbt_notify/ChangeLog b/apps/widbt_notify/ChangeLog index 8f1dab908..b9ecd7b7d 100644 --- a/apps/widbt_notify/ChangeLog +++ b/apps/widbt_notify/ChangeLog @@ -11,4 +11,5 @@ 0.12: Prevent repeated execution of `draw()` from the current app. 0.13: Added "connection restored" notification. Fixed restoring of the watchface. 0.14: Added configuration option -0.15: Added option to hide widget when connected \ No newline at end of file +0.15: Added option to hide widget when connected +0.16: Simplify code, add option to disable displaying a message \ No newline at end of file diff --git a/apps/widbt_notify/metadata.json b/apps/widbt_notify/metadata.json index 6def70c64..626ffcb8b 100644 --- a/apps/widbt_notify/metadata.json +++ b/apps/widbt_notify/metadata.json @@ -1,8 +1,8 @@ { "id": "widbt_notify", "name": "Bluetooth Widget with Notification", - "version": "0.15", - "description": "Show the current Bluetooth connection status in the top right of the watch. Optional buzz and/or and hide if disconnected", + "version": "0.16", + "description": "Show the current Bluetooth connection status with some optional features: show message, buzz on connect/loss, hide always/if connected.", "icon": "widget.png", "type": "widget", "tags": "widget,bluetooth", diff --git a/apps/widbt_notify/settings.js b/apps/widbt_notify/settings.js index 1e0d5036b..5c67fed7b 100644 --- a/apps/widbt_notify/settings.js +++ b/apps/widbt_notify/settings.js @@ -1,69 +1,57 @@ (function(back) { - var FILE = "widbt_notify.json"; + + var filename = "widbt_notify.json"; + + // set Storage and load settings + var storage = require("Storage"); var settings = Object.assign({ - secondsOnUnlock: false, - }, require('Storage').readJSON(FILE, true) || {}); + showWidget: true, + buzzOnConnect: true, + buzzOnLoss: true, + hideConnected: true, + showMessage: true, + nextBuzz: 30000 + }, storage.readJSON(filename, true) || {}); - function writeSettings() { - require('Storage').writeJSON(FILE, settings); - } - - // Helper method which uses int-based menu item for set of string values - function stringItems(startvalue, writer, values) { + // setup boolean menu entries + function boolEntry(key) { return { - value: (startvalue === undefined ? 0 : values.indexOf(startvalue)), - format: v => values[v], - min: 0, - max: values.length - 1, - wrap: true, - step: 1, + value: settings[key], onchange: v => { - writer(values[v]); - writeSettings(); + // change the value of key + settings[key] = v; + // write to storage + storage.writeJSON(filename, settings); } }; } - // Helper method which breaks string set settings down to local settings object - function stringInSettings(name, values) { - return stringItems(settings[name], v => settings[name] = v, values); - } - - var mainmenu = { + // setup menu + var menu = { "": { "title": "Bluetooth Widget WN" }, "< Back": () => back(), - "Show Widget": { - value: (settings.showWidget !== undefined ? settings.showWidget : true), + "Show Widget": boolEntry("showWidget"), + "Buzz on connect": boolEntry("buzzOnConnect"), + "Buzz on loss": boolEntry("buzzOnLoss"), + "Hide connected": boolEntry("hideConnected"), + "Show Message": boolEntry("showMessage"), + "Next Buzz": { + value: settings.nextBuzz, + step: 1000, + min: 1000, + max: 120000, + wrap: true, + format: v => (v / 1000) + "s", onchange: v => { - settings.showWidget = v; - writeSettings(); + settings.nextBuzz = v; + storage.writeJSON(filename, settings); } - }, - "Buzz on Connect": { - value: (settings.buzzOnConnect !== undefined ? settings.buzzOnConnect : true), - onchange: v => { - settings.buzzOnConnect = v; - writeSettings(); - } - }, - "Buzz on loss": { - value: (settings.buzzOnLoss !== undefined ? settings.buzzOnLoss : true), - onchange: v => { - settings.buzzOnLoss = v; - writeSettings(); - } - }, - "Hide connected": { - value: (settings.hideConnected !== undefined ? settings.hideConnected : false), - onchange: v => { - settings.hideConnected = v; - writeSettings(); - } - } + } }; - E.showMenu(mainmenu); + // draw main menu + E.showMenu(menu); -}); +}) \ No newline at end of file diff --git a/apps/widbt_notify/widget.js b/apps/widbt_notify/widget.js index de2baa3cf..8828d5ca3 100644 --- a/apps/widbt_notify/widget.js +++ b/apps/widbt_notify/widget.js @@ -1,110 +1,87 @@ -WIDGETS.bluetooth_notify = { +(function() { + // load settings + var settings = Object.assign({ + showWidget: true, + buzzOnConnect: true, + buzzOnLoss: true, + hideConnected: true, + showMessage: true, + nextBuzz: 30000 + }, require("Storage").readJSON("widbt_notify.json", true) || {}); + + // setup widget with to hide if not connected and option set + var widWidth = settings.hideConnected && !NRF.getSecurityStatus().connected ? 0 : 15; + + // write widget with loaded settings + WIDGETS.bluetooth_notify = Object.assign(settings, { + + // set area and width area: "tr", - width: 15, + width: widWidth, + + // setup warning status warningEnabled: 1, - // ------------ Settings -------- very lame - need to improve - readshowWidget: function() { - var showWidget; - const SETTINGSFILE = "widbt_notify.json"; - function def (value, def) {return value !== undefined ? value : def;} - var settings = require('Storage').readJSON(SETTINGSFILE, true) || {}; - showWidget = def(settings.showWidget, true); - return showWidget; - }, - - readBuzzOnConnect: function() { - var buzzOnConnect; - const SETTINGSFILE = "widbt_notify.json"; - function def (value, def) {return value !== undefined ? value : def;} - var settings = require('Storage').readJSON(SETTINGSFILE, true) || {}; - buzzOnConnect = def(settings.buzzOnConnect, true); - return buzzOnConnect; - }, - - readBuzzOnLoss: function() { - var buzzOnLoss; - const SETTINGSFILE = "widbt_notify.json"; - function def (value, def) {return value !== undefined ? value : def;} - var settings = require('Storage').readJSON(SETTINGSFILE, true) || {}; - buzzOnLoss = def(settings.buzzOnLoss, true); - return buzzOnLoss; - }, - - readHideConnected: function() { - var hideConnected; - const SETTINGSFILE = "widbt_notify.json"; - function def (value, def) {return value !== undefined ? value : def;} - var settings = require('Storage').readJSON(SETTINGSFILE, true) || {}; - hideConnected = def(settings.hideConnected, true); - return hideConnected; - }, - - - // ------------ Settings -------- - draw: function() { - if (WIDGETS.bluetooth_notify.readshowWidget()){ - g.reset(); - if (NRF.getSecurityStatus().connected) { - if (!WIDGETS.bluetooth_notify.readHideConnected()) { - g.setColor((g.getBPP() > 8) ? "#07f" : (g.theme.dark ? "#0ff" : "#00f")); - g.drawImage(atob("CxQBBgDgFgJgR4jZMawfAcA4D4NYybEYIwTAsBwDAA=="), 2 + this.x, 2 + this.y); - } - } else { - // g.setColor(g.theme.dark ? "#666" : "#999"); - g.setColor("#f00"); // red is easier to distinguish from blue - g.drawImage(atob("CxQBBgDgFgJgR4jZMawfAcA4D4NYybEYIwTAsBwDAA=="), 2 + this.x, 2 + this.y); - } + if (this.showWidget) { + g.reset(); + if (NRF.getSecurityStatus().connected) { + if (!this.hideConnected) { + g.setColor((g.getBPP() > 8) ? "#07f" : (g.theme.dark ? "#0ff" : "#00f")); + g.drawImage(atob("CxQBBgDgFgJgR4jZMawfAcA4D4NYybEYIwTAsBwDAA=="), 2 + this.x, 2 + this.y); + } + } else { + // g.setColor(g.theme.dark ? "#666" : "#999"); + g.setColor("#f00"); // red is easier to distinguish from blue + g.drawImage(atob("CxQBBgDgFgJgR4jZMawfAcA4D4NYybEYIwTAsBwDAA=="), 2 + this.x, 2 + this.y); } + } }, - - redrawCurrentApp: function(){ - if(typeof(draw)=='function'){ - g.clear(); - draw(); - Bangle.loadWidgets(); - Bangle.drawWidgets(); - }else{ - load(); // fallback. This might reset some variables + + redrawCurrentApp: function() { + if (typeof(draw) == 'function') { + g.clear(); + draw(); + Bangle.loadWidgets(); + Bangle.drawWidgets(); + } else { + load(); // fallback. This might reset some variables + } + }, + + onNRF: function(connect) { + if (this.warningEnabled) { + if (this.showMessage) { + E.showMessage( /*LANG*/ 'Connection\n' + (connect ? /*LANG*/ 'restored.' : /*LANG*/ 'lost.'), 'Bluetooth'); + setTimeout(() => { + WIDGETS.bluetooth_notify.redrawCurrentApp(); + }, 3000); // clear message - this will reload the widget, resetting 'warningEnabled'. } - }, - - connect: function() { - if(WIDGETS.bluetooth_notify.warningEnabled == 1){ - E.showMessage(/*LANG*/'Connection\nrestored.', 'Bluetooth'); - setTimeout(()=>{WIDGETS.bluetooth_notify.redrawCurrentApp();}, 3000); // clear message - this will reload the widget, resetting 'warningEnabled'. - - WIDGETS.bluetooth_notify.warningEnabled = 0; - setTimeout('WIDGETS.bluetooth_notify.warningEnabled = 1;', 30000); // don't buzz for the next 30 seconds. - - var quiet = (require('Storage').readJSON('setting.json',1)||{}).quiet; - if(!quiet && WIDGETS.bluetooth_notify.readBuzzOnConnect()){ - Bangle.buzz(700, 1); // buzz on connection resume - } - } - WIDGETS.bluetooth_notify.draw(); + this.warningEnabled = 0; + setTimeout('WIDGETS.bluetooth_notify.warningEnabled = 1;', this.nextBuzz); // don't buzz for the next X seconds. - }, - - disconnect: function() { - if(WIDGETS.bluetooth_notify.warningEnabled == 1){ - E.showMessage(/*LANG*/ 'Connection\nlost.', 'Bluetooth'); - setTimeout(()=>{WIDGETS.bluetooth_notify.redrawCurrentApp();}, 3000); // clear message - this will reload the widget, resetting 'warningEnabled'. - - WIDGETS.bluetooth_notify.warningEnabled = 0; - setTimeout('WIDGETS.bluetooth_notify.warningEnabled = 1;', 30000); // don't buzz for the next 30 seconds. - - var quiet = (require('Storage').readJSON('setting.json',1)||{}).quiet; - if(!quiet && WIDGETS.bluetooth_notify.readBuzzOnLoss()){ - Bangle.buzz(700, 1); // buzz on connection loss - } - } - - WIDGETS.bluetooth_notify.draw(); + var quiet = (require('Storage').readJSON('setting.json', 1) || {}).quiet; + if (!quiet && (connect ? this.buzzOnConnect : this.buzzOnLoss)) { + Bangle.buzz(700, 1); // buzz on connection resume or loss + } + } + // if hideConnected is en- and showMessage disabled, redraw widgets to hide + if (this.hideConnected && !this.showMessage) { + Bangle.drawWidgets(); + } else { + this.draw(); + } } -}; -NRF.on('connect', WIDGETS.bluetooth_notify.connect); -NRF.on('disconnect', WIDGETS.bluetooth_notify.disconnect); + }); + + // clear variables + settings = undefined; + widWidth = undefined; + + // setup bluetooth connection events + NRF.on('connect', (addr) => WIDGETS.bluetooth_notify.onNRF(addr)); + NRF.on('disconnect', () => WIDGETS.bluetooth_notify.onNRF()); + +})() \ No newline at end of file