mirror of https://github.com/espruino/BangleApps
ClockFace: add settings support
parent
ac5c80ff38
commit
ecd5868821
|
@ -10,7 +10,8 @@ function ClockFace(options) {
|
||||||
"precision",
|
"precision",
|
||||||
"init", "draw", "update",
|
"init", "draw", "update",
|
||||||
"pause", "resume",
|
"pause", "resume",
|
||||||
"up", "down", "upDown"
|
"up", "down", "upDown",
|
||||||
|
"settingsFile",
|
||||||
].includes(k)) throw `Invalid ClockFace option: ${k}`;
|
].includes(k)) throw `Invalid ClockFace option: ${k}`;
|
||||||
});
|
});
|
||||||
if (!options.draw && !options.update) throw "ClockFace needs at least one of draw() or update() functions";
|
if (!options.draw && !options.update) throw "ClockFace needs at least one of draw() or update() functions";
|
||||||
|
@ -33,7 +34,18 @@ function ClockFace(options) {
|
||||||
};
|
};
|
||||||
if (options.upDown) this._upDown = options.upDown;
|
if (options.upDown) this._upDown = options.upDown;
|
||||||
|
|
||||||
this.is12Hour = !!(require("Storage").readJSON("setting.json", 1) || {})["12hour"];
|
if (options.settingsFile) {
|
||||||
|
const settings = (require("Storage").readJSON(options.settingsFile, true) || {});
|
||||||
|
Object.keys(settings).forEach(k => {
|
||||||
|
this[k] = settings[k];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// these default to true
|
||||||
|
["showDate", "loadWidgets"].forEach(k => {
|
||||||
|
if (this[k]===undefined) this[k] = true;
|
||||||
|
});
|
||||||
|
// use global 24/12-hour setting if not set by clock-settings
|
||||||
|
if (!('is12Hour' in this)) this.is12Hour = !!(require("Storage").readJSON("setting.json", true) || {})["12hour"];
|
||||||
}
|
}
|
||||||
|
|
||||||
ClockFace.prototype.tick = function() {
|
ClockFace.prototype.tick = function() {
|
||||||
|
@ -46,7 +58,7 @@ ClockFace.prototype.tick = function() {
|
||||||
};
|
};
|
||||||
if (!this._last) {
|
if (!this._last) {
|
||||||
g.clear(true);
|
g.clear(true);
|
||||||
Bangle.drawWidgets();
|
if (global.WIDGETS) Bangle.drawWidgets();
|
||||||
g.reset();
|
g.reset();
|
||||||
this.draw.apply(this, [time, {d: true, h: true, m: true, s: true}]);
|
this.draw.apply(this, [time, {d: true, h: true, m: true, s: true}]);
|
||||||
} else {
|
} else {
|
||||||
|
@ -70,7 +82,7 @@ ClockFace.prototype.start = function() {
|
||||||
.CLOCK is set by Bangle.setUI('clock') but we want to load widgets so we can check appRect and *then*
|
.CLOCK is set by Bangle.setUI('clock') but we want to load widgets so we can check appRect and *then*
|
||||||
call setUI. see #1864 */
|
call setUI. see #1864 */
|
||||||
Bangle.CLOCK = 1;
|
Bangle.CLOCK = 1;
|
||||||
Bangle.loadWidgets();
|
if (this.loadWidgets) Bangle.loadWidgets();
|
||||||
if (this.init) this.init.apply(this);
|
if (this.init) this.init.apply(this);
|
||||||
if (this._upDown) Bangle.setUI("clockupdown", d=>this._upDown.apply(this,[d]));
|
if (this._upDown) Bangle.setUI("clockupdown", d=>this._upDown.apply(this,[d]));
|
||||||
else Bangle.setUI("clock");
|
else Bangle.setUI("clock");
|
||||||
|
|
|
@ -85,6 +85,7 @@ var clock = new ClockFace({
|
||||||
if (dir === -1) // Up
|
if (dir === -1) // Up
|
||||||
else // (dir === 1): Down
|
else // (dir === 1): Down
|
||||||
},
|
},
|
||||||
|
settingsFile: 'appid.settings.json', // optional, values from file will be applied to `this`
|
||||||
});
|
});
|
||||||
clock.start();
|
clock.start();
|
||||||
|
|
||||||
|
@ -110,11 +111,51 @@ clock.start();
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
SettingsFile
|
||||||
|
------------
|
||||||
|
If you use the `settingsFile` option, values from that file are loaded and set
|
||||||
|
directly on the clock.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
```json
|
||||||
|
// example.settings.json:
|
||||||
|
{
|
||||||
|
"showDate": false,
|
||||||
|
"foo": 123
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```js
|
||||||
|
var ClockFace = require("ClockFace");
|
||||||
|
var clock = new ClockFace({
|
||||||
|
draw: function(){/*...*/},
|
||||||
|
settingsFile: "example.settings.json",
|
||||||
|
});
|
||||||
|
// now
|
||||||
|
clock.showDate === false;
|
||||||
|
clock.foo === 123;
|
||||||
|
clock.loadWidgets === true; // default when not in settings file
|
||||||
|
clock.is12Hour === ??; // not in settings file: uses global setting
|
||||||
|
clock.start();
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
Properties
|
Properties
|
||||||
----------
|
----------
|
||||||
The following properties are automatically set on the clock:
|
The following properties are automatically set on the clock:
|
||||||
* `is12Hour`: `true` if the "Time Format" setting is set to "12h", `false` for "24h".
|
* `is12Hour`: `true` if the "Time Format" setting is set to "12h", `false` for "24h".
|
||||||
* `paused`: `true` while the clock is paused. (You don't need to check this inside your `draw()` code)
|
* `paused`: `true` while the clock is paused. (You don't need to check this inside your `draw()` code)
|
||||||
|
* `showDate`: `true` (if not overridden through the settings file.)
|
||||||
|
* `loadWidgets`: `true` (if not overridden through the settings file.)
|
||||||
|
If set to `false` before calling `start()`, the clock won't call `Bangle.loadWidgets();` for you.
|
||||||
|
Best is to add a setting for this, but if you never want to load widgets, you could do this:
|
||||||
|
```js
|
||||||
|
var ClockFace = require("ClockFace");
|
||||||
|
var clock = new ClockFace({draw: function(){/*...*/}});
|
||||||
|
clock.loadWidgets = false; // prevent loading of widgets
|
||||||
|
clock.start();
|
||||||
|
```
|
||||||
|
|
||||||
Inside the `draw()`/`update()` function you can access these using `this`:
|
Inside the `draw()`/`update()` function you can access these using `this`:
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
// boolean options, which default to true
|
||||||
|
exports.showDate =
|
||||||
|
exports.loadWidgets =
|
||||||
|
function(value, callback) {
|
||||||
|
if (value === undefined) value = true;
|
||||||
|
return {
|
||||||
|
value: !!value,
|
||||||
|
onchange: v=>callback(v),
|
||||||
|
};
|
||||||
|
};
|
Loading…
Reference in New Issue