Skip to content

Instantly share code, notes, and snippets.

@kwhinnery
Created February 1, 2011 16:33
Show Gist options
  • Save kwhinnery/806107 to your computer and use it in GitHub Desktop.
Save kwhinnery/806107 to your computer and use it in GitHub Desktop.
Titanium.UI.setBackgroundColor('#fff');
var tibh = {}; //create app namespace
Ti.include('ui.js','network.js','db.js');
tibh.tabGroup = tibh.ui.createApplicationTabGroup();
tibh.tabGroup.open();
(function() {
tibh.db = {};
//bootstrap database
var db = Ti.Database.open('TiBountyHunter');
db.execute('CREATE TABLE IF NOT EXISTS fugitives(id INTEGER PRIMARY KEY, name TEXT, captured INTEGER);');
db.close();
tibh.db.list = function(_captured) {
var fugitiveList = [];
var db = Ti.Database.open('TiBountyHunter');
var result = db.execute('SELECT * FROM fugitives WHERE captured = ? ORDER BY name ASC', (_captured) ? 1 : 0);
while (result.isValidRow()) {
fugitiveList.push({
//add these attributes for the benefit of a table view
title: result.fieldByName('name'),
id: result.fieldByName('id'), //custom data attribute to pass to detail page
hasChild:true,
//add actual db fields
name: result.fieldByName("name"),
captured: (result.fieldByName("captured") === 0) ? false : true
});
result.next();
}
result.close(); //make sure to close the result set
db.close();
return fugitiveList;
};
tibh.db.add = function(_name) {
var db = Ti.Database.open('TiBountyHunter');
db.execute("INSERT INTO fugitives(name,captured) VALUES(?,?)",_name,0);
db.close();
//Dispatch a message to let others know the database has been updated
Ti.App.fireEvent("databaseUpdated");
};
tibh.db.del = function(_id) {
var db = Ti.Database.open('TiBountyHunter');
db.execute("DELETE FROM fugitives WHERE id = ?",_id);
db.close();
//Dispatch a message to let others know the database has been updated
Ti.App.fireEvent("databaseUpdated");
};
tibh.db.bust = function(_id) {
var db = Ti.Database.open('TiBountyHunter');
db.execute("UPDATE fugitives SET captured = 1 WHERE id = ?",_id);
db.close();
//Dispatch a message to let others know the database has been updated
Ti.App.fireEvent("databaseUpdated");
};
//determine if the database needs to be seeded
if (!Ti.App.Properties.hasProperty('seeded')) {
tibh.net.getFugitives(function(data) {
for (var i = 0;i<data.length;i++) {
tibh.db.add(data[i].name);
}
});
Ti.App.Properties.setString('seeded','yuppers');
}
})();
(function() {
tibh.net = {};
tibh.net.getFugitives = function(_cb) {
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function() {
_cb(JSON.parse(this.responseText));
};
xhr.open("GET","http://bountyhunterapp.appspot.com/bounties");
xhr.send();
};
})();
(function() {
tibh.ui = {};
tibh.ui.createAddWindow = function() {
var win = Ti.UI.createWindow({
title:'New Fugitive',
layout:'vertical',
modal:true,
fullscreen:(Ti.Platform.osname == 'android'),
backgroundColor:'#fff'
});
if (Ti.Platform.osname === 'iphone') {
var b = Titanium.UI.createButton({
title:'Close',
style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN
});
b.addEventListener('click',function() {
win.close();
});
win.setRightNavButton(b);
}
var tf = Ti.UI.createTextField({
height:40,
top:10,
width:250,
keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
returnKeyType:Titanium.UI.RETURNKEY_DONE,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED,
hintText:'Fugitive Name'
});
win.add(tf);
var save = Ti.UI.createButton({
title:"Save",
height:40,
width:80,
top:10
});
save.addEventListener('click', function() {
tibh.db.add(tf.value);
win.close();
});
win.add(save);
return win;
};
tibh.ui.createDetailWindow = function(/*Object*/ _bounty) {
var win = Ti.UI.createWindow({
title:_bounty.title,
layout:'vertical'
});
win.add(Ti.UI.createLabel({
text:(_bounty.captured) ? 'Busted!' : 'Still At Large',
top:10,
textAlign:'center',
font: {
fontWeight:'bold',
fontSize:18
},
height:'auto'
}));
if (!_bounty.captured) {
var captureButton = Ti.UI.createButton({
title:'Capture',
top:10,
height:40,
width:200
});
captureButton.addEventListener('click', function() {
tibh.db.bust(_bounty.id);
win.close();
});
win.add(captureButton);
}
var deleteButton = Ti.UI.createButton({
title:'Delete',
top:10,
height:40,
width:200
});
deleteButton.addEventListener('click', function() {
tibh.db.del(_bounty.id);
win.close();
});
win.add(deleteButton);
return win;
};
tibh.ui.createBountyTableView = function(/*Boolean*/ _captured) {
var tv = Ti.UI.createTableView();
tv.addEventListener('click', function(_e) {
var tab = (_captured) ? tibh.capturedTab : tibh.atLargeTab;
tab.open(tibh.ui.createDetailWindow(_e.rowData));
});
function populateData() {
var results = tibh.db.list(_captured);
tv.setData(results);
}
Ti.App.addEventListener('databaseUpdated', populateData);
//run initial query
populateData();
return tv;
};
tibh.ui.createBountyWindow = function(/*Boolean*/ _captured) {
var win = Titanium.UI.createWindow({
title: (_captured) ? 'Captured' : 'At Large',
activity : {
onCreateOptionsMenu : function(e) {
var menu = e.menu;
var m1 = menu.add({ title : 'Add' });
m1.addEventListener('click', function(e) {
tibh.ui.createAddWindow().open();
});
}
}
});
win.add(tibh.ui.createBountyTableView(_captured));
if (Ti.Platform.osname === 'iphone') {
var b = Titanium.UI.createButton({
title:'Add',
style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN
});
b.addEventListener('click',function() {
tibh.ui.createAddWindow().open();
});
win.setRightNavButton(b);
}
return win;
};
tibh.ui.createApplicationTabGroup = function() {
var tabGroup = Titanium.UI.createTabGroup();
var atLarge = tibh.ui.createBountyWindow(false);
var captured = tibh.ui.createBountyWindow(true);
tibh.atLargeTab = Titanium.UI.createTab({
title: 'At Large',
window: atLarge
});
tibh.capturedTab = Titanium.UI.createTab({
title: 'Captured',
window: captured
});
tabGroup.addTab(tibh.atLargeTab);
tabGroup.addTab(tibh.capturedTab);
return tabGroup;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment