Last active
May 20, 2022 06:21
-
-
Save GMetaxakis/77c0973070ca67f501da19189846e3b1 to your computer and use it in GitHub Desktop.
Profile & ProfileArgType
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Profile(val firstName:String, val lastName:String){ | |
override fun toString(): String = Uri.encode(Gson().toJson(this)) | |
} | |
class ProfileArgType : JsonNavType<Profile>() { | |
override fun fromJsonParse(value: String): Profile = Gson().fromJson(value, Profile::class.java) | |
override fun FromProfileArguments.getJsonParse(): String = Gson().toJson(this) | |
} | |
//Discaimer : Gson or Moshi or any other library is up to you | |
NavHost(navController = navController, startDestination = "profile"){ | |
/*...*/ | |
composable( | |
route="friendprofile/{profile}", | |
arguments = listOf(navArgument("profile"){ | |
type = ProfileArgType() | |
}) | |
) { navBackStackEntry-> | |
val profile = navBackStackEntry.arguments?.getString("profile")?.let { Gson().fromJson(it, Profile::class.java) } | |
FriendProfile(navController, profile) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment