[widbt_notify] Simplify code, add some options

pull/2134/head
storm64 2022-09-14 21:53:04 +02:00
parent 674ef0aa13
commit a1e03d3229
4 changed files with 119 additions and 153 deletions

View File

@ -12,3 +12,4 @@
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
0.16: Simplify code, add option to disable displaying a message

View File

@ -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",

View File

@ -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();
}
},
"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();
settings.nextBuzz = v;
storage.writeJSON(filename, settings);
}
}
};
E.showMenu(mainmenu);
// draw main menu
E.showMenu(menu);
});
})

View File

@ -1,53 +1,32 @@
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()){
if (this.showWidget) {
g.reset();
if (NRF.getSecurityStatus().connected) {
if (!WIDGETS.bluetooth_notify.readHideConnected()) {
if (!this.hideConnected) {
g.setColor((g.getBPP() > 8) ? "#07f" : (g.theme.dark ? "#0ff" : "#00f"));
g.drawImage(atob("CxQBBgDgFgJgR4jZMawfAcA4D4NYybEYIwTAsBwDAA=="), 2 + this.x, 2 + this.y);
}
@ -59,52 +38,50 @@ WIDGETS.bluetooth_notify = {
}
},
redrawCurrentApp: function(){
if(typeof(draw)=='function'){
redrawCurrentApp: function() {
if (typeof(draw) == 'function') {
g.clear();
draw();
Bangle.loadWidgets();
Bangle.drawWidgets();
}else{
} else {
load(); // fallback. This might reset some variables
}
},
connect: function() {
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'.
}
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'.
this.warningEnabled = 0;
setTimeout('WIDGETS.bluetooth_notify.warningEnabled = 1;', this.nextBuzz); // don't buzz for the next X seconds.
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
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
}
}
WIDGETS.bluetooth_notify.draw();
},
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
// if hideConnected is en- and showMessage disabled, redraw widgets to hide
if (this.hideConnected && !this.showMessage) {
Bangle.drawWidgets();
} else {
this.draw();
}
}
WIDGETS.bluetooth_notify.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());
})()