Skip to content

Instantly share code, notes, and snippets.

@devluis
Created October 18, 2013 23:31
Show Gist options
  • Save devluis/7049778 to your computer and use it in GitHub Desktop.
Save devluis/7049778 to your computer and use it in GitHub Desktop.
//create the window
var win1 = Ti.UI.createWindow(
{
backgroundColor:"#fff",
title: "Concurso"
}
);
//create navigation group
var navGroup = Titanium.UI.iPhone.createNavigationGroup(
{
window:win1
}
);
// create the data for the table
var usernames = [];
//insert TableViewRow with custom data
usernames[0] = Ti.UI.createTableViewRow({hasChild:true,title:'Tweets From Thanuz',user:'Thanuz'});
usernames[1] = Ti.UI.createTableViewRow({hasChild:true,title:'Tweets From Felgarciah',user:'Felgarciah'});
usernames[2] = Ti.UI.createTableViewRow({hasChild:true,title:'Tweets From Chroposnos',user:'Chroposnos'});
usernames[3] = Ti.UI.createTableViewRow({hasChild:true,title:'Tweets From Siedrix',user:'Siedrix'});
usernames[4] = Ti.UI.createTableViewRow({hasChild:true,title:'Tweets From hdgam3r',user:'hdgam3r'});
//This function receives an array in Json format and return a table view with that data
function getTableView(tweetData){
rowArray = [];
Titanium.API.info(tweetData.length);
for(i=0;i<tweetData.length;i++){
rowArray[i] = Titanium.UI.createTableViewRow(
{
hasChild:false,
height:'auto'
}
);
rowArray[i].add(
Titanium.UI.createLabel(
{
text:tweetData[i].text,
height:'auto',
width:'auto',
textAlign:'left',
left:0,
top:10,
touchEnabled:false,
highlightedColor:'#9EC5F1'
}
)
);
}
return Titanium.UI.createTableView({data:rowArray});
}
//this function receive a title and a table and returns a window with that elements added
function createWindow(title,table){
var win2 = Titanium.UI.createWindow(
{
backgroundColor:"#000",
title:title
}
);
win2.add(table);
return win2;
}
//receives a navigator and a window and push that view
function pushWindow(navigator,window){
navigator.open(window);
}
//This function do a request to twitter api in order to getn 'count' number of tweets of the user 'username'
//with that data the function creates a window and push the view
function getTweet(username,count){
var consumer = Titanium.Network.createHTTPClient();
consumer.timeout=100000;
Titanium.API.info(username);
consumer.open('GET','http://api.twitter.com/1/statuses/user_timeline.json?screen_name='+username+'&count='+count);
consumer.onload = function(){
try{
var messages = eval('('+this.responseText+')');
var tableForTweets = getTableView(messages);
var windowForTweets = createWindow('tweets',tableForTweets);
pushWindow(navGroup,windowForTweets);
}catch(Error){
alert("Something weird ?!");
}
};
consumer.send();
}
//create the table with the data that was defined
var tabla1 = Titanium.UI.createTableView({data:usernames});
tabla1.addEventListener('click',function(table){
var user = table.rowData.user;
getTweet(user,100);
}
);
win1.add(tabla1);
var main = Titanium.UI.createWindow();
main.add(navGroup);
main.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment