· 8 years ago · Nov 23, 2017, 11:18 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 // Create the data array
115
116 var theUrls = [];
117
118 // Set the screen name
119
120 var screen_name = Titanium.App.Properties.getString("twitter_screen_name");
121
122 // Make the request to the API
123
124 twitterGET.execute('favorites.json?include_entities=true&screen_name=' + screen_name, action, function(response){
125
126 // Loop through the response
127
128 for (var i = 0; i < response.length; i++) {
129
130 // Set the response data to variables
131
132 var id = response[i].id;
133 var tweet = response[i].text;
134 var user = response[i].user.name;
135 var avatar = response[i].user.profile_image_url;
136 var created_at = prettyDate(response[i].created_at);
137
138 // Open the database
139
140 var db = Titanium.Database.open('tweevorites');
141
142 // Make sure the tables are all set
143
144 db.execute('CREATE TABLE IF NOT EXISTS read (id INTEGER PRIMARY KEY, token VARCHAR(255) NULL, tweet_id VARCHAR(255) NULL)');
145
146 // Check if the tweet is already read
147
148 var results = db.execute('SELECT token FROM read WHERE tweet_id = ?', id);
149
150 // If the account is not already saved
151
152 if (!results.isValidRow()) {
153
154 // Create the avatar image
155
156 var user_avatar = Titanium.UI.createImageView({
157
158 image: avatar,
159 top: 10,
160 left: 10,
161 width: 52,
162 height: 52,
163 borderRadius: 5,
164 backgroundColor: '#333',
165 preventDefaultImage: true
166
167 });
168
169 // Create the datetime label
170
171 var datetime = Titanium.UI.createLabel({
172
173 text: created_at,
174 font:{fontSize:11,fontWeight: 'normal'},
175 color: '#999',
176 right: 10,
177 top: 10,
178 width: 'auto',
179 height: 'auto'
180
181 });
182
183 // Create the fullname label
184
185 var fullname = Titanium.UI.createLabel({
186
187 text: user,
188 font:{fontSize:14, fontFamily:'Helvetica Neue', fontWeight:'bold'},
189 color: '#f4f4f4',
190 left: 72,
191 top: 7,
192 width: 200,
193 height: 20
194
195 });
196
197 // Create the message label
198
199 var message = Titanium.UI.createLabel({
200
201 text: tweet,
202 font:{fontSize:14},
203 color: '#828282',
204 left: 72,
205 top: 30,
206 bottom: 10,
207 width: 230,
208 height: 'auto'
209
210 });
211
212 // Links dialog
213
214 var dialog = Titanium.UI.createOptionDialog({
215
216 title: 'Open links',
217 options: findUrls(tweet),
218 cancel:1
219 });
220
221 // Create the row
222
223 var row = Ti.UI.createTableViewRow({
224
225 className: 'feed_item',
226 height: 'auto',
227 backgroundColor: '#191919',
228 tweet_id: id
229
230 });
231
232 // Add the avatar to the row
233
234 row.add(user_avatar);
235
236 // Add the avatar to the row
237
238 row.add(datetime);
239
240 // Add the fullname to the row
241
242 row.add(fullname);
243
244 // Add the mssage to the row
245
246 row.add(message);
247
248 // Add the row to the array
249
250 theData.push(row);
251
252 }
253
254 // Close the resultSet
255
256 results.close();
257
258 // Close the database
259
260 db.close();
261
262 }
263
264 // Add the data to the table
265
266 table.setData(theData);
267
268 // Hide the activity indicator
269
270 actInd.hide();
271
272 // Set the title to default again
273
274 window.setTitleControl(null);
275
276 // Create an event listener for the button
277
278 table.addEventListener('click', function(e){
279
280 // Load the database
281
282 dialog.show();
283
284 });
285
286 // Create an event listener for the button
287
288 table.addEventListener('delete', function(e){
289
290 // Load the database
291
292 var db = Ti.Database.open('tweevorites');
293
294 // Insert the tweet into the read table
295
296 db.execute('INSERT INTO read (token,tweet_id) VALUES (?,?)', Titanium.App.Properties.getString("twitter_token"), e.row.tweet_id);
297
298 });
299
300
301 });
302
303}
304
305// =======================================================================
306//
307// START THE PULL TO REFRESH
308//
309// =======================================================================
310
311// Create the table header
312
313var tableHeader = Titanium.UI.createView({
314
315 backgroundColor:"#191919",
316 width:320,
317 height:60
318
319});
320
321// Create the activity indicator
322
323var tableHeader_actInd = Titanium.UI.createActivityIndicator({
324
325 left:20,
326 bottom:13,
327 width:30,
328 height:30,
329 color: 'black',
330 style: Titanium.UI.iPhone.ActivityIndicatorStyle.DARK
331
332});
333
334// Create the loading arrow
335
336var tableHeader_arrow = Ti.UI.createView({
337
338 backgroundImage:"../images/arrow.png",
339 width:16,
340 height:16,
341 bottom:20,
342 left:25
343
344});
345
346// Create the loading label
347
348var tableHeader_label = Titanium.UI.createLabel({
349
350 text: 'Pull down to refresh',
351 font:{fontSize:13,fontWeight: 'bold'},
352 color: '#fff',
353 left: 10,
354 textAlign: 'center',
355 bottom: 22,
356 height: 'auto',
357 width: 320
358
359});
360
361// Add the loading arrow to the table
362
363tableHeader.add(tableHeader_arrow);
364
365// Add the loading icon to the table
366
367tableHeader.add(tableHeader_actInd);
368
369// Add the loading label to the table
370
371tableHeader.add(tableHeader_label);
372
373// Add the table header to the table header
374
375table.headerPullView = tableHeader;
376
377// Set the variable pulling to false
378
379var pulling = false;
380
381// Set the variable reloading to false
382
383var reloading = false;
384
385// Check for movement
386
387table.addEventListener('scroll',function(e) {
388
389 // Set the offset
390
391 var offset = e.contentOffset.y;
392
393 // Check the current position
394
395 if (offset <= -65.0 && !pulling) {
396
397 // Create the 2D matrix
398
399 var matrix = Ti.UI.create2DMatrix();
400
401 // Rotate the matrix
402
403 matrix = matrix.rotate(-180);
404
405 // Animate the arrow
406
407 tableHeader_arrow.animate({transform:matrix,duration:180});
408
409 // Set the default value
410
411 pulling = true;
412
413 // Set current status
414
415 tableHeader_label.text = "Release to refresh";
416
417 } else if (pulling && offset > -65.0 && offset < 0) {
418
419 // Set the default value
420
421 pulling = false;
422
423 // Create the 2D matrix
424
425 var t = Ti.UI.create2DMatrix();
426
427 // Animate the arrow
428
429 tableHeader_arrow.animate({transform:t,duration:180});
430
431 // Set current status
432
433 tableHeader_label.text = "Pull down to refresh";
434
435 }
436
437});
438
439// Define the end reload function
440
441function endReloading() {
442
443 // Reload the content
444
445 buildTable('delFromCache');
446
447 // Reset the table content inset
448
449 table.setContentInsets({top:0},{animated:true});
450
451 // Set reloading to false
452
453 reloading = false;
454
455 // Set the label text back again
456
457 tableHeader_label.text = "Pull down to refresh...";
458
459 // Hide the activity indicator
460
461 tableHeader_actInd.hide();
462
463 // Set back the arrow again
464
465 tableHeader_arrow.show();
466}
467
468function beginReloading() {
469
470 // Set a timeout for the reloading
471
472 setTimeout(endReloading,2000);
473}
474
475// Refresh content on release
476
477table.addEventListener('scrollEnd',function(e) {
478
479 // Check the current position
480
481 if (pulling && !reloading && e.contentOffset.y <= -65.0) {
482
483 // Set the variable reloading to true
484
485 reloading = true;
486
487 // Set the variable pulling to false
488
489 pulling = false;
490
491 // Hide the arrow
492
493 tableHeader_arrow.hide();
494
495 // Show the activity indicator
496
497 tableHeader_actInd.show();
498
499 // Set current status
500
501 tableHeader_label.text = "Reloading content...";
502
503 // Adjust the height
504
505 table.setContentInsets({top:60},{animated:true});
506
507 // Remove the pull to refresh
508
509 beginReloading();
510
511 }
512
513});
514
515// =======================================================================
516//
517// ADD TO THE CURRENT WINDOW
518//
519// =======================================================================
520
521// Get the data from the API call
522
523buildTable('delFromCache');
524
525// Add the button to the window
526
527window.setLeftNavButton(button_accounts);
528
529// Add the table to the window
530
531window.add(table);