Skip to content

Instantly share code, notes, and snippets.

@Mindelusions

Mindelusions/app.js

Created Feb 1, 2011
Embed
What would you like to do?
Titanium.UI.setBackgroundColor('#fff');
var tibh = {}; //create app namespace
Ti.include('ui.js','db.js');
tibh.tabGroup = tibh.ui.createApplicationTabGroup();
tibh.tabGroup.open();
(function() {
tibh.db = {};
var dbdata = [{"name":"Jeff Haynie"},{"name":"Nolan Wright"},{"name":"Don Thorp"},{"name":"Marshall Culpepper"},{"name":"Blain Hamon"}];
var db = Ti.Database.open('BountyHunter');
db.execute('CREATE TABLE IF NOT EXISTS fugitives(id INTEGER PRIMARY KEY, name TEXT, captured INTEGER);');
db.close();
tibh.db.list = function(captured) {
var fugitives = [];
var db = Ti.Database.open('BountyHunter');
var result = db.execute('SELECT * FROM fugitives WHERE captured = ? ORDER BY name ASC', (captured) ? 1 : 0);
while (result.isValidRow()) {
fugitives.push({
title: result.fieldByName('name'),
id: result.fieldByName('id'),
hasChild:true,
name: result.fieldByName("name"),
captured: (result.fieldByName("captured") === 0) ? false : true
});
result.next();
}
result.close();
db.close();
return fugitives;
};
tibh.db.add = function(_name) {
var db = Ti.Database.open('BountyHunter');
db.execute("INSERT INTO fugitives(name,captured) VALUES(?,?)",_name,0);
db.close();
Ti.App.fireEvent("DBUpdated");
};
tibh.db.del = function(_id) {
var db = Ti.Database.open('BountyHunter');
db.execute("DELETE FROM fugitives WHERE id = ?",_id);
db.close();
Ti.App.fireEvent("DBUpdated");
};
tibh.db.capture = function(_id) {
var db = Ti.Database.open('BountyHunter');
db.execute("UPDATE fugitives SET captured = 1 WHERE id = ?",_id);
db.close();
Ti.App.fireEvent("DBUpdated");
};
if (!Ti.App.Properties.hasProperty('popd')) {
for (var i = 0;i<dbdata.length;i++) {
tibh.db.add(dbdata[i].name);
}
Ti.App.Properties.setString('popd','true');
}
})();
(function() {
tibh.ui = {};
tibh.ui.createAddWindow = function() {
var win = Ti.UI.createWindow({
title:'Add 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:'Full 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.capture(_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('DBUpdated', 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