react-native-nitro-location-tracking 0.1.4 → 0.1.5

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.
@@ -4,10 +4,15 @@ import android.annotation.SuppressLint
4
4
  import android.content.Context
5
5
  import android.location.Location
6
6
  import android.os.Looper
7
+ import android.util.Log
7
8
  import com.google.android.gms.location.*
8
9
  import kotlin.coroutines.resume
9
10
 
10
11
  class LocationEngine(private val context: Context) {
12
+ companion object {
13
+ private const val TAG = "NitroLocationEngine"
14
+ }
15
+
11
16
  private val fusedClient =
12
17
  LocationServices.getFusedLocationProviderClient(context)
13
18
  private var locationCallback: LocationCallback? = null
@@ -23,6 +28,10 @@ class LocationEngine(private val context: Context) {
23
28
 
24
29
  @SuppressLint("MissingPermission")
25
30
  fun start(config: LocationConfig) {
31
+ if (tracking) {
32
+ locationCallback?.let { fusedClient.removeLocationUpdates(it) }
33
+ }
34
+
26
35
  val priority = when (config.desiredAccuracy) {
27
36
  AccuracyLevel.HIGH -> Priority.PRIORITY_HIGH_ACCURACY
28
37
  AccuracyLevel.BALANCED -> Priority.PRIORITY_BALANCED_POWER_ACCURACY
@@ -31,12 +40,17 @@ class LocationEngine(private val context: Context) {
31
40
  val request = LocationRequest.Builder(priority, config.intervalMs.toLong())
32
41
  .setMinUpdateDistanceMeters(config.distanceFilter.toFloat())
33
42
  .setMinUpdateIntervalMillis(config.fastestIntervalMs.toLong())
43
+ .setWaitForAccurateLocation(false)
34
44
  .build()
35
45
 
36
46
  locationCallback = object : LocationCallback() {
37
47
  override fun onLocationResult(result: LocationResult) {
38
48
  result.lastLocation?.let { processLocation(it) }
39
49
  }
50
+
51
+ override fun onLocationAvailability(availability: LocationAvailability) {
52
+ Log.d(TAG, "onLocationAvailability — isLocationAvailable=${availability.isLocationAvailable}")
53
+ }
40
54
  }
41
55
  fusedClient.requestLocationUpdates(
42
56
  request, locationCallback!!, Looper.getMainLooper())
@@ -45,6 +59,7 @@ class LocationEngine(private val context: Context) {
45
59
 
46
60
  fun stop() {
47
61
  locationCallback?.let { fusedClient.removeLocationUpdates(it) }
62
+ locationCallback = null
48
63
  tracking = false
49
64
  }
50
65
 
@@ -71,7 +86,6 @@ class LocationEngine(private val context: Context) {
71
86
 
72
87
  private fun processLocation(location: Location) {
73
88
  val data = locationToData(location)
74
- // dbWriter?.insert(data, currentRideId)
75
89
  onLocation?.invoke(data)
76
90
 
77
91
  val isMoving = location.speed > 0.5f
@@ -1,7 +1,15 @@
1
1
  import CoreLocation
2
2
 
3
3
  class LocationEngine: NSObject, CLLocationManagerDelegate {
4
+ /// Continuous tracking manager — only used for startUpdatingLocation / stopUpdatingLocation
4
5
  private let locationManager = CLLocationManager()
6
+ /// Separate one-shot manager so requestLocation() never kills startUpdatingLocation()
7
+ private lazy var oneShotManager: CLLocationManager = {
8
+ let mgr = CLLocationManager()
9
+ mgr.delegate = self
10
+ mgr.desiredAccuracy = kCLLocationAccuracyBest
11
+ return mgr
12
+ }()
5
13
  private var tracking = false
6
14
 
7
15
  var onLocation: ((LocationData) -> Void)?
@@ -65,9 +73,10 @@ class LocationEngine: NSObject, CLLocationManagerDelegate {
65
73
  return
66
74
  }
67
75
  }
68
- // Otherwise request a fresh location
76
+ // Use a SEPARATE manager for one-shot requests so we never
77
+ // stop the continuous startUpdatingLocation() on locationManager.
69
78
  pendingLocationCompletion = completion
70
- locationManager.requestLocation()
79
+ oneShotManager.requestLocation()
71
80
  }
72
81
 
73
82
  private var pendingLocationCompletion: ((LocationData?) -> Void)?
@@ -85,16 +94,16 @@ class LocationEngine: NSObject, CLLocationManagerDelegate {
85
94
  accuracy: location.horizontalAccuracy,
86
95
  timestamp: location.timestamp.timeIntervalSince1970 * 1000
87
96
  )
88
-
89
97
 
90
- // Handle pending one-shot request
91
- if let completion = pendingLocationCompletion {
98
+ // Handle pending one-shot request (from oneShotManager)
99
+ if manager === oneShotManager, let completion = pendingLocationCompletion {
92
100
  completion(data)
93
101
  pendingLocationCompletion = nil
102
+ return
94
103
  }
95
104
 
96
- // Save to SQLite immediately (offline-first)
97
- // dbWriter?.insert(data, rideId: currentRideId)
105
+ // Continuous updates from locationManager only
106
+ guard manager === locationManager else { return }
98
107
 
99
108
  // Notify JS via Nitro callback
100
109
  onLocation?(data)
@@ -105,10 +114,11 @@ class LocationEngine: NSObject, CLLocationManagerDelegate {
105
114
 
106
115
  func locationManager(_ manager: CLLocationManager,
107
116
  didFailWithError error: Error) {
108
- if let completion = pendingLocationCompletion {
117
+ if manager === oneShotManager, let completion = pendingLocationCompletion {
109
118
  completion(nil)
110
119
  pendingLocationCompletion = nil
111
120
  }
112
121
  }
113
122
  }
114
123
 
124
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-location-tracking",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
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",