-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathInterface.swift
439 lines (381 loc) · 15.2 KB
/
Interface.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import Combine
import ComposableArchitecture
import CoreLocation
/// A wrapper around Core Location's `CLLocationManager` that exposes its functionality through
/// effects and actions, making it easy to use with the Composable Architecture and easy to test.
///
/// To use it, one begins by adding an action to your domain that represents all of the actions the
/// manager can emit via the `CLLocationManagerDelegate` methods:
///
/// ```swift
/// import ComposableCoreLocation
///
/// enum AppAction {
/// case locationManager(LocationManager.Action)
///
/// // Your domain's other actions:
/// ...
/// }
/// ```
///
/// The `LocationManager.Action` enum holds a case for each delegate method of
/// `CLLocationManagerDelegate`, such as `didUpdateLocations`, `didEnterRegion`, `didUpdateHeading`,
/// and more.
///
/// Next we add a `LocationManager`, which is a wrapper around `CLLocationManager` that the library
/// provides, to the application's environment of dependencies:
///
/// ```swift
/// struct AppEnvironment {
/// var locationManager: LocationManager
///
/// // Your domain's other dependencies:
/// ...
/// }
/// ```
///
/// Then, we simultaneously subscribe to delegate actions and request authorization from our
/// application's reducer by returning an effect from an action to kick things off. One good choice
/// for such an action is the `onAppear` of your view.
///
/// ```swift
/// let appReducer = Reducer<AppState, AppAction, AppEnvironment> {
/// state, action, environment in
///
/// switch action {
/// case .onAppear:
/// return .merge(
/// environment.locationManager
/// .delegate()
/// .map(AppAction.locationManager),
///
/// environment.locationManager
/// .requestWhenInUseAuthorization()
/// .fireAndForget()
/// )
///
/// ...
/// }
/// }
/// ```
///
/// With that initial setup we will now get all of `CLLocationManagerDelegate`'s delegate methods
/// delivered to our reducer via actions. To handle a particular delegate action we can destructure
/// it inside the `.locationManager` case we added to our `AppAction`. For example, once we get
/// location authorization from the user we could request their current location:
///
/// ```swift
/// case .locationManager(.didChangeAuthorization(.authorizedAlways)),
/// .locationManager(.didChangeAuthorization(.authorizedWhenInUse)):
///
/// return environment.locationManager
/// .requestLocation()
/// .fireAndForget()
/// ```
///
/// If the user denies location access we can show an alert telling them that we need access to be
/// able to do anything in the app:
///
/// ```swift
/// case .locationManager(.didChangeAuthorization(.denied)),
/// .locationManager(.didChangeAuthorization(.restricted)):
///
/// state.alert = """
/// Please give location access so that we can show you some cool stuff.
/// """
/// return .none
/// ```
///
/// Otherwise, we'll be notified of the user's location by handling the `.didUpdateLocations`
/// action:
///
/// ```swift
/// case let .locationManager(.didUpdateLocations(locations)):
/// // Do something cool with user's current location.
/// ...
/// ```
///
/// Once you have handled all the `CLLocationManagerDelegate` actions you care about, you can ignore
/// the rest:
///
/// ```swift
/// case .locationManager:
/// return .none
/// ```
///
/// And finally, when creating the `Store` to power your application you will supply the "live"
/// implementation of the `LocationManager`, which is an instance that holds onto a
/// `CLLocationManager` on the inside and interacts with it directly:
///
/// ```swift
/// let store = Store(
/// initialState: AppState(),
/// reducer: appReducer,
/// environment: AppEnvironment(
/// locationManager: .live,
/// // And your other dependencies...
/// )
/// )
/// ```
///
/// This is enough to implement a basic application that interacts with Core Location.
///
/// The true power of building your application and interfacing with Core Location in this way is
/// the ability to _test_ how your application interacts with Core Location. It starts by creating
/// a `TestStore` whose environment contains a ``failing`` version of the `LocationManager`. Then,
/// you can selectively override whichever endpoints your feature needs to supply deterministic
/// functionality.
///
/// For example, to test the flow of asking for location authorization, being denied, and showing an
/// alert, we need to override the `create` and `requestWhenInUseAuthorization` endpoints. The
/// `create` endpoint needs to return an effect that emits the delegate actions, which we can
/// control via a publish subject. And the `requestWhenInUseAuthorization` endpoint is a
/// fire-and-forget effect, but we can make assertions that it was called how we expect.
///
/// ```swift
/// let store = TestStore(
/// initialState: AppState(),
/// reducer: appReducer,
/// environment: AppEnvironment(
/// locationManager: .failing
/// )
/// )
///
/// var didRequestInUseAuthorization = false
/// let locationManagerSubject = PassthroughSubject<LocationManager.Action, Never>()
///
/// store.environment.locationManager.create = { locationManagerSubject.eraseToEffect() }
/// store.environment.locationManager.requestWhenInUseAuthorization = {
/// .fireAndForget { didRequestInUseAuthorization = true }
/// }
/// ```
///
/// Then we can write an assertion that simulates a sequence of user steps and location manager
/// delegate actions, and we can assert against how state mutates and how effects are received. For
/// example, we can have the user come to the screen, deny the location authorization request, and
/// then assert that an effect was received which caused the alert to show:
///
/// ```swift
/// store.send(.onAppear)
///
/// // Simulate the user denying location access
/// locationManagerSubject.send(.didChangeAuthorization(.denied))
///
/// // We receive the authorization change delegate action from the effect
/// store.receive(.locationManager(.didChangeAuthorization(.denied))) {
/// $0.alert = """
/// Please give location access so that we can show you some cool stuff.
/// """
///
/// // Store assertions require all effects to be completed, so we complete
/// // the subject manually.
/// locationManagerSubject.send(completion: .finished)
/// ```
///
/// And this is only the tip of the iceberg. We can further test what happens when we are granted
/// authorization by the user and the request for their location returns a specific location that we
/// control, and even what happens when the request for their location fails. It is very easy to
/// write these tests, and we can test deep, subtle properties of our application.
///
public struct LocationManager {
/// Actions that correspond to `CLLocationManagerDelegate` methods.
///
/// See `CLLocationManagerDelegate` for more information.
public enum Action: Equatable {
case didChangeAuthorization(CLAuthorizationStatus)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
case didDetermineState(CLRegionState, region: Region)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
case didEnterRegion(Region)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
case didExitRegion(Region)
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
case didFailRanging(beaconConstraint: CLBeaconIdentityConstraint, error: Error)
case didFailWithError(Error)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
case didFinishDeferredUpdatesWithError(Error?)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
case didPauseLocationUpdates
@available(tvOS, unavailable)
@available(watchOS, unavailable)
case didResumeLocationUpdates
@available(tvOS, unavailable)
@available(watchOS, unavailable)
case didStartMonitoring(region: Region)
@available(macOS, unavailable)
@available(tvOS, unavailable)
case didUpdateHeading(newHeading: Heading)
case didUpdateLocations([Location])
@available(macCatalyst, deprecated: 13)
@available(tvOS, unavailable)
case didUpdateTo(newLocation: Location, oldLocation: Location)
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
case didVisit(Visit)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
case monitoringDidFail(region: Region?, error: Error)
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
case didRangeBeacons([Beacon], satisfyingConstraint: CLBeaconIdentityConstraint)
}
public struct Error: Swift.Error, Equatable {
public let error: NSError
public init(_ error: Swift.Error) {
self.error = error as NSError
}
}
public var accuracyAuthorization: () -> AccuracyAuthorization?
public var authorizationStatus: () -> CLAuthorizationStatus
public var delegate: () -> EffectPublisher<Action, Never>
public var dismissHeadingCalibrationDisplay: () -> EffectPublisher<Never, Never>
public var heading: () -> Heading?
public var headingAvailable: () -> Bool
public var isRangingAvailable: () -> Bool
public var location: () -> Location?
public var locationServicesEnabled: () -> Bool
public var maximumRegionMonitoringDistance: () -> CLLocationDistance
public var monitoredRegions: () -> Set<Region>
public var requestAlwaysAuthorization: () -> EffectPublisher<Never, Never>
public var requestLocation: () -> EffectPublisher<Never, Never>
public var requestWhenInUseAuthorization: () -> EffectPublisher<Never, Never>
public var requestTemporaryFullAccuracyAuthorization: (String) -> EffectPublisher<Never, Error>
public var set: (Properties) -> EffectPublisher<Never, Never>
public var significantLocationChangeMonitoringAvailable: () -> Bool
public var startMonitoringForRegion: (Region) -> EffectPublisher<Never, Never>
public var startMonitoringSignificantLocationChanges: () -> EffectPublisher<Never, Never>
public var startMonitoringVisits: () -> EffectPublisher<Never, Never>
public var startUpdatingHeading: () -> EffectPublisher<Never, Never>
public var startUpdatingLocation: () -> EffectPublisher<Never, Never>
public var stopMonitoringForRegion: (Region) -> EffectPublisher<Never, Never>
public var stopMonitoringSignificantLocationChanges: () -> EffectPublisher<Never, Never>
public var stopMonitoringVisits: () -> EffectPublisher<Never, Never>
public var stopUpdatingHeading: () -> EffectPublisher<Never, Never>
public var stopUpdatingLocation: () -> EffectPublisher<Never, Never>
/// Updates the given properties of a uniquely identified `CLLocationManager`.
public func set(
activityType: CLActivityType? = nil,
allowsBackgroundLocationUpdates: Bool? = nil,
desiredAccuracy: CLLocationAccuracy? = nil,
distanceFilter: CLLocationDistance? = nil,
headingFilter: CLLocationDegrees? = nil,
headingOrientation: CLDeviceOrientation? = nil,
pausesLocationUpdatesAutomatically: Bool? = nil,
showsBackgroundLocationIndicator: Bool? = nil
) -> EffectPublisher<Never, Never> {
#if os(macOS) || os(tvOS) || os(watchOS)
return .none
#else
return self.set(
Properties(
activityType: activityType,
allowsBackgroundLocationUpdates: allowsBackgroundLocationUpdates,
desiredAccuracy: desiredAccuracy,
distanceFilter: distanceFilter,
headingFilter: headingFilter,
headingOrientation: headingOrientation,
pausesLocationUpdatesAutomatically: pausesLocationUpdatesAutomatically,
showsBackgroundLocationIndicator: showsBackgroundLocationIndicator
)
)
#endif
}
}
extension LocationManager {
public struct Properties: Equatable {
var activityType: CLActivityType? = nil
var allowsBackgroundLocationUpdates: Bool? = nil
var desiredAccuracy: CLLocationAccuracy? = nil
var distanceFilter: CLLocationDistance? = nil
var headingFilter: CLLocationDegrees? = nil
var headingOrientation: CLDeviceOrientation? = nil
var pausesLocationUpdatesAutomatically: Bool? = nil
var showsBackgroundLocationIndicator: Bool? = nil
public static func == (lhs: Self, rhs: Self) -> Bool {
var isEqual = true
#if os(iOS) || targetEnvironment(macCatalyst) || os(watchOS)
isEqual =
isEqual
&& lhs.activityType == rhs.activityType
&& lhs.allowsBackgroundLocationUpdates == rhs.allowsBackgroundLocationUpdates
#endif
isEqual =
isEqual
&& lhs.desiredAccuracy == rhs.desiredAccuracy
&& lhs.distanceFilter == rhs.distanceFilter
#if os(iOS) || targetEnvironment(macCatalyst) || os(watchOS)
isEqual =
isEqual
&& lhs.headingFilter == rhs.headingFilter
&& lhs.headingOrientation == rhs.headingOrientation
#endif
#if os(iOS) || targetEnvironment(macCatalyst)
isEqual =
isEqual
&& lhs.pausesLocationUpdatesAutomatically == rhs.pausesLocationUpdatesAutomatically
&& lhs.showsBackgroundLocationIndicator == rhs.showsBackgroundLocationIndicator
#endif
return isEqual
}
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
public init(
activityType: CLActivityType? = nil,
allowsBackgroundLocationUpdates: Bool? = nil,
desiredAccuracy: CLLocationAccuracy? = nil,
distanceFilter: CLLocationDistance? = nil,
headingFilter: CLLocationDegrees? = nil,
headingOrientation: CLDeviceOrientation? = nil,
pausesLocationUpdatesAutomatically: Bool? = nil,
showsBackgroundLocationIndicator: Bool? = nil
) {
self.activityType = activityType
self.allowsBackgroundLocationUpdates = allowsBackgroundLocationUpdates
self.desiredAccuracy = desiredAccuracy
self.distanceFilter = distanceFilter
self.headingFilter = headingFilter
self.headingOrientation = headingOrientation
self.pausesLocationUpdatesAutomatically = pausesLocationUpdatesAutomatically
self.showsBackgroundLocationIndicator = showsBackgroundLocationIndicator
}
@available(iOS, unavailable)
@available(macCatalyst, unavailable)
@available(watchOS, unavailable)
public init(
desiredAccuracy: CLLocationAccuracy? = nil,
distanceFilter: CLLocationDistance? = nil
) {
self.desiredAccuracy = desiredAccuracy
self.distanceFilter = distanceFilter
}
@available(iOS, unavailable)
@available(macCatalyst, unavailable)
@available(macOS, unavailable)
@available(tvOS, unavailable)
public init(
activityType: CLActivityType? = nil,
allowsBackgroundLocationUpdates: Bool? = nil,
desiredAccuracy: CLLocationAccuracy? = nil,
distanceFilter: CLLocationDistance? = nil,
headingFilter: CLLocationDegrees? = nil,
headingOrientation: CLDeviceOrientation? = nil
) {
self.activityType = activityType
self.allowsBackgroundLocationUpdates = allowsBackgroundLocationUpdates
self.desiredAccuracy = desiredAccuracy
self.distanceFilter = distanceFilter
self.headingFilter = headingFilter
self.headingOrientation = headingOrientation
}
}
}