Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
Move refresh functions into delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Jun 25, 2024
1 parent c5d85e7 commit 84c08a6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,4 @@ abstract class BaseRvAdapter<M : IVhModelType> private constructor(
}

override fun getItemCount(): Int = delegate.list.size

override fun refreshItems(items: List<M>) {
delegate.refreshItems(items, ::notifyItemChanged)
}

override fun removeItem(index: Int) {
delegate.removeItem(index, ::notifyItemRemoved)
}

override fun removeItem(item: M) {
delegate.removeItem(item, ::notifyItemRemoved)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ internal interface RecyclerViewAdapter<VH : RecyclerView.ViewHolder> {
}

internal interface IRvAdapterDelegate<M : IVhModelType, VH : BindingViewHolder> :
IRvAdapter<M>,
IMutableRvAdapter<M>,
RecyclerViewAdapter<VH> {

@Suppress("TooManyFunctions")
class Impl<M : IVhModelType, AP> : IRvAdapterDelegate<M, BindingViewHolder>
where AP : IRvAdapter<M>,
AP : IRvBinding<M> {
AP : IRvBinding<M>,
AP : RecyclerView.Adapter<BindingViewHolder> {

private val ivdManager = ViewTypeDelegateManager<M>()
private val _list = mutableListOf<M>()
Expand Down Expand Up @@ -90,37 +91,49 @@ internal interface IRvAdapterDelegate<M : IVhModelType, VH : BindingViewHolder>
adapter.onBindVH(binding, model, payloads)
}

override fun refreshItems(items: List<M>) {
refreshItems(items, adapter::notifyItemChanged)
}

override fun removeItem(index: Int) {
removeItem(index = index, adapter::notifyItemRemoved)
}

override fun removeItem(item: M) {
removeItem(item = item, adapter::notifyItemRemoved)
}

/**
* Transform data list. Always return a new list.
*/
fun transform(original: List<M>): List<M> {
val result = mutableListOf<M>()
original.forEach { findLeaf(it, result) }
return result
}

/**
* Compare the list to find the same items and refresh them.
*/
inline fun refreshItems(items: List<M>, notify: (Int) -> Unit) {
private inline fun refreshItems(items: List<M>, notify: (Int) -> Unit) {
transform(items).forEach {
if (it in _list) {
notify(_list.indexOf(it))
}
}
}

inline fun removeItem(index: Int, notify: (Int) -> Unit) {
private inline fun removeItem(index: Int, notify: (Int) -> Unit) {
_list.removeAt(index)
notify(index)
}

inline fun removeItem(item: M, notify: (Int) -> Unit) {
private inline fun removeItem(item: M, notify: (Int) -> Unit) {
_list.indexOf(item).takeIf { it != -1 }?.let {
removeItem(it, notify)
}
}

/**
* Transform data list. Always return a new list.
*/
fun transform(original: List<M>): List<M> {
val result = mutableListOf<M>()
original.forEach { findLeaf(it, result) }
return result
}

/**
* Recursively traversing all leaf nodes.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,4 @@ abstract class BaseRvDiffAdapter<M : IDiffVhModelType> private constructor(
delegate.list = value
submitList(delegate.transform(value))
}

/**
* Please do not use it with setList() !
*/
override fun refreshItems(items: List<M>) {
delegate.refreshItems(items, ::notifyItemChanged)
}

override fun removeItem(index: Int) {
delegate.removeItem(index, ::notifyItemRemoved)
}

override fun removeItem(item: M) {
delegate.removeItem(item, ::notifyItemRemoved)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ abstract class BaseRvPagingAdapter<M : IDiffVhModelType> private constructor(

var onLoadStatusListener: OnLoadStatusListener? = null

override val list: List<M> get() = snapshot().items
override var list: List<M>
get() = snapshot().items
set(value) = error("You shouldn't call this setter. Use submitData() instead.")

constructor(callback: DiffCallback<M> = DiffCallback()) : this(callback, IRvAdapterDelegate.Impl()) {
@Suppress("LeakingThis")
Expand Down

0 comments on commit 84c08a6

Please sign in to comment.