· 8 years ago · Nov 23, 2017, 09:46 PM
1// =======================================================================
2//
3// SET THE CURRENT PAGE NAME
4//
5// =======================================================================
6
7// Log window name to console
8
9Ti.API.info("############### FAVORITES ##################");
10
11// =======================================================================
12//
13// THE CURRENT WINDOWS BASIC SETTINGS
14//
15// =======================================================================
16
17// Set the current window into a variable
18
19var window = Ti.UI.currentWindow;
20
21// =======================================================================
22//
23// SET THE CURRENT WINDOW BACKGROUND COLOR
24//
25// =======================================================================
26
27// Set the background color
28
29Titanium.UI.setBackgroundColor('#000');
30
31// =======================================================================
32//
33// INCLUDE THE DEPENDENCIES
34//
35// =======================================================================
36
37// Include the crud file
38
39Ti.include("../common/twitter_crud.js");
40
41// Include the utilities file
42
43Ti.include("../common/utilities.js");
44
45// =======================================================================
46//
47// THE MAIN BUTTONS
48//
49// =======================================================================
50
51// Create the button
52
53var button_accounts = Titanium.UI.createButton({
54
55 title: 'Accounts'
56
57});
58
59// =======================================================================
60//
61// SET THE TABLE
62//
63// =======================================================================
64
65// Create the table view
66
67var table = Ti.UI.createTableView({
68
69 editable: true,
70 backgroundColor: '#191919',
71 separatorColor: '#6a6a6a'
72
73});
74
75// =======================================================================
76//
77// ACTIVITY INDICATOR
78//
79// =======================================================================
80
81// Create the activity indicator
82
83var actInd = Titanium.UI.createActivityIndicator({
84
85 bottom:10,
86 height:50,
87 width:10,
88 style:Titanium.UI.iPhone.ActivityIndicatorStyle.PLAIN
89
90});
91
92// =======================================================================
93//
94// GET THE DATA
95//
96// =======================================================================
97
98// Define the function
99
100function buildTable(action) {
101
102 // Place the activity indicator
103
104 window.setTitleControl(actInd);
105
106 // Show the activity indicator
107
108 actInd.show();
109
110 // Create the data array
111
112 var theData = [];
113
114 // Set the screen name
115
116 var screen_name = Titanium.App.Properties.getString("twitter_screen_name");
117
118 // Make the request to the API
119
120 twitterGET.execute('favorites.json?include_entities=true&screen_name=' + screen_name, action, function(response){
121
122 // Loop through the response
123
124 for (var i = 0; i < response.length; i++) {
125
126 // Set the response data to variables
127
128 var id = response[i].id;
129 var tweet = response[i].text;
130 var user = response[i].user.name;
131 var avatar = response[i].user.profile_image_url;
132 var created_at = prettyDate(response[i].created_at);
133
134 // Open the database
135
136 var db = Titanium.Database.open('tweevorites');
137
138 // Make sure the tables are all set
139
140 db.execute('CREATE TABLE IF NOT EXISTS read (id INTEGER PRIMARY KEY, token VARCHAR(255) NULL, tweet_id VARCHAR(255) NULL)');
141
142 // Check if the tweet is already read
143
144 var results = db.execute('SELECT token FROM read WHERE tweet_id = ?', id);
145
146 // If the account is not already saved
147
148 if (!results.isValidRow()) {
149
150 // Create the avatar image
151
152 var user_avatar = Titanium.UI.createImageView({
153
154 image: avatar,
155 top: 10,
156 left: 10,
157 width: 52,
158 height: 52,
159 borderRadius: 5,
160 backgroundColor: '#333',
161 preventDefaultImage: true
162
163 });
164
165 // Create the datetime label
166
167 var datetime = Titanium.UI.createLabel({
168
169 text: created_at,
170 font:{fontSize:11,fontWeight: 'normal'},
171 color: '#999',
172 right: 10,
173 top: 10,
174 width: 'auto',
175 height: 'auto'
176
177 });
178
179 // Create the fullname label
180
181 var fullname = Titanium.UI.createLabel({
182
183 text: user,
184 font:{fontSize:14, fontFamily:'Helvetica Neue', fontWeight:'bold'},
185 color: '#f4f4f4',
186 left: 72,
187 top: 7,
188 width: 200,
189 height: 20
190
191 });
192
193 // Create the message label
194
195 var message = Titanium.UI.createLabel({
196
197 text: tweet,
198 font:{fontSize:14},
199 color: '#828282',
200 left: 72,
201 top: 30,
202 bottom: 10,
203 width: 230,
204 height: 'auto'
205
206 });
207
208 // Create the row
209
210 var row = Ti.UI.createTableViewRow({
211
212 className: 'feed_item',
213 height: 'auto',
214 backgroundColor: '#191919',
215 tweet_id: id,
216 tweet_links: findUrls(tweet)
217
218 });
219
220 // Add the avatar to the row
221
222 row.add(user_avatar);
223
224 // Add the avatar to the row
225
226 row.add(datetime);
227
228 // Add the fullname to the row
229
230 row.add(fullname);
231
232 // Add the mssage to the row
233
234 row.add(message);
235
236 // Add the row to the array
237
238 theData.push(row);
239
240 }
241
242 // Close the resultSet
243
244 results.close();
245
246 // Close the database
247
248 db.close();
249
250 }
251
252 // Add the data to the table
253
254 table.setData(theData);
255
256 // Hide the activity indicator
257
258 actInd.hide();
259
260 // Set the title to default again
261
262 window.setTitleControl(null);
263
264 // Create the links dialog
265
266 var dialog = Titanium.UI.createOptionDialog({});
267
268 // Create an event listener for the button
269
270 table.addEventListener('click', function(e){
271
272 // Get the length of the array
273
274 var array_length = e.row.tweet_links.length;
275
276 // Make sure that the cancel button is correct
277
278 if (array_length > 1) {
279
280 // Add options to dialog
281
282 dialog.options = e.row.tweet_links;
283
284 // Add cancel button to dialog
285
286 dialog.cancel = array_length - 1;
287
288 // Load the database
289
290 dialog.show();
291
292 }
293
294 });
295
296 // Create an event listener for the button
297
298 dialog.addEventListener('click', function(e){
299
300 // Make sure the user did not click the cancel button
301
302 if (e.source.options[e.index] != "Cancel") {
303
304 // Create the browser window
305
306 var win_browser = Titanium.UI.createWindow({
307
308 title: 'Browser',
309 barColor: '#000',
310 url:'browser.js',
311 navBarHidden: false,
312 tabBarHidden: false,
313 address: e.source.options[e.index]
314
315 });
316
317 // Animate the page turn
318
319 Titanium.UI.currentTab.open(win_browser);
320
321 }
322
323 });
324
325 // Create an event listener for the button
326
327 table.addEventListener('delete', function(e){
328
329 // Load the database
330
331 var db = Ti.Database.open('tweevorites');
332
333 // Insert the tweet into the read table
334
335 db.execute('INSERT INTO read (token,tweet_id) VALUES (?,?)', Titanium.App.Properties.getString("twitter_token"), e.row.tweet_id);
336
337 });
338
339 });
340
341}
342
343// =======================================================================
344//
345// START THE PULL TO REFRESH
346//
347// =======================================================================
348
349// Create the table header
350
351var tableHeader = Titanium.UI.createView({
352
353 backgroundColor:"#191919",
354 width:320,
355 height:60
356
357});
358
359// Create the activity indicator
360
361var tableHeader_actInd = Titanium.UI.createActivityIndicator({
362
363 left:20,
364 bottom:13,
365 width:30,
366 height:30,
367 color: 'black',
368 style: Titanium.UI.iPhone.ActivityIndicatorStyle.DARK
369
370});
371
372// Create the loading arrow
373
374var tableHeader_arrow = Ti.UI.createView({
375
376 backgroundImage:"../images/arrow.png",
377 width:16,
378 height:16,
379 bottom:20,
380 left:25
381
382});
383
384// Create the loading label
385
386var tableHeader_label = Titanium.UI.createLabel({
387
388 text: 'Pull down to refresh',
389 font:{fontSize:13,fontWeight: 'bold'},
390 color: '#fff',
391 left: 10,
392 textAlign: 'center',
393 bottom: 22,
394 height: 'auto',
395 width: 320
396
397});
398
399// Add the loading arrow to the table
400
401tableHeader.add(tableHeader_arrow);
402
403// Add the loading icon to the table
404
405tableHeader.add(tableHeader_actInd);
406
407// Add the loading label to the table
408
409tableHeader.add(tableHeader_label);
410
411// Add the table header to the table header
412
413table.headerPullView = tableHeader;
414
415// Set the variable pulling to false
416
417var pulling = false;
418
419// Set the variable reloading to false
420
421var reloading = false;
422
423// Check for movement
424
425table.addEventListener('scroll',function(e) {
426
427 // Set the offset
428
429 var offset = e.contentOffset.y;
430
431 // Check the current position
432
433 if (offset <= -65.0 && !pulling) {
434
435 // Create the 2D matrix
436
437 var matrix = Ti.UI.create2DMatrix();
438
439 // Rotate the matrix
440
441 matrix = matrix.rotate(-180);
442
443 // Animate the arrow
444
445 tableHeader_arrow.animate({transform:matrix,duration:180});
446
447 // Set the default value
448
449 pulling = true;
450
451 // Set current status
452
453 tableHeader_label.text = "Release to refresh";
454
455 } else if (pulling && offset > -65.0 && offset < 0) {
456
457 // Set the default value
458
459 pulling = false;
460
461 // Create the 2D matrix
462
463 var t = Ti.UI.create2DMatrix();
464
465 // Animate the arrow
466
467 tableHeader_arrow.animate({transform:t,duration:180});
468
469 // Set current status
470
471 tableHeader_label.text = "Pull down to refresh";
472
473 }
474
475});
476
477// Define the end reload function
478
479function endReloading() {
480
481 // Reload the content
482
483 buildTable('delFromCache');
484
485 // Reset the table content inset
486
487 table.setContentInsets({top:0},{animated:true});
488
489 // Set reloading to false
490
491 reloading = false;
492
493 // Set the label text back again
494
495 tableHeader_label.text = "Pull down to refresh...";
496
497 // Hide the activity indicator
498
499 tableHeader_actInd.hide();
500
501 // Set back the arrow again
502
503 tableHeader_arrow.show();
504}
505
506function beginReloading() {
507
508 // Set a timeout for the reloading
509
510 setTimeout(endReloading,2000);
511}
512
513// Refresh content on release
514
515table.addEventListener('scrollEnd',function(e) {
516
517 // Check the current position
518
519 if (pulling && !reloading && e.contentOffset.y <= -65.0) {
520
521 // Set the variable reloading to true
522
523 reloading = true;
524
525 // Set the variable pulling to false
526
527 pulling = false;
528
529 // Hide the arrow
530
531 tableHeader_arrow.hide();
532
533 // Show the activity indicator
534
535 tableHeader_actInd.show();
536
537 // Set current status
538
539 tableHeader_label.text = "Reloading content...";
540
541 // Adjust the height
542
543 table.setContentInsets({top:60},{animated:true});
544
545 // Remove the pull to refresh
546
547 beginReloading();
548
549 }
550
551});
552
553// =======================================================================
554//
555// ADD TO THE CURRENT WINDOW
556//
557// =======================================================================
558
559// Get the data from the API call
560
561buildTable('delFromCache');
562
563// Add the button to the window
564
565window.setLeftNavButton(button_accounts);
566
567// Add the table to the window
568
569window.add(table);