Skip to content

Instantly share code, notes, and snippets.

@codeboxed
Created March 5, 2011 20:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeboxed/856711 to your computer and use it in GitHub Desktop.
Save codeboxed/856711 to your computer and use it in GitHub Desktop.
Preferences class for storing/retrieving Titanium.App.Properties
import os.path
def check(mypath):
return os.path.isfile(mypath)
/**
* Class name: Preferences.js
* Author: Codeboxed
* URL: http://www.codeboxed.com
* Date: March 7, 2011
* Platform: Titanium
*/
var Preferences = function() {
// Private
var fileName = 'user.properties';
var file = null;
var properties = null;
var loaded = false;
/**
* Private method used to get the filepath
* @returns String
*/
var getPreferencesFile = function() {
return Titanium.API.application.dataPath + Titanium.Filesystem.getSeparator() + fileName;
};
/**
* Private method used to get the default properties
* @returns Titanium.App.Properties
*/
var getDefaultPreferences = function() {
return Titanium.App.createProperties({
integer: 0,
string: "test",
array: ['a', 'b']
});
};
/**
* Private method used to init the properties
*/
var loadProperties = function() {
file = getPreferencesFile();
if (check(file)) {
properties = Titanium.App.loadProperties(file);
}
// The file does not exists. Create it
if (properties === null) {
properties = getDefaultPreferences();
}
loaded = true;
};
/**
* Private method used to write the properties to the file
*/
var syncProperties = function() {
if (loaded) {
properties.saveTo(file);
}
};
/**
* Private method used to update a property
* @param {string} key The property name
* @param {string} value The property new value
* @returns true if successfull, false otherwise
*/
var updateProperty = function(key, value) {
if (loaded && properties.hasProperty(key)) {
switch (typeof value) {
case 'boolean':
properties.setInt(key, value);
break;
case 'number':
properties.setInt(key, value);
break;
case 'string':
properties.setString(key, value);
break;
case 'object':
properties.setList(key, value);
break;
default:
return false;
}
return true;
}
return false;
};
/**
* Private method used to get a property
* @param {string} key The property name
* @returns object|string|integer if successfull, null otherwise
*/
var getProperty = function(key) {
if (loaded && properties.hasProperty(key)) {
try {
return properties.getString(key);
} catch(err) {
try {
return properties.getInt(key);
} catch(err) {
try {
return properties.getList(key);
} catch(err){
console.log(err);
}
}
}
}
return null;
};
// Public
return {
/**
* Public method to act as a constructor
*/
init: function() {
loadProperties();
},
/**
* Public method used to update a property
* @param {string} key The property name
* @param {string} value The property new value
*/
change: function(key, value) {
updateProperty(key, value);
},
/**
* Public method used to get a property
* @param {string} key The property name
* @returns object|string|integer if successfull, null otherwise
*/
get: function(key) {
return getProperty(key);
},
/**
* Public method used to write the properties to the file
*/
save: function() {
syncProperties();
},
/**
* Private method used to sync the properties
*/
sync: function() {
loadProperties();
}
};
};
// HOW TO USE
var preferences = new Preferences();
preferences.init();
preferences.get('integer');
preferences.change('string', 'string changed');
preferences.save();
preferences.get('string');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment