react-native-move-sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +159 -0
  3. package/android/.project +17 -0
  4. package/android/build.gradle +145 -0
  5. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  6. package/android/gradle/wrapper/gradle-wrapper.properties +5 -0
  7. package/android/gradle.properties +4 -0
  8. package/android/gradlew +183 -0
  9. package/android/gradlew.bat +100 -0
  10. package/android/src/main/AndroidManifest.xml +3 -0
  11. package/android/src/main/java/in/dolph/move/sdk/MoveNotificationConfig.kt +41 -0
  12. package/android/src/main/java/in/dolph/move/sdk/MoveSdkConfig.kt +18 -0
  13. package/android/src/main/java/in/dolph/move/sdk/MoveSdkModule.kt +293 -0
  14. package/android/src/main/java/in/dolph/move/sdk/MoveSdkPackage.kt +19 -0
  15. package/android/src/main/java/in/dolph/move/sdk/MoveSdkRepository.kt +194 -0
  16. package/android/src/main/java/in/dolph/move/sdk/NativeMoveSdkWrapper.kt +305 -0
  17. package/android/src/main/res/drawable-anydpi-v24/ic_notification.xml +13 -0
  18. package/android/src/main/res/drawable-hdpi/ic_notification.png +0 -0
  19. package/android/src/main/res/drawable-mdpi/ic_notification.png +0 -0
  20. package/android/src/main/res/drawable-xhdpi/ic_notification.png +0 -0
  21. package/android/src/main/res/drawable-xxhdpi/ic_notification.png +0 -0
  22. package/android/src/main/res/values/strings.xml +9 -0
  23. package/android/src/main/strings.xml +0 -0
  24. package/ios/MoveSdk-Bridging-Header.h +2 -0
  25. package/ios/MoveSdk.xcodeproj/project.pbxproj +289 -0
  26. package/ios/MoveSdk.xcodeproj/project.xcworkspace/contents.xcworkspacedata +4 -0
  27. package/ios/MoveSdk.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  28. package/ios/NativeModule/DolphinSdk.h +58 -0
  29. package/ios/NativeModule/DolphinSdk.swift +353 -0
  30. package/ios/NativeModule/DolphinSdkDemoReactNative-Bridging-Header.h +8 -0
  31. package/lib/commonjs/index.js +280 -0
  32. package/lib/commonjs/index.js.map +1 -0
  33. package/lib/module/index.js +269 -0
  34. package/lib/module/index.js.map +1 -0
  35. package/lib/typescript/index.d.ts +98 -0
  36. package/package.json +135 -0
  37. package/react-native-move-sdk.podspec +20 -0
  38. package/src/index.ts +407 -0
