What is the best practice to upgrade properties?
I was stupid enough to publish my app with long properties, like "showSeparator" so I refactored it to have much sorter property names like "ss". But since my app already has more than 1000 downloads I thought it's worth to add some code to "upgrade" the properties. So I thought I'll do something like:
function initialize() {
AppBase.initialize();
updateProps();
}
function updateProps() {
if (getConfig("version") != null) {
renameProp("showIntHr", "si");
renameProp("showSeparator", "ss");
renameProp("intHrSide", "is");
renameProp("intHrPrefix", "ib");
renameProp("intHrPostfix", "ia");
renameProp("extHrPrefix", "eb");
renameProp("extHrPostfix", "ea");
deleteConfig("version");
}
}
function renameProp(oldKey as String, newKey as String) {
var val = getConfig(oldKey);
if (val != null) {
setConfig(newKey, val);
}
deleteConfig(oldKey);
}
function deleteConfig(key as Application.PropertyKeyType) {
if (App has :Storage) {
// use Application.Storage and Application.Properties methods
value = App.Properties.deleteValue(key);
} else {
// use Application.AppBase methods
value = App.AppBase.deleteProperty(key);
}
}
function setConfig(key as Application.PropertyKeyType, val as Application.PropertyValueType) {
var value = null;
if (App has :Storage) {
// use Application.Storage and Application.Properties methods
value = App.Properties.setValue(key, val);
} else {
// use Application.AppBase methods
value = App.AppBase.getProperty(key, val);
}
}
function getConfig(key as Application.PropertyKeyType, defaultValue) {
var value = null;
if (App has :Storage) {
// use Application.Storage and Application.Properties methods
value = App.Properties.getValue(key);
} else {
// use Application.AppBase methods
value = App.AppBase.getProperty(key);
}
if (value == null) {
value = defaultValue;
}
return value;
}
And after a few more releases (or when I'll have code size problems) I'll delete the above code.
But then I saw this in the documentation:
1. there's no way to delete a property in newer CIQ: Toybox.Application.Properties only has getValue, setValue, but no deleteValue. I tried to use Toybox.Application.Storage.deleteValue instead, but it's not removing the old property. Should I use Application.Properties.setValue(key, null) to remove it?
2. If I remove the old properties from properties.xml then I'm not able to read the old properties, because of: Application.Properties.getValue() : (Properties.InvalidKeyException) — Thrown if key does not exist in Application Properties. Do I need to keep the old properties in properties.xml?
(Garmin, for some reason I am not able to insert the the code in the above 2 boxes when I tried to do it in 1 box, and it's not because of it's length....)