Skip to content

Commit

Permalink
added more columns in the model ; added images support ; some refacto…
Browse files Browse the repository at this point in the history
…ring
  • Loading branch information
xavierlacot committed Feb 18, 2011
1 parent 0fefc08 commit 9e81fe6
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
@@ -1,6 +1,6 @@
[submodule "Resources/js/lib/vendor/joli.js"]
path = Resources/js/lib/vendor/joli.js
url = git://github.com/xavierlacot/joli.js.git
url = git@github.com:xavierlacot/joli.js.git
[submodule "Resources/js/lib/vendor/redux"]
path = Resources/js/lib/vendor/redux
url = git@github.com:xavierlacot/Appcelerator-Titanium-Redux.git
6 changes: 6 additions & 0 deletions Resources/js/lib/install.js
@@ -1,2 +1,8 @@
// create the tables if required

// for testing purpose
//var db = Titanium.Database.open('xavcc');
//db.execute('DROP TABLE IF EXISTS shorturl');
//db.close();

joli.models.initialize();
30 changes: 27 additions & 3 deletions Resources/js/lib/model/models.js
@@ -1,6 +1,24 @@
// open database
joli.connection = new joli.Connection('xavcc');

var getMaxPosition = function() {
var q = new joli.query()
.select('max(shorturl.position) as max')
.from('shorturl');
var result = q.execute();

if (!result.length) {
return 0;
} else {
if (null == result[0].max) {
return 0;
}

return parseInt(result[0].max);
}
}

// define the models
var models = (function() {
var m = {};

Expand All @@ -9,11 +27,17 @@ var models = (function() {
table: 'shorturl',
columns: {
id: 'INTEGER',
shorturl: 'TEXT',
created_at: 'TEXT',
longurl: 'TEXT',
viewcount: 'INTEGER',
media: 'TEXT',
position: 'INTEGER',
shorturl: 'TEXT',
title: 'TEXT',
position: 'INTEGER'
updated_at: 'TEXT',
viewcount: 'INTEGER'
},
methods: {
getMaxPosition: getMaxPosition
}
});

Expand Down
2 changes: 1 addition & 1 deletion Resources/js/lib/vendor/redux
Submodule redux updated 1 files
+4 −4 redux.js
133 changes: 126 additions & 7 deletions Resources/js/lib/xavcc.js
@@ -1,18 +1,23 @@
var xavcc = (function() {
var api = {};
api.url = {};

// create http client
var client = Titanium.Network.createHTTPClient();
client.timeout = 10000; // 10 s. timeout

api.createClient = function() {
var client = Titanium.Network.createHTTPClient();
client.timeout = 10000; // 10 s. timeout
return client;
};

api.decode = function(alias) {
var escapedUrl = api.encodeUrl(alias);
var url = Titanium.App.Properties.getString('api_url', 'http://api.xav.cc/');
url = url + 'simple/decode?alias=' + escapedUrl;
log('decode using: ' + url);

// create http client
var client = api.createClient();

client.onreadystatechange = function() {
Titanium.API.log('this.readyState: ' + this.readyState, 'info');
if (this.readyState == 4) {
Ti.App.fireEvent('xavcc.decode.result', { result: this.responseText });
}
Expand All @@ -30,16 +35,22 @@ var xavcc = (function() {


api.encode = function(longurl, alias) {
// first, search in the local history if enabled

// else shorten the value
var escapedUrl = api.encodeUrl(longurl);
var url = Titanium.App.Properties.getString('api_url', 'http://api.xav.cc/');
url = url + 'simple/encode?url=' + escapedUrl;
log('encode using: ' + url);

if (alias) {
url = url + '&alias=' + api.encodeUrl(alias);
}

// create http client
var client = api.createClient();

client.onreadystatechange = function() {
Titanium.API.log('this.readyState: ' + this.readyState, 'info');
if (this.readyState == 4) {
Ti.App.fireEvent('xavcc.encode.result', { result: this.responseText });
}
Expand Down Expand Up @@ -72,6 +83,23 @@ var xavcc = (function() {
};


api.parseDate = function(dt) {
var hit = dt.match(/^(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})/);

if (hit && hit.length === 7) {
return new Date(
Number(hit[1]),
Number(hit[2]) - 1,
Number(hit[3]),
Number(hit[4]),
Number(hit[5]),
Number(hit[6]));
} else {
return false;
};
}


api.showIndicator = function() {
var toolActInd;

Expand All @@ -80,7 +108,7 @@ var xavcc = (function() {
toolActInd.style = Titanium.UI.iPhone.ActivityIndicatorStyle.PLAIN;
toolActInd.font = {fontFamily:'Helvetica Neue', fontSize:15,fontWeight:'bold'};
toolActInd.color = 'white';
toolActInd.message = 'Chargement...';
toolActInd.message = 'Loading...';
Titanium.UI.currentWindow.setToolbar([toolActInd],{animated:true});
toolActInd.show();
}
Expand Down Expand Up @@ -144,5 +172,96 @@ var xavcc = (function() {
};


api.url.details = function(shorturl) {
var model = joli.models.get('shorturl');
var item = model.findOneBy('shorturl', shorturl);

if (!item || (item.length == 0)) {
return;
}

// compute the difference between item creation and last update date
var diff = parseInt(item.updated_at) - parseInt(item.created_at);

if ((item.title == null) && (item.created_at == null || (diff < 300 * 1000))) {
// if the title is null, and the item has never been updated or its
// update time has been made less than 2 minutes after it was created,
// load from REST service
var alias = shorturl.slice(api.strpos(shorturl, 'c/') + 2);
var url = Titanium.App.Properties.getString('api_url', 'http://api.xav.cc/');
url = url + 'sf_short_url?shorturl=' + alias;
log('get details using: ' + url);

// create http client
var client = api.createClient();

client.onreadystatechange = function() {
if (this.readyState == 4) {
// parse the response
var item = joli.jsonParse(this.responseText);

if (item && item.length > 0) {
// build the update array
item = item[0];
var date = new Date();
var updated_at = date.getTime() + (date.getTimezoneOffset() * 60000);
var created_at = api.parseDate(item.created_at);

if (created_at) {
created_at = created_at.getTime();
}

var update = {
viewcount: item.viewcount,
title: item.title,
created_at: created_at,
updated_at: updated_at
};

if (item.screencapture) {
var media_url = item.screencapture.small.slice(0, api.strpos(item.screencapture.small, '?'));
update.media = media_url;
}

// execute the update
var q = new joli.query()
.update('shorturl')
.set(update)
.where('shorturl = ?', shorturl);
q.execute();
}
}
};

if (Titanium.Platform.name != 'android') {
client.open('GET', url, true);
} else {
client.open('GET', url, false);
}

client.send(null);
}
};

api.url.has = function(shorturl) {
return new joli.query()
.count()
.from('shorturl')
.where('shorturl.shorturl = ?', shorturl)
.execute();
};

api.url.save = function(longurl, shorturl) {
var model = joli.models.get('shorturl');
var item = {
longurl: longurl,
shorturl: shorturl,
position: (model.getMaxPosition() + 1)
};
log(item);
model.newRecord(item).save();
};


return api;
})();
7 changes: 2 additions & 5 deletions Resources/js/views/decode.js
@@ -1,13 +1,10 @@
Ti.include('../../redux.js');
var win = Titanium.UI.currentWindow;
var clipboard = require('com.xavcc.Clipboard');

if (Titanium.Platform.name != 'android') {
win.hideNavBar(); // full screen app
}



// label and first field
var l1a = Titanium.UI.createLabel({
text:'Short url to decode*',
Expand Down Expand Up @@ -89,8 +86,8 @@ Ti.App.addEventListener('xavcc.decode.result', function(event) {
xavcc.showResponse(l2, url);

if (Titanium.App.Properties.getBool('auto_copy', true)) {
// put the shortened url in the clipboard
clipboard.setText(url);
// put the expanded url in the clipboard
Titanium.UI.Clipboard.setText(url);
}
} else if (url.length > 0) {
// something went wrong : display an alert message
Expand Down
22 changes: 12 additions & 10 deletions Resources/js/views/encode.js
@@ -1,6 +1,5 @@
Ti.include('../../redux.js');
var win = Titanium.UI.currentWindow;
var clipboard = require('com.xavcc.Clipboard');

if (Titanium.Platform.name != 'android') {
win.hideNavBar(); // full screen app
Expand Down Expand Up @@ -29,14 +28,10 @@ var tf1 = Titanium.UI.createTextField({
returnKeyType:Titanium.UI.RETURNKEY_DEFAULT,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
if (Titanium.App.Properties.getBool('auto_paste', false)) {
// put the shortened url in the clipboard
var text = clipboard.getText();

if (text) {
if (text.indexOf('http') == 0) {
tf1.value = text;
}
if (Titanium.UI.Clipboard.hasText() && Titanium.App.Properties.getBool('auto_paste', false)) {
var text = Titanium.UI.Clipboard.getText();
if (text.indexOf('http') == 0) {
tf1.value = text;
}
}

Expand Down Expand Up @@ -126,7 +121,14 @@ Ti.App.addEventListener('xavcc.encode.result', function(event) {

if (Titanium.App.Properties.getBool('auto_copy', true)) {
// put the shortened url in the clipboard
clipboard.setText(url);
alert(Titanium.App.Properties.getBool('auto_copy', true));
Titanium.UI.Clipboard.setText(url);
}

if (Titanium.App.Properties.getBool('use_history', true) && !xavcc.url.has(url)) {
// save in the local database
var longurl = xavcc.trim(tf1.value);
xavcc.url.save(longurl, url);
}
} else {
// something went wrong : display an alert message
Expand Down

0 comments on commit 9e81fe6

Please sign in to comment.