This repository has been archived by the owner on Oct 11, 2020. It is now read-only.
/
ns-app.service.ts
executable file
·163 lines (131 loc) · 4.89 KB
/
ns-app.service.ts
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
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { NSLocationStrategy } from 'nativescript-angular/router/ns-location-strategy';
// nativescript
import * as nsApp from 'application';
import { isAndroid, isIOS } from 'platform';
if (isIOS) {
/*
nsApp.ios.delegate = require('../PATH/TO/DELEGATE').CustomAppDelegate;
*/
}
// libs
import { Store } from '@ngrx/store';
import { TranslateService } from '@ngx-translate/core';
// app
import { AppService } from '../../../app/shared/core/services/app.service';
import { Config } from '../../../app/shared/core/utils';
import { LogService, WindowService, RouterExtensions } from '../../../app/shared/core/index';
import { ActionBarUtil } from '../utils/actionbar.util';
declare var android: any;
const trackingId = '<%= TNS_CONFIG.ANALYTICS_TRACKING_ID %>';
@Injectable()
export class NSAppService extends AppService {
nsApp = nsApp;
// Remember to update iOS and android constructors if you change dependencies
// @Inject decorator is used on injectables here since this component merely extends AppComponent
// Since @Component decorator is not used here, this ensures metadata will be generated
constructor(public log: LogService,
public store: Store<any>,
public router: Router,
public locationstrategy: NSLocationStrategy,
public translate: TranslateService,
public window: WindowService
) {
super(log);
this.log.debug('NSAppService constructor');
// translate.onLangChange.skip(1).subscribe((args) => {
// this.log.info(`NSAppComponent translate.onLangChange(${args.lang})`);
// // translate.setDefaultLang(args.lang);
// translate.currentLang = args.lang;
// window.navigator.language = args.lang;
// });
/**
* Top status bar on iOS and/or Android
* iOs {number}
* 0: default
* 1: light
* Android {string}
* hex value
*/
ActionBarUtil.STATUSBAR_STYLE(
isIOS ? 1 : '#3280CF'
);
if (String('<%= BUILD_TYPE %>') !== 'prod') {
log.debug('NSAppCmp ----');
router.events.subscribe((e) => {
this.log.debug(`Router Event: ${e.toString()}`);
});
}
// Fix: Reset all nsApp events before subscribing to avoid Duplicate events.
// this.unsubscribeAll();
nsApp.on(nsApp.launchEvent, (eventData: nsApp.LaunchEventData) => {
this.log.info('TNS Application - Launched!');
});
nsApp.on(nsApp.suspendEvent, (eventData: nsApp.ApplicationEventData) => {
this.log.info('TNS Application - Suspended');
});
nsApp.on(nsApp.resumeEvent, (eventData: nsApp.ApplicationEventData) => {
this.log.info('TNS Application - Resumed');
});
nsApp.on(nsApp.lowMemoryEvent, (eventData: nsApp.ApplicationEventData) => {
this.log.warn('TNS Application - !!! LOW MEMORY !!!');
});
nsApp.on(nsApp.exitEvent, (eventData: any) => {
this.log.info('TNS Application - EXIT');
this.unsubscribeAll();
});
this.setupAndroid();
this.setupIOS();
}
protected handleUncaughtError(err: any, platform: 'android' | 'ios') {
this.log.info(`TNS Application - Uncaught Error: ${err.message}`);
const errorDescription: any = {
message: err.message,
android: platform === 'android',
ios: platform === 'ios',
stackTrace: err.stackTrace,
nativeException: err.nativeException,
};
for (const key of Object.keys(errorDescription)) {
if (errorDescription[key]) {
console.log(`**** START - errorDescription.${key} - ****\n\n\n${errorDescription[key]}\n\n\n*** END - ${key} - ****`);
}
}
}
private unsubscribeAll() {
nsApp.off(nsApp.launchEvent);
nsApp.off(nsApp.suspendEvent);
nsApp.off(nsApp.resumeEvent);
nsApp.off(nsApp.lowMemoryEvent);
nsApp.off(nsApp.exitEvent);
nsApp.off(nsApp.uncaughtErrorEvent);
}
private setupAndroid() {
if (!isAndroid) {
return;
}
// Android specific code goes here
this.nsApp.android.on(this.nsApp.AndroidApplication.activityBackPressedEvent, (args) => {
const states = this.locationstrategy._getStates();
this.log.info(`Event: ${args.eventName}\nActivity: ${args.activity}\nstates: ${JSON.stringify(states)}`);
});
this.nsApp.android.on(this.nsApp.AndroidApplication.activityResumedEvent, (args) => {
if (args.activity.getIntent().getAction() === android.content.Intent.ACTION_VIEW) {
const intentData = args.activity.getIntent().getData();
console.log(`Android received VIEW intent, with data: ${intentData}`);
}
});
this.nsApp.on(this.nsApp.uncaughtErrorEvent, (err) => {
this.handleUncaughtError(err.android, 'android');
});
}
private setupIOS() {
if (!isIOS) {
return;
}
this.nsApp.on(this.nsApp.uncaughtErrorEvent, (err) => {
this.handleUncaughtError(err.ios, 'ios');
});
}
}