Skip to content

Commit

Permalink
Add bridging to ScreenViewController
Browse files Browse the repository at this point in the history
  • Loading branch information
n8chur committed Mar 30, 2023
1 parent 9dab8e5 commit 7a8f319
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions WorkflowUI/Sources/Screen/ScreenViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#if canImport(UIKit)

import UIKit
import ViewEnvironment
@_spi(ViewEnvironmentWiring) import ViewEnvironmentUI

/// Generic base class that can be subclassed in order to to define a UI implementation that is powered by the
/// given screen type.
Expand All @@ -39,31 +41,41 @@ open class ScreenViewController<ScreenType: Screen>: UIViewController {
public private(set) final var screen: ScreenType

public final var screenType: Screen.Type {
return ScreenType.self
ScreenType.self
}

public private(set) final var environment: ViewEnvironment
private var _environment: ViewEnvironment

public required init(screen: ScreenType, environment: ViewEnvironment) {
self.screen = screen
self.environment = environment
self._environment = environment
super.init(nibName: nil, bundle: nil)

let ancestor = ViewEnvironmentPropagationNode(
environmentDescendants: { [weak self] in
[self].compactMap { $0 }
},
customizeEnvironment: { [weak self] environment in
guard let self else { return }
environment = self._environment
}
)

environmentAncestorOverride = { ancestor }
}

@available(*, unavailable)
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public required init?(coder aDecoder: NSCoder) { fatalError() }

public final func update(screen: ScreenType, environment: ViewEnvironment) {
let previousScreen = self.screen
self.screen = screen
let previousEnvironment = self.environment
self.environment = environment
_environment = environment
setNeedsEnvironmentUpdate()
screenDidChange(from: previousScreen, previousEnvironment: previousEnvironment)
}

/// Subclasses should override this method in order to update any relevant UI bits when the screen model changes.
open func screenDidChange(from previousScreen: ScreenType, previousEnvironment: ViewEnvironment) {}
}

Expand Down

0 comments on commit 7a8f319

Please sign in to comment.