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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// =======================================================================
//
// SET THE CURRENT PAGE NAME
//
// ======================================================================= 

// Log window name to console

Ti.API.info("############### FAVORITES ##################");
	
// =======================================================================
//
// THE CURRENT WINDOWS BASIC SETTINGS
//
// =======================================================================

// Set the current window into a variable

var window = Ti.UI.currentWindow;

// =======================================================================
//
// SET THE CURRENT WINDOW BACKGROUND COLOR
//
// =======================================================================

// Set the background color

Titanium.UI.setBackgroundColor('#000');

// ======================================================================= 
//
// INCLUDE THE DEPENDENCIES
//
// =======================================================================

// Include the crud file

Ti.include("../common/twitter_crud.js");

// Include the utilities file

Ti.include("../common/utilities.js");

// =======================================================================
//
// THE MAIN BUTTONS
//
// ======================================================================= 

// Create the button

var button_accounts = Titanium.UI.createButton({

   title: 'Accounts'
   
});

// ======================================================================= 
//
// SET THE TABLE
//
// =======================================================================

// Create the table view
	
var table = Ti.UI.createTableView({

	editable: true, 
	backgroundColor: '#191919', 
	separatorColor: '#6a6a6a'
	
});

// ======================================================================= 
//
// ACTIVITY INDICATOR
//
// =======================================================================

// Create the activity indicator

var actInd = Titanium.UI.createActivityIndicator({

	bottom:10, 
	height:50,
	width:10,
	style:Titanium.UI.iPhone.ActivityIndicatorStyle.PLAIN
	
});

// =======================================================================
//
// GET THE DATA
//
// =======================================================================

// Define the function

function buildTable(action) {

	// Place the activity indicator
	
	window.setTitleControl(actInd);
	
	// Show the activity indicator
	
	actInd.show();
	
	// Create the data array

	var theData = [];
	
	// Set the screen name
	
	var screen_name = Titanium.App.Properties.getString("twitter_screen_name");
	
	// Make the request to the API
	
	twitterGET.execute('favorites.json?include_entities=true&screen_name=' + screen_name, action, function(response){
	
		// Loop through the response
	
		for (var i = 0; i < response.length; i++) {

			// Set the response data to variables
		
			var id = response[i].id;
			var tweet = response[i].text;
			var user = response[i].user.name;
			var avatar = response[i].user.profile_image_url;
			var created_at = prettyDate(response[i].created_at);

			// Open the database
        
       	 	var db = Titanium.Database.open('tweevorites');
       	 	
       	 	// Make sure the tables are all set
       	 	
       	 	db.execute('CREATE TABLE IF NOT EXISTS read (id INTEGER PRIMARY KEY, token VARCHAR(255) NULL, tweet_id VARCHAR(255) NULL)');
        			
			// Check if the tweet is already read
		
			var results = db.execute('SELECT token FROM read WHERE tweet_id = ?', id);
			
			// If the account is not already saved
		
			if (!results.isValidRow()) {
			
				// Create the avatar image
			
				var user_avatar = Titanium.UI.createImageView({
			
					image: avatar,
					top: 10,
					left: 10,
					width: 52,
					height: 52,
					borderRadius: 5,
					backgroundColor: '#333',
					preventDefaultImage: true
				
				});
			
				// Create the datetime label
	
				var datetime = Titanium.UI.createLabel({
					
					text: created_at,
					font:{fontSize:11,fontWeight: 'normal'},
					color: '#999',
					right: 10,
					top: 10,
					width: 'auto',
					height: 'auto'
				
				});
			
				// Create the fullname label
	
				var fullname = Titanium.UI.createLabel({
					
					text: user,
					font:{fontSize:14, fontFamily:'Helvetica Neue', fontWeight:'bold'},
					color: '#f4f4f4',
					left: 72,
					top: 7,
					width: 200,
					height: 20
				
				});

				// Create the message label
	
				var message = Titanium.UI.createLabel({	
				
					text: tweet,
					font:{fontSize:14},
					color: '#828282',
					left: 72,
					top: 30,
					bottom: 10,
					width: 230,
					height: 'auto'
				
				});

				// Create the row
			
				var row = Ti.UI.createTableViewRow({

  					className: 'feed_item',
  					height: 'auto',
  					backgroundColor: '#191919',
  					tweet_id: id,
  					tweet_links: findUrls(tweet)
  				  				
				});
			
				// Add the avatar to the row
			
				row.add(user_avatar);
			
				// Add the avatar to the row
			
				row.add(datetime);
			
				// Add the fullname to the row
			
				row.add(fullname);
			
				// Add the mssage to the row
			
				row.add(message);
								
				// Add the row to the array
		
				theData.push(row);
			
			}
		
			// Close the resultSet

			results.close();

			// Close the database

			db.close();
			
		}
	
		// Add the data to the table
	
		table.setData(theData);

		// Hide the activity indicator
		
		actInd.hide();
		
		// Set the title to default again
		
		window.setTitleControl(null);
		
		// Create the links dialog
				
		var dialog = Titanium.UI.createOptionDialog({});
	
		// Create an event listener for the button 

		table.addEventListener('click', function(e){ 
		
			// Get the length of the array
			
			var array_length = e.row.tweet_links.length;
			
			// Make sure that the cancel button is correct
			
			if (array_length > 1) {
			
				// Add options to dialog
				
				dialog.options = e.row.tweet_links;
			
				// Add cancel button to dialog
			
    			dialog.cancel = array_length - 1;
    		
				// Load the database
		
				dialog.show();
			
			}
		
		});
		
		// Create an event listener for the button 

		dialog.addEventListener('click', function(e){ 
		
			// Make sure the user did not click the cancel button
		
			if (e.source.options[e.index] != "Cancel") {
		
				// Create the browser window

				var win_browser = Titanium.UI.createWindow({

					title: 'Browser',
					barColor: '#000',
					url:'browser.js',
					navBarHidden: false,
					tabBarHidden: false,
					address: e.source.options[e.index]
	
				});
		
				// Animate the page turn
 	
    			Titanium.UI.currentTab.open(win_browser);
    		
    		}
		
		});
		
		// Create an event listener for the button 

		table.addEventListener('delete', function(e){ 
		
			// Load the database
		
			var db = Ti.Database.open('tweevorites');
			
			// Insert the tweet into the read table
		
			db.execute('INSERT INTO read (token,tweet_id) VALUES (?,?)', Titanium.App.Properties.getString("twitter_token"), e.row.tweet_id);
	
		});
			
	});
	
}

