Skip to content

Instantly share code, notes, and snippets.

Avatar
🐘
.

Patryk Kosieradzki k0siara

🐘
.
View GitHub Profile
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()
@k0siara
k0siara / NavDestinationsAndActions.kt
Created Aug 8, 2021
NavDestinationsAndActions.kt
View 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 / NavHostNavigationExample.kt
Last active Aug 8, 2021
NavHostNavigationExample.kt
View 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 Aug 8, 2021
NavHostExample.kt
View NavHostExample.kt
@Composable
fun NavHostExample() {
val navController = rememberNavController()
NavHost(navController, startDestination = "firstScreen") {
composable(route = "firstScreen") {
FirstScreen()
}
composable(route = "secondScreen") {
SecondScreen()
}
View SafeUpdateStateFlow.kt
_uiState.update { it.copy(title = "Something") }
View UpdateStateFlowExample.kt
_uiState.value = _uiState.value.copy(title = "Something")