@doorstepai/dropoff-sdk 2.1.2 → 2.2.2

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 (43) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/README.md +308 -41
  3. package/android/build.gradle +5 -1
  4. package/android/src/main/java/com/doorstepai/dropoffsdk/DoorstepAIModule.kt +566 -2
  5. package/ios/DoorstepAI.mm +12 -2
  6. package/ios/DoorstepAI.swift +151 -2
  7. package/ios/DoorstepDropoffSDK.xcframework/Info.plist +5 -5
  8. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64/DoorstepDropoffSDK.framework/DoorstepDropoffSDK +0 -0
  9. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64/DoorstepDropoffSDK.framework/Modules/DoorstepDropoffSDK.swiftmodule/arm64-apple-ios.abi.json +10582 -5745
  10. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64/DoorstepDropoffSDK.framework/Modules/DoorstepDropoffSDK.swiftmodule/arm64-apple-ios.private.swiftinterface +95 -4
  11. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64/DoorstepDropoffSDK.framework/Modules/DoorstepDropoffSDK.swiftmodule/arm64-apple-ios.swiftdoc +0 -0
  12. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64/DoorstepDropoffSDK.framework/Modules/DoorstepDropoffSDK.swiftmodule/arm64-apple-ios.swiftinterface +95 -4
  13. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64/DoorstepDropoffSDK.framework/_CodeSignature/CodeResources +10 -10
  14. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64/dSYMs/DoorstepDropoffSDK.framework.dSYM/Contents/Resources/DWARF/DoorstepDropoffSDK +0 -0
  15. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64/dSYMs/DoorstepDropoffSDK.framework.dSYM/Contents/Resources/Relocations/aarch64/DoorstepDropoffSDK.yml +3833 -2687
  16. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64_x86_64-simulator/DoorstepDropoffSDK.framework/DoorstepDropoffSDK +0 -0
  17. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64_x86_64-simulator/DoorstepDropoffSDK.framework/Modules/DoorstepDropoffSDK.swiftmodule/arm64-apple-ios-simulator.abi.json +10582 -5745
  18. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64_x86_64-simulator/DoorstepDropoffSDK.framework/Modules/DoorstepDropoffSDK.swiftmodule/arm64-apple-ios-simulator.private.swiftinterface +95 -4
  19. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64_x86_64-simulator/DoorstepDropoffSDK.framework/Modules/DoorstepDropoffSDK.swiftmodule/arm64-apple-ios-simulator.swiftdoc +0 -0
  20. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64_x86_64-simulator/DoorstepDropoffSDK.framework/Modules/DoorstepDropoffSDK.swiftmodule/arm64-apple-ios-simulator.swiftinterface +95 -4
  21. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64_x86_64-simulator/DoorstepDropoffSDK.framework/Modules/DoorstepDropoffSDK.swiftmodule/x86_64-apple-ios-simulator.abi.json +10582 -5745
  22. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64_x86_64-simulator/DoorstepDropoffSDK.framework/Modules/DoorstepDropoffSDK.swiftmodule/x86_64-apple-ios-simulator.private.swiftinterface +95 -4
  23. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64_x86_64-simulator/DoorstepDropoffSDK.framework/Modules/DoorstepDropoffSDK.swiftmodule/x86_64-apple-ios-simulator.swiftdoc +0 -0
  24. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64_x86_64-simulator/DoorstepDropoffSDK.framework/Modules/DoorstepDropoffSDK.swiftmodule/x86_64-apple-ios-simulator.swiftinterface +95 -4
  25. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64_x86_64-simulator/DoorstepDropoffSDK.framework/_CodeSignature/CodeResources +20 -20
  26. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64_x86_64-simulator/dSYMs/DoorstepDropoffSDK.framework.dSYM/Contents/Resources/DWARF/DoorstepDropoffSDK +0 -0
  27. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64_x86_64-simulator/dSYMs/DoorstepDropoffSDK.framework.dSYM/Contents/Resources/Relocations/aarch64/DoorstepDropoffSDK.yml +3833 -2687
  28. package/ios/DoorstepDropoffSDK.xcframework/ios-arm64_x86_64-simulator/dSYMs/DoorstepDropoffSDK.framework.dSYM/Contents/Resources/Relocations/x86_64/DoorstepDropoffSDK.yml +3802 -2674
  29. package/lib/module/DoorstepAI.js +425 -12
  30. package/lib/module/DoorstepAI.js.map +1 -1
  31. package/lib/module/RootDoorstepAI.js +88 -26
  32. package/lib/module/RootDoorstepAI.js.map +1 -1
  33. package/lib/module/index.js.map +1 -1
  34. package/lib/typescript/src/DoorstepAI.d.ts +349 -7
  35. package/lib/typescript/src/DoorstepAI.d.ts.map +1 -1
  36. package/lib/typescript/src/RootDoorstepAI.d.ts +31 -3
  37. package/lib/typescript/src/RootDoorstepAI.d.ts.map +1 -1
  38. package/lib/typescript/src/index.d.ts +1 -1
  39. package/lib/typescript/src/index.d.ts.map +1 -1
  40. package/package.json +2 -1
  41. package/src/DoorstepAI.tsx +575 -23
  42. package/src/RootDoorstepAI.tsx +130 -47
  43. package/src/index.tsx +10 -0
