Skip to content

Repository files navigation

NavType Parcelable Helper Library for Navigation Component

Simplify passing parcelables and complex strings to the New Navigation library

by Ioannis Anifantakis

The NavHelper library provides utilities to enhance Jetpack Compose navigation with type safety for Parcelable objects and the serialization of strings with special characters using URL encoding and decoding. This library requires Navigation Component version 2.8.0-alpha08 or later.

While the Navigation Component version 2.8.0-alpha08 and later supports type safety out of the box for primitive types, passing Parcelable objects and handling strings with special characters require additional code. The NavHelper library provides this extra code, saving you from writing it manually for each project. It includes: Features

  • Type-safe navigation for Parcelable objects.
  • URL encoding and decoding for strings with special characters.
  • Simplifies code required for passing Parcelables and serialized strings between screens.

Installation

  1. Add this to your dependencies
implementation("com.github.ioannisa:NavTypeParcelableHelperLibrary:1.0.1")
  1. Add Jitpack as a dependencies repository in your settings.gradle (or at Project's build.gradle for older Android projects) in order for this library to be able to download
repositories {
google()
mavenCentral()
maven(url ="https://jitpack.io") // <-- add this line
}

Usage

StringSanitizer

StringSanitizer is a custom serializer for handling strings with special characters, including but not limited to URLs. This ensures that such strings are safely encoded and decoded, preventing issues with special characters in navigation parameters. This encoding can be applied to any string containing special characters, not just URLs, to ensure they do not break during the serialization phase.

Example

importkotlinx.parcelize.Parcelizeimportkotlinx.serialization.Serializableimporteu.anifantakis.navhelper.serialization.StringSanitizer
@Serializable
@Parcelize
data classPerson(
valid:Int,
valsection:Int,
valname:String,
@Serializable(with =StringSanitizer::class)
valimageUrl:String,
@Serializable(with =StringSanitizer::class)
vallandingPage:String
) : Parcelable

NavType.Companion.mapper

NavType.Companion.mapper provides a NavType for Parcelable objects, enabling type-safe navigation in Jetpack Compose. For the Parcelable class you want to pass, you just need to declare it using the typeMap named variable.

Example

importandroid.os.Parcelableimportandroidx.compose.runtime.Composableimportandroidx.lifecycle.viewmodel.compose.viewModelimportandroidx.navigation.NavTypeimportandroidx.navigation.compose.NavHostimportandroidx.navigation.compose.composableimportandroidx.navigation.compose.rememberNavControllerimporteu.anifantakis.navhelper.navtype.mapperimportkotlinx.parcelize.Parcelizeimportkotlinx.serialization.Serializableimportkotlin.reflect.typeOf
@Serializable
@Parcelize
data classPerson(
valid:Int,
valsection:Int,
valname:String,
@Serializable(with =StringSanitizer::class)
valimageUrl:String,
@Serializable(with =StringSanitizer::class)
vallandingPage:String
) : Parcelable
sealedclassNavRoute {
@Serializable
object MainScreen
@Serializable
data classDetailScreen(
valperson:Person
)
}
@Composable
funNavigationRootNew() {
val navController = rememberNavController()
NavHost(navController = navController, startDestination =NavRoute.MainScreen) {
composable<NavRoute.MainScreen> {
val viewModel = viewModel<MainScreenViewModel>()
MainScreen(viewModel, onItemClick = { person ->
navController.navigate(NavRoute.DetailScreen(person))
})
}
composable<NavRoute.DetailScreen>(
typeMap =mapOf(typeOf<Person>() to NavType.mapper<Person>())
) {
val person = it.toRoute<NavRoute.DetailScreen>().person
DetailScreen(person, onBackPress = {
navController.popBackStack()
})
}
}
}

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages