Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Patryk Kosieradzki k0siara

🏠
Working from home
View GitHub Profile
@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")
View ExampleStateFlowUsageInViewModel.kt
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 Jul 14, 2021
HandleEventsAndStateWithSharedFlowAndStateFlowExample
View HandleEventsAndStateWithSharedFlowAndStateFlowExample.kt
class AddEmployeeViewModel(
private val employeeRepository: EmployeeRepository
) :
BaseViewModel<AddEmployeeContract.State, AddEmployeeContract.Event, AddEmployeeContract.Effect>(
initialState = AddEmployeeContract.State.Loading()
) {
...
override fun handleEvent(event: AddEmployeeContract.Event) {
@k0siara
k0siara / HandleEventsAndStateWithSharedFlowAndStateFlow.kt
Created Jul 14, 2021
HandleEventsAndStateWithSharedFlowAndStateFlow
View HandleEventsAndStateWithSharedFlowAndStateFlow.kt
// ViewModel with SharedFlow and StateFlow
abstract class BaseViewModel<STATE, EVENT : UiEvent, EFFECT : UiEffect>(
initialState: UiState<STATE> = UiState.Loading
) : ViewModel() {
...
// Get Current State
val currentState: UiState<STATE>
@k0siara
k0siara / HandleEventsWithSharedFlowExample.kt
Created Jul 14, 2021
HandleEventsWithSharedFlowExample
View HandleEventsWithSharedFlowExample.kt
class AddEmployeeViewModel(
private val employeeRepository: EmployeeRepository
) :
BaseViewModel<AddEmployeeContract.State, AddEmployeeContract.Event, AddEmployeeContract.Effect>(
initialState = AddEmployeeContract.State.Loading()
) {
...
override fun handleEvent(event: AddEmployeeContract.Event) {
View HandleEventsWithSharedFlow.kt
// ViewModel with SharedFlow
abstract class BaseViewModel<STATE, EVENT : UiEvent, EFFECT : UiEffect>(
initialState: UiState<STATE> = UiState.Loading
) : ViewModel() {
...
private val _event: MutableSharedFlow<EVENT> = MutableSharedFlow()
val event = _event.asSharedFlow()