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

Fix onDismiss not called when view controller is explicitly dismissed #255

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

acosmicflamingo
Copy link
Contributor

Looks like present(item:content:) function in UIKit does not successfully call onDismiss as one would expect. I'm not seeing a print statement when I do this:

// Using item parameter
present(item: $model.destination.alert, id: \.self, onDismiss: {
  print("Yay!!!")
}) { message in
  let alert = UIAlertController(
    title: "This is an alert",
    message: message,
    preferredStyle: .alert
  )
  alert.addAction(UIAlertAction(title: "OK", style: .default))
  return alert
}

// Using isPresented parameter
present(isPresented: UIBinding($model.destination.alert), onDismiss: {
  print("Yay!!!")
}) { //message in
  let alert = UIAlertController(
    title: "This is an alert",
    message: "LOL",
    preferredStyle: .alert
  )
  alert.addAction(UIAlertAction(title: "OK", style: .default))
  return alert
}

Going through breakpoints, it appears that controller is always nil: https://github.com/pointfreeco/swift-navigation/blob/main/Sources/UIKitNavigation/Navigation/Presentation.swift#L377. I find it strange because I the debugger does at some point set the dict with a value: https://github.com/pointfreeco/swift-navigation/blob/main/Sources/UIKitNavigation/Navigation/Presentation.swift#L365. Dismissal was working properly, and I have a better understanding of how caching works after spending a few minutes reading the code, so it didn't look like the problem resided there.

The next thing that stood out to me was the weakified controller property in Presented, and that the dictionary's controller properties were being immediately deallocated. Removing weak will certainly fix this, and I explicitly deallocate it in the deinit to mimic the weak behavior.

@mbrandonw
Copy link
Member

Hi @acosmicflamingo, thanks for looking into this! However, it seems to have broken the test testPushViewController_ManualPop. I don't have time to look into why right now, but that is something that would need to be taken care of.

It would also be good to get some test coverage on this change to prove that it behaves the way you say it does.

@acosmicflamingo
Copy link
Contributor Author

Oh okay! Sure, I can investigate and the test suggestion is a great one :)

@acosmicflamingo acosmicflamingo changed the title Do not weakify controller property in Presented class Fix onDismiss not called when view controller is explicitly dismissed Dec 19, 2024
@acosmicflamingo
Copy link
Contributor Author

acosmicflamingo commented Dec 19, 2024

@mbrandonw I added a test that checks whether vc.isPresented is false during explicit dismissal. It works as expected when dismissal occurs using UITraitCollection functionality (in testPresents_TraitDismissal function), but it looks like calling vc.presentedViewController?.dismiss(animated: false) does not actually set vc.isPresented to false as one would expect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants