Skip to content

Commit 905bf87

Browse files
committed
Feed and Logger
1 parent 9653681 commit 905bf87

File tree

3 files changed

+83
-30
lines changed

3 files changed

+83
-30
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Foundation
2+
import SwiftRex
3+
4+
let activityFeedReducer = Reducer<AppAction, AppState> { action, state in
5+
var state = state
6+
switch action {
7+
case .counterView(.counter),
8+
.favoritePrimes(.loadedFavoritePrimes),
9+
.favoritePrimes(.loadButtonTapped),
10+
.favoritePrimes(.saveButtonTapped):
11+
break
12+
case .counterView(.primeModal(.removeFavoritePrimeTapped)):
13+
state.activityFeed.append(.init(timestamp: Date(), type: .removedFavoritePrime(state.count)))
14+
15+
case .counterView(.primeModal(.saveFavoritePrimeTapped)):
16+
state.activityFeed.append(.init(timestamp: Date(), type: .addedFavoritePrime(state.count)))
17+
18+
case let .favoritePrimes(.deleteFavoritePrimes(indexSet)):
19+
for index in indexSet {
20+
state.activityFeed.append(.init(timestamp: Date(), type: .removedFavoritePrime(state.favoritePrimes[index])))
21+
}
22+
}
23+
return state
24+
}

SampleApp/PointFreePrimeTime/PrimeTime/ContentView.swift

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ extension AppState {
6161
import CasePaths
6262

6363
let appReducer: Reducer<AppAction, AppState> =
64-
counterViewReducer.lift(
64+
activityFeedReducer
65+
<> counterViewReducer.lift(
6566
actionGetter: /AppAction.counterView,
6667
stateGetter: { $0.counterView },
6768
stateSetter: setter(\AppState.counterView)
@@ -72,45 +73,24 @@ let appReducer: Reducer<AppAction, AppState> =
7273
)
7374

7475
let appMiddleware: ComposedMiddleware<AppAction, AppAction, AppState> =
75-
CounterMiddleware().lift(
76+
LoggerMiddleware().lift(
77+
inputActionMap: { $0 },
78+
outputActionMap: absurd,
79+
stateMap: { $0 }
80+
)
81+
82+
<> CounterMiddleware().lift(
7683
inputActionMap: (/AppAction.counterView .. /CounterViewAction.counter).extract,
7784
outputActionMap: (/AppAction.counterView .. /CounterViewAction.counter).embed,
7885
stateMap: { $0.count }
7986
)
87+
8088
<> FavoritePrimesMiddleware().lift(
8189
inputActionMap: (/AppAction.favoritePrimes).extract,
8290
outputActionMap: (/AppAction.favoritePrimes).embed,
8391
stateMap: { $0.favoritePrimes }
8492
)
8593

86-
87-
//func activityFeed(
88-
// _ reducer: @escaping Reducer<AppState, AppAction>
89-
//) -> Reducer<AppState, AppAction> {
90-
//
91-
// return { state, action in
92-
// switch action {
93-
// case .counterView(.counter),
94-
// .favoritePrimes(.loadedFavoritePrimes),
95-
// .favoritePrimes(.loadButtonTapped),
96-
// .favoritePrimes(.saveButtonTapped):
97-
// break
98-
// case .counterView(.primeModal(.removeFavoritePrimeTapped)):
99-
// state.activityFeed.append(.init(timestamp: Date(), type: .removedFavoritePrime(state.count)))
100-
//
101-
// case .counterView(.primeModal(.saveFavoritePrimeTapped)):
102-
// state.activityFeed.append(.init(timestamp: Date(), type: .addedFavoritePrime(state.count)))
103-
//
104-
// case let .favoritePrimes(.deleteFavoritePrimes(indexSet)):
105-
// for index in indexSet {
106-
// state.activityFeed.append(.init(timestamp: Date(), type: .removedFavoritePrime(state.favoritePrimes[index])))
107-
// }
108-
// }
109-
//
110-
// return reducer(&state, action)
111-
// }
112-
//}
113-
11494
struct ContentView: View {
11595
@ObservedObject var store: ObservableViewModel<AppAction, AppState>
11696

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import Combine
2+
import Foundation
3+
import os.log
4+
import SwiftRex
5+
6+
final class LoggerMiddleware: Middleware {
7+
typealias InputActionType = AppAction
8+
typealias OutputActionType = Never
9+
typealias StateType = AppState
10+
11+
private var getState: GetState<StateType>!
12+
private var cancellables = Set<AnyCancellable>()
13+
private let actionPrint: (AppAction) -> String?
14+
private let statePrint: (AppState) -> String?
15+
16+
init(actionPrint: @escaping (AppAction) -> String? = { "\($0)" }, statePrint: @escaping (AppState) -> String? = { "\($0)" }) {
17+
self.actionPrint = actionPrint
18+
self.statePrint = statePrint
19+
}
20+
21+
func receiveContext(getState: @escaping GetState<StateType>, output: AnyActionHandler<OutputActionType>) {
22+
self.getState = getState
23+
}
24+
25+
func handle(action: InputActionType, from dispatcher: ActionSource, afterReducer: inout AfterReducer) {
26+
let stateBefore = getState()
27+
afterReducer = .do {
28+
let stateAfter = self.getState()
29+
30+
var enabled = false
31+
if let actionString = self.actionPrint(action) {
32+
enabled = true
33+
let message = "🕹 \(actionString) from \(dispatcher)"
34+
os_log(.debug, log: .default, "%{PUBLIC}@", message)
35+
}
36+
37+
if stateBefore != stateAfter, let stateString = self.statePrint(stateAfter) {
38+
enabled = true
39+
os_log(.debug, log: .default, "🏛 %{PUBLIC}@", stateString)
40+
} else {
41+
os_log(.debug, log: .default, "🏛 No state mutation")
42+
}
43+
44+
if enabled {
45+
os_log(.debug, log: .default, "")
46+
}
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)