Wednesday, January 12, 2011

Application Settings

Setting up application settings is really straight forward with Titanium.

For my app I give the users the option of include full quotations or abstract single words.  For one set of clients the quotations are most relevant whereas another set of clients abstract words would be helpful.

Settings Screen

I wanted the user to be able to pick what gets included in the results: quotations only, abstract words only, or a mix.  As such, I created this settings screen:






Originally I wanted to go with a TabbedBar object with three options of "Quotations" - "Mixed" - "Abstracts" but then I found out the tabbed bar does not work with Android. As the whole point of this app is to work on both iPhone and Android I had to quickly ditch the tabbed bar.  If you look on the top right hand corner of the API documentation you'll see icons for what platform the page you're looking at applies to - I missed this initially.  You can see for the Tabbed Bar it only works on Apple (the apple icon and .08):


Settings

In my app.js file I created two global variables for the app settings. I then included my appSettings.js file where I'm trying to keep all my app settings code. I've found in other projects the more modular the program the easier it is to maintain and follow.

// app settings
var includeQuotations = true;
var includeAbstractWords = true;

Ti.include("appSettings.js"); 

Inside the appSettings.js file I have the following code which was very intuitive:

if(Ti.App.Properties.hasProperty("includeQuotations")) {
    includeQuotations = Ti.App.Properties.getBool("includeQuotations");
} else {
    Ti.App.Properties.setBool("includeQuotations",true);
}

if(Ti.App.Properties.hasProperty("includeAbstractWords")) {
    includeAbstractWords = Ti.App.Properties.getBool("includeAbstractWords");
} else {
    Ti.App.Properties.setBool("includeAbstractWords",true);
}


I first check whether or not the setting exists - if not, create it with a default value of true.

If the setting does exist, set the global variable to the value the user has selected. I'm not sure what happens if you try and get a property that does not exist with the getBool method but I'm sure it's not what is expected. 

You can then use the two app setting vars anywhere in your program where you need to get application preferences.

No comments:

Post a Comment