-
Notifications
You must be signed in to change notification settings - Fork 44
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
[WIP DNR] Allow wrapping screens to guarantee a described view controller #159
Draft
kyleve
wants to merge
7
commits into
main
Choose a base branch
from
kve/container-screen
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
10cec95
Allow wrapping screens to guarantee a described view controller
kyleve f641529
Merge remote-tracking branch 'origin/main' into kve/container-screen
kyleve 9784951
Doc cleanup, pass through animated flag for update method
kyleve 3f5a88f
Additional documentation
kyleve d81d939
Support transition for parent containers as well
kyleve 8843404
Animated by default, move test fixtures
kyleve 0bf4004
Add back generic constraint
kyleve File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
WorkflowUI/Sources/Screen/AnyContentScreen/AnyContentScreen.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2021 Square Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#if canImport(UIKit) | ||
|
||
import Foundation | ||
|
||
/// Creates a wrapper screen and view controller that will animate between underlying view controllers | ||
/// when their type changes. For example, you may use this screen to animate the transition between your | ||
/// loading state, content state, and empty states: | ||
/// | ||
/// ``` | ||
/// AnyContentScreen(transition: .fade) { | ||
/// if isLoading { | ||
/// return LoadingScreen(with: ...) | ||
/// } else if isEmpty { | ||
/// return EmptyStateScreen(with: ...) | ||
/// } else { | ||
/// return ContentScreen(with: ...) | ||
/// } | ||
/// } | ||
/// ``` | ||
public struct AnyContentScreen<Content: Screen>: Screen { | ||
/// The transition to use when the underlying screen changes. Defaults to `.fade`. | ||
public var transition: ViewTransition | ||
|
||
/// The content screen currently displayed. | ||
public let content: Content | ||
|
||
/// Creates a new screen with the given transition and content. | ||
public init( | ||
transition: ViewTransition = .fade(), | ||
content: () -> Content | ||
) { | ||
let content = content() | ||
|
||
if let content = content as? Self { | ||
self = content | ||
self.transition = transition | ||
} else { | ||
self.content = content | ||
} | ||
|
||
self.transition = transition | ||
} | ||
|
||
// MARK: Screen | ||
|
||
public func viewControllerDescription(environment: ViewEnvironment) -> ViewControllerDescription { | ||
let description = content.viewControllerDescription(environment: environment) | ||
|
||
return ViewControllerDescription( | ||
/// The inner `DescribedViewController` will respect `performInitialUpdate` from | ||
/// the nested screen – so our value should always be false. | ||
performInitialUpdate: false, | ||
transition: transition, | ||
type: DescribedViewController.self, | ||
build: { | ||
DescribedViewController(description: description) | ||
}, | ||
update: { vc in | ||
vc.update(description: description, animated: true) | ||
} | ||
) | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,17 +18,20 @@ | |
|
||
import UIKit | ||
|
||
/// Displays the backing `ViewControllerDescription` for a given `Screen`. | ||
public final class DescribedViewController: UIViewController { | ||
var currentViewController: UIViewController | ||
var content: UIViewController | ||
|
||
/// Creates a new view controller with the given description. | ||
public init(description: ViewControllerDescription) { | ||
self.currentViewController = description.buildViewController() | ||
self.content = description.buildViewController() | ||
super.init(nibName: nil, bundle: nil) | ||
|
||
addChild(currentViewController) | ||
currentViewController.didMove(toParent: self) | ||
addChild(content) | ||
content.didMove(toParent: self) | ||
} | ||
|
||
/// Creates a new view controller with the screen and environment. | ||
public convenience init<S: Screen>(screen: S, environment: ViewEnvironment) { | ||
self.init(description: screen.viewControllerDescription(environment: environment)) | ||
} | ||
|
@@ -38,93 +41,133 @@ | |
fatalError("init(coder:) is unavailable") | ||
} | ||
|
||
public func update(description: ViewControllerDescription) { | ||
if description.canUpdate(viewController: currentViewController) { | ||
description.update(viewController: currentViewController) | ||
/// Updates the content of the view controller with the given description. | ||
/// If the view controller can't be updated (because it's type is not the same), the old | ||
/// content will be transitioned out, and the new one will be transitioned in | ||
/// with the new description's `ViewTransition`. | ||
public func update(description: ViewControllerDescription, animated: Bool = true) { | ||
if description.canUpdate(viewController: content) { | ||
description.update(viewController: content) | ||
} else { | ||
currentViewController.willMove(toParent: nil) | ||
currentViewController.viewIfLoaded?.removeFromSuperview() | ||
currentViewController.removeFromParent() | ||
let old = content | ||
let new = description.buildViewController() | ||
|
||
currentViewController = description.buildViewController() | ||
|
||
addChild(currentViewController) | ||
content = new | ||
|
||
if isViewLoaded { | ||
currentViewController.view.frame = view.bounds | ||
view.addSubview(currentViewController.view) | ||
let animated = animated && view.window != nil | ||
|
||
addChild(new) | ||
old.willMove(toParent: nil) | ||
|
||
description.transition.transition( | ||
from: old.view, | ||
to: new.view, | ||
in: view, | ||
animated: animated, | ||
setup: { | ||
new.view.frame = self.view.bounds | ||
self.view.addSubview(new.view) | ||
}, | ||
completion: { | ||
new.didMove(toParent: self) | ||
|
||
old.view.removeFromSuperview() | ||
old.removeFromParent() | ||
|
||
self.currentViewControllerChanged() | ||
self.updatePreferredContentSizeIfNeeded() | ||
} | ||
) | ||
} else { | ||
addChild(new) | ||
new.didMove(toParent: self) | ||
|
||
old.willMove(toParent: nil) | ||
old.removeFromParent() | ||
|
||
updatePreferredContentSizeIfNeeded() | ||
} | ||
|
||
currentViewController.didMove(toParent: self) | ||
|
||
updatePreferredContentSizeIfNeeded() | ||
} | ||
} | ||
|
||
public func update<S: Screen>(screen: S, environment: ViewEnvironment) { | ||
update(description: screen.viewControllerDescription(environment: environment)) | ||
public func update<S: Screen>(screen: S, environment: ViewEnvironment, animated: Bool = true) { | ||
update(description: screen.viewControllerDescription(environment: environment), animated: animated) | ||
} | ||
|
||
override public func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
currentViewController.view.frame = view.bounds | ||
view.addSubview(currentViewController.view) | ||
content.view.frame = view.bounds | ||
view.addSubview(content.view) | ||
|
||
updatePreferredContentSizeIfNeeded() | ||
} | ||
|
||
override public func viewDidLayoutSubviews() { | ||
super.viewDidLayoutSubviews() | ||
currentViewController.view.frame = view.bounds | ||
content.view.frame = view.bounds | ||
} | ||
|
||
override public var childForStatusBarStyle: UIViewController? { | ||
return currentViewController | ||
return content | ||
} | ||
|
||
override public var childForStatusBarHidden: UIViewController? { | ||
return currentViewController | ||
return content | ||
} | ||
|
||
override public var childForHomeIndicatorAutoHidden: UIViewController? { | ||
return currentViewController | ||
return content | ||
} | ||
|
||
override public var childForScreenEdgesDeferringSystemGestures: UIViewController? { | ||
return currentViewController | ||
return content | ||
} | ||
|
||
override public var supportedInterfaceOrientations: UIInterfaceOrientationMask { | ||
return currentViewController.supportedInterfaceOrientations | ||
return content.supportedInterfaceOrientations | ||
} | ||
|
||
override public var preferredStatusBarUpdateAnimation: UIStatusBarAnimation { | ||
return currentViewController.preferredStatusBarUpdateAnimation | ||
return content.preferredStatusBarUpdateAnimation | ||
} | ||
|
||
@available(iOS 14.0, *) | ||
override public var childViewControllerForPointerLock: UIViewController? { | ||
return currentViewController | ||
return content | ||
} | ||
|
||
override public func preferredContentSizeDidChange( | ||
forChildContentContainer container: UIContentContainer | ||
) { | ||
super.preferredContentSizeDidChange(forChildContentContainer: container) | ||
|
||
guard container === currentViewController else { return } | ||
guard container === content else { return } | ||
|
||
updatePreferredContentSizeIfNeeded() | ||
} | ||
|
||
private func updatePreferredContentSizeIfNeeded() { | ||
let newPreferredContentSize = currentViewController.preferredContentSize | ||
let newPreferredContentSize = content.preferredContentSize | ||
|
||
guard newPreferredContentSize != preferredContentSize else { return } | ||
|
||
preferredContentSize = newPreferredContentSize | ||
} | ||
|
||
private func currentViewControllerChanged() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
setNeedsFocusUpdate() | ||
setNeedsUpdateOfHomeIndicatorAutoHidden() | ||
|
||
if #available(iOS 14.0, *) { | ||
self.setNeedsUpdateOfPrefersPointerLocked() | ||
} | ||
|
||
setNeedsUpdateOfScreenEdgesDeferringSystemGestures() | ||
setNeedsStatusBarAppearanceUpdate() | ||
|
||
UIAccessibility.post(notification: .screenChanged, argument: nil) | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, this drops the
transition