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

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/trunk' into tweak-a…
Browse files Browse the repository at this point in the history
…dapters-project

# Conflicts:
#	adapter/rv/src/main/kotlin/io/github/goooler/adapter/rv/internal/IRvAdapter.kt
#	adapter/rv/src/main/kotlin/io/github/goooler/adapter/rv/paging/BaseRvPagingAdapter.kt
  • Loading branch information
Goooler committed Jun 26, 2024
2 parents a657684 + e7c3167 commit 6688014
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ internal interface IRvAdapter<M : IVhModelType> : RecyclerViewAdapter<BindingVie

private val ivdManager = ViewTypeDelegateManager<M>()

@Suppress("PropertyName", "VariableNaming", "ktlint:standard:backing-property-naming")
protected val _list = mutableListOf<M>()

lateinit var adapter: AP

override val list: List<M> get() = Collections.unmodifiableList(_list)
/**
* Have to override this property in [adapter], we don't store any list data in this delegate.
*/
override val list: List<M> get() = adapter.list

override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
initManager(ivdManager)
Expand All @@ -84,10 +84,7 @@ internal interface IRvAdapter<M : IVhModelType> : RecyclerViewAdapter<BindingVie
recyclerView.adapter = null
}

override operator fun get(position: Int): M = _list.getOrElse(position) {
// Override get in adapters as a fallback.
adapter[position] ?: error("No such a element in position $position in adapter $adapter.")
}
override operator fun get(position: Int): M = list[position]

@LayoutRes
override fun getItemViewType(position: Int): Int = get(position).viewType
Expand Down Expand Up @@ -182,13 +179,18 @@ internal interface IMutableRvAdapter<M : IVhModelType> : IRvAdapter<M> {
AP : IRvBinding<M>,
AP : RecyclerView.Adapter<BindingViewHolder> {

private val _list = mutableListOf<M>()

override var list: List<M>
get() = super.list
// Copy a new list to avoid the original list being modified.
get() = _list.toMutableList()
set(value) {
_list.clear()
_list.addAll(flat(value))
}

override operator fun get(position: Int): M = _list[position]

override fun addItems(vararg items: Pair<Int, M>) {
items.forEach { (index, item) ->
check(index in _list.indices) { "Index $index out of bounds for length ${_list.size}" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@ public abstract class BaseRvPagingAdapter<M : IDiffVhModelType> private construc
IRvBinding<M>,
IRvAdapter<M> by delegate {

public var onLoadStatusListener: OnLoadStatusListener? = null

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

public constructor(callback: DiffCallback<M> = DiffCallback()) : this(callback, IRvAdapter.Impl()) {
@Suppress("LeakingThis")
delegate.adapter = this
}

public override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
public var onLoadStatusListener: OnLoadStatusListener? = null

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

public override operator fun get(position: Int): M? = getItem(position)

@LayoutRes
public override fun getItemViewType(position: Int): Int = getItem(position)?.viewType ?: 0

override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
delegate.onAttachedToRecyclerView(recyclerView)
addLoadStateListener(loadStateListener)
Expand All @@ -40,11 +45,6 @@ public abstract class BaseRvPagingAdapter<M : IDiffVhModelType> private construc
removeLoadStateListener(loadStateListener)
}

@LayoutRes
public override fun getItemViewType(position: Int): Int = getItem(position)?.viewType ?: 0

public override operator fun get(position: Int): M? = getItem(position)

private val loadStateListener: (CombinedLoadStates) -> Unit = {
when {
it.refresh is LoadState.Loading -> onLoadStatusListener?.onRefresh()
Expand Down

0 comments on commit 6688014

Please sign in to comment.