为Jetpack Compose的NavHost定义特定于味道的导航的最佳方法

huangapple go评论138阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月27日 18:03:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563759.html
  • android
  • android-jetpack-compose
  • android-productflavors
  • jetpack-compose-navigation
  • white-labelling
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定