react-native-move-sdk 0.1.4 → 0.2.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.
package/src/index.ts CHANGED
@@ -5,47 +5,60 @@ import {
5
5
  EmitterSubscription,
6
6
  } from 'react-native';
7
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';
8
+ export type SdkState = 'UNINITIALIZED' | 'READY' | 'RUNNING';
9
+ export type TripState = 'UNKNOWN' | 'DRIVING' | 'HALT' | 'IDLE' | 'IGNORED';
10
+ export type AuthState = 'UNKNOWN' | 'VALID' | 'EXPIRED';
11
+ export type DrivingService = 'DISTRACTION_FREE_DRIVING' | 'DRIVING_BEHAVIOUR';
12
+ export type WalkingService = 'LOCATION';
13
+ export type OtherService = 'POINTS_OF_INTEREST';
32
14
  export type TimelineDetectionService =
33
15
  | 'DRIVING'
34
- | 'BICYCLE'
16
+ | 'CYCLING'
35
17
  | 'WALKING'
36
- | 'PUBLIC_TRANSPORT';
18
+ | 'PUBLIC_TRANSPORT'
19
+ | 'AUTOMATIC_IMPACT_DETECTION'
20
+ | 'POINTS_OF_INTEREST';
21
+
22
+ export type ErrorReasons =
23
+ | 'BACKGROUND_LOCATION_PERMISSION_MISSING'
24
+ | 'LOCATION_PERMISSION_MISSING'
25
+ | 'MOTION_PERMISSION_MISSING'
26
+ | 'BATTERY_OPTIMIZATION'
27
+ | 'BATTERY_PERMISSION_MISSING'
28
+ | 'BLUETOOTH_PERMISSION_MISSING';
29
+
30
+ export type ErrorListService =
31
+ | 'DRIVING[]'
32
+ | 'DRIVING[DRIVING_BEHAVIOUR]'
33
+ | 'DRIVING[DISTRACTION_FREE_DRIVING]'
34
+ | 'DRIVING[DRIVING_BEHAVIOUR, DISTRACTION_FREE_DRIVING]'
35
+ | 'DRIVING[DISTRACTION_FREE_DRIVING, DRIVING_BEHAVIOUR]'
36
+ | 'WALKING[]'
37
+ | 'WALKING[LOCATION]'
38
+ | 'CYCLING'
39
+ | 'PUBLIC_TRANSPORT'
40
+ | 'AUTOMATIC_IMPACT_DETECTION'
41
+ | 'POINTS_OF_INTEREST';
42
+
43
+ export type ErrorListItem = {
44
+ reasons: Array<ErrorReasons>;
45
+ service?: ErrorListService;
46
+ };
47
+
48
+ export type ErrorListType = Array<ErrorListItem>;
49
+
37
50
  export type MoveSdkConfig = {
38
51
  timelineDetectionServices: TimelineDetectionService[];
39
52
  drivingServices: DrivingService[];
40
53
  walkingServices: WalkingService[];
41
- otherServices: OtherService[];
54
+ otherServices?: OtherService[];
42
55
  };
43
56
 
44
57
  export type MoveSdkAuth = {
45
- contractId: string;
58
+ userId: string;
46
59
  accessToken: string;
47
60
  refreshToken: string;
48
- productId: number;
61
+ projectId: number;
49
62
  };
50
63
 
