Skip to content

Commit

Permalink
Database abstraction and Timer class implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kardun committed Apr 20, 2011
1 parent 038e835 commit 3deca3e
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 8 deletions.
Binary file added Resources/default_app_logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions Resources/main_windows/db/db.js
@@ -0,0 +1,52 @@
Ti.include("../helpers/utils.js");

// Database constructor
function Db(){
this.db = Titanium.Database.open("timersdb");
this.db.execute('CREATE TABLE IF NOT EXISTS timers (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, intial_date DATE, final_date DATE)');
}

// Basic CRUD operations
Db.prototype.save = function(timer){
var sql = "INSERT INTO timers(name, initial_date, final_date) VALUES('{timer.name}', '{timer.initialDate}', '{timer.finalDate}')".supplant(timer);
this.db.execute(sql);
}

Db.prototype.remove = function(id){
var sql = "DELETE FROM timers WHERE id = {id}".supplant({'id': id});
this.db.execute(sql);
}

Db.prototype.update = function(timer){
var sql = "UPDATE timers SET name = '{name}', initial_date = '{initialDate}', final_date = '{finalDate}' where id = {id}".supplant(timer);
this.db.execute(sql);
}

Db.prototype.list = function(){
var sql = 'SELECT * FROM timers';
var cursor = this.db.execute(sql);
var timers = [];
while(cursor.isValidRow()){
var timer = populateTimerObject(cursor);
}
cursor.close();
return timers;
}

Db.prototype.get = function(id){
var sql = "SELECT * FROM timers WHERE id = {id}".supplant({'id':id});
var cursor = this.db.execute(sql);
var timer = null;
if(cursor.isValidRow()){
timer = populateTimerObject(cursor);
}
cursor.close();
return timer;
}

// Helper function to populate a new Timer object from a database cursor
function populateTimerObject(cursor){
var timer = new Timer(cursor.fieldByName('name'), cursor.fieldByName('initial_date'), cursor.fielByName('final_date'));
timer.id = cursor.field(0);
return timer;
}
20 changes: 20 additions & 0 deletions Resources/main_windows/helpers/utils.js
@@ -0,0 +1,20 @@
var isDate = function(obj){
return !!(obj && obj.getTimezoneOffset && obj.setUTCFullYear);
}

Date.prototype.strftime = function(){
function _zero(num){return (num < 10 ) ? "0"+num : ""+num };
return [this.getFullYear()+"", _zero(this.getMonth()+1), _zero(this.getDate()) ].join('-') + " " +
[this.getHours(), this.getMinutes(), this.getSeconds()].join(':')
}

if (!String.prototype.supplant) {
String.prototype.supplant = function (o) {
return this.replace(/{([^{}]*)}/g,
function (a, b) {
var r = o[b];
return isDate(o[b]) ? o[b].strftime(): r.toString();
}
);
};
}
20 changes: 20 additions & 0 deletions Resources/main_windows/model/timer.js
@@ -0,0 +1,20 @@
// Timer Object

// Constructor
function Timer(name, initialDate, finalDate){
this.name = name;
this.initialDate = initialDate;
this.finalDate = finalDate;
this.id = null;
}

// Check if a current date falls in the range
// of initial and final date
Timer.prototype.isValidDate = function(date){
// TODO: Validate that 'date' object is Date
return this.initialDate <= date <= this.finalDate
}

Timer.prototype.toString = function(){
return 'Timer['+this.name+'] = <'+this.initialDate+'><'+this.finalDate+'>'
}
16 changes: 8 additions & 8 deletions tiapp.xml
Expand Up @@ -5,14 +5,14 @@
<target device="ipad">false</target>
<target device="android">true</target>
</deployment-targets>
<id>com.flockonuskaos12.multitimer</id>
<name>MultiTimer</name>
<version>1.0</version>
<publisher>nos2011</publisher>
<url>http://flockonus.github.com</url>
<description>not specified</description>
<copyright>2011 by nos</copyright>
<icon>appicon.png</icon>
<id>com.flockonuskaos12.multitimer</id>
<name>MultiTimer</name>
<version>1.0</version>
<publisher>nos2011</publisher>
<url>http://flockonus.github.com</url>
<description>not specified</description>
<copyright>2011 by nos</copyright>
<icon>default_app_logo.png</icon>
<persistent-wifi>false</persistent-wifi>
<prerendered-icon>false</prerendered-icon>
<statusbar-style>default</statusbar-style>
Expand Down

0 comments on commit 3deca3e

Please sign in to comment.