Skip to content

Instantly share code, notes, and snippets.

View k0siara's full-sized avatar
🧐
Investigating a bug

Patryk Kosieradzki k0siara

🧐
Investigating a bug
View GitHub Profile
@k0siara
k0siara / NavHostNavigationExample.kt
Last active August 8, 2021 08:42
NavHostNavigationExample.kt
@Composable
fun NavHostExample() {
val navController = rememberNavController()
NavHost(navController, startDestination = "firstScreen") {
composable(route = "firstScreen") {
FirstScreen(navController)
}
composable(route = "secondScreen") {
SecondScreen()
}
@k0siara
k0siara / NavHostExample.kt
Last active August 8, 2021 08:43
NavHostExample.kt
@Composable
fun NavHostExample() {
val navController = rememberNavController()
NavHost(navController, startDestination = "firstScreen") {
composable(route = "firstScreen") {
FirstScreen()
}
composable(route = "secondScreen") {
SecondScreen()
}
@k0siara
k0siara / NavDestinationsAndActions.kt
Created August 8, 2021 15:09
NavDestinationsAndActions.kt
object NavigationDestinations {
const val firstScreen = "firstScreen"
const val secondScreen = "secondScreen"
const val thirdScreen = "thirdScreen"
}
interface NavigationAction {
val destination: String
val parcelableArguments: Map<String, Parcelable>
get() = emptyMap() // No parcelable arguments as default
@k0siara
k0siara / CustomComposeNavigator.kt
Created August 8, 2021 15:15
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()
@k0siara
k0siara / AddNavigatorToDI.kt
Created August 8, 2021 15:20
AddNavigatorToDI.kt
val appModule = module {
single<Navigator> {
ComposeCustomNavigator()
}
viewModel {
FirstSceenViewModel(
navigator = get()
)
}
@k0siara
k0siara / LifecycleAwareState.kt
Created August 8, 2021 15:44
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 / FirstScreenViewModel.kt
Created August 8, 2021 15:48
FirstScreenViewModel.kt
class FirstScreenViewModel(
private val navigator: Navigator
) : ViewModel {
...
fun navigateExample() {
navigator.navigate(NavigationActions.FirstScreen.firstScreenToSecondScreen())
}
@k0siara
k0siara / NavHostWithCustomNavigatorExample.kt
Last active August 8, 2021 16:11
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
)
@k0siara
k0siara / NavHostExample.kt
Created February 11, 2022 23:08
NavHostExample.kt
NavHost(startDestination = "profile/{userId}") {
...
composable(
"profile/{userId}",
arguments = listOf(navArgument("userId") { type = NavType.StringType })
) {...}
}
@k0siara
k0siara / NavHostExampleExtraction.kt
Created February 11, 2022 23:11
NavHostExampleExtraction.kt
composable(
"profile?userId={userId}",
arguments = listOf(navArgument("userId") { defaultValue = "me" })
) { backStackEntry ->
Profile(backStackEntry.arguments?.getString("userId")) { friendUserId ->
navController.navigate("profile?userId=$friendUserId")
}
}