Skip to content

Commit 548e7af

Browse files
committed
Allow wrapping screens to guarantee a described view controller
1 parent edb6f17 commit 548e7af

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// AnyContentContainerScreen.swift
3+
// WorkflowUI
4+
//
5+
// Created by Kyle Van Essen on 9/26/22.
6+
//
7+
8+
import Foundation
9+
10+
///
11+
///
12+
public struct AnyContentContainerScreen: Screen {
13+
public let content: AnyScreen
14+
15+
public init<ScreenType: Screen>(_ content: ScreenType) {
16+
if let content = content as? Self {
17+
self = content
18+
} else {
19+
self.content = content.asAnyScreen()
20+
}
21+
}
22+
23+
// MARK: Screen
24+
25+
public func viewControllerDescription(environment: ViewEnvironment) -> ViewControllerDescription {
26+
let description = content.viewControllerDescription(environment: environment)
27+
28+
return ViewControllerDescription(
29+
/// The inner `DescribedViewController` will respect `performInitialUpdate` from
30+
/// the nested screen – so our value should always be false.
31+
performInitialUpdate: false,
32+
type: DescribedViewController.self,
33+
build: {
34+
DescribedViewController(description: description)
35+
},
36+
update: { vc in
37+
vc.update(description: description)
38+
}
39+
)
40+
}
41+
}
42+
43+
extension Screen {
44+
///
45+
///
46+
public func inContainerScreen() -> AnyContentContainerScreen {
47+
AnyContentContainerScreen(self)
48+
}
49+
}

WorkflowUI/Sources/ViewControllerDescription/DescribedViewController.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import UIKit
2020

21+
/// Displays the backing `ViewControllerDescription` for a given `Screen`.
22+
///
2123
public final class DescribedViewController: UIViewController {
2224
var currentViewController: UIViewController
2325

@@ -63,7 +65,11 @@
6365
}
6466

6567
public func update<S: Screen>(screen: S, environment: ViewEnvironment) {
66-
update(description: screen.viewControllerDescription(environment: environment))
68+
if let screen = screen as? AnyContentContainerScreen {
69+
update(description: screen.content.viewControllerDescription(environment: environment))
70+
} else {
71+
update(description: screen.viewControllerDescription(environment: environment))
72+
}
6773
}
6874

6975
override public func viewDidLoad() {

0 commit comments

Comments
 (0)