Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid on-appear calls in tab views #3

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion App/Sources/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct App: SwiftUI.App {
shape: ShapeState(type: .square),
preview: PreviewState()
),
reducer: tabsReducer,
reducer: tabsReducer.debug(),
environment: ()
)

Expand Down
1 change: 1 addition & 0 deletions Color/Sources/ColorAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ public enum ColorAction: Equatable {
case didUpdateGreen(Double)
case didUpdateBlue(Double)
case apply
case onAppear
}
3 changes: 3 additions & 0 deletions Color/Sources/ColorReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ public let colorReducer = Reducer<ColorState, ColorAction, Void> { state, action

case .apply:
return .none

case .onAppear:
return .none
}
}
2 changes: 1 addition & 1 deletion Common/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
1 change: 1 addition & 0 deletions Preview/Sources/PreviewAction.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
public enum PreviewAction: Equatable {
case reset
case onAppear
}
3 changes: 3 additions & 0 deletions Preview/Sources/PreviewReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ public let previewReducer = Reducer<PreviewState, PreviewAction, Void> { state,
state.color = nil
state.shape = nil
return .none

case .onAppear:
return .none
}
}
56 changes: 29 additions & 27 deletions Preview/Sources/PreviewView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Shape/Sources/ShapeAction.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
public enum ShapeAction: Equatable {
case didSelectType(ShapeType)
case apply
case onAppear
}
3 changes: 3 additions & 0 deletions Shape/Sources/ShapeReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ public let shapeReducer = Reducer<ShapeState, ShapeAction, Void> { state, action

case .apply:
return .none

case .onAppear:
return .none
}
}
2 changes: 1 addition & 1 deletion Tabs/Sources/Tab.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public enum Tab: Int, CaseIterable {
public enum Tab: Int, CaseIterable, Equatable {
case color
case shape
case preview
Expand Down
11 changes: 10 additions & 1 deletion Tabs/Sources/TabsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public struct TabsView: View {
let store: Store<TabsState, TabsAction>

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
Expand Down Expand Up @@ -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))
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions Tabs/Sources/TabsViewState.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
struct TabsViewState: Equatable {
init(state: TabsState) {
selectedTab = state.selectedTab
}

var selectedTab: Tab
}