rundfunk47 / stinsen

Coordinators in SwiftUI. Simple, powerful and elegant.
MIT License
924 stars 98 forks source link

Tabbar items customisation #119

Open PomazanovaAnna opened 1 year ago

PomazanovaAnna commented 1 year ago

Hello! I want to add localization on the fly for my project. However, I have @Published property for the current language in custom TabCoordinator, which conforms TabCoordinatable. How can I update tab bar items each time when @Published property is changed? For now, tab bar items will change when the user press on some of them.

  final class AuthentificatedCoordinator: TabCoordinatable {

    @Route(tabItem: makeHomeTab) private var home = makeHomeCoordinator
    @Published var appLanguage: Language!

   }
@ViewBuilder
    func makeHomeTab(isActive: Bool) -> some View {
        Image(systemName: "house" + (isActive ? ".fill" : ""))
        Text("Home".localized(appLanguage)
    }

Thanks!

LePips commented 1 year ago

You may be able to use .id() to cause an update:

@ViewBuilder
func makeHomeTab(isActive: Bool) -> some View {
    Image(systemName: "house" + (isActive ? ".fill" : ""))

    Text("Home".localized(appLanguage)
        .id(appLanguage)
}
PomazanovaAnna commented 1 year ago

Unfortunately, it does not update with .id() When the appLanguage property changes, the method makeHomeTab() does not call. Only when the user press on some of the tab bar items.