mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
#3468 Switch from RvRenderer to AdapterDelegates - update BaseAdapter to be easier to use
This commit is contained in:
parent
c7d25e12cc
commit
58af240c9d
4 changed files with 44 additions and 22 deletions
|
|
@ -1,19 +1,12 @@
|
||||||
package fr.free.nrw.commons.explore.depictions
|
package fr.free.nrw.commons.explore.depictions
|
||||||
|
|
||||||
import androidx.recyclerview.widget.DiffUtil
|
import fr.free.nrw.commons.upload.categories.BaseDelegateAdapter
|
||||||
import fr.free.nrw.commons.upload.categories.BaseAdapter
|
|
||||||
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
|
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
|
||||||
|
|
||||||
|
|
||||||
class DepictionAdapter(clickListener: (DepictedItem) -> Unit) : BaseAdapter<DepictedItem>(
|
class DepictionAdapter(clickListener: (DepictedItem) -> Unit) : BaseDelegateAdapter<DepictedItem>(
|
||||||
object : DiffUtil.ItemCallback<DepictedItem>() {
|
depictionDelegate(clickListener),
|
||||||
override fun areItemsTheSame(oldItem: DepictedItem, newItem: DepictedItem) =
|
areItemsTheSame = { oldItem, newItem -> oldItem.id == newItem.id }
|
||||||
oldItem.id == newItem.id
|
|
||||||
|
|
||||||
override fun areContentsTheSame(oldItem: DepictedItem, newItem: DepictedItem) =
|
|
||||||
oldItem == newItem
|
|
||||||
},
|
|
||||||
depictionDelegate(clickListener)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
package fr.free.nrw.commons.upload.categories
|
||||||
|
|
||||||
|
import androidx.recyclerview.widget.DiffUtil
|
||||||
|
import com.hannesdorfmann.adapterdelegates4.AdapterDelegate
|
||||||
|
import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter
|
||||||
|
|
||||||
|
|
||||||
|
abstract class BaseDelegateAdapter<T>(
|
||||||
|
vararg delegates: AdapterDelegate<List<T>>,
|
||||||
|
areItemsTheSame: (T, T) -> Boolean,
|
||||||
|
areContentsTheSame: (T, T) -> Boolean = { old, new -> old == new }
|
||||||
|
) : AsyncListDifferDelegationAdapter<T>(
|
||||||
|
object : DiffUtil.ItemCallback<T>() {
|
||||||
|
override fun areItemsTheSame(oldItem: T, newItem: T) =
|
||||||
|
areItemsTheSame(oldItem, newItem)
|
||||||
|
|
||||||
|
override fun areContentsTheSame(oldItem: T, newItem: T) =
|
||||||
|
areContentsTheSame(oldItem, newItem)
|
||||||
|
},
|
||||||
|
*delegates
|
||||||
|
) {
|
||||||
|
fun indexOf(item: T): Int = items.indexOf(item)
|
||||||
|
|
||||||
|
fun getItemAt(position: Int) = items[position]
|
||||||
|
|
||||||
|
fun addAll(newResults: List<T>) {
|
||||||
|
items = (items ?: emptyList<T>()) + newResults
|
||||||
|
}
|
||||||
|
|
||||||
|
fun clear() {
|
||||||
|
items = emptyList()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -1,18 +1,14 @@
|
||||||
package fr.free.nrw.commons.upload.categories
|
package fr.free.nrw.commons.upload.categories
|
||||||
|
|
||||||
import androidx.recyclerview.widget.DiffUtil
|
|
||||||
import fr.free.nrw.commons.category.CategoryItem
|
import fr.free.nrw.commons.category.CategoryItem
|
||||||
|
|
||||||
class UploadCategoryAdapter(onCategoryClicked: (CategoryItem) -> Unit) :
|
class UploadCategoryAdapter(onCategoryClicked: (CategoryItem) -> Unit) :
|
||||||
BaseAdapter<CategoryItem>(
|
BaseDelegateAdapter<CategoryItem>(
|
||||||
object : DiffUtil.ItemCallback<CategoryItem>() {
|
uploadCategoryDelegate(onCategoryClicked),
|
||||||
override fun areItemsTheSame(oldItem: CategoryItem, newItem: CategoryItem) =
|
areItemsTheSame = { oldItem, newItem -> oldItem.name == newItem.name },
|
||||||
oldItem.name == newItem.name
|
areContentsTheSame = { oldItem, newItem ->
|
||||||
|
oldItem.name == newItem.name && oldItem.isSelected == newItem.isSelected
|
||||||
override fun areContentsTheSame(oldItem: CategoryItem, newItem: CategoryItem) =
|
}
|
||||||
oldItem.name == newItem.name && oldItem.isSelected == newItem.isSelected
|
|
||||||
},
|
|
||||||
uploadCategoryDelegate(onCategoryClicked)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ import fr.free.nrw.commons.R
|
||||||
import fr.free.nrw.commons.category.CategoryItem
|
import fr.free.nrw.commons.category.CategoryItem
|
||||||
import kotlinx.android.synthetic.main.layout_upload_categories_item.*
|
import kotlinx.android.synthetic.main.layout_upload_categories_item.*
|
||||||
|
|
||||||
|
|
||||||
fun uploadCategoryDelegate(onCategoryClicked: (CategoryItem) -> Unit) =
|
fun uploadCategoryDelegate(onCategoryClicked: (CategoryItem) -> Unit) =
|
||||||
adapterDelegateLayoutContainer<CategoryItem, CategoryItem>(R.layout.layout_upload_categories_item) {
|
adapterDelegateLayoutContainer<CategoryItem, CategoryItem>(R.layout.layout_upload_categories_item) {
|
||||||
containerView.setOnClickListener {
|
containerView.setOnClickListener {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue