johnpatrickmorgan / FlowStacks

FlowStacks allows you to hoist SwiftUI navigation and presentation state into a Coordinator
MIT License
854 stars 66 forks source link

No way to know when a sheet is dismissed #14

Closed lionel-alves closed 2 years ago

lionel-alves commented 2 years ago

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

zntfdr commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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)
    }