Skip to content

Commit 58af240

Browse files
committed
#3468 Switch from RvRenderer to AdapterDelegates - update BaseAdapter to be easier to use
1 parent c7d25e1 commit 58af240

File tree

4 files changed

+44
-22
lines changed

4 files changed

+44
-22
lines changed
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
package fr.free.nrw.commons.explore.depictions
22

3-
import androidx.recyclerview.widget.DiffUtil
4-
import fr.free.nrw.commons.upload.categories.BaseAdapter
3+
import fr.free.nrw.commons.upload.categories.BaseDelegateAdapter
54
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
65

76

8-
class DepictionAdapter(clickListener: (DepictedItem) -> Unit) : BaseAdapter<DepictedItem>(
9-
object : DiffUtil.ItemCallback<DepictedItem>() {
10-
override fun areItemsTheSame(oldItem: DepictedItem, newItem: DepictedItem) =
11-
oldItem.id == newItem.id
12-
13-
override fun areContentsTheSame(oldItem: DepictedItem, newItem: DepictedItem) =
14-
oldItem == newItem
15-
},
16-
depictionDelegate(clickListener)
7+
class DepictionAdapter(clickListener: (DepictedItem) -> Unit) : BaseDelegateAdapter<DepictedItem>(
8+
depictionDelegate(clickListener),
9+
areItemsTheSame = { oldItem, newItem -> oldItem.id == newItem.id }
1710
)
1811

1912

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package fr.free.nrw.commons.upload.categories
2+
3+
import androidx.recyclerview.widget.DiffUtil
4+
import com.hannesdorfmann.adapterdelegates4.AdapterDelegate
5+
import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter
6+
7+
8+
abstract class BaseDelegateAdapter<T>(
9+
vararg delegates: AdapterDelegate<List<T>>,
10+
areItemsTheSame: (T, T) -> Boolean,
11+
areContentsTheSame: (T, T) -> Boolean = { old, new -> old == new }
12+
) : AsyncListDifferDelegationAdapter<T>(
13+
object : DiffUtil.ItemCallback<T>() {
14+
override fun areItemsTheSame(oldItem: T, newItem: T) =
15+
areItemsTheSame(oldItem, newItem)
16+
17+
override fun areContentsTheSame(oldItem: T, newItem: T) =
18+
areContentsTheSame(oldItem, newItem)
19+
},
20+
*delegates
21+
) {
22+
fun indexOf(item: T): Int = items.indexOf(item)
23+
24+
fun getItemAt(position: Int) = items[position]
25+
26+
fun addAll(newResults: List<T>) {
27+
items = (items ?: emptyList<T>()) + newResults
28+
}
29+
30+
fun clear() {
31+
items = emptyList()
32+
}
33+
}
34+
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package fr.free.nrw.commons.upload.categories
22

3-
import androidx.recyclerview.widget.DiffUtil
43
import fr.free.nrw.commons.category.CategoryItem
54

65
class UploadCategoryAdapter(onCategoryClicked: (CategoryItem) -> Unit) :
7-
BaseAdapter<CategoryItem>(
8-
object : DiffUtil.ItemCallback<CategoryItem>() {
9-
override fun areItemsTheSame(oldItem: CategoryItem, newItem: CategoryItem) =
10-
oldItem.name == newItem.name
11-
12-
override fun areContentsTheSame(oldItem: CategoryItem, newItem: CategoryItem) =
13-
oldItem.name == newItem.name && oldItem.isSelected == newItem.isSelected
14-
},
15-
uploadCategoryDelegate(onCategoryClicked)
6+
BaseDelegateAdapter<CategoryItem>(
7+
uploadCategoryDelegate(onCategoryClicked),
8+
areItemsTheSame = { oldItem, newItem -> oldItem.name == newItem.name },
9+
areContentsTheSame = { oldItem, newItem ->
10+
oldItem.name == newItem.name && oldItem.isSelected == newItem.isSelected
11+
}
1612
)
1713

1814

app/src/main/java/fr/free/nrw/commons/upload/categories/UploadCategoryAdapterDelegates.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import fr.free.nrw.commons.R
55
import fr.free.nrw.commons.category.CategoryItem
66
import kotlinx.android.synthetic.main.layout_upload_categories_item.*
77

8-
98
fun uploadCategoryDelegate(onCategoryClicked: (CategoryItem) -> Unit) =
109
adapterDelegateLayoutContainer<CategoryItem, CategoryItem>(R.layout.layout_upload_categories_item) {
1110
containerView.setOnClickListener {

0 commit comments

Comments
 (0)