Skip to content

Commit deed364

Browse files
committed
convert didClose to output
1 parent e91728f commit deed364

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

Samples/SwiftUITestbed/Sources/AppDelegate.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2525
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
2626
window = UIWindow(frame: UIScreen.main.bounds)
2727
window?.rootViewController = WorkflowHostingController(
28-
workflow: RootWorkflow(close: nil)
28+
workflow: RootWorkflow()
2929
.mapRendering(MarketRootScreen.init)
3030
.mapRendering(ModalHostContainer.init)
3131
)

Samples/SwiftUITestbed/Sources/MainScreen.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct MainScreen: MarketScreen {
2828

2929
let title: String
3030
let didChangeTitle: (String) -> Void
31+
let canClose: Bool
3132

3233
let allCapsToggleIsOn: Bool
3334
let allCapsToggleIsEnabled: Bool
@@ -108,7 +109,7 @@ extension MainScreen: MarketBackStackContentScreen {
108109
func backStackItem(in environment: ViewEnvironment) -> MarketUI.MarketNavigationItem {
109110
MarketNavigationItem(
110111
title: .text(.init(regular: title)),
111-
backButton: didTapClose.map { .close(onTap: $0) } ?? .automatic()
112+
backButton: canClose ? .close(onTap: { didTapClose?() }) : .automatic()
112113
)
113114
}
114115

Samples/SwiftUITestbed/Sources/MainWorkflow.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ import MarketWorkflowUI
1818
import Workflow
1919

2020
struct MainWorkflow: Workflow {
21-
let didClose: (() -> Void)?
21+
let canClose: Bool
2222

2323
enum Output {
2424
case pushScreen
2525
case presentScreen
26+
case close
2627
}
2728

2829
struct State {
@@ -72,12 +73,13 @@ struct MainWorkflow: Workflow {
7273
return MainScreen(
7374
title: state.title,
7475
didChangeTitle: { sink.send(.changeTitle($0)) },
76+
canClose: canClose,
7577
allCapsToggleIsOn: state.isAllCaps,
7678
allCapsToggleIsEnabled: !state.title.isEmpty,
7779
didChangeAllCapsToggle: { sink.send(.changeAllCaps($0)) },
7880
didTapPushScreen: { sink.send(.pushScreen) },
7981
didTapPresentScreen: { sink.send(.presentScreen) },
80-
didTapClose: didClose
82+
didTapClose: canClose ? { sink.send(.close) } : nil
8183
)
8284
}
8385
}

Samples/SwiftUITestbed/Sources/RootWorkflow.swift

+13-5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import Workflow
1919
import WorkflowUI
2020

2121
struct RootWorkflow: Workflow {
22-
let close: (() -> Void)?
23-
24-
typealias Output = Never
22+
enum Output {
23+
case close
24+
}
2525

2626
struct State {
2727
var backStack: BackStack
@@ -57,6 +57,8 @@ struct RootWorkflow: Workflow {
5757
state.backStack.other.append(.main())
5858
case .main(.presentScreen):
5959
state.isPresentingModal = true
60+
case .main(.close):
61+
return .close
6062
case .popScreen:
6163
state.backStack.other.removeLast()
6264
case .dismissScreen:
@@ -75,7 +77,7 @@ struct RootWorkflow: Workflow {
7577
func rendering(_ screen: State.Screen, isRoot: Bool) -> AnyMarketBackStackContentScreen {
7678
switch screen {
7779
case .main(let id):
78-
return MainWorkflow(didClose: isRoot ? close : nil)
80+
return MainWorkflow(canClose: isRoot)
7981
.mapOutput(Action.main)
8082
.mapRendering(AnyMarketBackStackContentScreen.init)
8183
.rendered(in: context, key: id.uuidString)
@@ -96,7 +98,13 @@ struct RootWorkflow: Workflow {
9698
base: backStack,
9799
modals: {
98100
guard state.isPresentingModal else { return [] }
99-
let screen = RootWorkflow(close: { sink.send(.dismissScreen) })
101+
let screen = RootWorkflow()
102+
.mapOutput { output in
103+
switch output {
104+
case .close:
105+
return Action.dismissScreen
106+
}
107+
}
100108
.rendered(in: context)
101109
.asAnyScreen()
102110
let modal = Modal(

0 commit comments

Comments
 (0)