@transistorsoft/capacitor-background-geolocation 8.0.1 → 9.0.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 (67) hide show
  1. package/Package.swift +10 -16
  2. package/README.md +99 -211
  3. package/TransistorsoftCapacitorBackgroundGeolocation.podspec +7 -5
  4. package/android/build.gradle +16 -54
  5. package/android/src/main/java/com/transistorsoft/bggeo/capacitor/BackgroundGeolocationPlugin.java +132 -70
  6. package/dist/index.js +198 -189
  7. package/dist/index.js.map +1 -1
  8. package/dist/plugin.cjs.js +255 -205
  9. package/dist/plugin.cjs.js.map +1 -1
  10. package/dist/plugin.js +1141 -1092
  11. package/dist/plugin.js.map +1 -1
  12. package/ios/Sources/BackgroundGeolocationPlugin/BackgroundGeolocationPlugin.swift +353 -306
  13. package/package.json +11 -12
  14. package/src/index.d.ts +10 -0
  15. package/android/libs/com/transistorsoft/tslocationmanager/3.7.0/tslocationmanager-3.7.0.aar +0 -0
  16. package/android/libs/com/transistorsoft/tslocationmanager/3.7.0/tslocationmanager-3.7.0.aar.md5 +0 -1
  17. package/android/libs/com/transistorsoft/tslocationmanager/3.7.0/tslocationmanager-3.7.0.aar.sha1 +0 -1
  18. package/android/libs/com/transistorsoft/tslocationmanager/3.7.0/tslocationmanager-3.7.0.aar.sha256 +0 -1
  19. package/android/libs/com/transistorsoft/tslocationmanager/3.7.0/tslocationmanager-3.7.0.aar.sha512 +0 -1
  20. package/android/libs/com/transistorsoft/tslocationmanager/3.7.0/tslocationmanager-3.7.0.pom +0 -9
  21. package/android/libs/com/transistorsoft/tslocationmanager/3.7.0/tslocationmanager-3.7.0.pom.md5 +0 -1
  22. package/android/libs/com/transistorsoft/tslocationmanager/3.7.0/tslocationmanager-3.7.0.pom.sha1 +0 -1
  23. package/android/libs/com/transistorsoft/tslocationmanager/3.7.0/tslocationmanager-3.7.0.pom.sha256 +0 -1
  24. package/android/libs/com/transistorsoft/tslocationmanager/3.7.0/tslocationmanager-3.7.0.pom.sha512 +0 -1
  25. package/android/libs/com/transistorsoft/tslocationmanager/maven-metadata.xml +0 -13
  26. package/android/libs/com/transistorsoft/tslocationmanager-v21/3.7.0/tslocationmanager-v21-3.7.0.aar +0 -0
  27. package/android/libs/com/transistorsoft/tslocationmanager-v21/3.7.0/tslocationmanager-v21-3.7.0.aar.md5 +0 -1
  28. package/android/libs/com/transistorsoft/tslocationmanager-v21/3.7.0/tslocationmanager-v21-3.7.0.aar.sha1 +0 -1
  29. package/android/libs/com/transistorsoft/tslocationmanager-v21/3.7.0/tslocationmanager-v21-3.7.0.aar.sha256 +0 -1
  30. package/android/libs/com/transistorsoft/tslocationmanager-v21/3.7.0/tslocationmanager-v21-3.7.0.aar.sha512 +0 -1
  31. package/android/libs/com/transistorsoft/tslocationmanager-v21/3.7.0/tslocationmanager-v21-3.7.0.pom +0 -9
  32. package/android/libs/com/transistorsoft/tslocationmanager-v21/3.7.0/tslocationmanager-v21-3.7.0.pom.md5 +0 -1
  33. package/android/libs/com/transistorsoft/tslocationmanager-v21/3.7.0/tslocationmanager-v21-3.7.0.pom.sha1 +0 -1
  34. package/android/libs/com/transistorsoft/tslocationmanager-v21/3.7.0/tslocationmanager-v21-3.7.0.pom.sha256 +0 -1
  35. package/android/libs/com/transistorsoft/tslocationmanager-v21/3.7.0/tslocationmanager-v21-3.7.0.pom.sha512 +0 -1
  36. package/android/libs/com/transistorsoft/tslocationmanager-v21/maven-metadata.xml +0 -13
  37. package/dist/Events.js +0 -19
  38. package/dist/Events.js.map +0 -1
  39. package/dist/declarations/BackgroundGeolocation.d.ts +0 -1632
  40. package/dist/declarations/interfaces/Authorization.d.ts +0 -185
  41. package/dist/declarations/interfaces/AuthorizationEvent.d.ts +0 -34
  42. package/dist/declarations/interfaces/Config.d.ts +0 -2474
  43. package/dist/declarations/interfaces/ConnectivityChangeEvent.d.ts +0 -18
  44. package/dist/declarations/interfaces/CurrentPositionRequest.d.ts +0 -45
  45. package/dist/declarations/interfaces/DeviceInfo.d.ts +0 -33
  46. package/dist/declarations/interfaces/DeviceSettings.d.ts +0 -223
  47. package/dist/declarations/interfaces/Geofence.d.ts +0 -319
  48. package/dist/declarations/interfaces/GeofenceEvent.d.ts +0 -35
  49. package/dist/declarations/interfaces/GeofencesChangeEvent.d.ts +0 -22
  50. package/dist/declarations/interfaces/HeartbeatEvent.d.ts +0 -20
  51. package/dist/declarations/interfaces/HttpEvent.d.ts +0 -313
  52. package/dist/declarations/interfaces/Location.d.ts +0 -257
  53. package/dist/declarations/interfaces/LocationAuthorizationAlert.d.ts +0 -41
  54. package/dist/declarations/interfaces/Logger.d.ts +0 -359
  55. package/dist/declarations/interfaces/MotionActivityEvent.d.ts +0 -34
  56. package/dist/declarations/interfaces/MotionChangeEvent.d.ts +0 -15
  57. package/dist/declarations/interfaces/Notification.d.ts +0 -491
  58. package/dist/declarations/interfaces/PermissionRationale.d.ts +0 -31
  59. package/dist/declarations/interfaces/ProviderChangeEvent.d.ts +0 -74
  60. package/dist/declarations/interfaces/SQLQuery.d.ts +0 -60
  61. package/dist/declarations/interfaces/Sensors.d.ts +0 -31
  62. package/dist/declarations/interfaces/State.d.ts +0 -51
  63. package/dist/declarations/interfaces/Subscription.d.ts +0 -104
  64. package/dist/declarations/interfaces/TransistorAuthorizationToken.d.ts +0 -63
  65. package/dist/declarations/interfaces/WatchPositionRequest.d.ts +0 -45
  66. package/dist/declarations/types.d.ts +0 -188
  67. package/dist/index.d.ts +0 -3
@@ -4,21 +4,7 @@ import UIKit
4
4
  import Capacitor
5
5
  import TSLocationManager
6
6
 
7
- let EVENT_LOCATION = "location"
8
- let EVENT_WATCHPOSITION = "watchposition"
9
- let EVENT_PROVIDERCHANGE = "providerchange"
10
- let EVENT_MOTIONCHANGE = "motionchange"
11
- let EVENT_ACTIVITYCHANGE = "activitychange"
12
- let EVENT_GEOFENCESCHANGE = "geofenceschange"
13
- let EVENT_HTTP = "http"
14
- let EVENT_SCHEDULE = "schedule"
15
- let EVENT_GEOFENCE = "geofence"
16
- let EVENT_HEARTBEAT = "heartbeat"
17
- let EVENT_POWERSAVECHANGE = "powersavechange"
18
- let EVENT_CONNECTIVITYCHANGE = "connectivitychange"
19
- let EVENT_ENABLEDCHANGE = "enabledchange"
20
- let EVENT_NOTIFICATIONACTION = "notificationaction"
21
- let EVENT_AUTHORIZATION = "authorization"
7
+ // Event name constants are provided by TSEventName* from TSLocationManager SDK.
22
8
 
23
9
  @objc(BackgroundGeolocationModule)
24
10
  public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
@@ -60,8 +46,7 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
60
46
  CAPPluginMethod(name: "insertLocation", returnType: CAPPluginReturnPromise),
61
47
  CAPPluginMethod(name: "destroyLocations", returnType: CAPPluginReturnPromise),
62
48
  CAPPluginMethod(name: "destroyLocation", returnType: CAPPluginReturnPromise),
63
- CAPPluginMethod(name: "getProviderState", returnType: CAPPluginReturnPromise),
64
-
49
+
65
50
  CAPPluginMethod(name: "startBackgroundTask", returnType: CAPPluginReturnPromise),
66
51
  CAPPluginMethod(name: "stopBackgroundTask", returnType: CAPPluginReturnPromise),
67
52
 
@@ -88,10 +73,10 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
88
73
  var ready = false
89
74
 
