|
| 1 | +import CacheStore |
| 2 | +import CoreLocation |
| 3 | + |
| 4 | +/// `LocationStore` Keys |
| 5 | +/// - location: CLLocation |
| 6 | +public enum LocationStoreKey { |
| 7 | + case location // CLLocation |
| 8 | +} |
| 9 | + |
| 10 | +/// `LocationStore` Actions |
| 11 | +public enum LocationStoreAction { |
| 12 | + case updateLocation |
| 13 | + case locationUpdated(CLLocation?) |
| 14 | +} |
| 15 | + |
| 16 | +/// `LocationStore` Dependency |
| 17 | +public struct LocationStoreDependency { |
| 18 | + var updateLocation: () async -> CLLocation? |
| 19 | +} |
| 20 | + |
| 21 | +public extension LocationStoreDependency { |
| 22 | + /// Mock Dependency |
| 23 | + static func mock(location: CLLocation) -> LocationStoreDependency { |
| 24 | + LocationStoreDependency( |
| 25 | + updateLocation: { location } |
| 26 | + ) |
| 27 | + } |
| 28 | + |
| 29 | + /// Live Dependency using CLLocationManager and requestWhenInUseAuthorization if authorizationStatus == .notDetermined |
| 30 | + static var live: LocationStoreDependency { |
| 31 | + LocationStoreDependency( |
| 32 | + updateLocation: { |
| 33 | + let locationManager = CLLocationManager() |
| 34 | + |
| 35 | + if locationManager.authorizationStatus == .notDetermined { |
| 36 | + locationManager.requestWhenInUseAuthorization() |
| 37 | + } |
| 38 | + |
| 39 | + return locationManager.location |
| 40 | + } |
| 41 | + ) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// Higher-order StoreActionHandler |
| 46 | +public func locationStoreActionHandler( |
| 47 | + withActionHandler actionHandler: StoreActionHandler<LocationStoreKey, LocationStoreAction, LocationStoreDependency>? = nil |
| 48 | +) -> StoreActionHandler<LocationStoreKey, LocationStoreAction, LocationStoreDependency> { |
| 49 | + StoreActionHandler<LocationStoreKey, LocationStoreAction, LocationStoreDependency> { cacheStore, action, dependency in |
| 50 | + switch action { |
| 51 | + case .updateLocation: |
| 52 | + struct UpdateLocationActionEffectID: Hashable { } |
| 53 | + |
| 54 | + return ActionEffect(id: UpdateLocationActionEffectID()) { |
| 55 | + .locationUpdated(await dependency.updateLocation()) |
| 56 | + } |
| 57 | + |
| 58 | + case let .locationUpdated(location): |
| 59 | + cacheStore.set(value: location, forKey: .location) |
| 60 | + } |
| 61 | + |
| 62 | + guard |
| 63 | + let actionHandler = actionHandler, |
| 64 | + let nextAction = actionHandler.handle(store: &cacheStore, action: action, dependency: dependency) |
| 65 | + else { |
| 66 | + struct RepeatUpdateLocationActionEffectID: Hashable { } |
| 67 | + |
| 68 | + return ActionEffect(id: RepeatUpdateLocationActionEffectID()) { |
| 69 | + sleep(1) |
| 70 | + return .updateLocation |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return nextAction |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/// LocationStore that updates the user's location |
| 79 | +public class LocationStore: Store<LocationStoreKey, LocationStoreAction, LocationStoreDependency> { |
| 80 | + /// Create a LocationStore with an ActionHandler and Dependency |
| 81 | + public init( |
| 82 | + withActionHandler actionHandler: StoreActionHandler<LocationStoreKey, LocationStoreAction, LocationStoreDependency>? = nil, |
| 83 | + dependency: LocationStoreDependency = .live |
| 84 | + ) { |
| 85 | + super.init( |
| 86 | + initialValues: [:], |
| 87 | + actionHandler: locationStoreActionHandler(withActionHandler: actionHandler), |
| 88 | + dependency: dependency |
| 89 | + ) |
| 90 | + |
| 91 | + handle(action: .updateLocation) |
| 92 | + } |
| 93 | + |
| 94 | + /// Create a LocationStore using the Store init |
| 95 | + public required init( |
| 96 | + initialValues: [LocationStoreKey: Any], |
| 97 | + actionHandler: StoreActionHandler<LocationStoreKey, LocationStoreAction, LocationStoreDependency>, |
| 98 | + dependency: LocationStoreDependency |
| 99 | + ) { |
| 100 | + super.init( |
| 101 | + initialValues: initialValues, |
| 102 | + actionHandler: locationStoreActionHandler(withActionHandler: actionHandler), |
| 103 | + dependency: dependency |
| 104 | + ) |
| 105 | + |
| 106 | + handle(action: .updateLocation) |
| 107 | + } |
| 108 | +} |
0 commit comments