package/src/index.ts ADDED
@@ -0,0 +1,407 @@
1
+ import {
2
+ NativeModules,
3
+ NativeEventEmitter,
4
+ Platform,
5
+ EmitterSubscription,
6
+ } from 'react-native';
7
+
8
+ export type AndroidSdkState = 'Error' | 'Uninitialised' | 'Ready' | 'Running';
9
+ export type SdkState = 'error' | 'uninitialized' | 'ready' | 'running';
10
+
11
+ export type NativeSdkState = AndroidSdkState | SdkState;
12
+
13
+ export type TripState = 'unknown' | 'driving' | 'halt' | 'idle' | 'ignored';
14
+ export type AuthState = 'unknown' | 'valid' | 'expired';
15
+
16
+ export type AndroidTripState =
17
+ | 'UNKNOWN'
18
+ | 'DRIVING'
19
+ | 'HALT'
20
+ | 'IDLE'
21
+ | 'IGNORED';
22
+
23
+ export type NativeTripState = AndroidTripState | TripState;
24
+
25
+ export type AndroidAuthState = 'UNKNOWN' | 'VALID' | 'EXPIRED';
26
+ export type IosAuthState = 'unknown' | 'valid' | 'expired';
27
+ export type NativeAuthState = AndroidAuthState | IosAuthState;
28
+
29
+ export type DrivingService = 'DistractionFreeDriving' | 'DrivingBehaviour';
30
+ export type WalkingService = 'Location';
31
+ export type OtherService = 'PointsOfInterest';
32
+ export type TimelineDetectionService =
33
+ | 'DRIVING'
34
+ | 'BICYCLE'
35
+ | 'WALKING'
36
+ | 'PUBLIC_TRANSPORT';
37
+ export type MoveSdkConfig = {
38
+ timelineDetectionServices: TimelineDetectionService[];
39
+ drivingServices: DrivingService[];
40
+ walkingServices: WalkingService[];
41
+ otherServices: OtherService[];
42
+ };
43
+
44
+ export type MoveSdkAuth = {
45
+ contractId: string;
46
+ accessToken: string;
47
+ refreshToken: string;
48
+ productId: number;
49
+ };
50
+
51
+ export type NotificationConfig = {
52
+ title: string;
53
+ text: string;
54
+ channel: { id: string; name: string; description: string };
55
+ };
56
+
57
+ export type MoveSdkAndroidConfig = {
58
+ notifications: {
59
+ recognitionNotification: NotificationConfig;
60
+ tripNotification: NotificationConfig;
61
+ };
62
+ allowMockLocations: boolean;
63
+ };
64
+
65
+ export type ListenerSubscription = EmitterSubscription;
66
+
67
+ export type ConfigurationError =
68
+ | 'authinvalid'
69
+ | 'configmismatch'
70
+ | 'serviceunreachable';
71
+
72
+ export type IosConfigurationError =
73
+ | 'serviceUnreachable'
74
+ | 'authInvalid'
75
+ | 'configMismatch';
76
+
77
+ export type AndroidConfigurationError =
78
+ | 'ServiceUnreachable'
79
+ | 'AuthInvalid'
80
+ | 'ConfigMismatch';
81
+
82
+ export type NativeConfigurationError =
83
+ | IosConfigurationError
84
+ | AndroidConfigurationError;
85
+
86
+ export type AuthStateEvent = {
87
+ state: AuthState;
88
+ accessToken?: string;
89
+ refreshToken?: string;
90
+ };
91
+
92
+ const { DolphinSdk: NativeMoveSdk } = NativeModules;
93
+ const eventEmitter = new NativeEventEmitter(NativeMoveSdk);
94
+
95
+ export default class MoveSdk {
96
+ static ERROR: SdkState = 'error';
97
+ static READY: SdkState = 'ready';
98
+ static RUNNING: SdkState = 'running';
99
+ static UNINITIALIZED: SdkState = 'uninitialized';
100
+
101
+ static AUTH_INVALID: ConfigurationError = 'authinvalid';
102
+ static AUTH_EXPIRED: AuthState = 'expired';
103
+ static AUTH_VALID: AuthState = 'valid';
104
+
105
+ static UNKNOWN: TripState = 'unknown';
106
+ static DRIVING: TripState = 'driving';
107
+ static HALT: TripState = 'halt';
108
+ static IDLE: TripState = 'idle';
109
+ static IGNORED: TripState = 'ignored';
110
+
111
+ static sdkStateFromNative(nativeSdkState: NativeSdkState): SdkState | null {
112
+ let stateString = nativeSdkState?.toLowerCase() as SdkState;
113
+
114
+ if (stateString?.includes('(')) {
115
+ stateString = stateString
116
+ ?.substring(0, stateString?.indexOf('('))
117
+ .toLowerCase() as SdkState;
118
+ }
119
+
120
+ switch (stateString) {
121
+ case 'uninitialized':
122
+ return MoveSdk.UNINITIALIZED;
123
+
124
+ case 'ready':
125
+ return MoveSdk.READY;
126
+
127
+ case 'running':
128
+ return MoveSdk.RUNNING;
129
+
130
+ case 'error':
131
+ return MoveSdk.ERROR;
132
+ default:
133
+ return null;
134
+ }
135
+ }
136
+
137
+ static tripStateFromNative(
138
+ nativeTripState: NativeTripState
139
+ ): TripState | null {
140
+ const state = nativeTripState?.toLowerCase() as TripState;
141
+
142
+ if (
143
+ [
144
+ MoveSdk.UNKNOWN,
145
+ MoveSdk.DRIVING,
146
+ MoveSdk.HALT,
147
+ MoveSdk.IDLE,
148
+ MoveSdk.IGNORED,
149
+ ].includes(state)
150
+ ) {
151
+ return state;
152
+ }
153
+
154
+ return null;
155
+ }
156
+
157
+ static authStateFromNative(
158
+ nativeAuthState: NativeAuthState
159
+ ): AuthState | null {
160
+ const state = nativeAuthState?.toLowerCase() as AuthState;
161
+
162
+ if ([MoveSdk.AUTH_VALID, MoveSdk.AUTH_EXPIRED].includes(state)) {
163
+ return state;
164
+ }
165
+
166
+ return null;
167
+ }
168
+
169
+ static configurationErrorFromNative(
170
+ nativeInitError: NativeConfigurationError
171
+ ) {
172
+ const state = nativeInitError?.toLowerCase() as ConfigurationError;
173
+ return state;
174
+ }
175
+
176
+ static initialize(
177
+ config: MoveSdkConfig,
178
+ auth: MoveSdkAuth,
179
+ android: MoveSdkAndroidConfig
180
+ ) {
181
+ const {
182
+ timelineDetectionServices,
183
+ drivingServices,
184
+ walkingServices,
185
+ otherServices,
186
+ } = config;
187
+
188
+ const { contractId, productId, accessToken, refreshToken } = auth;
189
+
190
+ let platformParams: Array<string | boolean> = [];
191
+ if (Platform.OS === 'android') {
192
+ const { notifications, allowMockLocations } = android;
193
+
194
+ const { tripNotification, recognitionNotification } = notifications;
195
+
196
+ if (!tripNotification || !recognitionNotification) {
197
+ throw new Error(
198
+ 'MOVE SDK needs notification configuration for Android'
199
+ );
200
+ }
201
+
202
+ platformParams = [
203
+ recognitionNotification.title,
204
+ recognitionNotification.text,
205
+ recognitionNotification.channel.id,
206
+ recognitionNotification.channel.name,
207
+ recognitionNotification.channel.description,
208
+ tripNotification.title,
209
+ tripNotification.text,
210
+ tripNotification.channel.id,
211
+ tripNotification.channel.name,
212
+ tripNotification.channel.description,
213
+ allowMockLocations,
214
+ ];
215
+ }
216
+
217
+ NativeMoveSdk.initialize(
218
+ contractId,
219
+ accessToken,
220
+ refreshToken,
221
+ productId,
222
+ // Config
223
+ timelineDetectionServices,
224
+ drivingServices,
225
+ walkingServices,
226
+ otherServices,
227
+ // Platform config
228
+ ...platformParams
229
+ ).catch((e) => {
230
+ throw new Error(
231
+ `Initialization failed, Exception occured with message ${e.message}`
232
+ );
233
+ });
234
+ }
235
+
236
+ static resolveError() {
237
+ NativeMoveSdk.resolveError();
238
+ }
239
+
240
+ static updateAuth(auth: MoveSdkAuth) {
241
+ const { contractId, productId, accessToken, refreshToken } = auth;
242
+ NativeMoveSdk.updateAuth(contractId, accessToken, refreshToken, productId);
243
+ }
244
+
245
+ static startAutomaticDetection() {
246
+ NativeMoveSdk.startAutomaticDetection();
247
+ }
248
+
249
+ static forceTripRecognition() {
250
+ NativeMoveSdk.forceTripRecognition();
251
+ }
252
+
253
+ static keepInForeground(enabled: boolean) {
254
+ if (Platform.OS === 'android') {
255
+ NativeMoveSdk.keepInForeground(enabled);
256
+ } else {
257
+ console.warn('MoveSdk.keepInForeground() is Android OS only.');
258
+ }
259
+ }
260
+
261
+ static keepActive(enabled: boolean) {
262
+ if (Platform.OS === 'android') {
263
+ NativeMoveSdk.keepActive(enabled);
264
+ } else {
265
+ console.warn('MoveSdk.keepActive() is Android OS only.');
266
+ }
267
+ }
268
+
269
+ static stopAutomaticDetection() {
270
+ NativeMoveSdk.stopAutomaticDetection();
271
+ }
272
+
273
+ static synchronizeUserData() {
274
+ NativeMoveSdk.synchronizeUserData();
275
+ }
276
+
277
+ static shutdown() {
278
+ NativeMoveSdk.shutdown();
279
+ }
280
+
281
+ static getAuthState(): Promise<AuthState | null> {
282
+ return new Promise((resolve, _reject) => {
283
+ NativeMoveSdk.getAuthState().then((nativeState: NativeAuthState) =>
284
+ resolve(MoveSdk.authStateFromNative(nativeState))
285
+ );
286
+ });
287
+ }
288
+
289
+ static getState(): Promise<SdkState | null> {
290
+ return new Promise((resolve, _reject) => {
291
+ NativeMoveSdk.getState().then((nativeState: NativeSdkState) =>
292
+ resolve(MoveSdk.sdkStateFromNative(nativeState))
293
+ );
294
+ });
295
+ }
296
+
297
+ static getTripState(): Promise<TripState | null> {
298
+ return new Promise((resolve, _reject) => {
299
+ NativeMoveSdk.getTripState().then((nativeState: NativeTripState) =>
300
+ resolve(MoveSdk.tripStateFromNative(nativeState))
301
+ );
302
+ });
303
+ }
304
+
305
+ static addTripStateListener(
306
+ tripStateChanged: (state: TripState | null) => void
307
+ ): ListenerSubscription {
308
+ return eventEmitter.addListener('DolphinSdk-TripState', (event) =>
309
+ tripStateChanged(MoveSdk.tripStateFromNative(event.state))
310
+ );
311
+ }
312
+
313
+ static addSDKStateListener(
314
+ sdkStateChanged: (event: { state: SdkState | null }) => void
315
+ ): ListenerSubscription {
316
+ return eventEmitter.addListener('DolphinSdk-State', (event) => {
317
+ const transformedEvent = {
318
+ ...event,
319
+ state: MoveSdk.sdkStateFromNative(event.state),
320
+ };
321
+
322
+ sdkStateChanged(transformedEvent);
323
+ });
324
+ }
325
+
326
+ static addInitErrorListener(
327
+ initErrorReceived: (error: ConfigurationError) => void
328
+ ): ListenerSubscription {
329
+ return eventEmitter.addListener('DolphinSdk-InitError', (event) =>
330
+ initErrorReceived(MoveSdk.configurationErrorFromNative(event.error))
331
+ );
332
+ }
333
+
334
+ static addAuthStateListener(
335
+ authStateChanged: (event: AuthStateEvent) => void
336
+ ): ListenerSubscription {
337
+ return eventEmitter.addListener('DolphinSdk-AuthState', (event) => {
338
+ const transformedEvent = {
339
+ ...event,
340
+ state: MoveSdk.authStateFromNative(event.state),
341
+ };
342
+ authStateChanged(transformedEvent);
343
+ });
344
+ }
345
+
346
+ static addAppEventListener(
347
+ appEventReceived: (event: string) => void
348
+ ): ListenerSubscription {
349
+ return eventEmitter.addListener('DolphinSdk-AppEvent', (event) => {
350
+ appEventReceived(event);
351
+ });
352
+ }
353
+
354
+ static version(): string {
355
+ return Platform.OS === 'android'
356
+ ? NativeMoveSdk.getConstants().version
357
+ : 'Unknown';
358
+ }
359
+
360
+ static finishCurrentTrip() {
361
+ NativeMoveSdk.finishCurrentTrip();
362
+ }
363
+
364
+ static ignoreCurrentTrip() {
365
+ NativeMoveSdk.ignoreCurrentTrip();
366
+ }
367
+
368
+ // **** PERMISSIONS MODULE START *****
369
+
370
+ static canDrawOverlays(): Promise<boolean> {
371
+ return new Promise((resolve, _reject) => {
372
+ NativeMoveSdk.canDrawOverlays().then((result: boolean) =>
373
+ resolve(result)
374
+ );
375
+ });
376
+ }
377
+
378
+ static requestDrawOverlaysPermission() {
379
+ if (Platform.OS === 'android') {
380
+ NativeMoveSdk.requestDrawOverlaysPermission();
381
+ } else {
382
+ console.warn(
383
+ 'MoveSdk.requestAppIgnoringBatteryOptimization() is Android OS only.'
384
+ );
385
+ }
386
+ }
387
+
388
+ static isAppIgnoringBatteryOptimization(): Promise<boolean> {
389
+ return new Promise((resolve, _reject) => {
390
+ NativeMoveSdk.isAppIgnoringBatteryOptimization().then((result: boolean) =>
391
+ resolve(result)
392
+ );
393
+ });
394
+ }
395
+
396
+ static requestAppIgnoringBatteryOptimization() {
397
+ if (Platform.OS === 'android') {
398
+ NativeMoveSdk.requestAppIgnoringBatteryOptimization();
399
+ } else {
400
+ console.warn(
401
+ 'MoveSdk.requestAppIgnoringBatteryOptimization() is Android OS only.'
402
+ );
403
+ }
404
+ }
405
+
406
+ // **** PERMISSIONS MODULE END *****
407
+ }