Skip to content

Commit 2b08378

Browse files
committed
clockinfo 0.24: Only save settings once (not once per clockinfo)
Add .setFocus function to allow a clockinfo to be focused without faking a click on it
1 parent 320ffb8 commit 2b08378

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

apps/clock_info/ChangeLog

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@
2222
0.20: Altitude ClockInfo now uses the distance units set in locale.
2323
0.21: Add haptics when changing infos and a setting to toggle it on or off
2424
0.22: Tapping on step counter now disable step counting on 2v29+ firmwares
25-
0.23: Use filter function for better image borders if it exists (2v22)
25+
0.23: Use filter function for better image borders if it exists (2v22)
26+
0.24: Only save settings once (not once per clockinfo)
27+
Add .setFocus function to allow a clockinfo to be focused without faking a click on it

apps/clock_info/lib.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ clockInfoMenu is the 'options' parameter, with the following added:
235235
* `remove` : function - remove this clockInfo item
236236
* `redraw` : function - force a redraw
237237
* `focus` : bool to show if menu is focused or not
238+
* `setFocus(f)` function - set if we're focussed or not
238239
239240
You can have more than one clock_info at once as well, for instance:
240241
@@ -255,27 +256,21 @@ exports.addInteractive = function(menu, options) {
255256
if ("function" == typeof options) options = {draw:options}; // backwards compatibility
256257
options.index = 0|exports.loadCount;
257258
exports.loadCount = options.index+1;
259+
if (!exports.clockInfos.length)
260+
E.on("kill", exports.save); // only need one save handler
258261
exports.clockInfos[options.index] = options;
259262
options.focus = options.index==0 && options.x===undefined; // focus if we're the first one loaded and no position has been defined
260-
const appName = (options.app||"default")+":"+options.index;
261-
263+
options.appName = (options.app||"default")+":"+options.index;
262264
// load the currently showing clock_infos
263265
let settings = exports.loadSettings();
264-
if (settings.apps[appName]) {
265-
let a = settings.apps[appName].a|0;
266-
let b = settings.apps[appName].b|0;
266+
if (settings.apps[options.appName]) {
267+
let a = settings.apps[options.appName].a|0;
268+
let b = settings.apps[options.appName].b|0;
267269
if (menu[a] && menu[a].items[b]) { // all ok
268270
options.menuA = a;
269271
options.menuB = b;
270272
}
271273
}
272-
const save = () => {
273-
// save the currently showing clock_info
274-
const settings = exports.loadSettings();
275-
settings.apps[appName] = {a:options.menuA, b:options.menuB};
276-
require("Storage").writeJSON("clock_info.json",settings);
277-
};
278-
E.on("kill", save);
279274

280275
if (options.menuA===undefined) options.menuA = 0;
281276
if (options.menuB===undefined) options.menuB = Math.min(exports.loadCount, menu[options.menuA].items.length)-1;
@@ -307,6 +302,7 @@ exports.addInteractive = function(menu, options) {
307302
options.menuB += ud;
308303
if (options.menuB<0) options.menuB = menu[options.menuA].items.length-1;
309304
if (options.menuB>=menu[options.menuA].items.length) options.menuB = 0;
305+
options.modified = true;
310306
} else if (lr) {
311307
if (menu.length==1) return; // 1 item - can't move
312308
oldMenuItem = menu[options.menuA].items[options.menuB];
@@ -324,6 +320,7 @@ exports.addInteractive = function(menu, options) {
324320
while ((options.menuB < menu[options.menuA].items.length-1) &&
325321
exports.clockInfos.some(m => (m!=options) && m.menuA==options.menuA && m.menuB==options.menuB))
326322
options.menuB++;
323+
options.modified = true;
327324
}
328325
if (oldMenuItem) {
329326
menuHideItem(oldMenuItem);
@@ -383,15 +380,16 @@ exports.addInteractive = function(menu, options) {
383380
menuShowItem(menu[options.menuA].items[options.menuB]);
384381
// return an object with info that can be used to remove the info
385382
options.remove = function() {
386-
save();
387-
E.removeListener("kill", save);
383+
exports.save();
388384
Bangle.removeListener("swipe",swipeHandler);
389385
if (touchHandler) Bangle.removeListener("touch",touchHandler);
390386
if (lockHandler) Bangle.removeListener("lock", lockHandler);
391387
Bangle.CLKINFO_FOCUS--;
392388
menuHideItem(menu[options.menuA].items[options.menuB]);
393389
exports.loadCount--;
394390
delete exports.clockInfos[options.index];
391+
if (!exports.clockInfos.length) // if no more clockinfos, no need to save
392+
E.removeListener("kill", exports.save);
395393
// If nothing loaded now, clear our list of loaded menus
396394
if (exports.loadCount==0)
397395
exports.clockInfoMenus = undefined;
@@ -416,12 +414,16 @@ exports.addInteractive = function(menu, options) {
416414

417415
return true;
418416
};
417+
options.setFocus = function(f) {
418+
if (f==options.focus) return;
419+
if (f) focus(); else blur();
420+
};
419421
if (options.focus) focus();
420422
delete settings; // don't keep settings in RAM - save space
421423
return options;
422424
};
423425

424-
/* clockinfos usually return a 24x24 image. This draws that image but
426+
/** clockinfos usually return a 24x24 image. This draws that image but
425427
recolors it such that it is transparent, with the middle of the image as background
426428
and the image itself as foreground. options is passed to g.drawImage */
427429
exports.drawFilledImage = function(img,x,y,options) {
@@ -442,7 +444,7 @@ exports.drawFilledImage = function(img,x,y,options) {
442444
return g.drawImage(gfx, x-scale,y-scale,options);
443445
};
444446

445-
/* clockinfos usually return a 24x24 image. This creates a 26x26 gfx of the image but
447+
/** clockinfos usually return a 24x24 image. This creates a 26x26 gfx of the image but
446448
recolors it such that it is transparent, with the middle and border of the image as background
447449
and the image itself as foreground. options is passed to g.drawImage */
448450
exports.drawBorderedImage = function(img,x,y,options) {
@@ -470,6 +472,17 @@ exports.drawBorderedImage = function(img,x,y,options) {
470472
return g.drawImage(gfx, x-o,y-o,options);
471473
};
472474

475+
/** Save clockinfos if modified */
476+
exports.save = function() {
477+
if (!exports.clockInfos.some(ci=>ci.modified)) return; // all written?
478+
const settings = exports.loadSettings();
479+
exports.clockInfos.forEach(ci => {
480+
settings.apps[ci.appName] = {a:ci.menuA, b:ci.menuB};
481+
ci.modified = false;
482+
});
483+
require("Storage").writeJSON("clock_info.json",settings);
484+
};
485+
473486
// Code for testing (plots all elements from first list)
474487
/*
475488
g.clear();

apps/clock_info/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{ "id": "clock_info",
22
"name": "Clock Info Module",
33
"shortName": "Clock Info",
4-
"version":"0.23",
4+
"version":"0.24",
55
"description": "A library used by clocks to provide extra information on the clock face (Altitude, BPM, etc)",
66
"icon": "app.png",
77
"type": "module",

0 commit comments

Comments
 (0)