// =======================================================================
//
// START THE PULL TO REFRESH
//
// =======================================================================

// Create the table header
		
var tableHeader = Titanium.UI.createView({
			
	backgroundColor:"#191919",
	width:320,
	height:60
			
});
		
// Create the activity indicator

var tableHeader_actInd = Titanium.UI.createActivityIndicator({
		
	left:20,
	bottom:13,
	width:30,
	height:30,
	color: 'black',
	style: Titanium.UI.iPhone.ActivityIndicatorStyle.DARK
			
});
		
// Create the loading arrow

var tableHeader_arrow = Ti.UI.createView({
		
	backgroundImage:"../images/arrow.png",
	width:16,
	height:16,
	bottom:20,
	left:25
			
});

// Create the loading label
		
var tableHeader_label = Titanium.UI.createLabel({	
			
	text: 'Pull down to refresh',
	font:{fontSize:13,fontWeight: 'bold'},
	color: '#fff',
	left: 10,
	textAlign: 'center',
	bottom: 22,
	height: 'auto',
	width: 320
				
});

// Add the loading arrow to the table
		
tableHeader.add(tableHeader_arrow);
				
// Add the loading icon to the table
		
tableHeader.add(tableHeader_actInd);
		
// Add the loading label to the table
		
tableHeader.add(tableHeader_label);
			
// Add the table header to the table header
		
table.headerPullView = tableHeader;
			
// Set the variable pulling to false

var pulling = false;
		
// Set the variable reloading to false
		
var reloading = false;
		
// Check for movement

table.addEventListener('scroll',function(e) {
		
	// Set the offset
	
	var offset = e.contentOffset.y;
			
	// Check the current position
	
	if (offset <= -65.0 && !pulling) {
			
		// Create the 2D matrix
			
		var matrix = Ti.UI.create2DMatrix();
				
		// Rotate the matrix
				
		matrix = matrix.rotate(-180);
				
		// Animate the arrow
		
		tableHeader_arrow.animate({transform:matrix,duration:180});
				
		// Set the default value
			
		pulling = true;
				
		// Set current status
				
		tableHeader_label.text = "Release to refresh";
				
	} else if (pulling && offset > -65.0 && offset < 0) {
			
		// Set the default value
			
		pulling = false;
				
		// Create the 2D matrix
				
		var t = Ti.UI.create2DMatrix();
				
		// Animate the arrow
				
		tableHeader_arrow.animate({transform:t,duration:180});
				
		// Set current status
				
		tableHeader_label.text = "Pull down to refresh";
				
	}
	
});

// Define the end reload function

function endReloading() {

	// Reload the content
		
	buildTable('delFromCache');
		
    // Reset the table content inset
    
    table.setContentInsets({top:0},{animated:true});
    
    // Set reloading to false
    
    reloading = false;
    
    // Set the label text back again
    
    tableHeader_label.text = "Pull down to refresh...";
    
    // Hide the activity indicator
    
    tableHeader_actInd.hide();
    
    // Set back the arrow again
    
    tableHeader_arrow.show();
}

function beginReloading() {

	// Set a timeout for the reloading
	
	setTimeout(endReloading,2000);
}
		
// Refresh content on release

table.addEventListener('scrollEnd',function(e) {
		
	// Check the current position
		
	if (pulling && !reloading && e.contentOffset.y <= -65.0) {
			
		// Set the variable reloading to true
		
		reloading = true;
				
		// Set the variable pulling to false
				
		pulling = false;
		
		// Hide the arrow
				
		tableHeader_arrow.hide();
				
		// Show the activity indicator
				
		tableHeader_actInd.show();
				
		// Set current status
				
		tableHeader_label.text = "Reloading content...";
				
		// Adjust the height
				
		table.setContentInsets({top:60},{animated:true});
				
		// Remove the pull to refresh
		
		beginReloading();

	}
			
});		

// =======================================================================
//
// ADD TO THE CURRENT WINDOW
//
// =======================================================================

// Get the data from the API call
	
buildTable('delFromCache');

// Add the button to the window

window.setLeftNavButton(button_accounts);

// Add the table to the window

window.add(table);