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 / HandleEventsAndStateWithSharedFlowAndStateFlow.kt
Created July 14, 2021 20:49
HandleEventsAndStateWithSharedFlowAndStateFlow
// 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 July 14, 2021 20:18
HandleEventsWithSharedFlowExample
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 / HandleEventsWithSharedFlow.kt
Created July 14, 2021 20:15
HandleEventsWithSharedFlow
// 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()
@k0siara
k0siara / CollectingFlowExample.kt
Created July 14, 2021 19:38
CollectingFlowExample
// 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()
@k0siara
k0siara / FlowObserver.kt
Created July 14, 2021 19:32
FlowObserver
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(
@k0siara
k0siara / CustomListAdapter.kt
Created May 30, 2021 23:15
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])
}
@k0siara
k0siara / RenderItems.kt
Created May 30, 2021 23:14
RenderItems.kt
fun renderItems(newItems: List){
val diffResult: DiffUtil.DiffResult = DiffUtil.calculateDiff(DiffUtilCallback(oldItems, newItems))
diffResult.dispatchUpdatesTo(this) // this : RecyclerView.Adapter
}
@k0siara
k0siara / DiffUtilCallback.kt
Created May 30, 2021 23:14
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 {
@k0siara
k0siara / GetItemCount.kt
Created May 30, 2021 23:13
GetItemCount.kt
@Override
public int getItemCount() {
if (dataset == null)
return 0;
return dataset.size();
}
@k0siara
k0siara / OnBindViewHolder.kt
Created May 30, 2021 23:12
OnBindViewHolder.kt
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (holder is CustomViewHolder) {
// bind data to the views of CustomViewHolder
}
...
}