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

Split functions #102

Merged
merged 1 commit into from
Feb 28, 2024
Merged
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
27 changes: 16 additions & 11 deletions Sources/SnapshotPreviewsCore/View+Snapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,28 @@ public enum RenderingError: Error {
}

extension View {
public func snapshot(
layout: PreviewLayout,
window: UIWindow,
async: Bool,
completion: @escaping (Result<UIImage, RenderingError>, Float?, Bool?) -> Void)
{
public func makeExpandingView(layout: PreviewLayout, window: UIWindow) -> ExpandingViewController {
UIView.setAnimationsEnabled(false)
let animationDisabledView = self.transaction { transaction in
transaction.disablesAnimations = true
}
let controller = ExpandingViewController(rootView: animationDisabledView)
controller.setupView(layout: layout)

let (windowRootVC, containerVC) = Self.setupRootVC(subVC: controller)
let windowRootVC = Self.setupRootVC(subVC: controller)
window.rootViewController = windowRootVC
controller.expansionSettled = { [weak containerVC, weak controller] renderingMode, precision, accessibilityEnabled in
guard let containerVC, let controller else { return }
return controller
}

public func snapshot(
layout: PreviewLayout,
controller: ExpandingViewController,
window: UIWindow,
async: Bool,
completion: @escaping (Result<UIImage, RenderingError>, Float?, Bool?) -> Void)
{
controller.expansionSettled = { [weak controller, weak window] renderingMode, precision, accessibilityEnabled in
guard let controller, let window, let containerVC = controller.parent else { return }

if async {
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
Expand Down Expand Up @@ -71,7 +76,7 @@ extension View {
}
}

private static func setupRootVC(subVC: UIViewController) -> (UIViewController, UIViewController) {
private static func setupRootVC(subVC: UIViewController) -> UIViewController {
let windowRootVC = UIViewController()
windowRootVC.view.bounds = UIScreen.main.bounds
windowRootVC.view.backgroundColor = .clear
Expand All @@ -96,7 +101,7 @@ extension View {
subVC.view.widthAnchor.constraint(lessThanOrEqualToConstant: UIScreen.main.bounds.width).isActive = true
containerVC.view.heightAnchor.constraint(greaterThanOrEqualTo: subVC.view.heightAnchor, multiplier: 1).isActive = true

return (windowRootVC, containerVC)
return windowRootVC
}

private static func takeSnapshot(
Expand Down
2 changes: 2 additions & 0 deletions Sources/SnapshottingSwift/Snapshots.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ class Snapshots {
@MainActor func display(preview: SnapshotPreviewsCore.Preview, completion: @escaping (Result<UIImage, RenderingError>, Float?, Bool?) -> Void) throws {
var view = preview.view()
view = PreferredColorSchemeWrapper { AnyView(view) }
let controller = view.makeExpandingView(layout: preview.layout, window: window)
view.snapshot(
layout: preview.layout,
controller: controller,
window: window,
async: false,
completion: completion)
Expand Down
Loading