Skip to content

Commit

Permalink
Split functions (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahsmartin authored Feb 28, 2024
1 parent 1ab7f66 commit dfde4c5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
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

0 comments on commit dfde4c5

Please sign in to comment.