rundfunk47 / stinsen

Coordinators in SwiftUI. Simple, powerful and elegant.
MIT License
822 stars 87 forks source link

[Question] Reload Tab in TabCoordinatable? #89

Closed emartinson closed 1 year ago

emartinson commented 1 year ago

Hi! Regarding your Example App, I changed MainCoordinator to be my TabBarCoordinator (which is mostly looks like AuthenticatedCoordinator in Example App):

final class MainCoordinator: TabCoordinatable {
    var child = TabChild(startingItems: [
            \MainCoordinator.home,
            \MainCoordinator.chart,
            \MainCoordinator.profile
        ])

    var user: User?

    @Route(tabItem: makeHomeTab) var home = makeHome
    @Route(tabItem: makeChartTab) var chart = makeChart
    @Route(tabItem: makeProfileTab) var profile = makeProfile

    init() {
        self.user = nil // or read from AuthService
    }

    @ViewBuilder func sharedView(_ view: AnyView) -> some View {
        view.onReceive(AuthenticationService.shared.$status, perform: { status in
            switch status {
            case .unauthenticated:
                self.user = nil
            case .authenticated(let user):
                self.user = user
                // profile = makeProfile // <--- Error here: Cannot assign value of type '() -> NavigationViewCoordinator<ProfileCoordinator>' to type 'Content<MainCoordinator, NavigationViewCoordinator<ProfileCoordinator>>'
            }
        })
    }

What I want to know - how can I reload profile tab? I want to display user avatar and name on tab item if user is authenticated.

LePips commented 1 year ago

This is somewhat similar to my request https://github.com/rundfunk47/stinsen/issues/33.

What you should do now is that the view set to the tab item checks whether the user is authenticated or not and in the body shows the correct view.

emartinson commented 1 year ago

Yeah, this seems to be the only way and it's working! Thank you!