From 81c6a1ea8e0f434934cbc97dc519b62db4f52562 Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki Date: Mon, 12 Apr 2021 18:26:27 +0200 Subject: [PATCH 1/7] Add onAppear actions --- Color/Sources/ColorAction.swift | 1 + Color/Sources/ColorReducer.swift | 3 ++ Color/Sources/ColorView.swift | 3 ++ Preview/Sources/PreviewAction.swift | 1 + Preview/Sources/PreviewReducer.swift | 3 ++ Preview/Sources/PreviewView.swift | 59 +++++++++++++++------------- Shape/Sources/ShapeAction.swift | 1 + Shape/Sources/ShapeReducer.swift | 3 ++ Shape/Sources/ShapeView.swift | 3 ++ 9 files changed, 50 insertions(+), 27 deletions(-) diff --git a/Color/Sources/ColorAction.swift b/Color/Sources/ColorAction.swift index f18238a..ac522fd 100644 --- a/Color/Sources/ColorAction.swift +++ b/Color/Sources/ColorAction.swift @@ -3,4 +3,5 @@ public enum ColorAction: Equatable { case didUpdateGreen(Double) case didUpdateBlue(Double) case apply + case onAppear } diff --git a/Color/Sources/ColorReducer.swift b/Color/Sources/ColorReducer.swift index 1288923..d0a8d89 100644 --- a/Color/Sources/ColorReducer.swift +++ b/Color/Sources/ColorReducer.swift @@ -16,5 +16,8 @@ public let colorReducer = Reducer { state, action case .apply: return .none + + case .onAppear: + return .none } } diff --git a/Color/Sources/ColorView.swift b/Color/Sources/ColorView.swift index ee06885..b8ea0a9 100644 --- a/Color/Sources/ColorView.swift +++ b/Color/Sources/ColorView.swift @@ -63,6 +63,9 @@ public struct ColorView: View { .padding() } } + .onAppear { + viewStore.send(.onAppear) + } } } } diff --git a/Preview/Sources/PreviewAction.swift b/Preview/Sources/PreviewAction.swift index c34a55d..856f39f 100644 --- a/Preview/Sources/PreviewAction.swift +++ b/Preview/Sources/PreviewAction.swift @@ -1,3 +1,4 @@ public enum PreviewAction: Equatable { case reset + case onAppear } diff --git a/Preview/Sources/PreviewReducer.swift b/Preview/Sources/PreviewReducer.swift index bc7b0cc..163fd54 100644 --- a/Preview/Sources/PreviewReducer.swift +++ b/Preview/Sources/PreviewReducer.swift @@ -6,5 +6,8 @@ public let previewReducer = Reducer { state, state.color = nil state.shape = nil return .none + + case .onAppear: + return .none } } diff --git a/Preview/Sources/PreviewView.swift b/Preview/Sources/PreviewView.swift index 88983a5..980db38 100644 --- a/Preview/Sources/PreviewView.swift +++ b/Preview/Sources/PreviewView.swift @@ -12,38 +12,43 @@ public struct PreviewView: View { public var body: some View { WithViewStore(store) { viewStore in - if let color = viewStore.color, - let shape = viewStore.shape { - VStack { - Group { - switch shape { - case .circle: - Circle() + Group { + if let color = viewStore.color, + let shape = viewStore.shape { + VStack { + Group { + switch shape { + case .circle: + Circle() - case .square: - Rectangle() + case .square: + Rectangle() + } } - } - .aspectRatio(1, contentMode: .fit) - .padding() - .foregroundColor(Color( - .displayP3, - red: color.red, - green: color.green, - blue: color.blue, - opacity: 1 - )) + .aspectRatio(1, contentMode: .fit) + .padding() + .foregroundColor(Color( + .displayP3, + red: color.red, + green: color.green, + blue: color.blue, + opacity: 1 + )) - Button(action: { viewStore.send(.reset) }) { - Text("Reset") - .padding() + Button(action: { viewStore.send(.reset) }) { + Text("Reset") + .padding() + } + } + } else { + VStack { + Text("No preview").font(.title) + Text("Apply color and shape first") } } - } else { - VStack { - Text("No preview").font(.title) - Text("Apply color and shape first") - } + } + .onAppear { + viewStore.send(.onAppear) } } } diff --git a/Shape/Sources/ShapeAction.swift b/Shape/Sources/ShapeAction.swift index 646882e..07cf8b1 100644 --- a/Shape/Sources/ShapeAction.swift +++ b/Shape/Sources/ShapeAction.swift @@ -1,4 +1,5 @@ public enum ShapeAction: Equatable { case didSelectType(ShapeType) case apply + case onAppear } diff --git a/Shape/Sources/ShapeReducer.swift b/Shape/Sources/ShapeReducer.swift index fe9f2e2..b63466a 100644 --- a/Shape/Sources/ShapeReducer.swift +++ b/Shape/Sources/ShapeReducer.swift @@ -8,5 +8,8 @@ public let shapeReducer = Reducer { state, action case .apply: return .none + + case .onAppear: + return .none } } diff --git a/Shape/Sources/ShapeView.swift b/Shape/Sources/ShapeView.swift index a717f0a..7f504d8 100644 --- a/Shape/Sources/ShapeView.swift +++ b/Shape/Sources/ShapeView.swift @@ -55,6 +55,9 @@ public struct ShapeView: View { } } .padding() + .onAppear { + viewStore.send(.onAppear) + } } } } From c0d577a83d236e9d641316635768b6c5b99c9ada Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki Date: Mon, 12 Apr 2021 18:27:51 +0200 Subject: [PATCH 2/7] Turn on debug for main reducer --- App/Sources/App.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/App/Sources/App.swift b/App/Sources/App.swift index b7d3203..b96dd4d 100644 --- a/App/Sources/App.swift +++ b/App/Sources/App.swift @@ -15,7 +15,7 @@ struct App: SwiftUI.App { shape: ShapeState(type: .square), preview: PreviewState() ), - reducer: tabsReducer, + reducer: tabsReducer.debug(), environment: () ) From b711ce770ed38cdbe4085406ca0aea80751607b0 Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki Date: Mon, 12 Apr 2021 18:32:05 +0200 Subject: [PATCH 3/7] Updated ComposableArchitecture dependency --- Common/Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Common/Package.swift b/Common/Package.swift index 521dd8f..10da601 100644 --- a/Common/Package.swift +++ b/Common/Package.swift @@ -15,7 +15,7 @@ let package = Package( dependencies: [ .package( url: "https://github.com/pointfreeco/swift-composable-architecture.git", - from: "0.13.0" + from: "0.17.0" ) ], targets: [ From 2c81ca88ebedb970d6fc0b01ffb0ebfbd081d3cd Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki Date: Mon, 12 Apr 2021 18:39:52 +0200 Subject: [PATCH 4/7] Add TabsViewState This should prevent unneeded updates of the TabsView --- Tabs/Sources/TabsView.swift | 2 +- Tabs/Sources/TabsViewState.swift | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 Tabs/Sources/TabsViewState.swift diff --git a/Tabs/Sources/TabsView.swift b/Tabs/Sources/TabsView.swift index 1c36ed9..5afad14 100644 --- a/Tabs/Sources/TabsView.swift +++ b/Tabs/Sources/TabsView.swift @@ -12,7 +12,7 @@ public struct TabsView: View { let store: Store public var body: some View { - WithViewStore(store) { viewStore in + WithViewStore(store.scope(state: TabsViewState.init(state:))) { viewStore in TabView(selection: viewStore.binding( get: \.selectedTab, send: TabsAction.didSelectTab diff --git a/Tabs/Sources/TabsViewState.swift b/Tabs/Sources/TabsViewState.swift new file mode 100644 index 0000000..596f40c --- /dev/null +++ b/Tabs/Sources/TabsViewState.swift @@ -0,0 +1,7 @@ +struct TabsViewState: Equatable { + init(state: TabsState) { + selectedTab = state.selectedTab + } + + var selectedTab: Tab +} From e93419ae01d584f72fe9aa7288172155aa40a158 Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki Date: Mon, 12 Apr 2021 18:53:48 +0200 Subject: [PATCH 5/7] Move `.onAppear` modifiers outside `WithViewStore` --- Color/Sources/ColorView.swift | 6 +++--- Preview/Sources/PreviewView.swift | 6 +++--- Shape/Sources/ShapeView.swift | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Color/Sources/ColorView.swift b/Color/Sources/ColorView.swift index b8ea0a9..52cf060 100644 --- a/Color/Sources/ColorView.swift +++ b/Color/Sources/ColorView.swift @@ -63,9 +63,9 @@ public struct ColorView: View { .padding() } } - .onAppear { - viewStore.send(.onAppear) - } + } + .onAppear { + ViewStore(store.stateless).send(.onAppear) } } } diff --git a/Preview/Sources/PreviewView.swift b/Preview/Sources/PreviewView.swift index 980db38..ca551e3 100644 --- a/Preview/Sources/PreviewView.swift +++ b/Preview/Sources/PreviewView.swift @@ -47,9 +47,9 @@ public struct PreviewView: View { } } } - .onAppear { - viewStore.send(.onAppear) - } + } + .onAppear { + ViewStore(store.stateless).send(.onAppear) } } } diff --git a/Shape/Sources/ShapeView.swift b/Shape/Sources/ShapeView.swift index 7f504d8..3aeebea 100644 --- a/Shape/Sources/ShapeView.swift +++ b/Shape/Sources/ShapeView.swift @@ -55,9 +55,9 @@ public struct ShapeView: View { } } .padding() - .onAppear { - viewStore.send(.onAppear) - } + } + .onAppear { + ViewStore(store.stateless).send(.onAppear) } } } From 143dab890e2a30c370326e6cef12f48362264386 Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki Date: Mon, 12 Apr 2021 18:54:24 +0200 Subject: [PATCH 6/7] Add explicit Equatable conformance to Tab --- Tabs/Sources/Tab.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tabs/Sources/Tab.swift b/Tabs/Sources/Tab.swift index 966ae92..c27ced9 100644 --- a/Tabs/Sources/Tab.swift +++ b/Tabs/Sources/Tab.swift @@ -1,4 +1,4 @@ -public enum Tab: Int, CaseIterable { +public enum Tab: Int, CaseIterable, Equatable { case color case shape case preview From 48e36d1a7c18ca824a094a25e244684411e8ba59 Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki Date: Mon, 12 Apr 2021 18:58:34 +0200 Subject: [PATCH 7/7] Send `onAppear` actions from `TabsView` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Workaround, it works but I'm not sure why ¯\_(ツ)_/¯ --- Color/Sources/ColorView.swift | 3 --- Preview/Sources/PreviewView.swift | 3 --- Shape/Sources/ShapeView.swift | 3 --- Tabs/Sources/TabsView.swift | 9 +++++++++ 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Color/Sources/ColorView.swift b/Color/Sources/ColorView.swift index 52cf060..ee06885 100644 --- a/Color/Sources/ColorView.swift +++ b/Color/Sources/ColorView.swift @@ -64,9 +64,6 @@ public struct ColorView: View { } } } - .onAppear { - ViewStore(store.stateless).send(.onAppear) - } } } diff --git a/Preview/Sources/PreviewView.swift b/Preview/Sources/PreviewView.swift index ca551e3..8cc417c 100644 --- a/Preview/Sources/PreviewView.swift +++ b/Preview/Sources/PreviewView.swift @@ -48,9 +48,6 @@ public struct PreviewView: View { } } } - .onAppear { - ViewStore(store.stateless).send(.onAppear) - } } } diff --git a/Shape/Sources/ShapeView.swift b/Shape/Sources/ShapeView.swift index 3aeebea..a717f0a 100644 --- a/Shape/Sources/ShapeView.swift +++ b/Shape/Sources/ShapeView.swift @@ -56,9 +56,6 @@ public struct ShapeView: View { } .padding() } - .onAppear { - ViewStore(store.stateless).send(.onAppear) - } } } diff --git a/Tabs/Sources/TabsView.swift b/Tabs/Sources/TabsView.swift index 5afad14..d136a90 100644 --- a/Tabs/Sources/TabsView.swift +++ b/Tabs/Sources/TabsView.swift @@ -55,18 +55,27 @@ public struct TabsView: View { state: \.color, action: TabsAction.color )) + .onAppear { + ViewStore(store.stateless).send(.color(.onAppear)) + } case .shape: ShapeView(store: store.scope( state: \.shape, action: TabsAction.shape )) + .onAppear { + ViewStore(store.stateless).send(.shape(.onAppear)) + } case .preview: PreviewView(store: store.scope( state: \.preview, action: TabsAction.preview )) + .onAppear { + ViewStore(store.stateless).send(.preview(.onAppear)) + } } } }