@@ -3,79 +3,162 @@ import {
3
3
  View,
4
4
  Platform,
5
5
  StyleSheet,
6
- PermissionsAndroid,
6
+ AppState,
7
7
  } from 'react-native';
8
8
 
9
9
  import { useEffect } from 'react';
10
10
 
11
11
  import { DoorstepAI } from './DoorstepAI';
12
+ import type { DoorstepPermissionInput } from './DoorstepAI';
12
13
 
13
14
  const DoorstepAIView = requireNativeComponent('RootDoorstepAIView');
14
15
 
16
+ /**
17
+ * Drop-in bootstrap: mount it once, near the root, and it runs the SDK's
18
+ * `init` + `setApiKey` and the permission sequence for you. Everything it does is
19
+ * available a la carte on {@link DoorstepAI} — this component exists so a host
20
+ * that just wants the default flow does not have to write it.
21
+ */
15
22
  export function RootDoorstepAI(props: {
16
23
  apiKey: string;
17
24
  notificationTitle?: string;
18
25
  notificationText?: string;
19
- /** Opt-in: also request Bluetooth (BLE) permission. Off by default — only
20
- * needed if the `ble` config section is enabled for your fleet. */
26
+ /**
27
+ * The whitelist handed to `requestPermissions`: **exactly these buckets are
28
+ * requested and nothing else.** Omit it to request every bucket the platform
29
+ * has, which is the SDK's own default.
30
+ */
31
+ permissions?: DoorstepPermissionInput[];
32
+ /**
33
+ * @deprecated Pass a {@link permissions} whitelist instead — it can narrow any
34
+ * bucket, not just this one. Kept because it was the published prop: `false`
35
+ * drops the platform's Bluetooth bucket from the default whitelist. Ignored when
36
+ * `permissions` is given.
37
+ */
21
38
  requestBluetooth?: boolean;
39
+ /**
40
+ * Run the second-phase background-location request (`locationAlways`) once
41
+ * foreground location has been granted. Default on.
42
+ *
43
+ * It has to be a second phase because Android forbids bundling background
44
+ * location with the foreground ask — a bundled request is auto-denied from
45
+ * Android 11 on. Turn it off if your app asks for "Allow all the time" from its
46
+ * own onboarding UI; without the grant the SDK cannot collect while
47
+ * backgrounded and route geofences fire foreground-only.
48
+ */
49
+ requestBackgroundLocation?: boolean;
22
50
  }) {
51
+ const {
52
+ apiKey,
53
+ notificationTitle,
54
+ notificationText,
55
+ permissions,
56
+ requestBluetooth,
57
+ requestBackgroundLocation,
58
+ } = props;
59
+
23
60
  useEffect(() => {
61
+ let cancelled = false;
62
+ let backgroundPhaseSettled = false;
63
+
64
+ // The effective whitelist. `permissions` wins; `requestBluetooth: false` is
65
+ // expressed as "every bucket except this platform's BLE one", which is what
66
+ // the old two-boolean requester meant by it.
67
+ const whitelist: DoorstepPermissionInput[] | undefined =
68
+ permissions ??
69
+ (requestBluetooth === false
70
+ ? DoorstepAI.allPermissionBuckets().filter(
71
+ (bucket) => bucket !== 'bluetooth' && bucket !== 'bluetoothScan'
72
+ )
73
+ : undefined);
74
+
75
+ // Whether background location is ours to ask for at all: the host can turn the
76
+ // phase off, and a whitelist that does not name the bucket must not have it
77
+ // added back behind the host's back.
78
+ const backgroundInScope =
79
+ requestBackgroundLocation !== false &&
80
+ (whitelist == null ||
81
+ whitelist.some((p) => p === 'locationAlways' || p === 'location'));
82
+
83
+ /**
84
+ * Phase 2. `locationAlways` is only ever asked ALONE and only after foreground
85
+ * location is granted — the SDK enforces both rules and reports a refusal to
86
+ * ask rather than asking anyway, so this can be called optimistically. It is
87
+ * re-run on each foreground because on Android the foreground grant result is
88
+ * delivered to the Activity, not to JS.
89
+ */
90
+ const runBackgroundLocationPhase = async () => {
91
+ if (cancelled || backgroundPhaseSettled || !backgroundInScope) return;
92
+ try {
93
+ const { statuses } = await DoorstepAI.checkPermissions([
94
+ 'locationWhenInUse',
95
+ 'locationAlways',
96
+ ]);
97
+ const always = statuses.locationAlways?.state;
98
+ if (always === 'granted') {
99
+ backgroundPhaseSettled = true;
100
+ return;
101
+ }
102
+ // Foreground first: asking earlier spends the request on an automatic
103
+ // denial (Android) or shows the wrong prompt (iOS).
104
+ if (statuses.locationWhenInUse?.state !== 'granted') return;
105
+ // On iOS `whenInUseOnly` is a deliberate user choice and the SDK declines
106
+ // to re-prompt for it; the call is still made because native is the right
107
+ // place for that decision, and it comes back `deferred` rather than
108
+ // prompting.
109
+ backgroundPhaseSettled = true;
110
+ await DoorstepAI.requestPermissions(['locationAlways']);
111
+ } catch (err) {
112
+ console.warn('DOORSTEPAI - background location request failed:', err);
113
+ }
114
+ };
115
+
116
+ // Registered synchronously so the effect cleanup always has it to remove.
117
+ const appStateSubscription = AppState.addEventListener(
118
+ 'change',
119
+ (state) => {
120
+ if (state === 'active') runBackgroundLocationPhase();
121
+ }
122
+ );
123
+
24
124
  const initializeAndRequestPermissions = async () => {
25
125
  if (Platform.OS === 'android') {
26
- await DoorstepAI.init(
27
- props.notificationTitle || '',
28
- props.notificationText || ''
29
- );
126
+ await DoorstepAI.init(notificationTitle || '', notificationText || '');
30
127
  }
31
128
 
32
- DoorstepAI.setApiKey(props.apiKey);
129
+ DoorstepAI.setApiKey(apiKey);
130
+ if (cancelled) return;
33
131
 
34
- if (Platform.OS === 'android') {
35
- try {
36
- const permissionsToRequest = [
37
- PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
38
- PermissionsAndroid.PERMISSIONS.ACTIVITY_RECOGNITION,
39
- ];
40
- // Opt-in Bluetooth (BLE) — Android 12+ "Nearby devices" (BLUETOOTH_SCAN).
41
- if (props.requestBluetooth && PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN) {
42
- permissionsToRequest.push(PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN);
43
- }
44
-
45
- const granted =
46
- await PermissionsAndroid.requestMultiple(permissionsToRequest);
47
-
48
- if (
49
- granted[PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION] ===
50
- PermissionsAndroid.RESULTS.GRANTED &&
51
- granted[PermissionsAndroid.PERMISSIONS.ACTIVITY_RECOGNITION] ===
52
- PermissionsAndroid.RESULTS.GRANTED
53
- ) {
54
- console.log('Required Android permissions granted');
55
- } else {
56
- console.warn('One or more required Android permissions denied');
57
- }
58
- } catch (err) {
59
- console.warn('Error requesting Android permissions:', err);
60
- }
61
- } else if (Platform.OS === 'ios') {
62
- console.log(
63
- 'iOS: Ensure location and motion usage descriptions are in Info.plist'
64
- );
65
- // Opt-in Bluetooth: triggers the iOS BT prompt (needs
66
- // NSBluetoothAlwaysUsageDescription in the host Info.plist).
67
- if (props.requestBluetooth) {
68
- DoorstepAI.requestAllPermissions(true, true);
132
+ try {
133
+ // One requester for both platforms; native dispatches. Safe at launch on
134
+ // iOS only because native defaults `onlyIfNotDetermined` to true: an
135
+ // already-answered permission is left alone, so this cannot re-escalate a
136
+ // user's "While Using the App" choice on every launch.
137
+ const { statuses } = await DoorstepAI.requestPermissions(whitelist);
138
+ if (cancelled) return;
139
+ // Resolves only once the user has ANSWERED, so this state is final and
140
+ // phase 2 can run straight away instead of waiting for the next foreground.
141
+ if (statuses.locationWhenInUse?.state === 'granted') {
142
+ await runBackgroundLocationPhase();
69
143
  }
144
+ } catch (err) {
145
+ console.warn('DOORSTEPAI - permission request failed:', err);
70
146
  }
71
147
  };
72
148
 
73
149
  initializeAndRequestPermissions();
150
+
151
+ return () => {
152
+ cancelled = true;
153
+ appStateSubscription.remove();
154
+ };
74
155
  }, [
75
- props.apiKey,
76
- props.notificationTitle,
77
- props.notificationText,
78
- props.requestBluetooth,
156
+ apiKey,
157
+ notificationTitle,
158
+ notificationText,
159
+ permissions,
160
+ requestBluetooth,
161
+ requestBackgroundLocation,
79
162
  ]);
80
163
 
81
164
  // On iOS, the native view might handle triggering permission prompts if needed.
package/src/index.tsx CHANGED
@@ -4,6 +4,16 @@ export type {
4
4
  AddressType,
5
5
  LatLngObject,
6
6
  StartDeliveryOptions,
7
+ DoorstepPermission,
8
+ DoorstepPermissionAlias,
9
+ DoorstepPermissionInput,
10
+ DoorstepPermissionState,
11
+ DoorstepPermissionStatus,
12
+ PermissionState,
13
+ DoorstepPermissionType,
14
+ PermissionStatus,
15
+ PermissionRequestOptions,
16
+ PermissionRequestResult,
7
17
  SDKLogLevel,
8
18
  DeliveryStop,
9
19
  RouteGeofenceOptions,