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

No way to know when a sheet is dismissed #14

Closed
lionel-alves opened this issue Feb 9, 2022 · 3 comments
Closed

No way to know when a sheet is dismissed #14

lionel-alves opened this issue Feb 9, 2022 · 3 comments

Comments

@lionel-alves
Copy link

Hi, first of all thanks for making SwiftUI navigation easier with you library, it is the best approach I came across by far. As a big fan of coordinators, I think your approach is even better that with UIKit since it easy to have one coordinator for a small flow that can both push and present, very convenient!

My issue: Presenting sheets, there is no way to know when it is dismissed (the onDismiss on the native call is passed to nil in you library). Exposing that would be very convenient as the onAppear is not called for sheets (well known problem).

Best regards

@lionel-alves lionel-alves changed the title Exposing onDismiss when presenting sheet There is no way to know when a sheet is dismissed Feb 9, 2022
@lionel-alves lionel-alves changed the title There is no way to know when a sheet is dismissed No way to know when a sheet is dismissed Feb 9, 2022
@zntfdr
Copy link
Contributor

zntfdr commented Feb 9, 2022

Probably not as elegant as what you're proposing in #15, but if your Screen type conforms to Equatable, you can also observe changes to the coordinator @State via an onChange(of:perform:) modifier.

For example:

struct Coordinator: View {
  @State var routes: Routes<Screen> = [.root(.home)]
    
  var body: some View {
    Router($routes) { screen, _ in
      switch screen {
        case .home:
          HomeView(onComplete: {  routes.presentSheet(.second)  })
        case .second:
          SecondView(onComplete: { routes.presentSheet(.third) })
        case .third:
          ThirdView(onComplete: { ... })
      }
    }
    .onChange(of: routes) { [routes] newValue in
      print(routes) // old value
      print(newValue) // new value
      // compare the two values above to check whether a sheet got dismissed")
    }
  }
}

@lionel-alves
Copy link
Author

Thanks Federico, that's a good workaround, we know everything from the routes @State changes indeed. Not as convenient as having a onDismiss but it works.

@johnpatrickmorgan
Copy link
Owner

#15 is now merged, so I'll close this issue. Thanks @lionel-alves for raising it. And thanks @zntfdr for your suggestion - I think I prefer that approach as each Route can remain a simple 'bag of values', and it's not much work to be notified when a route is dismissed, e.g.:

  Router(...)
    .onChange(of: routes) { [oldRoutes = routes] newRoutes in
      let dismissedRoutes = oldRoutes.suffix(from: min(oldRoutes.endIndex, newRoutes.endIndex))
      print(dismissedRoutes)
    }

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

No branches or pull requests

3 participants