@capacitor/geolocation 7.1.0-dev.2 → 7.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -48,7 +48,7 @@ Read about [Setting Permissions](https://capacitorjs.com/docs/android/configurat
48
48
 
49
49
  </docgen-index>
50
50
 
51
- For list of error codes, see [Errors](#Errors)
51
+ For list of error codes, see [Errors](#errors)
52
52
 
53
53
  <docgen-api>
54
54
  <!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
@@ -2,6 +2,7 @@ import Capacitor
2
2
  import IONGeolocationLib
3
3
 
4
4
  private enum GeolocationCallbackType {
5
+ case requestPermissions
5
6
  case location
6
7
  case watch
7
8
 
@@ -10,7 +11,7 @@ private enum GeolocationCallbackType {
10
11
  }
11
12
 
12
13
  var shouldClearAfterSending: Bool {
13
- self == .location
14
+ self == .location || self == .requestPermissions
14
15
  }
15
16
  }
16
17
 
@@ -20,23 +21,34 @@ private struct GeolocationCallbackGroup {
20
21
  }
21
22
 
22
23
  final class GeolocationCallbackManager {
24
+ private(set) var requestPermissionsCallbacks: [CAPPluginCall]
23
25
  private(set) var locationCallbacks: [CAPPluginCall]
24
26
  private(set) var watchCallbacks: [String: CAPPluginCall]
25
27
  private let capacitorBridge: CAPBridgeProtocol?
26
28
 
27
29
  private var allCallbackGroups: [GeolocationCallbackGroup] {
28
30
  [
31
+ .init(ids: requestPermissionsCallbacks, type: .requestPermissions),
29
32
  .init(ids: locationCallbacks, type: .location),
30
33
  .init(ids: Array(watchCallbacks.values), type: .watch)
31
34
  ]
32
35
  }
36
+ private var requestPermissionsCallbackGroup: GeolocationCallbackGroup? {
37
+ allCallbackGroups.first { $0.type == .requestPermissions }
38
+ }
33
39
 
34
40
  init(capacitorBridge: CAPBridgeProtocol?) {
35
41
  self.capacitorBridge = capacitorBridge
42
+ self.requestPermissionsCallbacks = []
36
43
  self.locationCallbacks = []
37
44
  self.watchCallbacks = [:]
38
45
  }
39
46
 
47
+ func addRequestPermissionsCallback(capacitorCall call: CAPPluginCall) {
48
+ capacitorBridge?.saveCall(call)
49
+ requestPermissionsCallbacks.append(call)
50
+ }
51
+
40
52
  func addLocationCallback(capacitorCall call: CAPPluginCall) {
41
53
  capacitorBridge?.saveCall(call)
42
54
  locationCallbacks.append(call)
@@ -47,6 +59,13 @@ final class GeolocationCallbackManager {
47
59
  watchCallbacks[watchId] = call
48
60
  }
49
61
 
62
+ func clearRequestPermissionsCallbacks() {
63
+ requestPermissionsCallbacks.forEach {
64
+ capacitorBridge?.releaseCall($0)
65
+ }
66
+ requestPermissionsCallbacks.removeAll()
67
+ }
68
+
50
69
  func clearWatchCallbackIfExists(_ watchId: String) {
51
70
  if let callbackToRemove = watchCallbacks[watchId] {
52
71
  capacitorBridge?.releaseCall(callbackToRemove)
@@ -69,10 +88,25 @@ final class GeolocationCallbackManager {
69
88
  call.resolve(data)
70
89
  }
71
90
 
91
+ func sendRequestPermissionsSuccess(_ permissionsResult: String) {
92
+ if let group = requestPermissionsCallbackGroup {
93
+ let data = [
94
+ Constants.AuthorisationStatus.ResultKey.location: permissionsResult,
95
+ Constants.AuthorisationStatus.ResultKey.coarseLocation: permissionsResult
96
+ ]
97
+ send(.success(data), to: group)
98
+ }
99
+ }
100
+
72
101
  func sendSuccess(with position: IONGLOCPositionModel) {
73
102
  createPluginResult(status: .success(position.toJSObject()))
74
103
  }
75
104
 
105
+ func sendError(_ call: CAPPluginCall, error: GeolocationError) {
106
+ let errorModel = error.toCodeMessagePair()
107
+ call.reject(errorModel.1, errorModel.0)
108
+ }
109
+
76
110
  func sendError(_ error: GeolocationError) {
77
111
  createPluginResult(status: .error(error.toCodeMessagePair()))
78
112
  }
@@ -87,6 +121,7 @@ private enum CallResultStatus {
87
121
  }
88
122
 
89
123
  private extension GeolocationCallbackManager {
124
+
90
125
  func createPluginResult(status: CallResultStatus) {
91
126
  allCallbackGroups.forEach {
92
127
  send(status, to: $0)
@@ -112,6 +147,8 @@ private extension GeolocationCallbackManager {
112
147
  func clearCallbacks(for type: GeolocationCallbackType) {
113
148
  if case .location = type {
114
149
  clearLocationCallbacks()
150
+ } else if case .requestPermissions = type {
151
+ clearRequestPermissionsCallbacks()
115
152
  }
116
153
  }
117
154
  }
@@ -53,8 +53,8 @@ public class GeolocationPlugin: CAPPlugin, CAPBridgedPlugin {
53
53
  callbackManager?.sendSuccess(call)
54
54
  }
55
55
 
56
- override public func checkPermissions(_ call: CAPPluginCall) {
57
- checkIfLocationServicesAreEnabled()
56
+ @objc override public func checkPermissions(_ call: CAPPluginCall) {
57
+ guard checkIfLocationServicesAreEnabled(call) else { return }
58
58
 
59
59
  let status = switch locationService?.authorisationStatus {
60
60
  case .restricted, .denied: Constants.AuthorisationStatus.Status.denied
@@ -69,11 +69,12 @@ public class GeolocationPlugin: CAPPlugin, CAPBridgedPlugin {
69
69
  callbackManager?.sendSuccess(call, with: callResultData)
70
70
  }
71
71
 
72
- override public func requestPermissions(_ call: CAPPluginCall) {
73
- checkIfLocationServicesAreEnabled()
72
+ @objc override public func requestPermissions(_ call: CAPPluginCall) {
73
+ guard checkIfLocationServicesAreEnabled(call) else { return }
74
74
 
75
75
  if locationService?.authorisationStatus == .notDetermined {
76
76
  shouldSetupBindings()
77
+ callbackManager?.addRequestPermissionsCallback(capacitorCall: call)
77
78
  } else {
78
79
  checkPermissions(call)
79
80
  }
@@ -94,13 +95,13 @@ private extension GeolocationPlugin {
94
95
 
95
96
  switch status {
96
97
  case .denied:
97
- self.callbackManager?.sendError(.permissionDenied)
98
+ self.onLocationPermissionNotGranted(error: .permissionDenied)
98
99
  case .notDetermined:
99
100
  self.requestLocationAuthorisation(type: .whenInUse)
100
101
  case .restricted:
101
- self.callbackManager?.sendError(.permissionRestricted)
102
+ self.onLocationPermissionNotGranted(error: .permissionRestricted)
102
103
  case .authorisedAlways, .authorisedWhenInUse:
103
- self.requestLocation()
104
+ self.onLocationPermissionGranted()
104
105
  @unknown default: break
105
106
  }
106
107
  })
@@ -120,23 +121,41 @@ private extension GeolocationPlugin {
120
121
 
121
122
  func requestLocationAuthorisation(type requestType: IONGLOCAuthorisationRequestType) {
122
123
  DispatchQueue.global(qos: .background).async {
123
- self.checkIfLocationServicesAreEnabled()
124
+ guard self.checkIfLocationServicesAreEnabled() else { return }
124
125
  self.locationService?.requestAuthorisation(withType: requestType)
125
126
  }
126
127
  }
127
128
 
128
- func checkIfLocationServicesAreEnabled() {
129
- guard locationService?.areLocationServicesEnabled() ?? false else {
130
- callbackManager?.sendError(.locationServicesDisabled)
131
- return
129
+ func checkIfLocationServicesAreEnabled(_ call: CAPPluginCall? = nil) -> Bool {
130
+ guard locationService?.areLocationServicesEnabled() == true else {
131
+ call.map { callbackManager?.sendError($0, error: .locationServicesDisabled) }
132
+ ?? callbackManager?.sendError(.locationServicesDisabled)
133
+ return false
134
+ }
135
+ return true
136
+ }
137
+
138
+ func onLocationPermissionNotGranted(error: GeolocationError) {
139
+ let shouldNotifyRequestPermissionsResult = callbackManager?.requestPermissionsCallbacks.isEmpty == false
140
+ let shouldNotifyPermissionError = callbackManager?.locationCallbacks.isEmpty == false || callbackManager?.watchCallbacks.isEmpty == false
141
+
142
+ if shouldNotifyRequestPermissionsResult {
143
+ self.callbackManager?.sendRequestPermissionsSuccess(Constants.AuthorisationStatus.Status.denied)
144
+ }
145
+ if shouldNotifyPermissionError {
146
+ self.callbackManager?.sendError(error)
132
147
  }
133
148
  }
134
149
 
135
- func requestLocation() {
136
- // should request if callbacks exist and are not empty
150
+ func onLocationPermissionGranted() {
151
+ let shouldNotifyPermissionGranted = callbackManager?.requestPermissionsCallbacks.isEmpty == false
152
+ // should request location if callbacks below exist and are not empty
137
153
  let shouldRequestCurrentPosition = callbackManager?.locationCallbacks.isEmpty == false
138
154
  let shouldRequestLocationMonitoring = callbackManager?.watchCallbacks.isEmpty == false
139
155
 
156
+ if shouldNotifyPermissionGranted {
157
+ callbackManager?.sendRequestPermissionsSuccess(Constants.AuthorisationStatus.Status.granted)
158
+ }
140
159
  if shouldRequestCurrentPosition {
141
160
  locationService?.requestSingleLocation()
142
161
  }
@@ -156,7 +175,7 @@ private extension GeolocationPlugin {
156
175
  }
157
176
 
158
177
  switch locationService?.authorisationStatus {
159
- case .authorisedAlways, .authorisedWhenInUse: requestLocation()
178
+ case .authorisedAlways, .authorisedWhenInUse: onLocationPermissionGranted()
160
179
  case .denied: callbackManager?.sendError(.permissionDenied)
161
180
  case .restricted: callbackManager?.sendError(.permissionRestricted)
162
181
  default: break
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@capacitor/geolocation",
3
- "version": "7.1.0-dev.2",
4
- "description": "Geolocation plugin for Capacitor",
3
+ "version": "7.1.1",
4
+ "description": "The Geolocation API provides simple methods for getting and tracking the current position of the device using GPS, along with altitude, heading, and speed information if available.",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
7
7
  "types": "dist/esm/index.d.ts",
@@ -86,4 +86,4 @@
86
86
  "src": "android"
87
87
  }
88
88
  }
89
- }
89
+ }