android-templates icon indicating copy to clipboard operation
android-templates copied to clipboard

[#352] [Sample] 1/2: Setup the nav graph and navigation logic

Open luongvo opened this issue 2 years ago • 1 comments

https://github.com/nimblehq/android-templates/issues/352

What happened 👀

  • Set up nav graph https://developer.android.com/jetpack/compose/navigation.
  • Provide ViewModel directly in composable by hiltViewModel(). This solves the last known issue https://github.com/nimblehq/jetpack-compose-crypto/pull/45#pullrequestreview-1125833203 👏 .
  • Stop passing NavController to composable, pass the navigator callback with AppDestination event instead. This navigator callback helps us to send the navigation command from our composable & ViewModel to the NavGraph for navigating.
  • Clean up a lot of redundant unused/deprecated components for XML.
  • 🚧 We will apply all necessary changes back to the Compose template in the second part 2/2. 🚧
  • 🚧 We will migrate to use the new permission requesting flow for Compose in https://github.com/nimblehq/android-templates/issues/370. 🚧

Insight 📝

Following what we introduced in https://docs.google.com/presentation/d/1xJYi25WrYOdMxigM0LTCIVX5zLozccCFGvq_ulyeTd8/edit#slide=id.p, we will have AppDestination class. AppDestination is designed to provide the route and arguments config for each composable screen. It's also a navigation command which contains a built destination for navigating.

  • For nav graph config 🔨
composable(
    route = AppDestination.Home.route,
    arguments = AppDestination.Home.arguments
) {
    HomeScreen(
        navigator = { destination -> navController.navigate(destination) }
    )
}
  • For navigation command sending 🚀
override fun onMyCoinsItemClick(coin: CoinItemUiModel) {
    execute {
        _navigator.emit(AppDestination.CoinDetail.buildDestination(coin.id))
    }
}
  • For navigation command callback executing 💁‍♂️
private fun NavHostController.navigate(destination: AppDestination) {
    when (destination) {
        is AppDestination.Up -> popBackStack()
        else -> navigate(route = destination.destination)
    }
}

Proof Of Work 📹

The existing navigation from the Home to the Second screen should work properly as it is.

luongvo avatar Dec 09 '22 08:12 luongvo

6 Warnings
:warning: Big PR
:warning: Uh oh! BaseViewModel.kt is under 95% coverage!
:warning: Uh oh! MainActivity.kt is under 95% coverage!
:warning: Uh oh! MainViewModel.kt is under 95% coverage!
:warning: Uh oh! Your project is under 80% coverage!
:warning: Uh oh! Theme.kt is under 95% coverage!

Kover report for template-xml:

🧛 Template - XML Unit Tests Code Coverage: 29.95%

Coverage of Modified Files:

File Coverage
BaseViewModel.kt 0.00%
MainActivity.kt 0.00%
MainViewModel.kt 0.00%

Modified Files Not Found In Coverage Report:

AppBar.kt AppDestination.kt AppNavigation.kt Configurations.kt HomeComposeScreen.kt HomeComposeViewModel.kt Item.kt ItemList.kt SecondScreen.kt Theme.kt Versions.kt build.gradle.kts build.gradle.kts detekt-config.yml detekt-config.yml

Codebase cunningly covered by count Shroud 🧛

Kover report for template-compose:

🧛 Template - Compose Unit Tests Code Coverage: 13.05%

Coverage of Modified Files:

File Coverage
BaseViewModel.kt 0.00%
MainActivity.kt 0.00%
MainViewModel.kt 0.00%
Theme.kt 0.00%

Modified Files Not Found In Coverage Report:

AppBar.kt AppDestination.kt AppNavigation.kt Configurations.kt HomeComposeScreen.kt HomeComposeViewModel.kt Item.kt ItemList.kt SecondScreen.kt Versions.kt build.gradle.kts build.gradle.kts detekt-config.yml detekt-config.yml

Codebase cunningly covered by count Shroud 🧛

Generated by :no_entry_sign: Danger

github-actions[bot] avatar Dec 09 '22 09:12 github-actions[bot]

@luongvo Would you mind updating the collectAsState to collectAsStateWithLifecycle in this PR? 😁

lydiasama avatar Dec 26 '22 04:12 lydiasama

@luongvo Would you mind updating the collectAsState to collectAsStateWithLifecycle in this PR? 😁

@lydiasama I think it's out of this PR's scope and needs to verify & vote on. Can we go through an RFC and contribute to improve it later cc @Tuubz?

luongvo avatar Dec 26 '22 04:12 luongvo