-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
IdentifiedListTestCase.swift
86 lines (82 loc) · 2.38 KB
/
IdentifiedListTestCase.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
@_spi(Logging) import ComposableArchitecture
import SwiftUI
struct IdentifiedListView: View {
@State var store = Store(initialState: Feature.State()) {
Feature()
}
struct ViewState: Equatable {
var firstCount: Int?
init(state: Feature.State) {
self.firstCount = state.rows.first?.count
}
}
var body: some View {
WithViewStore(self.store, observe: ViewState.init) { viewStore in
List {
Section {
if let firstCount = viewStore.firstCount {
HStack {
Button("Increment First") {
self.store.send(.incrementFirstButtonTapped)
}
Spacer()
Text("Count: \(firstCount)")
}
}
}
ForEachStore(self.store.scope(state: \.rows, action: { .row(id: $0, action: $1) })) { store in
WithViewStore(store.scope(state: \.id, action: { $0 }), observe: { $0 }) { viewStore in
Section {
HStack {
VStack {
BasicsView(store: store)
}
Spacer()
Button(action: { self.store.send(.removeButtonTapped(id: viewStore.state)) }) {
Image(systemName: "trash")
}
}
}
.buttonStyle(.borderless)
}
}
}
.toolbar {
ToolbarItem {
Button("Add") { self.store.send(.addButtonTapped) }
}
}
}
}
struct Feature: Reducer {
struct State: Equatable {
var rows: IdentifiedArrayOf<BasicsView.Feature.State> = []
}
enum Action {
case addButtonTapped
case incrementFirstButtonTapped
case removeButtonTapped(id: BasicsView.Feature.State.ID)
case row(id: BasicsView.Feature.State.ID, action: BasicsView.Feature.Action)
}
var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .addButtonTapped:
state.rows.append(BasicsView.Feature.State())
return .none
case .incrementFirstButtonTapped:
state.rows[id: state.rows.ids[0]]?.count += 1
return .none
case let .removeButtonTapped(id: id):
state.rows.remove(id: id)
return .none
case .row:
return .none
}
}
.forEach(\.rows, action: /Action.row) {
BasicsView.Feature()
}
}
}
}