Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Patryk Kosieradzki k0siara

🏠
Working from home
View GitHub Profile
@k0siara
k0siara / ExampleNavHostWithCustomNavType.kt
Last active Feb 12, 2022
ExampleNavHostWithCustomNavType.kt
View ExampleNavHostWithCustomNavType.kt
NavHost(...) {
composable("home") {
Home(
onClick = {
val example = Example("1", "Example")
val json = Uri.encode(Gson().toJson(example))
navController.navigate("details/$json")
}
)
}
View CustomNavTypeExample.kt
class ExampleNavType : NavType<Example>(isNullableAllowed = false) {
override fun get(bundle: Bundle, key: String): Example? {
return bundle.getParcelable(key)
}
override fun parseValue(value: String): Example {
return Gson().fromJson(value, Example::class.java)
}
override fun put(bundle: Bundle, key: String, value: Example) {
View ParcelableExample.kt
@Parcelize
data class Example(val id: String, val name: String) : Parcelable
@k0siara
k0siara / NavHostExampleExtraction.kt
Created Feb 11, 2022
NavHostExampleExtraction.kt
View NavHostExampleExtraction.kt
composable(
"profile?userId={userId}",
arguments = listOf(navArgument("userId") { defaultValue = "me" })
) { backStackEntry ->
Profile(backStackEntry.arguments?.getString("userId")) { friendUserId ->
navController.navigate("profile?userId=$friendUserId")
}
}
View NavHostExample.kt
NavHost(startDestination = "profile/{userId}") {
...
composable(
"profile/{userId}",
arguments = listOf(navArgument("userId") { type = NavType.StringType })
) {...}
}
View FirstScreenViewModel.kt
class FirstScreenViewModel(
private val navigator: Navigator
) : ViewModel {
...
fun navigateExample() {
navigator.navigate(NavigationActions.FirstScreen.firstScreenToSecondScreen())
}
View LifecycleAwareState.kt
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.flowWithLifecycle
import kotlinx.coroutines.flow.Flow
@Composable
@k0siara
k0siara / NavHostWithCustomNavigatorExample.kt
Last active Aug 8, 2021
NavHostWithCustomNavigatorExample.kt
View NavHostWithCustomNavigatorExample.kt
@Composable
fun NavHostExample(
navController: NavHostController = rememberNavController(),
navigator: Navigator = get(),
) {
val lifecycleOwner = LocalLifecycleOwner.current
val navigatorState by navigator.navActions.asLifecycleAwareState(
lifecycleOwner = lifecycleOwner,
initialState = null
)
View AddNavigatorToDI.kt
val appModule = module {
single<Navigator> {
ComposeCustomNavigator()
}
viewModel {
FirstSceenViewModel(
navigator = get()
)
}
@k0siara
k0siara / CustomComposeNavigator.kt
Created Aug 8, 2021
CustomComposeNavigator.kt
View CustomComposeNavigator.kt
interface Navigator {
val navActions: StateFlow<NavigationAction?>
fun navigate(navAction: NavigationAction?)
}
class ComposeCustomNavigator : Navigator {
private val _navActions: MutableStateFlow<NavigationAction?> by lazy {
MutableStateFlow(null)
}
override val navActions = _navActions.asStateFlow()