aboutsummaryrefslogtreecommitdiffstats
path: root/Resources/search.js
blob: 973e44877d1118cea017a2aff5a3ebc6274a48aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Create a reference to the current window
var win = Titanium.UI.currentWindow;

// Create an object to contain some useful functions
// Namespacing can be useful for keeping code organised in Ti applications
// In simple applications such as this, it's not exactly necessary
var TSearch = {
	s: function(query) {
		var row_data = []; // an array to store our result rows in
		                   // (this gets fed into the result_table)
		
		var request = Titanium.Network.createHTTPClient();
		request.open('GET', 'http://search.twitter.com/search.json?q=' + query);
		
		request.onload = function() {
			var results = (eval('(' + this.responseText + ')')).results;
			
			// No result case?
			
			// Open a database to store our result set (Ti uses asynchronous
			// requests, so this is what I came up with to separate view 
			// and data code)
			var db = Titanium.Database.open('twitsearch');
			db.execute('CREATE TABLE IF NOT EXISTS "search_results" (tweet TEXT, user STRING, avatar STRING)');
			db.execute('DELETE FROM "search_results"');
			
			for (var i = 0; i < results.length; i++) {
//				var tweet  = results[i].text;              // Tweet text
//				var user   = results[i].from_user;         // Username
//				var avatar = results[i].profile_image_url; // Avatar URL
				
// 				row_data[i] = {
// 					tweet  : results[i].text,              // Tweet text
// 					user   : results[i].from_user,         // Username
// 					avatar : results[i].profile_image_url  // Avatar URL					
// 				};
				
				db.execute('INSERT INTO "search_results" (tweet, user, avatar) VALUES (?, ?, ?)', results[i].text, results[i].from_user, results[i].profile_image_url);
			}
			
			db.close();
		};
		
		request.send();
		
		return row_data;
	}
};


// Create a search input bar
var search_input = Titanium.UI.createSearchBar({
	hintText: 'Enter a search query', // let user know what to type
	height: 43,
	top: 0
});

var result_table = Titanium.UI.createTableView();


search_input.addEventListener('return', function(e) {
	TSearch.s(e.value);
	
	var db = Titanium.Database.open('twitsearch');
	//db.execute('CREATE TABLE IF NOT EXISTS "search_results" (tweet TEXT, user STRING, avatar STRING)');
	var results = db.execute('SELECT * FROM "search_results"');
	
	while (results.isValidRow()) {
		Titanium.API.info(results.fieldByName('tweet'));
		Titanium.API.info(results.fieldByName('user'));
		Titanium.API.info(results.fieldByName('avatar'));
		
		results.next();
	}
	
	results.close();
	db.close();
});

var label1 = Titanium.UI.createLabel({
	color:'#999',
	text:'I am Window 1',
	font:{fontSize:20,fontFamily:'Helvetica Neue'},
	textAlign:'center',
	width:'auto'
});

win.add(label1);

win.add(search_input);