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

Tweak RvAdapters #409

Merged
merged 8 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,14 @@ abstract class BaseRvAdapter<M : IVhModelType> :
}

override fun refreshItems(items: List<M>) {
helper.refreshItems(items) {
if (it in 0 until itemCount) {
notifyItemChanged(it)
}
}
helper.refreshItems(items, ::notifyItemChanged)
}

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

override fun removeItem(item: M) {
helper.removeItem(item) {
notifyItemRemoved(it)
}
helper.removeItem(item, ::notifyItemRemoved)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import androidx.recyclerview.widget.RecyclerView
*/
internal interface IRvAdapter<M : IVhModelType> {

/**
* Get data list.
*/
val list: List<M>

/**
* What to do when creating the viewHolder for all.
*/
Expand Down Expand Up @@ -59,7 +64,7 @@ internal interface IMutableRvAdapter<M : IVhModelType> : IRvAdapter<M> {
/**
* Set or Get data list.
*/
var list: List<M>
override var list: List<M>

/**
* Refresh some items.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@ import io.goooler.demoapp.adapter.rv.core.ISpanSize.Companion.SPAN_SIZE_FULL
internal class RvAdapterHelper<M : IVhModelType>(private val adapter: IRvAdapter<M>) {

private val ivdManager = ViewTypeDelegateManager<M>()

private val dataList = mutableListOf<M>()
private val _list = mutableListOf<M>()

var list: List<M>
get() = dataList
get() = _list
set(value) {
dataList.run {
clear()
addAll(transform(value))
}
_list.clear()
_list.addAll(transform(value))
}

/**
Expand Down Expand Up @@ -76,8 +73,8 @@ internal class RvAdapterHelper<M : IVhModelType>(private val adapter: IRvAdapter
*/
fun refreshItems(items: List<M>, notify: (Int) -> Unit) {
transform(items).forEach {
if (it in dataList) {
notify(dataList.indexOf(it))
if (it in _list) {
notify(_list.indexOf(it))
}
}
}
Expand All @@ -91,16 +88,14 @@ internal class RvAdapterHelper<M : IVhModelType>(private val adapter: IRvAdapter
return result
}

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import androidx.databinding.ViewDataBinding
*/
class ViewTypeDelegateManager<M : IVhModelType> {

private val mIVDs = SparseArrayCompat<ViewTypeDelegate<ViewDataBinding, M>>()
private val ivDs = SparseArrayCompat<ViewTypeDelegate<ViewDataBinding, M>>()

/**
* When creating viewHolder. if VTD.getViewType() == viewType executes VTD.onCreateVH().
Expand All @@ -25,8 +25,8 @@ class ViewTypeDelegateManager<M : IVhModelType> {
* @param viewType viewType
*/
internal fun onCreateVH(binding: ViewDataBinding, @LayoutRes viewType: Int) {
if (mIVDs.isEmpty) return
mIVDs[viewType]?.onCreateVH(binding)
if (ivDs.isEmpty) return
ivDs[viewType]?.onCreateVH(binding)
}

/**
Expand All @@ -36,8 +36,8 @@ class ViewTypeDelegateManager<M : IVhModelType> {
* @param model model
*/
internal fun onBindVH(binding: ViewDataBinding, model: M) {
if (mIVDs.isEmpty) return
mIVDs[model.viewType]?.onBindVH(binding, model)
if (ivDs.isEmpty) return
ivDs[model.viewType]?.onBindVH(binding, model)
}

/**
Expand All @@ -47,13 +47,13 @@ class ViewTypeDelegateManager<M : IVhModelType> {
*/
@Suppress("UNCHECKED_CAST")
fun <X : ViewDataBinding, Y : M> add(ivd: ViewTypeDelegate<X, Y>) {
mIVDs[ivd.viewType] = ivd as ViewTypeDelegate<ViewDataBinding, M>
ivDs[ivd.viewType] = ivd as ViewTypeDelegate<ViewDataBinding, M>
}

/**
* Eliminate all item's VTD.
*/
fun clear() {
mIVDs.clear()
ivDs.clear()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,14 @@ abstract class BaseRvDiffAdapter<M : IDiffVhModelType> :
* Please do not use it with setList() !
*/
override fun refreshItems(items: List<M>) {
helper.refreshItems(items) {
if (it in 0 until itemCount) {
notifyItemChanged(it)
}
}
helper.refreshItems(items, ::notifyItemChanged)
}

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

override fun removeItem(item: M) {
helper.removeItem(item) {
notifyItemRemoved(it)
}
helper.removeItem(item, ::notifyItemRemoved)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ abstract class BaseRvPagingAdapter<M : IDiffVhModelType>(callback: DiffCallBack<

var onLoadStatusListener: OnLoadStatusListener? = null

override val list: List<M> get() = snapshot().items

override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
helper.onAttachedToRecyclerView(recyclerView)
Expand Down Expand Up @@ -84,22 +86,22 @@ abstract class BaseRvPagingAdapter<M : IDiffVhModelType>(callback: DiffCallBack<
fun onLoadMore() {}

/**
* 没有在下拉刷新或上拉加载
* Not loading
*/
fun onNotLoading()

/**
* 没有更多数据
* No more data
*/
fun onNoMoreData()

/**
* 第一页请求为空
* Empty data
*/
fun onEmpty()

/**
* 暂时没有区分第一页加载失败或是中间页加载失败
* Error occurred
*/
fun onError(t: Throwable)
}
Expand Down