Skip to content

Commit

Permalink
Fix dismissing a sheet (issue #6)
Browse files Browse the repository at this point in the history
When dismissing a sheet, the sheets ContentView sometimes
already reflects that change in presentation while the
View slides off screen. Showing an error.

Now we keep a handle to the actual VC being presented.
  • Loading branch information
helje5 committed May 1, 2022
1 parent 6907cec commit b151f52
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions Sources/ViewController/Presentations/AutoPresentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,48 @@ internal struct AutoPresentationViewModifier<VC>: ViewModifier
@ObservedObject var presentingViewController : VC
let mode : ViewControllerPresentationMode

var body: some View {
// Keep a handle to the VC being presented. We do this to avoid issue #6,
// i.e. when a sheet is dismissed and transitions off-screen, the
// "presentedViewController" is already gone (dismissed).
// The `body` of this `Present` View would then evaluate to the
// `TypeMismatchInfoView` during the dismiss.
// So we keep the VC being presented around, to make sure we still have a
// handle for the content-view while it is being dismissed.
@State private var viewController : _ViewController?

private var activeVC: _ViewController? {
if let activeVC = viewController { return activeVC }

if let presentation =
presentingViewController.activePresentation(for: mode)
presentingViewController.activePresentation(for: mode)
{
let presentedViewController = presentation.viewController
// Note: Do not modify `@State` in here! (i.e. do not push to the
// `viewController` variable as part of the evaluation)
// This happens if the VC is getting presented.
return presentation.viewController
}

return nil
}

var body: some View {
if let presentedViewController = activeVC {
presentedViewController.anyControlledContentView
.environment(\.viewControllerPresentationMode, mode)
.navigationTitle(presentedViewController.navigationTitle)
.onAppear {
viewController = presentedViewController
}
.onDisappear { // This seems to be a proper onDidDisappear
viewController = nil
}
}
else {
TypeMismatchInfoView<AnyViewController, VC>(
parent: presentingViewController, expectedMode: mode
)
#if DEBUG
TypeMismatchInfoView<AnyViewController, VC>(
parent: presentingViewController, expectedMode: mode
)
#endif
}
}
}
Expand Down

0 comments on commit b151f52

Please sign in to comment.