Skip to content

Commit 12d61bd

Browse files
committed
Update Tests and README
1 parent f7ad71e commit 12d61bd

4 files changed

Lines changed: 87 additions & 52 deletions

File tree

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,60 @@
11
# LocationCacheStore
22

3-
## 🚧
3+
*CoreLocation State Store*
4+
5+
## What is [`CacheStore`](https://github.com/0xOpenBytes/CacheStore)?
6+
7+
`CacheStore` is a SwiftUI state management framework that uses a dictionary as the state. Scoping creates a single source of truth for the parent state. `CacheStore` uses [`c`](https://github.com/0xOpenBytes/c), which a simple composition framework. [`c`](https://github.com/0xOpenBytes/c) has the ability to create transformations that are either unidirectional or bidirectional.
8+
9+
## LocationStore
10+
```swift
11+
public typealias LocationStore = Store<LocationStoreKey, LocationStoreAction, LocationStoreDependency>
12+
```
13+
14+
## Keys
15+
```swift
16+
/// `LocationStore` Keys
17+
/// - location: CLLocation
18+
public enum LocationStoreKey {
19+
case location // CLLocation
20+
}
21+
```
22+
23+
## Actions
24+
```swift
25+
/// `LocationStore` Actions
26+
public enum LocationStoreAction {
27+
case updateLocation
28+
case locationUpdated(CLLocation?)
29+
}
30+
```
31+
32+
## Dependency
33+
```swift
34+
/// `LocationStore` Dependency
35+
public struct LocationStoreDependency {
36+
public var shouldContinuouslyUpdate: Bool
37+
public var updateLocation: () async -> CLLocation?
38+
39+
public init(
40+
shouldContinuouslyUpdate: Bool = true,
41+
updateLocation: @escaping () async -> CLLocation?
42+
) {
43+
self.shouldContinuouslyUpdate = shouldContinuouslyUpdate
44+
self.updateLocation = updateLocation
45+
}
46+
}
47+
```
48+
49+
## StoreActionHandler
50+
```swift
51+
/// Higher-order StoreActionHandler
52+
public func locationStoreActionHandler(
53+
withActionHandler actionHandler: StoreActionHandler<LocationStoreKey, LocationStoreAction, LocationStoreDependency>? = nil
54+
) -> StoreActionHandler<LocationStoreKey, LocationStoreAction, LocationStoreDependency>
55+
```
56+
57+
## DefaultLocationStore
58+
```swift
59+
public class DefaultLocationStore: LocationStore
60+
```

Sources/LocationCacheStore/LocationStore.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ public enum LocationStoreAction {
1515

1616
/// `LocationStore` Dependency
1717
public struct LocationStoreDependency {
18+
public var shouldContinuouslyUpdate: Bool
1819
public var updateLocation: () async -> CLLocation?
1920

2021
public init(
22+
shouldContinuouslyUpdate: Bool = true,
2123
updateLocation: @escaping () async -> CLLocation?
2224
) {
25+
self.shouldContinuouslyUpdate = shouldContinuouslyUpdate
2326
self.updateLocation = updateLocation
2427
}
2528
}
@@ -71,18 +74,24 @@ public func locationStoreActionHandler(
7174
else {
7275
struct RepeatUpdateLocationActionEffectID: Hashable { }
7376

74-
return ActionEffect(id: RepeatUpdateLocationActionEffectID()) {
75-
sleep(1)
76-
return .updateLocation
77+
if dependency.shouldContinuouslyUpdate {
78+
return ActionEffect(id: RepeatUpdateLocationActionEffectID()) {
79+
sleep(1)
80+
return .updateLocation
81+
}
7782
}
83+
84+
return .none
7885
}
7986

8087
return nextAction
8188
}
8289
}
8390

91+
public typealias LocationStore = Store<LocationStoreKey, LocationStoreAction, LocationStoreDependency>
92+
8493
/// LocationStore that updates the user's location
85-
public class LocationStore: Store<LocationStoreKey, LocationStoreAction, LocationStoreDependency> {
94+
public class DefaultLocationStore: LocationStore {
8695
/// Create a LocationStore with an ActionHandler and Dependency
8796
public init(
8897
withActionHandler actionHandler: StoreActionHandler<LocationStoreKey, LocationStoreAction, LocationStoreDependency>? = nil,

Tests/LocationCacheStoreTests/LocationCacheStoreTests.swift

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,27 @@ final class LocationCacheStoreTests: XCTestCase {
88
func testExample() throws {
99
let initLocation = CLLocation()
1010

11-
let locationStore = LocationStore(
12-
initialValues: [:],
13-
actionHandler: locationStoreActionHandler(),
14-
dependency: .mock(location: initLocation)
15-
)
16-
17-
enum ExampleKey {
18-
case location
19-
}
20-
21-
struct ExampleStoreContent: StoreContent {
22-
var store: Store<ExampleKey, Void, Void>
23-
24-
var location: CLLocation {
25-
store.resolve(.location)
26-
}
27-
}
28-
29-
var exampleStore: TestStore<ExampleKey, Void, Void> {
30-
TestStore(
31-
store: locationStore.actionlessScope(
32-
keyTransformation: (
33-
from: { parent in
34-
switch parent {
35-
case .location: return .location
36-
default: return nil
37-
}
38-
},
39-
to: { child in
40-
switch child {
41-
case .location: return .location
42-
default: return nil
43-
}
44-
}
45-
),
46-
dependencyTransformation: { _ in () }
11+
let store = TestStore(
12+
store: LocationStore(
13+
initialValues: [:],
14+
actionHandler: locationStoreActionHandler(),
15+
dependency: LocationStoreDependency(
16+
shouldContinuouslyUpdate: false,
17+
updateLocation: { initLocation }
4718
)
4819
)
49-
}
20+
)
21+
22+
store.send(.updateLocation, expecting: { _ in })
5023

51-
// MARK: - Test
52-
exampleStore.content { (content: ExampleStoreContent) in
53-
try t.assert(content.location.coordinate.latitude, isEqualTo: initLocation.coordinate.latitude)
54-
try t.assert(content.location.coordinate.latitude, isEqualTo: initLocation.coordinate.latitude)
24+
store.receive(.locationUpdated(initLocation)) { cacheStore in
25+
cacheStore.set(value: initLocation, forKey: .location)
5526
}
5627

5728
let newLocation = CLLocation(latitude: 1, longitude: 1)
58-
locationStore.handle(action: .locationUpdated(newLocation))
5929

60-
exampleStore.content { (content: ExampleStoreContent) in
61-
try t.assert(content.location.coordinate.latitude, isEqualTo: newLocation.coordinate.latitude)
62-
try t.assert(content.location.coordinate.latitude, isEqualTo: newLocation.coordinate.latitude)
30+
store.send(.locationUpdated(newLocation)) { cacheStore in
31+
cacheStore.set(value: newLocation, forKey: .location)
6332
}
6433
}
6534
}

0 commit comments

Comments
 (0)