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 / 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 / AddNavigatorToDI.kt
Created August 8, 2021 15:20
AddNavigatorToDI.kt
val appModule = module {
single<Navigator> {
ComposeCustomNavigator()
}
viewModel {
FirstSceenViewModel(
navigator = get()
)
}
@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 / 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 / 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()
}
_uiState.update { it.copy(title = "Something") }
_uiState.value = _uiState.value.copy(title = "Something")
class ExampleViewModel(
private val initialState: ExampleViewState
) : ViewModel() {
private val _uiState: MutableStateFlow<ExampleViewState> = MutableStateFlow(initialState)
val uiState = _uiState.asStateFlow()
}
data class ExampleViewState(
val title: String = "",
val description: String = ""
@k0siara
k0siara / HandleEventsAndStateWithSharedFlowAndStateFlowExample.kt
Created July 14, 2021 20:53
HandleEventsAndStateWithSharedFlowAndStateFlowExample
class AddEmployeeViewModel(
private val employeeRepository: EmployeeRepository
) :
BaseViewModel<AddEmployeeContract.State, AddEmployeeContract.Event, AddEmployeeContract.Effect>(
initialState = AddEmployeeContract.State.Loading()
) {
...
override fun handleEvent(event: AddEmployeeContract.Event) {