react-native-nitro-location-tracking 0.1.7 → 0.1.8

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.
@@ -1,7 +1,11 @@
1
1
  package com.margelo.nitro.nitrolocationtracking
2
2
 
3
+ import android.content.pm.PackageManager
3
4
  import android.util.Log
5
+ import androidx.core.app.ActivityCompat
6
+ import androidx.core.content.ContextCompat
4
7
  import com.facebook.proguard.annotations.DoNotStrip
8
+ import com.facebook.react.bridge.ReactContext
5
9
  import com.margelo.nitro.NitroModules
6
10
  import com.margelo.nitro.core.Promise
7
11
  import kotlin.coroutines.resume
@@ -13,6 +17,7 @@ class NitroLocationTracking : HybridNitroLocationTrackingSpec() {
13
17
 
14
18
  companion object {
15
19
  private const val TAG = "NitroLocationTracking"
20
+ private const val PERMISSION_REQUEST_CODE = 9001
16
21
  }
17
22
 
18
23
  private var locationEngine: LocationEngine? = null
@@ -32,6 +37,7 @@ class NitroLocationTracking : HybridNitroLocationTrackingSpec() {
32
37
 
33
38
  private var locationConfig: LocationConfig? = null
34
39
 
40
+
35
41
  private fun ensureInitialized(): Boolean {
36
42
  if (locationEngine != null) return true
37
43
  val context = NitroModules.applicationContext
@@ -260,22 +266,19 @@ class NitroLocationTracking : HybridNitroLocationTrackingSpec() {
260
266
  override fun getLocationPermissionStatus(): PermissionStatus {
261
267
  val context = NitroModules.applicationContext ?: return PermissionStatus.NOTDETERMINED
262
268
 
263
- val fineGranted = androidx.core.content.ContextCompat.checkSelfPermission(
269
+ val fineGranted = ContextCompat.checkSelfPermission(
264
270
  context, android.Manifest.permission.ACCESS_FINE_LOCATION
265
- ) == android.content.pm.PackageManager.PERMISSION_GRANTED
271
+ ) == PackageManager.PERMISSION_GRANTED
266
272
 
267
273
  if (!fineGranted) {
268
- // Check if we've ever asked — if the app just installed, it's "notDetermined"
269
- // Android doesn't have a direct "notDetermined" state, so we treat
270
- // not-granted as DENIED (the JS side can use requestPermission to prompt)
271
274
  return PermissionStatus.DENIED
272
275
  }
273
276
 
274
277
  // Fine location granted — check background
275
278
  if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
276
- val bgGranted = androidx.core.content.ContextCompat.checkSelfPermission(
279
+ val bgGranted = ContextCompat.checkSelfPermission(
277
280
  context, android.Manifest.permission.ACCESS_BACKGROUND_LOCATION
278
- ) == android.content.pm.PackageManager.PERMISSION_GRANTED
281
+ ) == PackageManager.PERMISSION_GRANTED
279
282
  return if (bgGranted) PermissionStatus.ALWAYS else PermissionStatus.WHENINUSE
280
283
  }
281
284
 
@@ -283,6 +286,66 @@ class NitroLocationTracking : HybridNitroLocationTrackingSpec() {
283
286
  return PermissionStatus.ALWAYS
284
287
  }
285
288
 
289
+ override fun requestLocationPermission(): Promise<PermissionStatus> {
290
+ return Promise.async {
291
+ suspendCoroutine { cont ->
292
+ val context = NitroModules.applicationContext
293
+ if (context == null) {
294
+ cont.resume(PermissionStatus.DENIED)
295
+ return@suspendCoroutine
296
+ }
297
+
298
+ // Already granted — return current status immediately
299
+ val fineGranted = ContextCompat.checkSelfPermission(
300
+ context, android.Manifest.permission.ACCESS_FINE_LOCATION
301
+ ) == PackageManager.PERMISSION_GRANTED
302
+
303
+ if (fineGranted) {
304
+ cont.resume(getLocationPermissionStatus())
305
+ return@suspendCoroutine
306
+ }
307
+
308
+ // Need to request — get the current Activity
309
+ val activity = (context as? ReactContext)?.currentActivity
310
+ if (activity == null) {
311
+ Log.e(TAG, "requestLocationPermission — no current Activity available")
312
+ cont.resume(PermissionStatus.DENIED)
313
+ return@suspendCoroutine
314
+ }
315
+
316
+ // Use PermissionAwareActivity if available (React Native's Activity)
317
+ if (activity is com.facebook.react.modules.core.PermissionAwareActivity) {
318
+ activity.requestPermissions(
319
+ arrayOf(
320
+ android.Manifest.permission.ACCESS_FINE_LOCATION,
321
+ android.Manifest.permission.ACCESS_COARSE_LOCATION
322
+ ),
323
+ PERMISSION_REQUEST_CODE
324
+ ) { requestCode, _, _ ->
325
+ if (requestCode == PERMISSION_REQUEST_CODE) {
326
+ cont.resume(getLocationPermissionStatus())
327
+ }
328
+ true // PermissionListener requires Boolean return
329
+ }
330
+ } else {
331
+ // Fallback: use ActivityCompat (result won't be captured directly)
332
+ ActivityCompat.requestPermissions(
333
+ activity,
334
+ arrayOf(
335
+ android.Manifest.permission.ACCESS_FINE_LOCATION,
336
+ android.Manifest.permission.ACCESS_COARSE_LOCATION
337
+ ),
338
+ PERMISSION_REQUEST_CODE
339
+ )
340
+ // Poll for result after user interacts with the dialog
341
+ android.os.Handler(android.os.Looper.getMainLooper()).postDelayed({
342
+ cont.resume(getLocationPermissionStatus())
343
+ }, 5000)
344
+ }
345
+ }
346
+ }
347
+ }
348
+
286
349
  // === Notifications ===
287
350
 
288
351
  override fun showLocalNotification(title: String, body: String) {
@@ -11,6 +11,8 @@ class LocationEngine: NSObject, CLLocationManagerDelegate {
11
11
  return mgr
12
12
  }()
13
13
  private var tracking = false
14
+ private var pendingPermissionCompletion: ((CLAuthorizationStatus) -> Void)?
15
+ private var pendingStartAfterPermission = false
14
16
 
15
17
  var onLocation: ((LocationData) -> Void)?
16
18
  var onMotionChange: ((Bool) -> Void)?
@@ -45,6 +47,7 @@ class LocationEngine: NSObject, CLLocationManagerDelegate {
45
47
  func start() {
46
48
  guard CLLocationManager.authorizationStatus() == .authorizedAlways ||
47
49
  CLLocationManager.authorizationStatus() == .authorizedWhenInUse else {
50
+ pendingStartAfterPermission = true
48
51
  locationManager.requestAlwaysAuthorization()
49
52
  return
50
53
  }
@@ -52,10 +55,21 @@ class LocationEngine: NSObject, CLLocationManagerDelegate {
52
55
  tracking = true
53
56
  }
54
57
 
58
+ func requestPermission(completion: @escaping (CLAuthorizationStatus) -> Void) {
59
+ let currentStatus = CLLocationManager.authorizationStatus()
60
+ if currentStatus != .notDetermined {
61
+ completion(currentStatus)
62
+ return
63
+ }
64
+ pendingPermissionCompletion = completion
65
+ locationManager.requestAlwaysAuthorization()
66
+ }
67
+
55
68
  func stop() {
56
69
  locationManager.stopUpdatingLocation()
57
70
  tracking = false
58
71
  }
72
+
59
73
 
60
74
  var isTracking: Bool { tracking }
61
75
 
@@ -80,11 +94,11 @@ class LocationEngine: NSObject, CLLocationManagerDelegate {
80
94
  }
81
95
  // Use a SEPARATE manager for one-shot requests so we never
82
96
  // stop the continuous startUpdatingLocation() on locationManager.
83
- pendingLocationCompletion = completion
97
+ pendingLocationCompletions.append(completion)
84
98
  oneShotManager.requestLocation()
85
99
  }
86
100
 
87
- private var pendingLocationCompletion: ((LocationData?) -> Void)?
101
+ private var pendingLocationCompletions: [(LocationData?) -> Void] = []
88
102
 
89
103
  func locationManager(_ manager: CLLocationManager,
90
104
  didUpdateLocations locations: [CLLocation]) {
@@ -104,9 +118,12 @@ class LocationEngine: NSObject, CLLocationManagerDelegate {
104
118
  )
105
119
 
106
120
  // Handle pending one-shot request (from oneShotManager)
107
- if manager === oneShotManager, let completion = pendingLocationCompletion {
108
- completion(data)
109
- pendingLocationCompletion = nil
121
+ if manager === oneShotManager {
122
+ let completions = pendingLocationCompletions
123
+ pendingLocationCompletions.removeAll()
124
+ for completion in completions {
125
+ completion(data)
126
+ }
110
127
  return
111
128
  }
112
129
 
@@ -131,9 +148,12 @@ class LocationEngine: NSObject, CLLocationManagerDelegate {
131
148
 
132
149
  func locationManager(_ manager: CLLocationManager,
133
150
  didFailWithError error: Error) {
134
- if manager === oneShotManager, let completion = pendingLocationCompletion {
135
- completion(nil)
136
- pendingLocationCompletion = nil
151
+ if manager === oneShotManager {
152
+ let completions = pendingLocationCompletions
153
+ pendingLocationCompletions.removeAll()
154
+ for completion in completions {
155
+ completion(nil)
156
+ }
137
157
  }
138
158
  }
139
159
 
@@ -169,6 +189,24 @@ class LocationEngine: NSObject, CLLocationManagerDelegate {
169
189
 
170
190
  func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
171
191
  guard manager === locationManager else { return }
192
+
193
+ let authStatus = CLLocationManager.authorizationStatus()
194
+
195
+ // Resolve pending permission request
196
+ if let completion = pendingPermissionCompletion {
197
+ pendingPermissionCompletion = nil
198
+ completion(authStatus)
199
+ }
200
+
201
+ // Auto-start tracking if permission was granted after start() was called
202
+ if pendingStartAfterPermission {
203
+ pendingStartAfterPermission = false
204
+ if authStatus == .authorizedAlways || authStatus == .authorizedWhenInUse {
205
+ locationManager.startUpdatingLocation()
206
+ tracking = true
207
+ }
208
+ }
209
+
172
210
  let enabled = CLLocationManager.locationServicesEnabled()
173
211
  let status: LocationProviderStatus = enabled ? .enabled : .disabled
174
212
  providerStatusCallback?(status, status)
@@ -3,7 +3,6 @@ import CoreLocation
3
3
  import NitroModules
4
4
 
5
5
  class NitroLocationTracking: HybridNitroLocationTrackingSpec {
6
-
7
6
  private let locationEngine = LocationEngine()
8
7
  private let connectionManager = ConnectionManager()
9
8
  private let dbWriter = NativeDBWriter()
@@ -17,6 +16,7 @@ class NitroLocationTracking: HybridNitroLocationTrackingSpec {
17
16
  private var geofenceCallback: ((GeofenceEvent, String) -> Void)?
18
17
  private var speedAlertCallback: ((SpeedAlertType, Double) -> Void)?
19
18
  private var providerStatusCallback: ((LocationProviderStatus, LocationProviderStatus) -> Void)?
19
+ private var permissionPromise: Promise<PermissionStatus>?
20
20
 
21
21
  override init() {
22
22
  super.init()
@@ -44,6 +44,7 @@ class NitroLocationTracking: HybridNitroLocationTrackingSpec {
44
44
  locationEngine.stop()
45
45
  }
46
46
 
47
+
47
48
  func getCurrentLocation() throws -> Promise<LocationData> {
48
49
  let promise = Promise<LocationData>()
49
50
  locationEngine.getCurrentLocation { data in
@@ -58,7 +59,7 @@ class NitroLocationTracking: HybridNitroLocationTrackingSpec {
58
59
  return promise
59
60
  }
60
61
 
61
- func isTracking() throws -> Bool {
62
+ func isTracking() throws -> Bool {
62
63
  return locationEngine.isTracking
63
64
  }
64
65
 
@@ -212,6 +213,29 @@ class NitroLocationTracking: HybridNitroLocationTrackingSpec {
212
213
  }
213
214
  }
214
215
 
216
+ func requestLocationPermission() throws -> Promise<PermissionStatus> {
217
+ let promise = Promise<PermissionStatus>()
218
+ locationEngine.requestPermission { authStatus in
219
+ let result: PermissionStatus
220
+ switch authStatus {
221
+ case .notDetermined:
222
+ result = .notdetermined
223
+ case .restricted:
224
+ result = .restricted
225
+ case .denied:
226
+ result = .denied
227
+ case .authorizedWhenInUse:
228
+ result = .wheninuse
229
+ case .authorizedAlways:
230
+ result = .always
231
+ @unknown default:
232
+ result = .notdetermined
233
+ }
234
+ promise.resolve(withResult: result)
235
+ }
236
+ return promise
237
+ }
238
+
215
239
  // MARK: - Notifications
216
240
 
217
241
  func showLocalNotification(title: String, body: String) throws {
@@ -96,6 +96,7 @@ export interface NitroLocationTracking extends HybridObject<{
96
96
  isLocationServicesEnabled(): boolean;
97
97
  onProviderStatusChange(callback: ProviderStatusCallback): void;
98
98
  getLocationPermissionStatus(): PermissionStatus;
99
+ requestLocationPermission(): Promise<PermissionStatus>;
99
100
  showLocalNotification(title: string, body: string): void;
100
101
  updateForegroundNotification(title: string, body: string): void;
101
102
  destroy(): void;
@@ -1 +1 @@
1
- {"version":3,"file":"NitroLocationTracking.nitro.d.ts","sourceRoot":"","sources":["../../../src/NitroLocationTracking.nitro.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAI/D,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,UAAU,GAAG,KAAK,CAAC;AACxD,MAAM,MAAM,eAAe,GAAG,WAAW,GAAG,cAAc,GAAG,cAAc,CAAC;AAI5E,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,cAAc,CAAC,EAAE,OAAO,CAAC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AAS,CAAC;IACjC,YAAY,IAAI,SAAS,CAAC;IAC1B,oBAAoB,IAAI,IAAI,CAAC;IAG7B,yBAAyB,IAAI,OAAO,CAAC;IACrC,sBAAsB,CAAC,QAAQ,EAAE,sBAAsB,GAAG,IAAI,CAAC;IAG/D,2BAA2B,IAAI,gBAAgB,CAAC;IAChD,yBAAyB,IAAI,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAGvD,qBAAqB,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACzD,4BAA4B,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAGhE,OAAO,IAAI,IAAI,CAAC;CACjB"}
@@ -265,6 +265,22 @@ namespace margelo::nitro::nitrolocationtracking {
265
265
  auto __result = method(_javaPart);
266
266
  return __result->toCpp();
267
267
  }
268
+ std::shared_ptr<Promise<PermissionStatus>> JHybridNitroLocationTrackingSpec::requestLocationPermission() {
269
+ static const auto method = javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>()>("requestLocationPermission");
270
+ auto __result = method(_javaPart);
271
+ return [&]() {
272
+ auto __promise = Promise<PermissionStatus>::create();
273
+ __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
274
+ auto __result = jni::static_ref_cast<JPermissionStatus>(__boxedResult);
275
+ __promise->resolve(__result->toCpp());
276
+ });
277
+ __result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JThrowable>& __throwable) {
278
+ jni::JniException __jniError(__throwable);
279
+ __promise->reject(std::make_exception_ptr(__jniError));
280
+ });
281
+ return __promise;
282
+ }();
283
+ }
268
284
  void JHybridNitroLocationTrackingSpec::showLocalNotification(const std::string& title, const std::string& body) {
269
285
  static const auto method = javaClassStatic()->getMethod<void(jni::alias_ref<jni::JString> /* title */, jni::alias_ref<jni::JString> /* body */)>("showLocalNotification");
270
286
  method(_javaPart, jni::make_jstring(title), jni::make_jstring(body));
@@ -86,6 +86,7 @@ namespace margelo::nitro::nitrolocationtracking {
86
86
  bool isLocationServicesEnabled() override;
87
87
  void onProviderStatusChange(const std::function<void(LocationProviderStatus /* gps */, LocationProviderStatus /* network */)>& callback) override;
88
88
  PermissionStatus getLocationPermissionStatus() override;
89
+ std::shared_ptr<Promise<PermissionStatus>> requestLocationPermission() override;
89
90
  void showLocalNotification(const std::string& title, const std::string& body) override;
90
91
  void updateForegroundNotification(const std::string& title, const std::string& body) override;
91
92
  void destroy() override;
@@ -205,6 +205,10 @@ abstract class HybridNitroLocationTrackingSpec: HybridObject() {
205
205
  @Keep
206
206
  abstract fun getLocationPermissionStatus(): PermissionStatus
207
207
 
208
+ @DoNotStrip
209
+ @Keep
210
+ abstract fun requestLocationPermission(): Promise<PermissionStatus>
211
+
208
212
  @DoNotStrip
209
213
  @Keep
210
214
  abstract fun showLocalNotification(title: String, body: String): Unit
@@ -78,6 +78,14 @@ namespace margelo::nitro::nitrolocationtracking::bridge::swift {
78
78
  };
79
79
  }
80
80
 
81
+ // pragma MARK: std::function<void(PermissionStatus /* result */)>
82
+ Func_void_PermissionStatus create_Func_void_PermissionStatus(void* NON_NULL swiftClosureWrapper) noexcept {
83
+ auto swiftClosure = NitroLocationTracking::Func_void_PermissionStatus::fromUnsafe(swiftClosureWrapper);
84
+ return [swiftClosure = std::move(swiftClosure)](PermissionStatus result) mutable -> void {
85
+ swiftClosure.call(static_cast<int>(result));
86
+ };
87
+ }
88
+
81
89
  // pragma MARK: std::shared_ptr<HybridNitroLocationTrackingSpec>
82
90
  std::shared_ptr<HybridNitroLocationTrackingSpec> create_std__shared_ptr_HybridNitroLocationTrackingSpec_(void* NON_NULL swiftUnsafePointer) noexcept {
83
91
  NitroLocationTracking::HybridNitroLocationTrackingSpec_cxx swiftPart = NitroLocationTracking::HybridNitroLocationTrackingSpec_cxx::fromUnsafe(swiftUnsafePointer);
@@ -268,6 +268,40 @@ namespace margelo::nitro::nitrolocationtracking::bridge::swift {
268
268
  return Func_void_LocationProviderStatus_LocationProviderStatus_Wrapper(std::move(value));
269
269
  }
270
270
 
271
+ // pragma MARK: std::shared_ptr<Promise<PermissionStatus>>
272
+ /**
273
+ * Specialized version of `std::shared_ptr<Promise<PermissionStatus>>`.
274
+ */
275
+ using std__shared_ptr_Promise_PermissionStatus__ = std::shared_ptr<Promise<PermissionStatus>>;
276
+ inline std::shared_ptr<Promise<PermissionStatus>> create_std__shared_ptr_Promise_PermissionStatus__() noexcept {
277
+ return Promise<PermissionStatus>::create();
278
+ }
279
+ inline PromiseHolder<PermissionStatus> wrap_std__shared_ptr_Promise_PermissionStatus__(std::shared_ptr<Promise<PermissionStatus>> promise) noexcept {
280
+ return PromiseHolder<PermissionStatus>(std::move(promise));
281
+ }
282
+
283
+ // pragma MARK: std::function<void(PermissionStatus /* result */)>
284
+ /**
285
+ * Specialized version of `std::function<void(PermissionStatus)>`.
286
+ */
287
+ using Func_void_PermissionStatus = std::function<void(PermissionStatus /* result */)>;
288
+ /**
289
+ * Wrapper class for a `std::function<void(PermissionStatus / * result * /)>`, this can be used from Swift.
290
+ */
291
+ class Func_void_PermissionStatus_Wrapper final {
292
+ public:
293
+ explicit Func_void_PermissionStatus_Wrapper(std::function<void(PermissionStatus /* result */)>&& func): _function(std::make_unique<std::function<void(PermissionStatus /* result */)>>(std::move(func))) {}
294
+ inline void call(int result) const noexcept {
295
+ _function->operator()(static_cast<PermissionStatus>(result));
296
+ }
297
+ private:
298
+ std::unique_ptr<std::function<void(PermissionStatus /* result */)>> _function;
299
+ } SWIFT_NONCOPYABLE;
300
+ Func_void_PermissionStatus create_Func_void_PermissionStatus(void* NON_NULL swiftClosureWrapper) noexcept;
301
+ inline Func_void_PermissionStatus_Wrapper wrap_Func_void_PermissionStatus(Func_void_PermissionStatus value) noexcept {
302
+ return Func_void_PermissionStatus_Wrapper(std::move(value));
303
+ }
304
+
271
305
  // pragma MARK: std::shared_ptr<HybridNitroLocationTrackingSpec>
272
306
  /**
273
307
  * Specialized version of `std::shared_ptr<HybridNitroLocationTrackingSpec>`.
@@ -351,5 +385,14 @@ namespace margelo::nitro::nitrolocationtracking::bridge::swift {
351
385
  inline Result_PermissionStatus_ create_Result_PermissionStatus_(const std::exception_ptr& error) noexcept {
352
386
  return Result<PermissionStatus>::withError(error);
353
387
  }
388
+
389
+ // pragma MARK: Result<std::shared_ptr<Promise<PermissionStatus>>>
390
+ using Result_std__shared_ptr_Promise_PermissionStatus___ = Result<std::shared_ptr<Promise<PermissionStatus>>>;
391
+ inline Result_std__shared_ptr_Promise_PermissionStatus___ create_Result_std__shared_ptr_Promise_PermissionStatus___(const std::shared_ptr<Promise<PermissionStatus>>& value) noexcept {
392
+ return Result<std::shared_ptr<Promise<PermissionStatus>>>::withValue(value);
393
+ }
394
+ inline Result_std__shared_ptr_Promise_PermissionStatus___ create_Result_std__shared_ptr_Promise_PermissionStatus___(const std::exception_ptr& error) noexcept {
395
+ return Result<std::shared_ptr<Promise<PermissionStatus>>>::withError(error);
396
+ }
354
397
 
355
398
  } // namespace margelo::nitro::nitrolocationtracking::bridge::swift
@@ -310,6 +310,14 @@ namespace margelo::nitro::nitrolocationtracking {
310
310
  auto __value = std::move(__result.value());
311
311
  return __value;
312
312
  }
313
+ inline std::shared_ptr<Promise<PermissionStatus>> requestLocationPermission() override {
314
+ auto __result = _swiftPart.requestLocationPermission();
315
+ if (__result.hasError()) [[unlikely]] {
316
+ std::rethrow_exception(__result.error());
317
+ }
318
+ auto __value = std::move(__result.value());
319
+ return __value;
320
+ }
313
321
  inline void showLocalNotification(const std::string& title, const std::string& body) override {
314
322
  auto __result = _swiftPart.showLocalNotification(title, body);
315
323
  if (__result.hasError()) [[unlikely]] {
@@ -0,0 +1,46 @@
1
+ ///
2
+ /// Func_void_PermissionStatus.swift
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ import NitroModules
9
+
10
+ /**
11
+ * Wraps a Swift `(_ value: PermissionStatus) -> Void` as a class.
12
+ * This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`.
13
+ */
14
+ public final class Func_void_PermissionStatus {
15
+ public typealias bridge = margelo.nitro.nitrolocationtracking.bridge.swift
16
+
17
+ private let closure: (_ value: PermissionStatus) -> Void
18
+
19
+ public init(_ closure: @escaping (_ value: PermissionStatus) -> Void) {
20
+ self.closure = closure
21
+ }
22
+
23
+ @inline(__always)
24
+ public func call(value: Int32) -> Void {
25
+ self.closure(margelo.nitro.nitrolocationtracking.PermissionStatus(rawValue: value)!)
26
+ }
27
+
28
+ /**
29
+ * Casts this instance to a retained unsafe raw pointer.
30
+ * This acquires one additional strong reference on the object!
31
+ */
32
+ @inline(__always)
33
+ public func toUnsafe() -> UnsafeMutableRawPointer {
34
+ return Unmanaged.passRetained(self).toOpaque()
35
+ }
36
+
37
+ /**
38
+ * Casts an unsafe pointer to a `Func_void_PermissionStatus`.
39
+ * The pointer has to be a retained opaque `Unmanaged<Func_void_PermissionStatus>`.
40
+ * This removes one strong reference from the object!
41
+ */
42
+ @inline(__always)
43
+ public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void_PermissionStatus {
44
+ return Unmanaged<Func_void_PermissionStatus>.fromOpaque(pointer).takeRetainedValue()
45
+ }
46
+ }
@@ -44,6 +44,7 @@ public protocol HybridNitroLocationTrackingSpec_protocol: HybridObject {
44
44
  func isLocationServicesEnabled() throws -> Bool
45
45
  func onProviderStatusChange(callback: @escaping (_ gps: LocationProviderStatus, _ network: LocationProviderStatus) -> Void) throws -> Void
46
46
  func getLocationPermissionStatus() throws -> PermissionStatus
47
+ func requestLocationPermission() throws -> Promise<PermissionStatus>
47
48
  func showLocalNotification(title: String, body: String) throws -> Void
48
49
  func updateForegroundNotification(title: String, body: String) throws -> Void
49
50
  func destroy() throws -> Void
@@ -524,6 +524,25 @@ open class HybridNitroLocationTrackingSpec_cxx {
524
524
  }
525
525
  }
526
526
 
527
+ @inline(__always)
528
+ public final func requestLocationPermission() -> bridge.Result_std__shared_ptr_Promise_PermissionStatus___ {
529
+ do {
530
+ let __result = try self.__implementation.requestLocationPermission()
531
+ let __resultCpp = { () -> bridge.std__shared_ptr_Promise_PermissionStatus__ in
532
+ let __promise = bridge.create_std__shared_ptr_Promise_PermissionStatus__()
533
+ let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_PermissionStatus__(__promise)
534
+ __result
535
+ .then({ __result in __promiseHolder.resolve(__result) })
536
+ .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
537
+ return __promise
538
+ }()
539
+ return bridge.create_Result_std__shared_ptr_Promise_PermissionStatus___(__resultCpp)
540
+ } catch (let __error) {
541
+ let __exceptionPtr = __error.toCpp()
542
+ return bridge.create_Result_std__shared_ptr_Promise_PermissionStatus___(__exceptionPtr)
543
+ }
544
+ }
545
+
527
546
  @inline(__always)
528
547
  public final func showLocalNotification(title: std.string, body: std.string) -> bridge.Result_void_ {
529
548
  do {
@@ -45,6 +45,7 @@ namespace margelo::nitro::nitrolocationtracking {
45
45
  prototype.registerHybridMethod("isLocationServicesEnabled", &HybridNitroLocationTrackingSpec::isLocationServicesEnabled);
46
46
  prototype.registerHybridMethod("onProviderStatusChange", &HybridNitroLocationTrackingSpec::onProviderStatusChange);
47
47
  prototype.registerHybridMethod("getLocationPermissionStatus", &HybridNitroLocationTrackingSpec::getLocationPermissionStatus);
48
+ prototype.registerHybridMethod("requestLocationPermission", &HybridNitroLocationTrackingSpec::requestLocationPermission);
48
49
  prototype.registerHybridMethod("showLocalNotification", &HybridNitroLocationTrackingSpec::showLocalNotification);
49
50
  prototype.registerHybridMethod("updateForegroundNotification", &HybridNitroLocationTrackingSpec::updateForegroundNotification);
50
51
  prototype.registerHybridMethod("destroy", &HybridNitroLocationTrackingSpec::destroy);
@@ -113,6 +113,7 @@ namespace margelo::nitro::nitrolocationtracking {
113
113
  virtual bool isLocationServicesEnabled() = 0;
114
114
  virtual void onProviderStatusChange(const std::function<void(LocationProviderStatus /* gps */, LocationProviderStatus /* network */)>& callback) = 0;
115
115
  virtual PermissionStatus getLocationPermissionStatus() = 0;
116
+ virtual std::shared_ptr<Promise<PermissionStatus>> requestLocationPermission() = 0;
116
117
  virtual void showLocalNotification(const std::string& title, const std::string& body) = 0;
117
118
  virtual void updateForegroundNotification(const std::string& title, const std::string& body) = 0;
118
119
  virtual void destroy() = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-location-tracking",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "A React Native Nitro module for location tracking",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",
@@ -143,6 +143,7 @@ export interface NitroLocationTracking
143
143
 
144
144
  // === Permission Status ===
145
145
  getLocationPermissionStatus(): PermissionStatus;
146
+ requestLocationPermission(): Promise<PermissionStatus>;
146
147
 
147
148
  // === Notifications ===
148
149
  showLocalNotification(title: string, body: string): void;