From 15737c96741f177c344e3dc82b4e3528794f373a Mon Sep 17 00:00:00 2001 From: Nico Hinderling Date: Tue, 3 Sep 2024 14:48:20 -0700 Subject: [PATCH] Add ExpandingVC Timeout + Explicitly Call updateScrollViewHeight on main thread (#182) * Changes * verify dispatch main is still necessary * address noah comments * keep main thread dispatch for test * just the timer logic * address noah comments * remove anchor changes when timer ends * delete print --- .gitignore | 3 ++ .../ExpandingViewController.swift | 46 +++++++++++++++++-- .../SnapshotPreviewsCore/View+Snapshot.swift | 14 +++++- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 9ec44a0..62dc33c 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,6 @@ DerivedData/ DemoApp/DemoApp.xcodeproj/xcuserdata/ .xcuserstate PreviewsSupport/PreviewsSupport.xcframework/**/*.private.swiftinterface + + +DemoApp/DemoApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/ \ No newline at end of file diff --git a/Sources/SnapshotPreviewsCore/ExpandingViewController.swift b/Sources/SnapshotPreviewsCore/ExpandingViewController.swift index e3b4228..44aa100 100644 --- a/Sources/SnapshotPreviewsCore/ExpandingViewController.swift +++ b/Sources/SnapshotPreviewsCore/ExpandingViewController.swift @@ -20,13 +20,18 @@ public final class ExpandingViewController: UIHostingController Void)? { + private var startTime: Date? + private var timer: Timer? + + public var expansionSettled: ((EmergeRenderingMode?, Float?, Bool?, Error?) -> Void)? { didSet { didCall = false } } @@ -69,19 +74,25 @@ public final class ExpandingViewController: UIHostingController= HeightExpansionTimeLimitInSeconds else { + return + } + let timeoutError = RenderingError.expandingViewTimeout(CGSize(width: UIScreen.main.bounds.size.width, + height: firstScrollView?.visibleContentHeight ?? -1)) + NSLog("ExpandingViewController: Expanding Scroll View timed out. Current height is \(firstScrollView?.visibleContentHeight ?? -1)") + runCallback(timeoutError) + } + } + + func stopAndResetTimer() { + timer?.invalidate() + timer = nil + startTime = nil + } + } #endif diff --git a/Sources/SnapshotPreviewsCore/View+Snapshot.swift b/Sources/SnapshotPreviewsCore/View+Snapshot.swift index 0af3d76..bb4892a 100644 --- a/Sources/SnapshotPreviewsCore/View+Snapshot.swift +++ b/Sources/SnapshotPreviewsCore/View+Snapshot.swift @@ -15,6 +15,7 @@ import SnapshotSharedModels public enum RenderingError: Error { case failedRendering(CGSize) case maxSize(CGSize) + case expandingViewTimeout(CGSize) } extension AccessibilityMarker: AccessibilityMark { @@ -57,8 +58,17 @@ extension View { async: Bool, completion: @escaping (SnapshotResult) -> Void) { - controller.expansionSettled = { [weak controller, weak window] renderingMode, precision, accessibilityEnabled in - guard let controller, let window, let containerVC = controller.parent else { return } + controller.expansionSettled = { [weak controller, weak window] renderingMode, precision, accessibilityEnabled, error in + guard let controller, let window, let containerVC = controller.parent else { + return + } + + if let error { + DispatchQueue.main.async { + completion(SnapshotResult(image: .failure(error), precision: precision, accessibilityEnabled: accessibilityEnabled, accessibilityMarkers: nil, colorScheme: _colorScheme)) + } + return + } if async { DispatchQueue.main.asyncAfter(deadline: .now() + 2) {