This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun renderItems(newItems: List){ | |
val diffResult: DiffUtil.DiffResult = DiffUtil.calculateDiff(DiffUtilCallback(oldItems, newItems)) | |
diffResult.dispatchUpdatesTo(this) // this : RecyclerView.Adapter | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public int getItemCount() { | |
if (dataset == null) | |
return 0; | |
return dataset.size(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { | |
if (holder is CustomViewHolder) { | |
// bind data to the views of CustomViewHolder | |
} | |
... | |
} |