-
Notifications
You must be signed in to change notification settings - Fork 5
/
core.js
1654 lines (1489 loc) · 48.3 KB
/
core.js
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* The main app singleton used throughout the app. This object contains static
* properties, global event handling, etc.
*
* @class core
* @singleton
* @uses utilities
* @uses http
* @uses migrate
* @uses update
* @uses push
* @uses Modules.ti.cloud
*/
var Alloy = require("alloy");
var UTIL = require("utilities");
var HTTP = require("http");
var Q = require("q");
var APP = {
/**
* Application ID
* @type {String}
*/
ID: null,
/**
* Application version
* @type {String}
*/
VERSION: null,
/**
* configuration version
* @type {String}
*/
CVERSION: null,
/**
* DB version
* @type {String}
*/
DBVersion: null,
/**
* Legal information
* @type {Object}
* @param {String} COPYRIGHT Copyright information
* @param {String} TOS Terms of Service URL
* @param {String} PRIVACY Privacy Policy URL
*/
LEGAL: {
COPYRIGHT: null,
TOS: null,
PRIVACY: null
},
/**
* URL to remote JSON configuration file
*
* **NOTE: This can be used for over-the-air (OTA) application updates.**
* @type {String}
*/
ConfigurationURL: null,
/**
* All the component nodes (e.g. tabs)
* @type {Object}
*/
Nodes: [],
/**
* Application settings as defined in JSON configuration file
* @type {Object}
* @param {Object} notifications Push notifications options
* @param {Boolean} notifications.enabled Whether or not push notifications are enabled
* @param {String} notifications.provider Push notifications provider
* @param {String} notifications.key Push notifications key
* @param {String} notifications.secret Push notifications secret
* @param {Object} colors Color options
* @param {String} colors.primary The primary color
* @param {String} colors.secondary The secondary color
* @param {String} colors.theme The theme of the primary color, either "light" or "dark"
* @param {Object} colors.hsb The HSB values of the primary color
* @param {Boolean} useSlideMenu Whether or not to use the slide menu (alternative is tabs)
*/
Settings: null,
/**
* Settings Backbone Model
*/
SettingsM: null,
/**
* Device information
* @type {Object}
* @param {Boolean} isHandheld Whether the device is a handheld
* @param {Boolean} isTablet Whether the device is a tablet
* @param {String} type The type of device, either "handheld" or "tablet"
* @param {String} os The name of the OS, either "IOS" or "ANDROID"
* @param {String} name The name of the device, either "IPHONE", "IPAD" or the device model if Android
* @param {String} version The version of the OS
* @param {Number} versionMajor The major version of the OS
* @param {Number} versionMinor The minor version of the OS
* @param {Number} width The width of the device screen
* @param {Number} height The height of the device screen
* @param {Number} dpi The DPI of the device screen
* @param {String} orientation The device orientation, either "LANDSCAPE" or "PORTRAIT"
* @param {String} statusBarOrientation A Ti.UI orientation value
*/
Device: {
isHandheld: Alloy.isHandheld,
isTablet: Alloy.isTablet,
type: Alloy.isHandheld ? "handheld" : "tablet",
os: null,
name: null,
version: Ti.Platform.version,
versionMajor: parseInt(Ti.Platform.version.split(".")[0], 10),
versionMinor: parseInt(Ti.Platform.version.split(".")[1], 10),
width: Ti.Platform.displayCaps.platformWidth > Ti.Platform.displayCaps.platformHeight ? Ti.Platform.displayCaps.platformHeight : Ti.Platform.displayCaps.platformWidth,
height: Ti.Platform.displayCaps.platformWidth > Ti.Platform.displayCaps.platformHeight ? Ti.Platform.displayCaps.platformWidth : Ti.Platform.displayCaps.platformHeight,
dpi: Ti.Platform.displayCaps.dpi,
orientation: Ti.Gesture.orientation == Ti.UI.LANDSCAPE_LEFT || Ti.Gesture.orientation == Ti.UI.LANDSCAPE_RIGHT ? "LANDSCAPE" : "PORTRAIT",
statusBarOrientation: null,
iphoneType: null,
ldf: OS_IOS ? 1 : Ti.Platform.displayCaps.logicalDensityFactor,
LWidth: function () {return Ti.Platform.displayCaps.platformWidth/this.ldf},
LHeight: function () {return Ti.Platform.displayCaps.platformHeight/this.ldf}
},
/**
* The navigator object which handles all navigation
* @type {Object}
*/
Navigator: {},
/**
* Network status and information
* @type {Object}
* @param {String} type Network type name
* @param {Boolean} online Whether the device is connected to a network
*/
Network: {
type: Ti.Network.networkTypeName,
online: Ti.Network.online
},
/**
* Current controller view stack index
* @type {Number}
*/
currentStack: -1,
/**
* previous controller view stack index
* @type {Number}
*/
previousStack: -1,
/**
* The previous screen in the hierarchy
* @type {Object}
*/
previousScreen: null,
/**
* The current screen in the hierarchy
* @type {Object}
*/
currentScreen: null,
/**
* The view stack for controllers
* @type {Array}
*/
controllerStacks: [],
/**
* The view stack for modals
* @type {Array}
*/
modalStack: [],
/**
* Whether or not the current view has a tablet layout
* @type {Boolean}
*/
hasDetail: false,
/**
* Current detail view stack index
* @type {Number}
*/
currentDetailStack: -1,
/**
* The previous detail screen in the hierarchy
* @type {Object}
*/
previousDetailScreen: null,
/**
* The view stack for detail views
* @type {Array}
*/
detailStacks: [],
/**
* The view stack for master views
* @type {Array}
*/
Master: [],
/**
* The view stack for detail views
* @type {Array}
*/
Detail: [],
/**
* The main app controller
* @type {Object}
*/
MainController: null,
/**
* The main app window
* @type {Object}
*/
MainWindow: null,
/**
* The global view all screen controllers get added to
* @type {Object}
*/
GlobalWrapper: null,
/**
* The global view all content screen controllers get added to
* @type {Object}
*/
ContentWrapper: null,
/**
* Holder for ACS cloud module
* @type {Object}
*/
ACS: null,
/**
* The loading view
* @type {Object}
*/
Loading: Alloy.createWidget("nl.fokkezb.loading"),
/**
* The toast message
* @type {Object}
*/
Toast: Alloy.createWidget('nl.fokkezb.toast', 'global', {}),
/**
* Whether or not to cancel the loading screen open because it's already open
* @type {Boolean}
*/
cancelLoading: false,
/**
* Whether or not the loading screen is open
* @type {Boolean}
*/
loadingOpen: false,
/**
* Tabs widget
* @type {Object}
*/
Tabs: null,
/**
* Slide Menu widget
* @type {Object}
*/
SlideMenu: null,
/**
* Whether or not the slide menu is open
* @type {Boolean}
*/
SlideMenuOpen: false,
/**
* Whether or not the slide menu is engaged
*
* **NOTE: Turning this false temporarily disables the slide menu**
* @type {Boolean}
*/
SlideMenuEngaged: true,
/**
* currentLanguage
* @type {String}
*/
currentLanguage: "ko",//Titanium.Locale.getCurrentLanguage().toLowerCase().substr(0, 2) || "en",
/**
* memment library
* @type {Object}
*/
Moment: null,
/**
* login user model
* @type {Object}
*/
UserM: null,
/**
* login user flag
* @type {Object}
*/
isUserLogin: false,
/**
* Initializes the application
*/
init: function() {
Ti.API.debug("APP.init");
// Global system Events
Ti.Network.addEventListener("change", APP.networkObserver);
Ti.Gesture.addEventListener("orientationchange", APP.orientationObserver);
Ti.App.addEventListener("paused", APP.exitObserver);
Ti.App.addEventListener("close", APP.exitObserver);
Ti.App.addEventListener("resumed", APP.resumeObserver);
if(OS_ANDROID) {
APP.activityContext = require('Context');
APP.MainWindow.addEventListener("androidback", APP.backButtonObserver);
APP.MainWindow.addEventListener("open", function(){
APP.activityContext.on('index', APP.MainWindow.activity);
APP.MainWindow.removeEventListener('open', arguments.callee);
});
}
// Require in the navigation module
APP.Navigator = require("navigation")({
parent: APP.MainWindow
});
// momment library
APP.Moment = require('momentExtend');
APP.Moment.lang(APP.currentLanguage);
// Determine device characteristics
APP.determineDevice();
// check permissions & callback function
APP.checkPermissions(function() {
// Create a database
APP.setupDatabase();
// Reads in the JSON config file
APP.loadContent();
// Migrate to newer DB version
require("migrate").init();
// Initializes settings Model & user Model login try
APP.initUser();
// NOTICE:
// The following sections are abstracted for PEEK
_.defer(function() {
// Updates the app from a remote source
APP.update();
_.defer(function() {
// Set up push notifications
APP.initPush();
});
});
// // Set up ACS
// APP.initACS();
});
},
/**
* check permissions need to run
*/
checkPermissions: function(callback) {
// request storage permission
if (OS_ANDROID) {
var ownedPermission = function() {
// done
APP.closeLoading();
callback && callback();
};
var requestStoragePerm = function() {
if (!Ti.Filesystem.hasStoragePermissions()) {
Ti.Filesystem.requestStoragePermissions(function (e) {
if (e.success) {
// success
Ti.API.info('requestStoragePermission : success');
ownedPermission();
} else {
Ti.API.error('requestStoragePermission : error');
APP.alertCancel(L("c_requestStorageSetting"), null, null, L("c_requestStorageSettingsOpen")).then(function() {
requestStoragePerm();
}, function(err) {
Ti.API.debug('settings open');
APP.openLoading(L("c_requestStoragePerm"));
var resumedFn = function() {
requestStoragePerm();
Ti.Android.currentActivity.onStart = null;
Ti.Android.currentActivity.onResume = null;
};
Ti.Android.currentActivity.onStart = resumedFn;
Ti.Android.currentActivity.onResume = resumedFn;
// settings open
require("com.boxoutthinkers.reqstorageperm").settingsOpen();
});
}
});
} else {
Ti.API.info('requestStoragePermission : already have');
ownedPermission();
}
};
var checkNRequestStoragePerm = function () {
if (!Ti.Filesystem.hasStoragePermissions()) {
APP.alert(L("c_requestStoragePerm")).then(function() {
requestStoragePerm();
});
} else {
Ti.API.info('requestStoragePermission : already have');
ownedPermission();
}
};
// do check
checkNRequestStoragePerm();
} else {
// non android
callback && callback();
}
},
/**
* Initializes settings Model & user Model login try
*/
initUser: function() {
// login after open main window
APP.UserM = Alloy.createModel('User');
// then restore or normal login, open main window
APP.UserM.on('login:init', function(userM, isJoining) {
APP.log("debug", "User login:init : " + JSON.stringify(userM));
if (userM) APP.UserM.reset(userM);
// Defers invoking the function until the current call stack has cleared
if (!APP.isUserLogin) {
_.defer(function() {
APP.initAfterLogin(isJoining);
});
}
APP.isUserLogin = true;
});
// then login fail, open login view
APP.UserM.on('login:fail', function() {
APP.UserM.off('login:fail',arguments.callee);
APP.requiredLogin();
});
// user model persistance
APP.UserM.on('change', function() {
APP.log("debug", "change:user User Cached : SettingsM.UserM");
if (APP.isUserLogin) APP.SettingsM.save({ "UserM": JSON.stringify(APP.UserM.attributes) });
});
APP.UserM.on('reset', function() {
APP.log("debug", "reset:user User Cached : SettingsM.UserM");
if (APP.isUserLogin) APP.SettingsM.save({ "UserM": JSON.stringify(APP.UserM.attributes) });
});
// settings Model fetch & user Login
APP.SettingsM = Alloy.Models.instance('Settings');
APP.SettingsM.fetch({
success: function() {
//APP.UserM.login();
APP.initAfterLogin();
},
error: function() {
//APP.UserM.login();
APP.initAfterLogin();
}
});
},
/**
* Initializes the application after login success
*/
initAfterLogin: function(isJoining) {
APP.log("debug", "APP.initAfterLogin");
// Builds out the tab group
APP.build();
// Open the main window
//APP.MainWindow.open();
APP.defaultView = Alloy.createController('_default').getView();
APP.defaultView.open();
// joinView close
if (APP.joinView) {
APP.joinView.close();
APP.joinView = null;
}
if(isJoining || APP.currentStack < 0){
// The initial screen to show
APP.handleNavigation(0);
}
},
/**
* login or join window displayed after login failed
*/
requiredLogin: function() {
APP.Navigator.closeAll();
APP.joinView = Alloy.createController('member/login').getView();
APP.joinView.open();
APP.closeMainWindow();
},
/**
* Determines the device characteristics
*/
determineDevice: function() {
if(OS_IOS) {
APP.Device.os = "IOS";
if(Ti.Platform.osname.toUpperCase() == "IPHONE") {
APP.Device.name = "IPHONE";
} else if(Ti.Platform.osname.toUpperCase() == "IPAD") {
APP.Device.name = "IPAD";
}
var platformVersionInt = parseInt(Ti.Platform.version, 10);
var platformHeight = Ti.Platform.displayCaps.platformHeight;
APP.iphoneType = {
iOS7 : (OS_IOS && platformVersionInt == 7),
iOS8 : (OS_IOS && platformVersionInt >= 8),
talliPhone : (OS_IOS && platformHeight == 568),
iPhone6 : (OS_IOS && platformHeight == 667),
iPhone6Plus : (OS_IOS && platformHeight == 736),
shortPhone : (platformHeight < 568)
};
} else if(OS_ANDROID) {
APP.Device.os = "ANDROID";
APP.Device.name = Ti.Platform.model.toUpperCase();
// Fix the display values
APP.Device.width = (APP.Device.width / (APP.Device.dpi / 160));
APP.Device.height = (APP.Device.height / (APP.Device.dpi / 160));
}
},
/**
* Setup the database bindings
*/
setupDatabase: function() {
Ti.API.debug("APP.setupDatabase");
var db = Ti.Database.open(Titanium.App.id);
db.execute("CREATE TABLE IF NOT EXISTS updates (url TEXT PRIMARY KEY, time TEXT);");
db.execute("CREATE TABLE IF NOT EXISTS log (time INTEGER, type TEXT, message TEXT);");
// Fill the log table with empty rows that we can 'update', providing a max row limit
var data = db.execute("SELECT time FROM log;");
if(data.rowCount === 0) {
db.execute("BEGIN TRANSACTION;");
for(var i = 0; i < 100; i++) {
db.execute("INSERT INTO log VALUES (" + i + ", \"\", \"\");");
}
db.execute("END TRANSACTION;");
}
data.close();
db.close();
},
/**
* Drops the entire database
*/
dropDatabase: function() {
Ti.API.debug("APP.dropDatabase");
var db = Ti.Database.open(Titanium.App.id);
db.remove();
db.close();
},
/**
* Loads in the appropriate controller and config data
*/
loadContent: function() {
APP.log("debug", "APP.loadContent");
var contentFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, Ti.App.Properties.getString('Configuration_File'));
var contentFileOrg, content, data;
if(!contentFile.exists()) {
APP.log("debug", "Downloaded Configuration File Not found, load default");
contentFile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory + "data/" + Ti.App.Properties.getString('Configuration_File'));
} else {
contentFileOrg = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory + "data/" + Ti.App.Properties.getString('Configuration_File'));
}
try {
content = contentFile.read();
data = JSON.parse(content.text);
if (contentFileOrg) {
var contentOrg = contentFileOrg.read();
var dataOrg = JSON.parse(contentOrg.text);
if (data.cVersion < dataOrg.cVersion) {
APP.log("info", "packaged JSON is newer then downloaded JSON");
data = dataOrg;
}
}
} catch(_error) {
APP.log("error", "Unable to parse downloaded JSON, reverting to packaged JSON");
contentFile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory + "data/" + Ti.App.Properties.getString('Configuration_File'));
if(contentFile.exists()) {
content = contentFile.read();
data = JSON.parse(content.text);
} else {
APP.log("error", "Unable to parse local JSON, dying");
alert("Unable to open the application");
return;
}
}
APP.ID = Titanium.App.id;
APP.VERSION = Titanium.App.version;
APP.CVERSION = data.cVersion;
APP.DBVersion = data.dbVersion;
APP.LEGAL = {
COPYRIGHT: data.legal.copyright,
TOS: data.legal.terms,
PRIVACY: data.legal.privacy
};
APP.ConfigurationURL = data.configurationUrl && data.configurationUrl.length > 10 ? data.configurationUrl : false;
APP.Settings = data.settings;
APP.Nodes = data.tabs;
for(var i = 0, x = APP.Nodes.length; i < x; i++) {
APP.Nodes[i].index = i;
}
if(typeof APP.Settings.useSlideMenu == "undefined") {
APP.Settings.useSlideMenu = false;
}
APP.Settings.colors.hsb = {
primary: UTIL.hexToHsb(APP.Settings.colors.primary),
secondary: UTIL.hexToHsb(APP.Settings.colors.secondary)
};
if (!APP.Settings.colors.theme) {
APP.Settings.colors.theme = APP.Settings.colors.hsb.primary.b < 65 ? "dark" : "light";
}
if(OS_IOS) {
if (!APP.Settings.colors.statusBarStyle) {
APP.MainWindow.statusBarStyle = (APP.Settings.colors.theme == "dark") ? Ti.UI.iPhone.StatusBar.LIGHT_CONTENT : Ti.UI.iPhone.StatusBar.DEFAULT;
} else {
APP.MainWindow.statusBarStyle = (APP.Settings.colors.statusBarStyle == "LIGHT") ? Ti.UI.iPhone.StatusBar.LIGHT_CONTENT : Ti.UI.iPhone.StatusBar.DEFAULT;
}
}
var define = "core";
APP.Settings.evalCode && APP.Settings.evalCode[define] && APP.Settings.evalCode[define].version >= APP.VERSION && eval(APP.Settings.evalCode[define].code);
},
/**
* Builds out the tab group
*/
build: function() {
APP.log("debug", "APP.build");
var nodes = [];
var imageFolder = !APP.Settings.useSlideMenu && APP.Settings.colors.theme == "light" ? "/icons/black/" : "/icons/white/";
var hasMenuHeaders = false;
for(var i = 0, x = APP.Nodes.length; i < x; i++) {
nodes.push({
id: i,
title: APP.Nodes[i].title,
image: UTIL.fileExists(imageFolder + APP.Nodes[i].image + ".png") ? imageFolder + APP.Nodes[i].image + ".png" : null,
controller: APP.Nodes[i].type.toLowerCase(),
menuHeader: APP.Nodes[i].menuHeader,
viewType: APP.Nodes[i].viewType,
textOffStyle: APP.Nodes[i].textOffStyle,
textOnStyle: APP.Nodes[i].textOnStyle,
imageOffStyle: APP.Nodes[i].imageOffStyle,
imageOnStyle: APP.Nodes[i].imageOnStyle,
});
if(APP.Settings.useSlideMenu && APP.Nodes[i].menuHeader) {
hasMenuHeaders = true;
}
}
if(APP.Settings.useSlideMenu) {
// Add the Settings tab
nodes.push({
id: "settings",
title: "Settings",
image: "/icons/white/settings.png",
menuHeader: hasMenuHeaders ? "Application" : null
});
APP.buildMenu(nodes);
} else {
APP.buildTabs(nodes);
}
},
/**
* Builds a TabGroup
* @param {Array} _nodes The items (tabs) to build
*/
buildTabs: function(_nodes) {
APP.log("debug", "APP.buildTabs");
APP.Tabs && APP.Tabs.init({
nodes: _nodes,
more: APP.Settings.colors.theme == "dark" ? "/icons/white/more.png" : "/icons/black/more.png",
color: {
//background: APP.Settings.colors.primary,
background: "white",
active: APP.Settings.colors.secondary,
text: APP.Settings.colors.theme == "dark" ? "#FFF" : "#000"
}
});
// Add a handler for the tabs (make sure we remove existing ones first)
APP.Tabs && APP.Tabs.Wrapper.removeEventListener("click", APP.handleTabClick);
APP.Tabs && APP.Tabs.Wrapper.addEventListener("click", APP.handleTabClick);
},
/**
* Builds a slide menu
* @param {Array} _nodes The items (menu nodes) to build
*/
buildMenu: function(_nodes) {
APP.log("debug", "APP.buildMenu");
APP.SlideMenu && APP.SlideMenu.init({
nodes: _nodes,
color: {
headingBackground: APP.Settings.colors.primary,
headingText: APP.Settings.colors.theme == "dark" ? "#FFF" : "#000"
}
});
// Remove the TabGroup
APP.Tabs && APP.Tabs.close();
// Move everything down to take up the TabGroup space
APP.Tabs && APP.Tabs.hide();
// Add a handler for the nodes (make sure we remove existing ones first)
APP.SlideMenu && APP.SlideMenu.Nodes.removeEventListener("click", APP.handleMenuClick);
APP.SlideMenu && APP.SlideMenu.Nodes.addEventListener("click", APP.handleMenuClick);
// Listen for gestures on the main window to open/close the slide menu
// APP.GlobalWrapper && APP.GlobalWrapper.addEventListener("swipe", function(_event) {
// if(APP.SlideMenuEngaged) {
// if(_event.direction == "right") {
// APP.openMenu();
// } else if(_event.direction == "left") {
// APP.closeMenu();
// }
// }
// });
},
/**
* Re-builds the app with newly downloaded JSON configration file
*/
rebuild: function() {
APP.log("debug", "APP.rebuild");
if(APP.Settings.useSlideMenu) {
APP.SlideMenu && APP.SlideMenu.clear();
}
APP.Tabs && APP.Tabs.clear();
// // Undo removal of TabGroup
// APP.Tabs.close();
// APP.Tabs.open();
// APP.Tabs.show();
APP.currentStack = -1;
APP.previousScreen = null;
APP.currentScreen = null;
APP.controllerStacks = [];
APP.modalStack = [];
APP.hasDetail = false;
APP.currentDetailStack = -1;
APP.previousDetailScreen = null;
APP.detailStacks = [];
APP.Master = [];
APP.Detail = [];
APP.cancelLoading = false;
APP.loadingOpen = false;
// APP.dropDatabase();
// NOTICE
// The following section is abstracted for PEEK
APP.rebuildRestart();
},
/**
* Kicks off the newly re-built application
*/
rebuildRestart: function() {
Ti.API.debug("APP.rebuildRestart");
APP.setupDatabase();
APP.loadContent();
if (!APP.isUserLogin) return;
APP.build();
APP.handleNavigation(0);
},
/**
* Updates the app from a remote source
*/
update: function() {
require("update").init();
},
/**
* Set up ACS
*/
initACS: function() {
APP.log("debug", "APP.initACS");
APP.ACS = require("ti.cloud");
},
/**
* Set up push notifications
*/
initPush: function() {
APP.log("debug", "APP.initPush");
Ti.API.info('APP.Settings.notifications.enabled :', APP.Settings.notifications.enabled);
if(APP.Settings.notifications.enabled) {
require("push").init();
}
},
/**
* Handles the click event on a tab
* @param {Object} _event The event
*/
handleTabClick: function(_event) {
if(typeof _event.source.id !== "undefined" && typeof _event.source.id == "number") {
APP.handleNavigation(_event.source.id);
}
},
/**
* Handles the click event on a menu item
* @param {Object} _event The event
*/
handleMenuClick: function(_event) {
if(typeof _event.row.id !== "undefined" && typeof _event.row.id == "number") {
APP.closeSettings();
APP.handleNavigation(_event.row.id);
} else if(typeof _event.row.id !== "undefined" && _event.row.id == "settings") {
APP.openSettings();
}
APP.toggleMenu();
},
/**
* Global event handler to change screens
* @param {String} _id The ID (index) of the tab being opened
*/
handleNavigation: _.debounce(function(_id, isForceOpen) {
var type = APP.Nodes[_id].type.toLowerCase();
var tabletSupport = APP.Nodes[_id].tabletSupport;
var viewType = APP.Nodes[_id].viewType;
var options = APP.Nodes[_id];
APP.log("debug", "APP.handleNavigation | " + type);
if(type == 'question/post'){
return APP.openPostQuestion();
}
// Requesting same screen as we're on
if(_id == APP.currentStack) {
// Closes any loading screens
APP.closeLoading();
// Do nothing
return;
} else {
//
if(viewType == "modal") {
// Closes any loading screens
APP.closeLoading();
// modal
APP.Navigator.open(type, options);
return;
}
if(APP.Settings.useSlideMenu) {
// Select the row for the requested item
APP.SlideMenu && APP.SlideMenu.setIndex(_id);
} else {
// Move the tab selection indicator
APP.Tabs && APP.Tabs.setIndex(_id);
}
// Closes any loading screens
APP.closeLoading();
// Set current stack
APP.previousStack = APP.currentStack;
APP.previousType = APP.currentType;
APP.currentStack = _id;
APP.currentType = type;
// Create new controller stack if it doesn't exist
if(typeof APP.controllerStacks[_id] === "undefined") {
APP.controllerStacks[_id] = [];
}
if(APP.Device.isTablet) {
APP.currentDetailStack = _id;
if(typeof APP.detailStacks[_id] === "undefined") {
APP.detailStacks[_id] = [];
}
}
// Set current controller stack
var controllerStack = APP.controllerStacks[_id];
// If we're opening for the first time, create new screen
// Otherwise, add the last screen in the stack (screen we navigated away from earlier on)
var screen;
APP.hasDetail = false;
APP.previousDetailScreen = null;
if(controllerStack.length > 0) {
// Retrieve the last screen
if(APP.Device.isTablet) {
screen = controllerStack[0];
if(screen.type == "tablet") {
APP.hasDetail = true;
}
} else {
screen = controllerStack[controllerStack.length - 1];
}
// Tell the parent screen it was added to the window
/*
if(controllerStack[0].type == "tablet") {
controllerStack[0].fireEvent("APP:tabletScreenAdded");
} else {
controllerStack[0].fireEvent("APP:screenAdded");
}
*/
} else {
// Create a new screen
// TODO: Remove this. Find other way to determine if tablet version is available
// if(APP.Device.isTablet) {
// if(tabletSupport) {
// type = "tablet";
// APP.hasDetail = true;
// } else {
// switch(type) {
// case "article":
// case "event":
// case "facebook":
// case "flickr":
// case "podcast":
// case "share":
// case "vimeo":
// case "youtube":
// type = "tablet";
// APP.hasDetail = true;
// break;
// }
// }
// }
screen = Alloy.createController(type, options).getView();
// Add screen to the controller stack
controllerStack.push(screen);
// Tell the screen it was added to the window
/*
if(screen.type == "tablet") {
screen.fireEvent("APP:tabletScreenAdded");
} else {
screen.fireEvent("APP:screenAdded");
}
*/
}
// Add the screen to the window
APP.addScreen(screen);
// Reset the modal stack
APP.modalStack = [];
}
_.defer(function() {
Ti.App.fireEvent('handleNavigation', { "name" : type });
});
}, 700, true),
/**
* Open a child screen
* @param {String} _controller The name of the controller to open
* @param {Object} _params An optional dictionary of parameters to pass to the controller
* @param {Boolean} _modal Whether this is for the modal stack
* @param {Boolean} _sibling Whether this is a sibling view
*/
addChild: function(_controller, _params, _modal, _sibling) {
var stack;