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: () ) 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/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: [ 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..8cc417c 100644 --- a/Preview/Sources/PreviewView.swift +++ b/Preview/Sources/PreviewView.swift @@ -12,37 +12,39 @@ 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") } } } 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/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 diff --git a/Tabs/Sources/TabsView.swift b/Tabs/Sources/TabsView.swift index 1c36ed9..d136a90 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 @@ -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)) + } } } } 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 +}