英文:
The best way to define flavor-specific navigation for NavHost with Jetpack Compose
问题
I have a multi-flavored, modularized app. Currently, I have 4 flavors and just one feature module, e.g. :feature:cars
At the moment, the :feature:cars module should be connected only to 3 flavors out of 4. But in the future, there will be more flavors and feature modules.
For now, I define additional composable inside the NavHost using the addSpecificNavigation function that is created in each flavor folder, where the :feature:cars module is not used, the function does nothing, and in the other 3 files the code is exactly the same.
I don't like this solution, but I can't figure out how to do it in a more correct way. Any advices?
Current NavHost:
NavHost(navController, startDestination = startDestinationRoute) {
        addSpecificNavigation(navController)
        composable(Onboarding.route) {
            OnboardingScreen(appState = appState)
        }
        ...
}
FlavorA/FlavorSpecificNavigation:
fun NavGraphBuilder.addSpecificNavigation(navController: NavHostController) {
    //no differences yet
}
FlavorB/FlavorSpecificNavigation:
fun NavGraphBuilder.addSpecificNavigation(navController: NavHostController) {
    composable(Destination.Cars.route) {
        CarsScreen()
    }
    composable(Destination.CreateCar.route) {
        CreateCarScreen()
    }
    ...
}
英文:
I have a multi-flavored, modularized app. Currently, I have 4 flavors and just one feature module, e.g. :feature:cars
At the moment, the :feature:cars module should be connected only to 3 flavors out of 4. But in the future, there will be more flavors and feature modules.
For now, I define additional composable inside the NavHost using the addSpecificNavigation function that is created in each flavor folder, where the :feature:cars module is not used, the function does nothing, and in the other 3 files the code is exactly the same.
I don't like this solution, but I can't figure out how to do it in a more correct way. Any advices?
Current NavHost:
NavHost(navController, startDestination = startDestinationRoute) {
        addSpecificNavigation(navController)
        composable(Onboarding.route) {
            OnboardingScreen(appState = appState)
        }
        ...
}
FlavorA/FlavorSpecificNavigation:
fun NavGraphBuilder.addSpecificNavigation(navController: NavHostController) {
    //no differences yet
}
FlavorB/FlavorSpecificNavigation:
fun NavGraphBuilder.addSpecificNavigation(navController: NavHostController) {
    composable(Destination.Cars.route) {
        CarsScreen()
    }
    composable(Destination.CreateCar.route) {
        CreateCarScreen()
    }
    ...
}
答案1
得分: 0
在“:feature:cards”模块的navigation/CardsNavigation.kt文件中,我决定通过将所有特定模块的导航移动到此模块的扩展函数来执行它。
fun NavGraphBuilder.cardsScreens() {
    composable(Destination.Cars.route) {
        CarsScreen()
    }
    composable(Destination.CreateCar.route) {
        CreateCarScreen()
    }
}
以及在“FlavorB/FlavorSpecificNavigation”中:
fun NavGraphBuilder.addSpecificNavigation(navController: NavHostController) {
    cardsScreens()
}
英文:
So I decide to do it by moving all module-specific navigation to the extension function in this module.
in the :feature:cards, navigation/CardsNavigation.kt:
fun NavGraphBuilder.cardsScreens() {
    composable(Destination.Cars.route) {
        CarsScreen()
    }
    composable(Destination.CreateCar.route) {
        CreateCarScreen()
    }
}
and in the FlavorB/FlavorSpecificNavigation:
fun NavGraphBuilder.addSpecificNavigation(navController: NavHostController) {
    cardsScreens()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论