Skip to content

Instantly share code, notes, and snippets.

Avatar
🐘
.

Patryk Kosieradzki k0siara

🐘
.
View GitHub Profile
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()
View CollectingFlowExample.kt
// ViewModel with some Flows to observe and collect
abstract class BaseViewModel<STATE, EVENT : UiEvent, EFFECT : UiEffect>(
initialState: UiState<STATE> = UiState.Loading
) : ViewModel() {
...
private val _uiState: MutableStateFlow<UiState<STATE>> = MutableStateFlow(initialState)
val uiState = _uiState.asStateFlow()
View FlowObserver.kt
class FlowObserver<T>(
lifecycleOwner: LifecycleOwner,
private val flow: Flow<T>,
private val collector: suspend (T) -> Unit
) {
private var job: Job? = null
init {
lifecycleOwner.lifecycle.addObserver(
View CustomListAdapter.kt
class CustomAdapter : ListAdapter<Item, ItemViewHolder>(DiffUtilCallback) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SpotViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
return ItemViewHolder(view)
}
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
holder.bind(currentList[position])
}
View RenderItems.kt
fun renderItems(newItems: List){
val diffResult: DiffUtil.DiffResult = DiffUtil.calculateDiff(DiffUtilCallback(oldItems, newItems))
diffResult.dispatchUpdatesTo(this) // this : RecyclerView.Adapter
}
View DiffUtilCallback.kt
class DiffUtilCallback(
private val oldItems: List,
private val newItems: List
) : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return oldItems[oldItemPosition].id == newItems[newItemPosition].id
}
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {