Skip to content

Commit

Permalink
[#352] Setup the nav graph and navigation logic on Sample Compose pro…
Browse files Browse the repository at this point in the history
…ject
  • Loading branch information
luongvo committed Dec 13, 2022
1 parent 0cefd11 commit a6143ba
Show file tree
Hide file tree
Showing 32 changed files with 256 additions and 572 deletions.
30 changes: 8 additions & 22 deletions sample-compose/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ plugins {
id("kotlin-parcelize")

id("dagger.hilt.android.plugin")
id("androidx.navigation.safeargs.kotlin")

id("kover")
}
Expand Down Expand Up @@ -58,7 +57,7 @@ android {
}
}

flavorDimensions(Flavor.DIMENSIONS)
flavorDimensions += Flavor.DIMENSION_VERSION
productFlavors {
create(Flavor.STAGING) {
applicationIdSuffix = ".staging"
Expand All @@ -81,27 +80,20 @@ android {
}

composeOptions {
kotlinCompilerVersion = Versions.KOTLIN_VERSION
kotlinCompilerExtensionVersion = Versions.COMPOSE_COMPILER_VERSION
}

buildFeatures {
viewBinding = true
compose = true
}

lintOptions {
isCheckDependencies = true
lint {
checkDependencies = true
xmlReport = true
xmlOutput = file("build/reports/lint/lint-result.xml")
}

testOptions {
unitTests {
// Robolectric resource processing/loading
isIncludeAndroidResources = true
isReturnDefaultValues = true
}
unitTests.all {
if (it.name != "testStagingDebugUnitTest") {
it.extensions.configure(kotlinx.kover.api.KoverTaskExtension::class) {
Expand All @@ -122,22 +114,17 @@ dependencies {

implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))

implementation("androidx.activity:activity-ktx:${Versions.ANDROIDX_ACTIVITY_KTX_VERSION}")
implementation("androidx.appcompat:appcompat:${Versions.ANDROIDX_SUPPORT_VERSION}")
implementation("androidx.core:core-ktx:${Versions.ANDROIDX_CORE_KTX_VERSION}")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:${Versions.ANDROIDX_LIFECYCLE_VERSION}")

implementation("androidx.compose.ui:ui:${Versions.COMPOSE_VERSION}")
implementation("androidx.compose.ui:ui-tooling:${Versions.COMPOSE_VERSION}")
implementation("androidx.compose.foundation:foundation:${Versions.COMPOSE_VERSION}")
implementation("androidx.compose.material:material:${Versions.COMPOSE_VERSION}")

implementation("androidx.fragment:fragment-ktx:${Versions.ANDROIDX_FRAGMENT_KTX_VERSION}")
implementation("androidx.navigation:navigation-fragment-ktx:${Versions.ANDROIDX_NAVIGATION_VERSION}")
implementation("androidx.navigation:navigation-runtime-ktx:${Versions.ANDROIDX_NAVIGATION_VERSION}")
implementation("androidx.navigation:navigation-ui-ktx:${Versions.ANDROIDX_NAVIGATION_VERSION}")
implementation("androidx.navigation:navigation-compose:${Versions.COMPOSE_NAVIGATION_VERSION}")

implementation("com.google.dagger:hilt-android:${Versions.HILT_VERSION}")
implementation("androidx.hilt:hilt-navigation-compose:${Versions.HILT_NAVIGATION_COMPOSE_VERSION}")

implementation("com.jakewharton.timber:timber:${Versions.TIMBER_LOG_VERSION}")

Expand All @@ -148,16 +135,15 @@ dependencies {

implementation("com.markodevcic:peko:${Versions.PEKO_VERSION}")

kapt("com.google.dagger:hilt-compiler:${Versions.HILT_VERSION}")

debugImplementation("com.github.chuckerteam.chucker:library:${Versions.CHUCKER_VERSION}")
releaseImplementation("com.github.chuckerteam.chucker:library-no-op:${Versions.CHUCKER_VERSION}")

kapt("com.google.dagger:hilt-compiler:${Versions.HILT_VERSION}")

// Testing
testImplementation("io.kotest:kotest-assertions-core:${Versions.TEST_KOTEST_VERSION}")
testImplementation("junit:junit:${Versions.TEST_JUNIT_VERSION}")
testImplementation("org.robolectric:robolectric:${Versions.TEST_ROBOLECTRIC_VERSION}")
testImplementation("androidx.test:core:${Versions.ANDROIDX_TEST_CORE_VERSION}")
testImplementation("androidx.test:core:${Versions.TEST_ANDROIDX_CORE_VERSION}")
testImplementation("androidx.test:runner:${Versions.TEST_RUNNER_VERSION}")
testImplementation("androidx.test:rules:${Versions.TEST_RUNNER_VERSION}")
testImplementation("androidx.test.ext:junit-ktx:${Versions.TEST_JUNIT_ANDROIDX_EXT_VERSION}")
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package co.nimblehq.sample.compose.ui

import androidx.navigation.*

const val KEY_ID = "id"

sealed class AppDestination(val route: String = "") {

open val arguments: List<NamedNavArgument> = emptyList()

open var destination: String = route

//====================================================//

object Up : AppDestination()

object Home : AppDestination("home")

/**
* We can define route as "coin/details" without "coinId" parameter because we're passing it as argument already.
* So either passing "coinId" via arguments or passing it via route.
*
* We keep passing "coinId" in both route and arguments for this destination to give example of navigation concept
* about how to build a destination with parameters.
*/
object Second : AppDestination("second/{$KEY_ID}") {

override val arguments = listOf(
navArgument(KEY_ID) { type = NavType.StringType }
)

fun buildDestination(id: String) = apply {
destination = "second/$id"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package co.nimblehq.sample.compose.ui

import androidx.compose.runtime.Composable
import androidx.navigation.*
import androidx.navigation.compose.*
import co.nimblehq.sample.compose.ui.screens.home.HomeComposeScreen
import co.nimblehq.sample.compose.ui.screens.second.SecondScreen

@Composable
fun AppNavigation(
navController: NavHostController = rememberNavController(),
startDestination: String = AppDestination.Home.destination
) {
NavHost(
navController = navController,
startDestination = startDestination
) {
composable(AppDestination.Home) {
HomeComposeScreen(
navigator = { destination -> navController.navigate(destination) }
)
}

composable(AppDestination.Second) {
SecondScreen(
navigator = { destination -> navController.navigate(destination) },
id = it.arguments?.getString(KEY_ID).orEmpty()
)
}
}
}

private fun NavGraphBuilder.composable(
destination: AppDestination,
deepLinks: List<NavDeepLink> = emptyList(),
content: @Composable (NavBackStackEntry) -> Unit
) {
composable(
route = destination.route,
arguments = destination.arguments,
deepLinks = deepLinks,
content = content
)
}

private fun NavHostController.navigate(destination: AppDestination) {
when (destination) {
is AppDestination.Up -> popBackStack()
else -> navigate(route = destination.destination)
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit a6143ba

Please sign in to comment.