### ViewModel Argument Injection with Koin Source: https://github.com/kiwicom/navigation-compose-typed/blob/main/readme.md This snippet illustrates how to inject destination arguments into a ViewModel using Koin. It shows the setup for a Koin module and how to obtain a `DemoViewModel` in a `DemoScreen`, passing the `HomeDestinations.Demo` arguments during its creation. ```kotlin val KoinModule = module { viewModelOf(::DemoViewModel) } fun DemoScreen(arguments: HomeDestinations.Demo) { val viewModel = getViewModel { parametersOf(arguments) } } class DemoViewModel( arguments: HomeDestinations.Demo, ) ``` -------------------------------- ### Navigate Using Typed Destinations with NavController Source: https://github.com/kiwicom/navigation-compose-typed/blob/main/readme.md This example shows how to use the `navigate` extension method on a standard `NavController` to navigate between typed destinations. It includes setting up the `NavGraph` within `AppNavHost` and a `Home` composable that accepts a navigation function to trigger navigation to an `Article` destination. ```kotlin import com.kiwi.navigationcompose.typed.Destination import com.kiwi.navigationcompose.typed.navigate @Composable fun AppNavHost() { val navController = rememberNavController() NavGraph( navController = navController, ) { composable { Home(navController::navigate) } } } @Composable private fun Home( onNavigate: (Destination) -> Unit, ) { Home( onArticleClick = { id -> onNavigate(Destinations.Article(id)) }, ) } @Composable private fun Home( onArticleClick: (id: Int) -> Unit, ) { Column { Button(onClick = { onArticleClick(1) }) { Text("...") } Button(onClick = { onArticleClick(2) }) { Text("...") } } } ``` -------------------------------- ### Compose Navigation Graph with Typed Composables Source: https://github.com/kiwicom/navigation-compose-typed/blob/main/readme.md This snippet demonstrates how to define a navigation graph using the `composable` function from the Navigation Compose Typed library. It sets `Destinations.Home` as the start destination and defines composable destinations for `Home` and `Article`, extracting arguments where necessary. ```kotlin import com.kiwi.navigationcompose.typed.composable import com.kiwi.navigationcompose.typed.createRoutePattern NavGraph( startDestination = createRoutePattern(), ) { composable { Home() } composable { // this is Destinations.Article Article(id) } } ``` -------------------------------- ### Define Destinations with Serializable Data Classes Source: https://github.com/kiwicom/navigation-compose-typed/blob/main/readme.md This code defines sealed interface `Destinations` that extends `Destination`. It includes an example of creating serializable data objects `Home` and `Article` with a string ID parameter. These destinations will be used within the navigation graph. ```kotlin import com.kiwi.navigationcompose.typed.Destination sealed interface Destinations : Destination { @Serializable data object Home : Destinations @Serializable data class Article( val id: String, ) : Destinations ``` -------------------------------- ### Add Navigation Compose Typed Dependencies Source: https://github.com/kiwicom/navigation-compose-typed/blob/main/readme.md This snippet shows how to add the necessary dependencies for the Navigation Compose Typed library and KotlinX.Serialization to your project. It includes the Kotlin serialization plugin and the core library dependency, along with the KotlinX.Serialization core library. ```kotlin plugins { id("org.jetbrains.kotlin.plugin.serialization") version "1.8.10" } dependencies { implementation("com.kiwi.navigation-compose.typed:core:") implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.5.0") } ``` -------------------------------- ### Result Sharing with ResultDestination (Kotlin) Source: https://github.com/kiwicom/navigation-compose-typed/blob/main/readme.md This code illustrates how to implement result sharing between navigation destinations using `ResultDestination` and associated effects. It defines a `ResultDestination` with a specific result type and shows how to observe and set results using `DialogResultEffect`, `ComposableResultEffect`, and the `setResult` extension function on `NavController`. Ensure the result class is not sealed to avoid issues. ```kotlin import com.kiwi.navigationcompose.typed.Destination import com.kiwi.navigationcompose.typed.DialogResultEffect import com.kiwi.navigationcompose.typed.ResultDestination import com.kiwi.navigationcompose.typed.setResult sealed interface Destinations : Destination { @Serializable data object Dialog : Destinations, ResultDestination { @Serializable data class Result( val something: Int, ) } } @Composable fun Host(navController: NavController) { DialogResultEffect(navController) { result: Destinations.Dialog.Result -> println(result) // process the result } Button( onClick = { navController.navigate(Destinations.Dialog) }, ) { Text("Open") } } @Composable fun Dialog(navController: NavController) { Button( onClick = { navController.setResult(Destinations.Dialog.Result(something = 42)) navController.popBackStack() } ) { Text("Set and close") } } ``` -------------------------------- ### Extend with Bottom Sheet Integration (Kotlin) Source: https://github.com/kiwicom/navigation-compose-typed/blob/main/readme.md This snippet demonstrates how to integrate custom bottom sheet functionality with the navigation-compose-typed library. It leverages functions like `createRoutePattern`, `createNavArguments`, `decodeArguments`, and `registerDestinationType` to create a reusable bottom sheet navigation pattern. This allows for type-safe navigation to bottom sheet destinations. ```kotlin import com.kiwi.navigationcompose.typed.createRoutePattern import com.kiwi.navigationcompose.typed.createNavArguments import com.kiwi.navigationcompose.typed.decodeArguments import com.kiwi.navigationcompose.typed.Destination import com.kiwi.navigationcompose.typed.registerDestinationType private inline fun NavGraphBuilder.bottomSheet( noinline content: @Composable T.(NavBackStackEntry) -> Unit, ) { val serializer = serializer() registerDestinationType(T::class, serializer) bottomSheet( route = createRoutePattern(serializer), arguments = createNavArguments(serializer), ) { val arguments = decodeArguments(serializer, it) arguments.content(it) } } NavGraph { bottomSheet { Article(id) } } ``` -------------------------------- ### ViewModel Argument Retrieval from SavedStateHandle Source: https://github.com/kiwicom/navigation-compose-typed/blob/main/readme.md This code demonstrates how to retrieve typed destination arguments from a `SavedStateHandle` instance within a ViewModel. It shows a `DemoViewModel` that decodes its arguments using the `decodeArguments` extension function, assuming the arguments conform to `HomeDestinations.Demo`. ```kotlin class DemoViewModel( state: SavedStateHandle, ) : ViewModel() { val arguments = state.decodeArguments() } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.