ekazaev / route-composer

Protocol oriented, Cocoa UI abstractions based library that helps to handle view controllers composition, navigation and deep linking tasks in the iOS application. Can be used as the universal replacement for the Coordinator pattern.
MIT License
896 stars 63 forks source link

Change tabs in TabBar in runtime #83

Closed snowtema closed 2 years ago

snowtema commented 2 years ago

I use this code to init tab bar with 3 tabs – First, Second and Profile:

    var tabBarScreen: DestinationStep<UITabBarController, Any?> {
        StepAssembly(
            finder: ClassFinder<UITabBarController, Any?>(options: .current, startingPoint: .root),
            factory: Configuration.completeFactory)
            .using(GeneralAction.replaceRoot())
            .from(GeneralStep.root())
            .assemble()
    }
...

   static let tabBarFactory = CompleteFactoryAssembly(
               factory: TabBarControllerFactory(nibName: nil, bundle: nil, delegate: nil))
        .with(firstAssemble)
        .with(secondAssemble)
        .with(profileAssemble)
        .assemble()

After the user opens the Second tab bar the first time and finished some flow, how can I change in runtime Second flow for the second tab to another flow, like Second_v2?

ekazaev commented 2 years ago

Hi @snowtema

Sorry, I don't think I fully understood the question. Do you want to replace the view controller in the second tab?

snowtema commented 2 years ago

Hi @ekazaev, thank you for the fast response! Yeah, exactly

ekazaev commented 2 years ago

@snowtema at the end of the flow you can call something like this:

let step =  StepAssembly(
            finder: ClassFinder(options: .currentAllStack),
            factory: NewSecondTabControllerFactory())
            .using(UITabBarController.add(at: 1, replacing: true))
            .from(tabBarScreen)
            .assemble()

You can write your own more advanced UITabBarController.add(...) action the will take instead of index a finder of the view controller that needs to be replaced to be independent from the numeric indexes.

One thing to keep in mind, is that if which view controller should appear in the second tab depends on some flag, you need to cover it in the TabBarFactory, so that on the next application start TabBarFactory build itself with a proper view controller in the second tab. It probably worth to consider to write a custom TabBarFactory instead of using CompleteFactoryAssembly so that it will have that logic inside.

snowtema commented 2 years ago

Thank you a lot! Will do this soon

snowtema commented 2 years ago

@ekazaev It works perfectly! But is there any way to replace view controller for UITabBarController without navigating to it immediately? Like when the user finished some flow and then manually opens the second tab, he will see a new view controller there.

ekazaev commented 2 years ago

@snowtema Then youll have to do it manually. Basically youll have to do what the router would just without navigation. Use a finder to find a TabBarController then use a factory and replace it.