51
64
  export type NotificationConfig = {
@@ -64,81 +77,46 @@ export type MoveSdkAndroidConfig = {
64
77
 
65
78
  export type ListenerSubscription = EmitterSubscription;
66
79
 
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
80
  export type AuthStateEvent = {
87
81
  state: AuthState;
88
82
  accessToken?: string;
89
83
  refreshToken?: string;
90
84
  };
91
85
 
92
- const { DolphinSdk: NativeMoveSdk } = NativeModules;
86
+ const { MoveSdk: NativeMoveSdk } = NativeModules;
93
87
  const eventEmitter = new NativeEventEmitter(NativeMoveSdk);
94
88
 
95
89
  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;
90
+ static READY: SdkState = 'READY';
91
+ static RUNNING: SdkState = 'RUNNING';
92
+ static UNINITIALIZED: SdkState = 'UNINITIALIZED';
123
93
 
124
- case 'ready':
125
- return MoveSdk.READY;
94
+ static AUTH_EXPIRED: AuthState = 'EXPIRED';
95
+ static AUTH_VALID: AuthState = 'VALID';
96
+ static AUTH_UNKNOWN: AuthState = 'UNKNOWN';
126
97
 
127
- case 'running':
128
- return MoveSdk.RUNNING;
98
+ static UNKNOWN: TripState = 'UNKNOWN';
99
+ static DRIVING: TripState = 'DRIVING';
100
+ static HALT: TripState = 'HALT';
101
+ static IDLE: TripState = 'IDLE';
102
+ static IGNORED: TripState = 'IGNORED';
129
103
 
130
- case 'error':
131
- return MoveSdk.ERROR;
132
- default:
133
- return null;
104
+ static sdkStateFromNative(sdkState: SdkState): SdkState {
105
+ if (
106
+ [
107
+ MoveSdk.UNINITIALIZED,
108
+ MoveSdk.READY,
109
+ MoveSdk.RUNNING,
110
+ MoveSdk.UNINITIALIZED,
111
+ ].includes(sdkState)
112
+ ) {
113
+ return sdkState;
134
114
  }
135
- }
136
115
 
137
- static tripStateFromNative(
138
- nativeTripState: NativeTripState
139
- ): TripState | null {
140
- const state = nativeTripState?.toLowerCase() as TripState;
116
+ return MoveSdk.UNINITIALIZED;
117
+ }
141
118
 
119
+ static tripStateFromNative(nativeTripState: TripState): TripState {
142
120
  if (
143
121
  [
144
122
  MoveSdk.UNKNOWN,
@@ -146,50 +124,35 @@ export default class MoveSdk {
146
124
  MoveSdk.HALT,
147
125
  MoveSdk.IDLE,
148
126
  MoveSdk.IGNORED,
149
- ].includes(state)
127
+ ].includes(nativeTripState)
150
128
  ) {
151
- return state;
129
+ return nativeTripState;
152
130
  }
153
131
 
154
- return null;
132
+ return MoveSdk.UNKNOWN;
155
133
  }
156
134
 
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;
135
+ static authStateFromNative(nativeAuthState: AuthState): AuthState {
136
+ if ([MoveSdk.AUTH_VALID, MoveSdk.AUTH_EXPIRED].includes(nativeAuthState)) {
137
+ return nativeAuthState;
164
138
  }
165
139
 
166
- return null;
167
- }
168
-
169
- static configurationErrorFromNative(
170
- nativeInitError: NativeConfigurationError
171
- ) {
172
- const state = nativeInitError?.toLowerCase() as ConfigurationError;
173
- return state;
140
+ return MoveSdk.AUTH_UNKNOWN;
174
141
  }
175
142
 
176
- static initialize(
143
+ static async setup(
177
144
  config: MoveSdkConfig,
178
145
  auth: MoveSdkAuth,
179
146
  android: MoveSdkAndroidConfig
180
- ) {
181
- const {
182
- timelineDetectionServices,
183
- drivingServices,
184
- walkingServices,
185
- otherServices,
186
- } = config;
147
+ ): Promise<void> {
148
+ const { timelineDetectionServices, drivingServices, walkingServices } =
149
+ config;
187
150
 
188
- const { contractId, productId, accessToken, refreshToken } = auth;
151
+ const { userId, projectId, accessToken, refreshToken } = auth;
189
152
 
190
153
  let platformParams: Array<string | boolean> = [];
191
154
  if (Platform.OS === 'android') {
192
- const { notifications, allowMockLocations } = android;
155
+ const { notifications } = android;
193
156
 
194
157
  const { tripNotification, recognitionNotification } = notifications;
195
158
 
@@ -210,27 +173,31 @@ export default class MoveSdk {
210
173
  tripNotification.channel.id,
211
174
  tripNotification.channel.name,
212
175
  tripNotification.channel.description,
213
- allowMockLocations,
214
176
  ];
215
177
  }
216
178
 
217
- NativeMoveSdk.initialize(
218
- contractId,
179
+ return await NativeMoveSdk.setup(
180
+ userId,
219
181
  accessToken,
220
182
  refreshToken,
221
- productId,
183
+ projectId,
222
184
  // Config
223
185
  timelineDetectionServices,
224
186
  drivingServices,
225
187
  walkingServices,
226
- otherServices,
188
+
227
189
  // Platform config
228
190
  ...platformParams
229
- ).catch((e) => {
230
- throw new Error(
231
- `Initialization failed, Exception occured with message ${e.message}`
232
- );
233
- });
191
+ );
192
+ }
193
+
194
+ static allowMockLocations(allowMockLocations: boolean) {
195
+ NativeMoveSdk.allowMockLocations(allowMockLocations);
196
+ }
197
+
198
+ /* Deprecated */
199
+ static init() {
200
+ NativeMoveSdk.init();
234
201
  }
235
202
 
236
203
  static resolveError() {
@@ -238,14 +205,18 @@ export default class MoveSdk {
238
205
  }
239
206
 
240
207
  static updateAuth(auth: MoveSdkAuth) {
241
- const { contractId, productId, accessToken, refreshToken } = auth;
242
- NativeMoveSdk.updateAuth(contractId, accessToken, refreshToken, productId);
208
+ const { userId, projectId, accessToken, refreshToken } = auth;
209
+ NativeMoveSdk.updateAuth(userId, accessToken, refreshToken, projectId);
243
210
  }
244
211
 
245
212
  static startAutomaticDetection() {
246
213
  NativeMoveSdk.startAutomaticDetection();
247
214
  }
248
215
 
216
+ static stopAutomaticDetection() {
217
+ NativeMoveSdk.stopAutomaticDetection();
218
+ }
219
+
249
220
  static forceTripRecognition() {
250
221
  NativeMoveSdk.forceTripRecognition();
251
222
  }
@@ -266,79 +237,61 @@ export default class MoveSdk {
266
237
  }
267
238
  }
268
239
 
269
- static stopAutomaticDetection() {
270
- NativeMoveSdk.stopAutomaticDetection();
240
+ static async synchronizeUserData(): Promise<boolean> {
241
+ return await NativeMoveSdk.synchronizeUserData();
271
242
  }
272
243
 
273
- static synchronizeUserData(): Promise<boolean> {
274
- return new Promise((resolve, _reject) => {
275
- NativeMoveSdk.synchronizeUserData().then((value: boolean) =>
276
- resolve(value)
277
- );
278
- });
244
+ static async getAuthState(): Promise<AuthState> {
245
+ const authState = await NativeMoveSdk.getAuthState();
246
+ return MoveSdk.authStateFromNative(authState);
279
247
  }
280
248
 
281
- static shutdown() {
282
- NativeMoveSdk.shutdown();
249
+ static async getState(): Promise<SdkState> {
250
+ const state = await NativeMoveSdk.getState();
251
+ return MoveSdk.sdkStateFromNative(state);
283
252
  }
284
253
 
285
- static getAuthState(): Promise<AuthState | null> {
286
- return new Promise((resolve, _reject) => {
287
- NativeMoveSdk.getAuthState().then((nativeState: NativeAuthState) =>
288
- resolve(MoveSdk.authStateFromNative(nativeState))
289
- );
290
- });
254
+ static async getTripState(): Promise<TripState> {
255
+ const tripState = await NativeMoveSdk.getTripState();
256
+ return MoveSdk.tripStateFromNative(tripState);
291
257
  }
292
258
 
293
- static getState(): Promise<SdkState | null> {
294
- return new Promise((resolve, _reject) => {
295
- NativeMoveSdk.getState().then((nativeState: NativeSdkState) =>
296
- resolve(MoveSdk.sdkStateFromNative(nativeState))
297
- );
298
- });
259
+ static async getErrors(): Promise<ErrorListType> {
260
+ return await NativeMoveSdk.getErrors();
299
261
  }
300
262
 
301
- static getTripState(): Promise<TripState | null> {
302
- return new Promise((resolve, _reject) => {
303
- NativeMoveSdk.getTripState().then((nativeState: NativeTripState) =>
304
- resolve(MoveSdk.tripStateFromNative(nativeState))
305
- );
306
- });
263
+ static async getWarnings(): Promise<ErrorListType> {
264
+ return await NativeMoveSdk.getWarnings();
265
+ }
266
+
267
+ static async initiateAssistanceCall(): Promise<boolean> {
268
+ return await NativeMoveSdk.initiateAssistanceCall();
269
+ }
270
+
271
+ static setAssistanceMetaData(data: string) {
272
+ NativeMoveSdk.setAssistanceMetaData(data);
307
273
  }
308
274
 
309
275
  static addTripStateListener(
310
- tripStateChanged: (state: TripState | null) => void
276
+ tripStateChanged: (state: TripState) => void
311
277
  ): ListenerSubscription {
312
- return eventEmitter.addListener('DolphinSdk-TripState', (event) =>
278
+ return eventEmitter.addListener('MOVE_SDK_TRIP_STATE', (event) =>
313
279
  tripStateChanged(MoveSdk.tripStateFromNative(event.state))
314
280
  );
315
281
  }
316
282
 
317
- static addSDKStateListener(
318
- sdkStateChanged: (event: { state: SdkState | null }) => void
283
+ static addSdkStateListener(
284
+ sdkStateChanged: (state: SdkState) => void
319
285
  ): ListenerSubscription {
320
- return eventEmitter.addListener('DolphinSdk-State', (event) => {
321
- const transformedEvent = {
322
- ...event,
323
- state: MoveSdk.sdkStateFromNative(event.state),
324
- };
325
-
326
- sdkStateChanged(transformedEvent);
286
+ return eventEmitter.addListener('MOVE_SDK_STATE', (event) => {
287
+ sdkStateChanged(MoveSdk.sdkStateFromNative(event.state));
327
288
  });
328
289
  }
329
290
 
330
- static addInitErrorListener(
331
- initErrorReceived: (error: ConfigurationError) => void
332
- ): ListenerSubscription {
333
- return eventEmitter.addListener('DolphinSdk-InitError', (event) =>
334
- initErrorReceived(MoveSdk.configurationErrorFromNative(event.error))
335
- );
336
- }
337
-
338
291
  static addAuthStateListener(
339
292
  authStateChanged: (event: AuthStateEvent) => void
340
293
  ): ListenerSubscription {
341
- return eventEmitter.addListener('DolphinSdk-AuthState', (event) => {
294
+ return eventEmitter.addListener('MOVE_SDK_AUTH_STATE', (event) => {
342
295
  const transformedEvent = {
343
296
  ...event,
344
297
  state: MoveSdk.authStateFromNative(event.state),
@@ -347,10 +300,26 @@ export default class MoveSdk {
347
300
  });
348
301
  }
349
302
 
303
+ static addErrorsListener(
304
+ errorsReceived: (event: ErrorListType) => void
305
+ ): ListenerSubscription {
306
+ return eventEmitter.addListener('MOVE_SDK_ERRORS', (event) => {
307
+ errorsReceived(event.errors);
308
+ });
309
+ }
310
+
311
+ static addWarningsListener(
312
+ warningsReceived: (event: ErrorListType) => void
313
+ ): ListenerSubscription {
314
+ return eventEmitter.addListener('MOVE_SDK_WARNINGS', (event) => {
315
+ warningsReceived(event.warnings);
316
+ });
317
+ }
318
+
350
319
  static addAppEventListener(
351
320
  appEventReceived: (event: string) => void
352
321
  ): ListenerSubscription {
353
- return eventEmitter.addListener('DolphinSdk-AppEvent', (event) => {
322
+ return eventEmitter.addListener('MOVE_SDK_APP_EVENT', (event) => {
354
323
  appEventReceived(event);
355
324
  });
356
325
  }
@@ -369,14 +338,22 @@ export default class MoveSdk {
369
338
  NativeMoveSdk.ignoreCurrentTrip();
370
339
  }
371
340
 
341
+ static requestMotionPermission() {
342
+ if (Platform.OS === 'ios') {
343
+ NativeMoveSdk.requestMotionPermission();
344
+ } else {
345
+ console.warn('MoveSdk.requestMotionPermission() is iOS only.');
346
+ }
347
+ }
348
+
349
+ static shutdown() {
350
+ NativeMoveSdk.shutdown();
351
+ }
352
+
372
353
  // **** PERMISSIONS MODULE START *****
373
354
 
374
- static canDrawOverlays(): Promise<boolean> {
375
- return new Promise((resolve, _reject) => {
376
- NativeMoveSdk.canDrawOverlays().then((result: boolean) =>
377
- resolve(result)
378
- );
379
- });
355
+ static async canDrawOverlays(): Promise<boolean> {
356
+ return await NativeMoveSdk.isAppIgnoringBatteryOptimization();
380
357
  }
381
358
 
382
359
  static requestDrawOverlaysPermission() {
@@ -389,12 +366,8 @@ export default class MoveSdk {
389
366
  }
390
367
  }
391
368
 
392
- static isAppIgnoringBatteryOptimization(): Promise<boolean> {
393
- return new Promise((resolve, _reject) => {
394
- NativeMoveSdk.isAppIgnoringBatteryOptimization().then((result: boolean) =>
395
- resolve(result)
396
- );
397
- });
369
+ static async isAppIgnoringBatteryOptimization(): Promise<boolean> {
370
+ return await NativeMoveSdk.isAppIgnoringBatteryOptimization();
398
371
  }
399
372
 
400
373
  static requestAppIgnoringBatteryOptimization() {