90
75
  public override func load() {
91
- let locationManager = TSLocationManager.sharedInstance()
76
+ let locationManager = BackgroundGeolocation.sharedInstance()
92
77
 
93
78
  if let root = UIApplication.shared.delegate?.window??.rootViewController {
94
- locationManager?.viewController = root
79
+ locationManager.viewController = root
95
80
  }
96
81
  ready = false
97
82
  registerEventListeners()
@@ -99,112 +84,121 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
99
84
 
100
85
  func registerEventListeners() {
101
86
 
102
- guard let locationManager = TSLocationManager.sharedInstance() else { return }
87
+ let locationManager = BackgroundGeolocation.sharedInstance()
103
88
  weak var me = self
104
89
 
105
- locationManager.onLocation({ (location: TSLocation?) in
106
- guard let me = me, me.hasListeners(EVENT_LOCATION) else { return }
107
- if let locationDict = location?.toDictionary() as? [String: Any] {
108
- me.notifyListeners(EVENT_LOCATION, data: locationDict)
90
+ locationManager.onLocation({ (event: TSLocationEvent?) in
91
+ guard let me = me, me.hasListeners(TSEventNameLocation) else { return }
92
+ if let locationDict = event?.toDictionary() as? [String: Any] {
93
+ me.notifyListeners(TSEventNameLocation, data: locationDict)
109
94
  }
110
95
  }, failure: { error in
111
- guard let me = me, me.hasListeners(EVENT_LOCATION) else { return }
96
+ guard let me = me, me.hasListeners(TSEventNameLocation) else { return }
112
97
  if let nsError = error as? NSError {
113
- me.notifyListeners(EVENT_LOCATION, data: ["error": nsError.code] as [String: Any])
98
+ me.notifyListeners(TSEventNameLocation, data: ["error": nsError.code] as [String: Any])
114
99
  }
115
100
  })
116
101
 
117
- locationManager.onMotionChange({ (tsLocation: TSLocation?) in
118
- guard let me = me, me.hasListeners(EVENT_MOTIONCHANGE) else { return }
102
+ locationManager.onMotionChange({ (event: TSLocationEvent?) in
103
+ guard let me = me, me.hasListeners(TSEventNameMotionChange) else { return }
119
104
  let params: [String: Any] = [
120
- "isMoving": tsLocation?.isMoving as Any,
121
- "location": tsLocation?.toDictionary() as Any
105
+ "isMoving": event?.isMoving as Any,
106
+ "location": event?.toDictionary() as Any
122
107
  ]
123
- me.notifyListeners(EVENT_MOTIONCHANGE, data: params)
108
+ me.notifyListeners(TSEventNameMotionChange, data: params)
124
109
  })
125
110
 
126
111
  locationManager.onActivityChange({ (event: TSActivityChangeEvent?) in
127
- guard let me = me, me.hasListeners(EVENT_ACTIVITYCHANGE) else { return }
112
+ guard let me = me, me.hasListeners(TSEventNameActivityChange) else { return }
128
113
  let params: [String: Any] = [
129
114
  "activity": event?.activity as Any,
130
115
  "confidence": event?.confidence as Any
131
116
  ]
132
- me.notifyListeners(EVENT_ACTIVITYCHANGE, data: params)
117
+ me.notifyListeners(TSEventNameActivityChange, data: params)
133
118
  })
134
119
 
135
- locationManager.onHeartbeat({ (event:TSHeartbeatEvent?) in
136
- guard let me = me, me.hasListeners(EVENT_HEARTBEAT) else { return }
137
- let params: [String: Any] = [
138
- "location": event?.location.toDictionary() as Any
139
- ]
140
- me.notifyListeners(EVENT_HEARTBEAT, data: params)
120
+ locationManager.onHeartbeat({ (event: TSHeartbeatEvent?) in
121
+ guard let me = me, me.hasListeners(TSEventNameHeartbeat) else { return }
122
+ if let eventData = event?.toDictionary() as? [String: Any] {
123
+ me.notifyListeners(TSEventNameHeartbeat, data: eventData)
124
+ }
141
125
  })
142
126
 
143
127
  locationManager.onGeofence({ (event: TSGeofenceEvent?) in
144
- guard let me = me, me.hasListeners(EVENT_GEOFENCE) else { return }
145
- if var params = event?.toDictionary() as? [String: Any] {
146
- params["location"] = event?.location.toDictionary() as? [String: Any]
147
- me.notifyListeners(EVENT_GEOFENCE, data: params)
128
+ guard let me = me, me.hasListeners(TSEventNameGeofence) else { return }
129
+ if let eventData = event?.toDictionary() as? [String: Any] {
130
+ me.notifyListeners(TSEventNameGeofence, data: eventData)
148
131
  }
149
132
  })
150
-
133
+
151
134
  locationManager.onGeofencesChange({ (event: TSGeofencesChangeEvent?) in
152
- guard let me = me, me.hasListeners(EVENT_GEOFENCESCHANGE) else { return }
135
+ guard let me = me, me.hasListeners(TSEventNameGeofencesChange) else { return }
153
136
  if let eventData = event?.toDictionary() as? [String: Any] {
154
- me.notifyListeners(EVENT_GEOFENCESCHANGE, data: eventData)
137
+ me.notifyListeners(TSEventNameGeofencesChange, data: eventData)
155
138
  }
156
139
  })
157
-
140
+
158
141
  locationManager.onHttp({ (event: TSHttpEvent?) in
159
- guard let me = me, me.hasListeners(EVENT_HTTP) else { return }
142
+ guard let me = me, me.hasListeners(TSEventNameHttp) else { return }
160
143
  if let eventData = event?.toDictionary() as? [String: Any] {
161
- me.notifyListeners(EVENT_HTTP, data: eventData)
144
+ me.notifyListeners(TSEventNameHttp, data: eventData)
162
145
  }
163
146
  })
164
147
 
165
148
  locationManager.onProviderChange({ (event: TSProviderChangeEvent?) in
166
- guard let me = me, me.hasListeners(EVENT_PROVIDERCHANGE) else { return }
149
+ guard let me = me, me.hasListeners(TSEventNameProviderChange) else { return }
167
150
  if let eventData = event?.toDictionary() as? [String: Any] {
168
- me.notifyListeners(EVENT_PROVIDERCHANGE, data: eventData)
151
+ me.notifyListeners(TSEventNameProviderChange, data: eventData)
169
152
  }
170
153
  })
171
154
 
172
155
  locationManager.onSchedule({ (event: TSScheduleEvent?) in
173
- guard let me = me, me.hasListeners(EVENT_SCHEDULE) else { return }
156
+ guard let me = me, me.hasListeners(TSEventNameSchedule) else { return }
174
157
  if let state = event?.state as? [String: Any] {
175
- me.notifyListeners(EVENT_SCHEDULE, data: state)
158
+ me.notifyListeners(TSEventNameSchedule, data: state)
176
159
  }
177
160
  })
178
-
161
+
179
162
  locationManager.onPowerSaveChange({ (event: TSPowerSaveChangeEvent?) in
180
- guard let me = me, me.hasListeners(EVENT_POWERSAVECHANGE) else { return }
163
+ guard let me = me, me.hasListeners(TSEventNamePowerSaveChange) else { return }
181
164
  let params: [String: Any] = ["value": event?.isPowerSaveMode as Any]
182
- me.notifyListeners(EVENT_POWERSAVECHANGE, data: params)
165
+ me.notifyListeners(TSEventNamePowerSaveChange, data: params)
183
166
  })
184
167
 
185
168
  locationManager.onConnectivityChange({ (event: TSConnectivityChangeEvent?) in
186
- guard let me = me, me.hasListeners(EVENT_CONNECTIVITYCHANGE) else { return }
169
+ guard let me = me, me.hasListeners(TSEventNameConnectivityChange) else { return }
187
170
  let params: [String: Any] = ["connected": event?.hasConnection as Any]
188
- me.notifyListeners(EVENT_CONNECTIVITYCHANGE, data: params)
171
+ me.notifyListeners(TSEventNameConnectivityChange, data: params)
189
172
  })
190
173
 
191
174
  locationManager.onEnabledChange({ (event: TSEnabledChangeEvent?) in
192
- guard let me = me, me.hasListeners(EVENT_ENABLEDCHANGE) else { return }
193
- me.notifyListeners(EVENT_ENABLEDCHANGE, data: ["value": event?.enabled as Any])
175
+ guard let me = me, me.hasListeners(TSEventNameEnabledChange) else { return }
176
+ me.notifyListeners(TSEventNameEnabledChange, data: ["value": event?.enabled as Any])
194
177
  })
195
178
 
179
+ locationManager.onAuthorization({ (event: TSAuthorizationEvent?) in
180
+ guard let me = me, me.hasListeners(TSEventNameAuthorization) else { return }
181
+ if let eventData = event?.toDictionary() as? [String: Any] {
182
+ me.notifyListeners(TSEventNameAuthorization, data: eventData)
183
+ }
184
+ })
196
185
 
197
186
  }
198
187
 
199
188
  @objc func registerPlugin(_ call: CAPPluginCall) {
200
- let pluginId = call.getString("id")
189
+ guard let pluginId = call.getString("id") else {
190
+ call.reject("Missing required parameter: id")
191
+ return
192
+ }
201
193
  let config = TSConfig.sharedInstance()
202
- config?.registerPlugin(pluginId)
194
+ config.registerPlugin(pluginId)
195
+ call.resolve()
203
196
  }
204
197
 
205
198
  @objc func ready(_ call: CAPPluginCall) {
206
- guard let locationManager = TSLocationManager.sharedInstance(),
207
- let config = TSConfig.sharedInstance() else { return }
199
+ let locationManager = BackgroundGeolocation.sharedInstance()
200
+ let config = TSConfig.sharedInstance()
201
+
208
202
  let params = call.getObject("options") ?? [:]
209
203
  let reset = (params["reset"] as? Bool) ?? true
210
204
  if ready {
@@ -227,11 +221,11 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
227
221
  config.update(with: params)
228
222
  } else {
229
223
  if reset {
230
- config.reset(true)
224
+ config.resetConfig(true)
231
225
  config.update(with: params)
232
226
  } else if let auth = params["authorization"] as? [String: Any] {
233
- config.update { builder in
234
- builder?.authorization = TSAuthorization.create(with: auth)
227
+ config.batchUpdate { cfg in
228
+ cfg.authorization.update(with: auth)
235
229
  }
236
230
  }
237
231
  }
@@ -246,13 +240,15 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
246
240
 
247
241
  @objc func reset(_ call: CAPPluginCall) {
248
242
  let params = call.getObject("options") ?? [:]
249
- guard let config = TSConfig.sharedInstance() else { return }
243
+ let config = TSConfig.sharedInstance()
244
+
250
245
  if !params.isEmpty {
251
- config.reset(true)
246
+ config.resetConfig(true)
252
247
  config.update(with: params)
253
248
  } else {
254
- config.reset()
249
+ config.resetConfig(false)
255
250
  }
251
+
256
252
  if let configDict = config.toDictionary() as? [String: Any] {
257
253
  call.resolve(configDict)
258
254
  } else {
@@ -262,7 +258,7 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
262
258
 
263
259
  @objc func setConfig(_ call: CAPPluginCall) {
264
260
  let params = call.getObject("options") ?? [:]
265
- guard let config = TSConfig.sharedInstance() else { return }
261
+ let config = TSConfig.sharedInstance()
266
262
  config.update(with: params)
267
263
  if let configDict = config.toDictionary() as? [String: Any] {
268
264
  call.resolve(configDict)
@@ -272,7 +268,7 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
272
268
  }
273
269
 
274
270
  @objc func getState(_ call: CAPPluginCall) {
275
- guard let config = TSConfig.sharedInstance() else { return }
271
+ let config = TSConfig.sharedInstance()
276
272
  if let configDict = config.toDictionary() as? [String: Any] {
277
273
  call.resolve(configDict)
278
274
  } else {
@@ -282,7 +278,7 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
282
278
 
283
279
  @objc func start(_ call: CAPPluginCall) {
284
280
  DispatchQueue.main.async {
285
- guard let locationManager = TSLocationManager.sharedInstance() else { return }
281
+ let locationManager = BackgroundGeolocation.sharedInstance()
286
282
  locationManager.start()
287
283
  if let state = locationManager.getState() as? [String: Any] {
288
284
  call.resolve(state)
@@ -293,7 +289,7 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
293
289
  }
294
290
 
295
291
  @objc func stop(_ call: CAPPluginCall) {
296
- guard let locationManager = TSLocationManager.sharedInstance() else { return }
292
+ let locationManager = BackgroundGeolocation.sharedInstance()
297
293
  locationManager.stop()
298
294
  if let state = locationManager.getState() as? [String: Any] {
299
295
  call.resolve(state)
@@ -303,8 +299,8 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
303
299
  }
304
300
 
305
301
  @objc func startSchedule(_ call: CAPPluginCall) {
306
- guard let config = TSConfig.sharedInstance(),
307
- let locationManager = TSLocationManager.sharedInstance() else { return }
302
+ let config = TSConfig.sharedInstance()
303
+ let locationManager = BackgroundGeolocation.sharedInstance()
308
304
  locationManager.startSchedule()
309
305
  if let configDict = config.toDictionary() as? [String: Any] {
310
306
  call.resolve(configDict)
@@ -314,8 +310,8 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
314
310
  }
315
311
 
316
312
  @objc func stopSchedule(_ call: CAPPluginCall) {
317
- guard let config = TSConfig.sharedInstance(),
318
- let locationManager = TSLocationManager.sharedInstance() else { return }
313
+ let config = TSConfig.sharedInstance()
314
+ let locationManager = BackgroundGeolocation.sharedInstance()
319
315
  locationManager.stopSchedule()
320
316
  if let configDict = config.toDictionary() as? [String: Any] {
321
317
  call.resolve(configDict)
@@ -325,8 +321,8 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
325
321
  }
326
322
 
327
323
  @objc func startGeofences(_ call: CAPPluginCall) {
328
- guard let config = TSConfig.sharedInstance(),
329
- let locationManager = TSLocationManager.sharedInstance() else { return }
324
+ let config = TSConfig.sharedInstance()
325
+ let locationManager = BackgroundGeolocation.sharedInstance()
330
326
  locationManager.startGeofences()
331
327
  if let configDict = config.toDictionary() as? [String: Any] {
332
328
  call.resolve(configDict)
@@ -337,133 +333,157 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
337
333
 
338
334
  @objc func changePace(_ call: CAPPluginCall) {
339
335
  let isMoving = call.getBool("isMoving") ?? false
340
- guard let locationManager = TSLocationManager.sharedInstance() else { return }
336
+ let locationManager = BackgroundGeolocation.sharedInstance()
341
337
  locationManager.changePace(isMoving)
342
338
  call.resolve()
343
339
  }
344
340
 
345
341
  @objc func startBackgroundTask(_ call: CAPPluginCall) {
346
- guard let locationManager = TSLocationManager.sharedInstance() else { return }
342
+ let locationManager = BackgroundGeolocation.sharedInstance()
347
343
  let taskId = locationManager.createBackgroundTask()
348
- call.resolve(["taskId": taskId])
344
+ call.resolve(["taskId": taskId.rawValue])
349
345
  }
350
346
 
351
347
  @objc func stopBackgroundTask(_ call: CAPPluginCall) {
352
- let taskId = call.getInt("taskId") ?? -1
353
- guard let locationManager = TSLocationManager.sharedInstance() else { return }
354
- locationManager.stopBackgroundTask(UIBackgroundTaskIdentifier(rawValue: taskId))
348
+ guard let taskIdNumber = call.options["taskId"] as? NSNumber else {
349
+ call.reject("Missing required parameter: taskId")
350
+ return
351
+ }
352
+ let locationManager = BackgroundGeolocation.sharedInstance()
353
+ locationManager.stopBackgroundTask(UIBackgroundTaskIdentifier(rawValue: taskIdNumber.intValue))
355
354
  call.resolve()
356
355
  }
357
-
356
+
358
357
  @objc func getCurrentPosition(_ call: CAPPluginCall) {
359
358
  let options = call.getObject("options") ?? [:]
360
- guard let locationManager = TSLocationManager.sharedInstance() else { return }
361
-
362
- let request = TSCurrentPositionRequest(success: { (location: TSLocation?) in
363
- if let locationData = location?.toDictionary() as? [String: Any] {
364
- call.resolve(locationData)
365
- } else {
366
- call.reject("Failed to convert location to dictionary", nil, nil, [:])
367
- }
368
- }, failure: { error in
369
- if let nsError = error as? NSError {
370
- call.reject("\(nsError.code)", nil, error, nsError.userInfo)
371
- } else {
372
- call.reject("Unknown error occurred", nil, error, [:])
359
+ let locationManager = BackgroundGeolocation.sharedInstance()
360
+
361
+ let request = TSCurrentPositionRequest.make(
362
+ type: .current,
363
+ success: { event in
364
+ if let eventData = event.data as? [String: Any] {
365
+ call.resolve(eventData)
366
+ } else if let dict = event.toDictionary() as? [String: Any] {
367
+ call.resolve(dict)
368
+ } else {
369
+ call.reject("Failed to convert location to dictionary", nil, nil, [:])
370
+ }
371
+ },
372
+ failure: { error in
373
+ call.reject("get_current_position_error", error.localizedDescription, error, [:])
373
374
  }
374
- })
375
+ )
375
376
 
376
377
  if let timeout = options["timeout"] as? Double {
377
- request?.timeout = timeout
378
+ request.timeout = timeout
378
379
  }
379
- if let maximumAge = options["maximumAge"] as? Double {
380
- request?.maximumAge = maximumAge
380
+ if let maximumAge = options["maximumAge"] as? Int {
381
+ request.maximumAge = maximumAge
381
382
  }
382
383
  if let persist = options["persist"] as? Bool {
383
- request?.persist = persist
384
+ request.persist = persist
384
385
  }
385
386
  if let samples = options["samples"] as? Int {
386
- request?.samples = Int32(samples)
387
+ request.samples = samples
387
388
  }
388
389
  if let desiredAccuracy = options["desiredAccuracy"] as? Double {
389
- request?.desiredAccuracy = desiredAccuracy
390
+ request.desiredAccuracy = desiredAccuracy
390
391
  }
391
392
  if let extras = options["extras"] as? [String: Any] {
392
- request?.extras = extras
393
+ request.extras = extras
393
394
  }
395
+
394
396
  locationManager.getCurrentPosition(request)
395
397
  }
396
-
398
+
397
399
  @objc func watchPosition(_ call: CAPPluginCall) {
398
400
  let options = call.getObject("options") ?? [:]
399
401
  weak var me = self
400
-
401
- let request = TSWatchPositionRequest(success: { (location: TSLocation?) in
402
- guard let me = me else { return }
403
- if !me.hasListeners(EVENT_WATCHPOSITION) {
404
- TSLocationManager.sharedInstance()?.stopWatchPosition()
405
- return
406
- }
407
- if let locationData = location?.toDictionary() as? [String: Any] {
408
- me.notifyListeners(EVENT_WATCHPOSITION, data: locationData)
409
- }
410
- }, failure: { error in
411
- guard let me = me else { return }
412
- if !me.hasListeners(EVENT_WATCHPOSITION) {
413
- TSLocationManager.sharedInstance()?.stopWatchPosition()
414
- return
415
- }
416
- if let nsError = error as? NSError {
417
- let result: [String: Any] = [
418
- "error": [
419
- "code": nsError.code,
420
- "message": nsError.localizedDescription
402
+ let locationManager = BackgroundGeolocation.sharedInstance()
403
+
404
+ let request = TSWatchPositionRequest.make(
405
+ interval: 1000,
406
+ success: { event in
407
+ guard let me = me else { return }
408
+
409
+ if let eventData = event.locationEvent.toDictionary() as? [String: Any] {
410
+ me.notifyListeners(TSEventNameWatchPosition, data: eventData)
411
+ } else if let dict = event.toDictionary() as? [String: Any] {
412
+ me.notifyListeners(TSEventNameWatchPosition, data: dict)
413
+ }
414
+ },
415
+ failure: { error in
416
+ guard let me = me else { return }
417
+
418
+ if let nsError = error as? NSError {
419
+ let result: [String: Any] = [
420
+ "error": [
421
+ "code": nsError.code,
422
+ "message": nsError.localizedDescription
423
+ ]
424
+ ]
425
+ me.notifyListeners(TSEventNameWatchPosition, data: result)
426
+ } else {
427
+ let result: [String: Any] = [
428
+ "error": [
429
+ "code": -1,
430
+ "message": error.localizedDescription
431
+ ]
421
432
  ]
422
- ]
423
- me.notifyListeners(EVENT_WATCHPOSITION, data: result)
433
+ me.notifyListeners(TSEventNameWatchPosition, data: result)
434
+ }
424
435
  }
425
- })
436
+ )
426
437
 
427
438
  if let interval = options["interval"] as? Double {
428
- request?.interval = interval
439
+ request.interval = interval
429
440
  }
441
+
430
442
  if let desiredAccuracy = options["desiredAccuracy"] as? Double {
431
- request?.desiredAccuracy = desiredAccuracy
443
+ request.desiredAccuracy = desiredAccuracy
432
444
  }
445
+
433
446
  if let persist = options["persist"] as? Bool {
434
- request?.persist = persist
447
+ request.persist = persist
435
448
  }
449
+
436
450
  if let extras = options["extras"] as? [String: Any] {
437
- request?.extras = extras
451
+ request.extras = extras
438
452
  }
439
- if let timeout = options["timeout"] as? Double {
440
- request?.timeout = timeout
453
+
454
+ if let timeoutNumber = options["timeout"] as? NSNumber {
455
+ request.timeout = timeoutNumber.doubleValue
441
456
  }
442
- TSLocationManager.sharedInstance()?.watchPosition(request)
443
- call.resolve()
457
+
458
+ let watchId = Int(locationManager.watchPosition(request))
459
+ call.resolve(["watchId": NSNumber(value: watchId)])
444
460
  }
445
461
 
462
+
446
463
  @objc func stopWatchPosition(_ call: CAPPluginCall) {
447
- TSLocationManager.sharedInstance()?.stopWatchPosition()
464
+ guard let watchIdNumber = call.options["watchId"] as? NSNumber else {
465
+ call.reject("Missing required parameter: watchId")
466
+ return
467
+ }
468
+ let watchId = watchIdNumber.intValue
469
+ BackgroundGeolocation.sharedInstance().stopWatchPosition(watchId)
448
470
  call.resolve()
449
471
  }
450
472
 
451
473
  // Locations Database
452
474
 
453
475
  @objc func getLocations(_ call: CAPPluginCall) {
454
- TSLocationManager.sharedInstance()?.getLocations({ records in
476
+ let locationManager = BackgroundGeolocation.sharedInstance()
477
+ locationManager.getLocations({ records in
455
478
  call.resolve(["locations": records as Any])
456
479
  }, failure: { error in
457
- if let errorString = error {
458
- call.reject(errorString, nil, nil, [:])
459
- } else {
460
- call.reject("Unknown error", nil, nil, [:])
461
- }
480
+ call.reject(error, nil, nil, [:])
462
481
  })
463
482
  }
464
483
 
465
484
  @objc func sync(_ call: CAPPluginCall) {
466
- TSLocationManager.sharedInstance()?.sync({ records in
485
+ let locationManager = BackgroundGeolocation.sharedInstance()
486
+ locationManager.sync({ records in
467
487
  call.resolve(["locations": records as Any])
468
488
  }, failure: { error in
469
489
  if let nsError = error as? NSError {
@@ -475,44 +495,42 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
475
495
  }
476
496
 
477
497
  @objc func getGeofences(_ call: CAPPluginCall) {
478
- TSLocationManager.sharedInstance()?.getGeofences({ geofences in
479
- if let geofenceArray = geofences {
480
- let result = geofenceArray.compactMap { ($0 as? TSGeofence)?.toDictionary() as? [String: Any] }
481
- call.resolve(["geofences": result])
482
- } else {
483
- call.resolve(["geofences": []])
484
- }
498
+ let locationManager = BackgroundGeolocation.sharedInstance()
499
+ locationManager.getGeofences({ geofences in
500
+ let geofenceArray = geofences ?? []
501
+ let result = geofenceArray.compactMap { ($0 as? TSGeofence)?.toDictionary() as? [String: Any] }
502
+ call.resolve(["geofences": result])
485
503
  }, failure: { error in
486
- if let errorString = error {
487
- call.reject(errorString, nil, nil, [:])
488
- } else {
489
- call.reject("Unknown error", nil, nil, [:])
490
- }
504
+ call.reject(error, nil, nil, [:])
491
505
  })
492
506
  }
493
-
494
507
 
495
508
  @objc func getGeofence(_ call: CAPPluginCall) {
496
- let identifier = call.getString("identifier")
497
- TSLocationManager.sharedInstance()?.getGeofence(identifier, success: { geofence in
498
- if let geofence = geofence,
499
- let geofenceData = geofence.toDictionary() as? [String: Any] {
509
+ guard let identifier = call.getString("identifier") else {
510
+ call.reject("Missing required parameter: identifier")
511
+ return
512
+ }
513
+
514
+ let locationManager = BackgroundGeolocation.sharedInstance()
515
+ locationManager.getGeofence(identifier, success: { geofence in
516
+ if let geofenceData = geofence.toDictionary() as? [String: Any] {
500
517
  call.resolve(geofenceData)
501
518
  } else {
502
519
  call.reject("Failed to convert geofence to dictionary", nil, nil, [:])
503
520
  }
504
521
  }, failure: { error in
505
- if let errorString = error {
506
- call.reject(errorString, nil, nil, [:])
507
- } else {
508
- call.reject("Unknown error", nil, nil, [:])
509
- }
522
+ call.reject(error, nil, nil, [:])
510
523
  })
511
524
  }
512
525
 
513
526
  @objc func geofenceExists(_ call: CAPPluginCall) {
514
- let identifier = call.getString("identifier")
515
- TSLocationManager.sharedInstance()?.geofenceExists(identifier, callback: { exists in
527
+ guard let identifier = call.getString("identifier") else {
528
+ call.reject("Missing required parameter: identifier")
529
+ return
530
+ }
531
+
532
+ let locationManager = BackgroundGeolocation.sharedInstance()
533
+ locationManager.geofenceExists(identifier, callback: { exists in
516
534
  call.resolve(["exists": exists])
517
535
  })
518
536
  }
@@ -524,14 +542,12 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
524
542
  call.reject(error, nil, nil, [:])
525
543
  return
526
544
  }
527
- TSLocationManager.sharedInstance()?.add(geofence, success: {
545
+
546
+ let locationManager = BackgroundGeolocation.sharedInstance()
547
+ locationManager.add(geofence, success: {
528
548
  call.resolve()
529
549
  }, failure: { error in
530
- if let errorString = error {
531
- call.reject(errorString, nil, nil, [:])
532
- } else {
533
- call.reject("Unknown error", nil, nil, [:])
534
- }
550
+ call.reject(error, nil, nil, [:])
535
551
  })
536
552
  }
537
553
 
@@ -540,6 +556,7 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
540
556
  call.reject("Invalid geofences data", nil, nil, [:])
541
557
  return
542
558
  }
559
+
543
560
  var geofences: [TSGeofence] = []
544
561
  for params in data {
545
562
  if let geofence = buildGeofence(params) {
@@ -550,73 +567,70 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
550
567
  return
551
568
  }
552
569
  }
553
- TSLocationManager.sharedInstance()?.addGeofences(geofences, success: {
570
+
571
+ let locationManager = BackgroundGeolocation.sharedInstance()
572
+ locationManager.addGeofences(geofences, success: {
554
573
  call.resolve()
555
574
  }, failure: { error in
556
- if let errorString = error {
557
- call.reject(errorString, nil, nil, [:])
558
- } else {
559
- call.reject("Unknown error", nil, nil, [:])
560
- }
575
+ call.reject(error, nil, nil, [:])
561
576
  })
562
577
  }
563
578
 
564
579
  @objc func removeGeofence(_ call: CAPPluginCall) {
565
- let identifier = call.getString("identifier")
566
- TSLocationManager.sharedInstance()?.removeGeofence(identifier, success: {
580
+ guard let identifier = call.getString("identifier") else {
581
+ call.reject("Missing required parameter: identifier")
582
+ return
583
+ }
584
+
585
+ let locationManager = BackgroundGeolocation.sharedInstance()
586
+ locationManager.removeGeofence(identifier, success: {
567
587
  call.resolve()
568
588
  }, failure: { error in
569
- if let errorString = error {
570
- call.reject(errorString, nil, nil, [:])
571
- } else {
572
- call.reject("Unknown error", nil, nil, [:])
573
- }
589
+ call.reject(error, nil, nil, [:])
574
590
  })
575
591
  }
576
592
 
577
593
  @objc func removeGeofences(_ call: CAPPluginCall) {
578
- guard let identifiers = call.options["identifiers"] as? [String] else {
579
- call.reject("Invalid identifiers", nil, nil, [:])
580
- return
581
- }
582
- TSLocationManager.sharedInstance()?.removeGeofences(identifiers, success: {
594
+ let identifiers = call.options["identifiers"] as? [String] ?? []
595
+ let locationManager = BackgroundGeolocation.sharedInstance()
596
+ locationManager.removeGeofences(identifiers, success: {
583
597
  call.resolve()
584
598
  }, failure: { error in
585
- if let errorString = error {
586
- call.reject(errorString, nil, nil, [:])
587
- } else {
588
- call.reject("Unknown error", nil, nil, [:])
589
- }
599
+ call.reject(error, nil, nil, [:])
590
600
  })
591
601
  }
592
602
 
593
603
  @objc func getOdometer(_ call: CAPPluginCall) {
594
- guard let locationManager = TSLocationManager.sharedInstance() else { return }
604
+ let locationManager = BackgroundGeolocation.sharedInstance()
595
605
  let distance = locationManager.getOdometer()
596
606
  call.resolve(["odometer": distance])
597
607
  }
598
608
 
599
609
  @objc func setOdometer(_ call: CAPPluginCall) {
600
610
  let value = call.getDouble("odometer") ?? 0
601
- guard let locationManager = TSLocationManager.sharedInstance() else { return }
602
- let request = TSCurrentPositionRequest(success: { location in
603
- if let locationData = location?.toDictionary() as? [String: Any] {
604
- call.resolve(locationData)
605
- } else {
606
- call.reject("Failed to convert location to dictionary", nil, nil, [:])
607
- }
608
- }, failure: { error in
609
- if let nsError = error as? NSError {
610
- call.reject("\(nsError.code)", nil, error, nsError.userInfo)
611
- } else {
612
- call.reject("Unknown error occurred", nil, error, [:])
611
+ let locationManager = BackgroundGeolocation.sharedInstance()
612
+
613
+ let request = TSCurrentPositionRequest.make(
614
+ type: .current,
615
+ success: { event in
616
+ if let eventData = event.data as? [String: Any] {
617
+ call.resolve(eventData)
618
+ } else if let dict = event.toDictionary() as? [String: Any] {
619
+ call.resolve(dict)
620
+ } else {
621
+ call.reject("Failed to convert location to dictionary", nil, nil, [:])
622
+ }
623
+ },
624
+ failure: { error in
625
+ call.reject("set_odometer_error", error.localizedDescription, error, [:])
613
626
  }
614
- })
627
+ )
628
+
615
629
  locationManager.setOdometer(value, request: request)
616
630
  }
617
-
631
+
618
632
  @objc func destroyLocations(_ call: CAPPluginCall) {
619
- guard let locationManager = TSLocationManager.sharedInstance() else { return }
633
+ let locationManager = BackgroundGeolocation.sharedInstance()
620
634
  let result = locationManager.destroyLocations()
621
635
  if result {
622
636
  call.resolve()
@@ -626,54 +640,48 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
626
640
  }
627
641
 
628
642
  @objc func destroyLocation(_ call: CAPPluginCall) {
629
- let uuid = call.getString("uuid")
630
- TSLocationManager.sharedInstance()?.destroyLocation(uuid, success: {
643
+ guard let uuid = call.getString("uuid") else {
644
+ call.reject("Missing required parameter: uuid")
645
+ return
646
+ }
647
+
648
+ let locationManager = BackgroundGeolocation.sharedInstance()
649
+ locationManager.destroyLocation(uuid, success: {
631
650
  call.resolve()
632
651
  }, failure: { error in
633
- if let errorString = error {
634
- call.reject(errorString, nil, nil, [:])
635
- } else {
636
- call.reject("Unknown error", nil, nil, [:])
637
- }
652
+ call.reject(error, nil, nil, [:])
638
653
  })
639
654
  }
640
655
 
641
656
  @objc func getCount(_ call: CAPPluginCall) {
642
- guard let locationManager = TSLocationManager.sharedInstance() else { return }
657
+ let locationManager = BackgroundGeolocation.sharedInstance()
643
658
  let count = locationManager.getCount()
644
659
  call.resolve(["count": count])
645
660
  }
646
661
 
647
662
  @objc func insertLocation(_ call: CAPPluginCall) {
648
663
  let params = call.getObject("options") ?? [:]
649
- TSLocationManager.sharedInstance()?.insertLocation(params, success: { uuid in
650
- call.resolve(["uuid": uuid as Any])
664
+ let locationManager = BackgroundGeolocation.sharedInstance()
665
+ locationManager.insertLocation(params, success: { uuid in
666
+ call.resolve(["uuid": uuid])
651
667
  }, failure: { error in
652
- if let errorString = error {
653
- call.reject(errorString, nil, nil, [:])
654
- } else {
655
- call.reject("Unknown error", nil, nil, [:])
656
- }
668
+ call.reject(error, nil, nil, [:])
657
669
  })
658
670
  }
659
671
 
660
672
  @objc func getLog(_ call: CAPPluginCall) {
661
673
  let params = call.getObject("options") ?? [:]
662
- guard let locationManager = TSLocationManager.sharedInstance() else { return }
674
+ let locationManager = BackgroundGeolocation.sharedInstance()
663
675
  let query = LogQuery(dictionary: params)
664
676
  locationManager.getLog(query, success: { log in
665
- call.resolve(["log": log as Any])
677
+ call.resolve(["log": log])
666
678
  }, failure: { error in
667
- if let errorString = error {
668
- call.reject(errorString, nil, nil, [:])
669
- } else {
670
- call.reject("Unknown error", nil, nil, [:])
671
- }
679
+ call.reject(error, nil, nil, [:])
672
680
  })
673
681
  }
674
682
 
675
683
  @objc func destroyLog(_ call: CAPPluginCall) {
676
- guard let locationManager = TSLocationManager.sharedInstance() else { return }
684
+ let locationManager = BackgroundGeolocation.sharedInstance()
677
685
  let result = locationManager.destroyLog()
678
686
  if result {
679
687
  call.resolve()
@@ -684,43 +692,45 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
684
692
 
685
693
  @objc func emailLog(_ call: CAPPluginCall) {
686
694
  let params = call.getObject("query") ?? [:]
687
- let email = call.getString("email")
695
+ guard let email = call.getString("email") else {
696
+ call.reject("Missing required parameter: email")
697
+ return
698
+ }
699
+
688
700
  let query = LogQuery(dictionary: params)
689
- TSLocationManager.sharedInstance()?.emailLog(email, query: query, success: {
701
+ let locationManager = BackgroundGeolocation.sharedInstance()
702
+ locationManager.emailLog(email, query: query, success: {
690
703
  call.resolve()
691
704
  }, failure: { error in
692
- if let errorString = error {
693
- call.reject(errorString, nil, nil, [:])
694
- } else {
695
- call.reject("Unknown error", nil, nil, [:])
696
- }
705
+ call.reject(error, nil, nil, [:])
697
706
  })
698
707
  }
699
708
 
700
709
  @objc func uploadLog(_ call: CAPPluginCall) {
701
710
  let params = call.getObject("query") ?? [:]
702
- let url = call.getString("url")
711
+ guard let url = call.getString("url") else {
712
+ call.reject("Missing required parameter: url")
713
+ return
714
+ }
715
+
703
716
  let query = LogQuery(dictionary: params)
704
- TSLocationManager.sharedInstance()?.uploadLog(url, query: query, success: {
717
+ let locationManager = BackgroundGeolocation.sharedInstance()
718
+ locationManager.uploadLog(url, query: query, success: {
705
719
  call.resolve()
706
720
  }, failure: { error in
707
- if let errorString = error {
708
- call.reject(errorString, nil, nil, [:])
709
- } else {
710
- call.reject("Unknown error", nil, nil, [:])
711
- }
721
+ call.reject(error, nil, nil, [:])
712
722
  })
713
723
  }
714
724
 
715
725
  @objc func log(_ call: CAPPluginCall) {
716
726
  let level = call.getString("level") ?? "debug"
717
727
  let message = call.getString("message") ?? "no message"
718
- TSLocationManager.sharedInstance()?.log(level, message: message)
728
+ BackgroundGeolocation.sharedInstance().log(level, message: message)
719
729
  call.resolve()
720
730
  }
721
731
 
722
732
  @objc func getSensors(_ call: CAPPluginCall) {
723
- guard let locationManager = TSLocationManager.sharedInstance() else { return }
733
+ let locationManager = BackgroundGeolocation.sharedInstance()
724
734
  let sensors: [String: Any] = [
725
735
  "platform": "ios",
726
736
  "accelerometer": locationManager.isAccelerometerAvailable(),
@@ -733,7 +743,7 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
733
743
 
734
744
  @objc func getDeviceInfo(_ call: CAPPluginCall) {
735
745
  let deviceInfo = TSDeviceInfo.sharedInstance()
736
- if let infoDict = deviceInfo?.toDictionary("capacitor") as? [String: Any] {
746
+ if let infoDict = deviceInfo.toDictionary("capacitor") as? [String: Any] {
737
747
  call.resolve(infoDict)
738
748
  } else {
739
749
  call.reject("Failed to get device info", nil, nil, [:])
@@ -741,7 +751,7 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
741
751
  }
742
752
 
743
753
  @objc func isPowerSaveMode(_ call: CAPPluginCall) {
744
- guard let locationManager = TSLocationManager.sharedInstance() else { return }
754
+ let locationManager = BackgroundGeolocation.sharedInstance()
745
755
  call.resolve([
746
756
  "isPowerSaveMode": locationManager.isPowerSaveMode()
747
757
  ])
@@ -762,16 +772,16 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
762
772
  }
763
773
 
764
774
  @objc func requestPermission(_ call: CAPPluginCall) {
765
- TSLocationManager.sharedInstance()?.requestPermission({ status in
766
- call.resolve(["success": true, "status": status as Any])
775
+ BackgroundGeolocation.sharedInstance().requestPermission({ status in
776
+ call.resolve(["success": true, "status": status])
767
777
  }, failure: { status in
768
- call.resolve(["success": false, "status": status as Any])
778
+ call.resolve(["success": false, "status": status])
769
779
  })
770
780
  }
771
781
 
772
782
  @objc func requestTemporaryFullAccuracy(_ call: CAPPluginCall) {
773
783
  let purpose = call.getString("purpose") ?? ""
774
- TSLocationManager.sharedInstance()?.requestTemporaryFullAccuracy(purpose, success: { accuracyAuthorization in
784
+ BackgroundGeolocation.sharedInstance().requestTemporaryFullAccuracy(purpose, success: { accuracyAuthorization in
775
785
  call.resolve(["accuracyAuthorization": accuracyAuthorization])
776
786
  }, failure: { error in
777
787
  if let nsError = error as? NSError,
@@ -784,65 +794,98 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
784
794
  }
785
795
 
786
796
  @objc func getProviderState(_ call: CAPPluginCall) {
787
- guard let locationManager = TSLocationManager.sharedInstance() else { return }
788
- if let event = locationManager.getProviderState(),
789
- let stateData = event.toDictionary() as? [String: Any] {
797
+ let locationManager = BackgroundGeolocation.sharedInstance()
798
+ let event = locationManager.getProviderState()
799
+ if let stateData = event.toDictionary() as? [String: Any] {
790
800
  call.resolve(stateData)
791
801
  } else {
792
802
  call.reject("Failed to convert provider state to dictionary", nil, nil, [:])
793
803
  }
794
804
  }
795
-
805
+
796
806
  @objc func getTransistorToken(_ call: CAPPluginCall) {
797
- let orgname = call.getString("org")
798
- let username = call.getString("username")
799
- let url = call.getString("url")
800
- TransistorAuthorizationToken.findOrCreate(withOrg: orgname, username: username, url: url, framework: "capacitor", success: { token in
801
- if let token = token,
802
- let tokenData = token.toDictionary() as? [String: Any] {
803
- let result: [String: Any] = [
804
- "success": true,
805
- "token": tokenData
806
- ]
807
+ guard let orgname = call.getString("org") else {
808
+ call.resolve([
809
+ "success": false,
810
+ "status": -1,
811
+ "message": "Missing required parameter: org"
812
+ ])
813
+ return
814
+ }
815
+
816
+ guard let username = call.getString("username") else {
817
+ call.resolve([
818
+ "success": false,
819
+ "status": -1,
820
+ "message": "Missing required parameter: username"
821
+ ])
822
+ return
823
+ }
824
+
825
+ guard let url = call.getString("url") else {
826
+ call.resolve([
827
+ "success": false,
828
+ "status": -1,
829
+ "message": "Missing required parameter: url"
830
+ ])
831
+ return
832
+ }
833
+
834
+ TransistorAuthorizationToken.findOrCreate(
835
+ withOrg: orgname,
836
+ username: username,
837
+ url: url,
838
+ framework: "capacitor",
839
+ success: { token in
840
+ if let tokenData = token.toDictionary() as? [String: Any] {
841
+ let result: [String: Any] = [
842
+ "success": true,
843
+ "token": tokenData
844
+ ]
845
+ call.resolve(result)
846
+ } else {
847
+ call.reject("Failed to convert token to dictionary", nil, nil, [:])
848
+ }
849
+ },
850
+ failure: { error in
851
+ let result: [String: Any]
852
+ if let nsError = error as? NSError {
853
+ result = [
854
+ "success": false,
855
+ "status": nsError.code,
856
+ "message": nsError.localizedDescription
857
+ ]
858
+ } else {
859
+ result = [
860
+ "success": false,
861
+ "status": -1,
862
+ "message": "Unknown error occurred"
863
+ ]
864
+ }
807
865
  call.resolve(result)
808
- } else {
809
- call.reject("Failed to convert token to dictionary", nil, nil, [:])
810
- }
811
- }, failure: { error in
812
- let result: [String: Any]
813
- if let nsError = error as? NSError {
814
- result = [
815
- "success": false,
816
- "status": nsError.code,
817
- "message": nsError.localizedDescription
818
- ]
819
- } else {
820
- result = [
821
- "success": false,
822
- "status": -1,
823
- "message": "Unknown error occurred"
824
- ]
825
866
  }
826
- call.resolve(result)
827
- })
867
+ )
828
868
  }
829
-
869
+
830
870
  @objc func destroyTransistorToken(_ call: CAPPluginCall) {
831
- let url = call.getString("url")
871
+ guard let url = call.getString("url") else {
872
+ call.reject("Missing required parameter: url")
873
+ return
874
+ }
832
875
  TransistorAuthorizationToken.destroy(withUrl: url)
833
876
  call.resolve()
834
877
  }
835
878
 
879
+
836
880
  @objc func playSound(_ call: CAPPluginCall) {
837
881
  let soundId = call.getInt("soundId") ?? 0
838
- TSLocationManager.sharedInstance()?.playSound(UInt32(soundId))
882
+ BackgroundGeolocation.sharedInstance().playSound(UInt32(soundId))
839
883
  call.resolve()
840
884
  }
841
885
 
842
886
  @objc func removeAllEventListeners(_ call: CAPPluginCall) {
843
887
  self.removeAllListeners(call)
844
888
  NSLog("BackgroundGeolocation plugin removeAllListeners")
845
- call.resolve()
846
889
  }
847
890
 
848
891
  func buildGeofence(_ params: [String: Any]) -> TSGeofence? {
@@ -850,15 +893,19 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
850
893
  (params["vertices"] != nil || (params["radius"] != nil && params["latitude"] != nil && params["longitude"] != nil)) else {
851
894
  return nil
852
895
  }
853
- let radius = (params["radius"] as? Double) ?? 0
854
- let latitude = (params["latitude"] as? Double) ?? 0
855
- let longitude = (params["longitude"] as? Double) ?? 0
896
+
897
+ let radius = (params["radius"] as? NSNumber)?.doubleValue ?? 0
898
+ let latitude = (params["latitude"] as? NSNumber)?.doubleValue ?? 0
899
+ let longitude = (params["longitude"] as? NSNumber)?.doubleValue ?? 0
856
900
  let notifyOnEntry = (params["notifyOnEntry"] as? Bool) ?? false
857
901
  let notifyOnExit = (params["notifyOnExit"] as? Bool) ?? false
858
902
  let notifyOnDwell = (params["notifyOnDwell"] as? Bool) ?? false
859
- let loiteringDelay = (params["loiteringDelay"] as? Double) ?? 0
903
+ let loiteringDelay = (params["loiteringDelay"] as? NSNumber)?.doubleValue ?? 0
860
904
  let extras = params["extras"] as? [String: Any]
861
- let vertices = params["vertices"] as? [[Double]]
905
+
906
+ let vertices: [[Double]]? = (params["vertices"] as? [[NSNumber]])?.map {
907
+ $0.map { $0.doubleValue }
908
+ }
862
909
 
863
910
  return TSGeofence(
864
911
  identifier: identifier,
@@ -875,6 +922,6 @@ public class BackgroundGeolocationModule: CAPPlugin, CAPBridgedPlugin {
875
922
  }
876
923
 
877
924
  deinit {
878
- TSLocationManager.sharedInstance()?.removeListeners()
925
+ BackgroundGeolocation.sharedInstance().removeListeners()
879
926
  }
880
927
  }