mirror of https://github.com/espruino/BangleApps
Settings library: use `appid.json`, update README.md and sanitycheck.js
parent
b44df86f27
commit
efd12d627a
23
README.md
23
README.md
|
@ -377,40 +377,37 @@ that handles configuring the app.
|
||||||
When the app settings are opened, this function is called with one
|
When the app settings are opened, this function is called with one
|
||||||
argument, `back`: a callback to return to the settings menu.
|
argument, `back`: a callback to return to the settings menu.
|
||||||
|
|
||||||
Usually it will save any information in `app.json` where `app` is the name
|
Usually it will save any information in `myappid.json` where `myappid` is the name
|
||||||
of your app - so you should change the example accordingly.
|
of your app - so you should change the example accordingly.
|
||||||
|
|
||||||
Example `settings.js`
|
Example `settings.js`
|
||||||
```js
|
```js
|
||||||
// make sure to enclose the function in parentheses
|
// make sure to enclose the function in parentheses
|
||||||
(function(back) {
|
(function(back) {
|
||||||
let settings = require('Storage').readJSON('app.json',1)||{};
|
function get(key, def) { return require('Settings').get('myappid', key, def); }
|
||||||
function save(key, value) {
|
function set(key, value) { require('Settings').set('myappid', key, value); }
|
||||||
settings[key] = value;
|
|
||||||
require('Storage').write('app.json',settings);
|
|
||||||
}
|
|
||||||
const appMenu = {
|
const appMenu = {
|
||||||
'': {'title': 'App Settings'},
|
'': {'title': 'App Settings'},
|
||||||
'< Back': back,
|
'< Back': back,
|
||||||
'Monkeys': {
|
'Monkeys': {
|
||||||
value: settings.monkeys||12,
|
value: get('monkeys', 12),
|
||||||
onchange: (m) => {save('monkeys', m)}
|
onchange: (m) => set('monkeys', m)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
E.showMenu(appMenu)
|
E.showMenu(appMenu)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
In this example the app needs to add `app.settings.js` to `storage` in `apps.json`.
|
In this example the app needs to add `myappid.settings.js` to `storage` in `apps.json`.
|
||||||
It should also add `app.json` to `data`, to make sure it is cleaned up when the app is uninstalled.
|
It should also add `myappid.json` to `data`, to make sure it is cleaned up when the app is uninstalled.
|
||||||
```json
|
```json
|
||||||
{ "id": "app",
|
{ "id": "myappid",
|
||||||
...
|
...
|
||||||
"storage": [
|
"storage": [
|
||||||
...
|
...
|
||||||
{"name":"app.settings.js","url":"settings.js"},
|
{"name":"myappid.settings.js","url":"settings.js"}
|
||||||
],
|
],
|
||||||
"data": [
|
"data": [
|
||||||
{"name":"app.json"}
|
{"name":"myappid.json"}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
```
|
```
|
||||||
|
|
|
@ -209,6 +209,8 @@ apps.forEach((app,appIdx) => {
|
||||||
// prefer "appid.json" over "appid.settings.json" (TODO: change to ERROR once all apps comply?)
|
// prefer "appid.json" over "appid.settings.json" (TODO: change to ERROR once all apps comply?)
|
||||||
if (dataNames.includes(app.id+".settings.json") && !dataNames.includes(app.id+".json"))
|
if (dataNames.includes(app.id+".settings.json") && !dataNames.includes(app.id+".json"))
|
||||||
WARN(`App ${app.id} uses data file ${app.id+'.settings.json'} instead of ${app.id+'.json'}`)
|
WARN(`App ${app.id} uses data file ${app.id+'.settings.json'} instead of ${app.id+'.json'}`)
|
||||||
|
else if (dataNames.includes(app.id+".settings.json"))
|
||||||
|
WARN(`App ${app.id} uses data file ${app.id+'.settings.json'}`)
|
||||||
// settings files should be listed under data, not storage (TODO: change to ERROR once all apps comply?)
|
// settings files should be listed under data, not storage (TODO: change to ERROR once all apps comply?)
|
||||||
if (fileNames.includes(app.id+".settings.json"))
|
if (fileNames.includes(app.id+".settings.json"))
|
||||||
WARN(`App ${app.id} uses storage file ${app.id+'.settings.json'} instead of data file`)
|
WARN(`App ${app.id} uses storage file ${app.id+'.settings.json'} instead of data file`)
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
/*
|
/*
|
||||||
|
- Read/write app settings, stored in <appid>.json
|
||||||
|
- Read/write global settings (stored in setting.json)
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
|
|
||||||
```
|
```
|
||||||
// read a single app setting
|
// read a single app setting
|
||||||
value = require('Settings').get(appname, key, default);
|
value = require('Settings').get(appid, key, default);
|
||||||
// omit key to read all app settings
|
// omit key to read all app settings
|
||||||
value = require('Settings').get();
|
value = require('Settings').get();
|
||||||
// write a single app setting
|
// write a single app setting
|
||||||
require('Settings').set(appname, key, value)
|
require('Settings').set(appid, key, value)
|
||||||
// omit key and pass an object as values to overwrite all settings
|
// omit key and pass an object as values to overwrite all settings
|
||||||
require('Settings').set(appname, values)
|
require('Settings').set(appid, values)
|
||||||
|
|
||||||
// read Bangle settings by passing the Bangle object instead of an app name
|
// read Bangle settings by passing the Bangle object instead of an app name
|
||||||
value = require('Settings').get(Bangle, key, default);
|
value = require('Settings').get(Bangle, key, default);
|
||||||
|
@ -22,9 +23,9 @@ require('Settings').set(Bangle, key, value)
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
```
|
```
|
||||||
require('Settings').set('test', 'foo', 123); // writes to 'test.settings.json'
|
require('Settings').set('test', 'foo', 123); // writes to 'test.json'
|
||||||
require('Settings').set('test', 'bar', 456); // updates 'test.settings.json'
|
require('Settings').set('test', 'bar', 456); // updates 'test.json'
|
||||||
// 'test.settings.json' now contains {baz:123,bam:456}
|
// 'test.json' now contains {baz:123,bam:456}
|
||||||
baz = require('Settings').get('test', 'foo'); // baz = 123
|
baz = require('Settings').get('test', 'foo'); // baz = 123
|
||||||
def = require('Settings').get('test', 'jkl', 789); // def = 789
|
def = require('Settings').get('test', 'jkl', 789); // def = 789
|
||||||
all = require('Settings').get('test'); // all = {foo: 123, bar: 456}
|
all = require('Settings').get('test'); // all = {foo: 123, bar: 456}
|
||||||
|
@ -34,7 +35,7 @@ baz = require('Settings').get('test', 'baz'); // baz = undefined
|
||||||
vibrate = require('Settings').get(Bangle, 'vibrate', true);
|
vibrate = require('Settings').get(Bangle, 'vibrate', true);
|
||||||
|
|
||||||
// Hint: if your app reads multiple settings, you can create a helper function:
|
// Hint: if your app reads multiple settings, you can create a helper function:
|
||||||
function s(key, def){return require('Settings').get('myapp', key, def);}
|
function s(key, def) { return require('Settings').get('myapp', key, def); }
|
||||||
var foo = s('foo setting', 'default value'), bar = s('bar setting');
|
var foo = s('foo setting', 'default value'), bar = s('bar setting');
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -54,10 +55,7 @@ function get(file, key, def) {
|
||||||
// get(file) or get(file, def): get all settings
|
// get(file) or get(file, def): get all settings
|
||||||
return (s!==undefined) ? s : key;
|
return (s!==undefined) ? s : key;
|
||||||
}
|
}
|
||||||
if (typeof s!=="object" || !(key in s)) {
|
return ((typeof s==="object") && (key in s)) ? s[key] : def;
|
||||||
return def;
|
|
||||||
}
|
|
||||||
return s[key];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -74,9 +72,7 @@ function set(file, key, value) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var s = require("Storage").readJSON(file, 1);
|
var s = require("Storage").readJSON(file, 1);
|
||||||
if (typeof s!=="object") {
|
if (typeof s!=="object") s = {};
|
||||||
s = {};
|
|
||||||
}
|
|
||||||
s[key] = value;
|
s[key] = value;
|
||||||
require("Storage").write(file, s);
|
require("Storage").write(file, s);
|
||||||
}
|
}
|
||||||
|
@ -90,7 +86,7 @@ function set(file, key, value) {
|
||||||
* @return {*} Setting value (or default if not found)
|
* @return {*} Setting value (or default if not found)
|
||||||
*/
|
*/
|
||||||
exports.get = function(app, key, def) {
|
exports.get = function(app, key, def) {
|
||||||
return get((app===Bangle) ? setting.json : app+".settings.json", key, def);
|
return get((app===Bangle) ? 'setting.json' : app+".json", key, def);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -101,5 +97,5 @@ exports.get = function(app, key, def) {
|
||||||
* @param {*} val Value to store
|
* @param {*} val Value to store
|
||||||
*/
|
*/
|
||||||
exports.set = function(app, key, val) {
|
exports.set = function(app, key, val) {
|
||||||
set((app===Bangle) ? setting.json : app+".settings.json", key, val);
|
set((app===Bangle) ? 'setting.json' : app+".json", key, val);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue