CF1 = (app.getProperty("CF_1")!= null && !app.getProperty("CF_1").equals("")) ? app.getProperty("CF_1") : 999 ;
just to check if the usersettings portion of things is null or blank. This is taking up quite a lot of code space.
CF1 = (app.getProperty("CF_1")!= null && !app.getProperty("CF_1").equals("")) ? app.getProperty("CF_1") : 999 ;
I've got all of my settings access going through a single class...module Settings {
using Toybox.Application as App;
using Toybox.Lang as Lang;
function getProperty(key, value) {
var val = App.getApp().getProperty(key);
if (val == null) {
return value; // return the default
}
return val;
}
function setProperty(key, value) {
App.getApp().setProperty(key, value);
}
}
This does add a function call to get the app (this could be resolved easily), but it has the advantage that I don't have to pass the app around all of the time, and I get a single point of entry for accessing the object store.
If you extended that, you'd only need to add the check in one place...function getStringProperty(key, dflt) {
assert(dflt instanceof Lang.String);
var val = App.getApp().getProperty(key);
if (val != null && val instanceof Lang.String && !"".equals(val)) {
return val;
}
return dflt;
}
function getBooleanProperty(key, dflt) {
return getTypedProperty(key, dflt, Lang.Boolean);
}
function getNumberProperty(key, dflt) {
return getTypedProperty(key, dflt, Lang.Number);
}
function getFloatProperty(key, dflt) {
return getFloatProperty(key, dflt, Lang.Float);
}
function getDoubleProperty(key, dflt) {
return getTypedProperty(key, dflt, Lang.Double);
}
//...
// I'm not sure this works, but I believe it should
hidden function getTypedProperty(key, dflt, type) {
assert(dflt instanceof type);
var val = App.getApp().getProperty(key);
if (val instanceof type) {
return val;
}
return dflt;
}
Assuming you can use this, now you just write...CF1 = Settings.getNumberProperty("CF_1", 999);