Refactor adapters
This commit is contained in:
@@ -1,33 +1,14 @@
|
||||
package org.koitharu.kotatsu.widget.shelf.adapter
|
||||
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter
|
||||
import org.koitharu.kotatsu.core.ui.BaseListAdapter
|
||||
import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
|
||||
import org.koitharu.kotatsu.widget.shelf.model.CategoryItem
|
||||
|
||||
class CategorySelectAdapter(
|
||||
clickListener: OnListItemClickListener<CategoryItem>
|
||||
) : AsyncListDifferDelegationAdapter<CategoryItem>(DiffCallback()) {
|
||||
) : BaseListAdapter<CategoryItem>() {
|
||||
|
||||
init {
|
||||
delegatesManager.addDelegate(categorySelectItemAD(clickListener))
|
||||
}
|
||||
|
||||
private class DiffCallback : DiffUtil.ItemCallback<CategoryItem>() {
|
||||
|
||||
override fun areItemsTheSame(oldItem: CategoryItem, newItem: CategoryItem): Boolean {
|
||||
return oldItem.id == newItem.id
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(oldItem: CategoryItem, newItem: CategoryItem): Boolean {
|
||||
return oldItem == newItem
|
||||
}
|
||||
|
||||
override fun getChangePayload(oldItem: CategoryItem, newItem: CategoryItem): Any? {
|
||||
if (oldItem.isSelected != newItem.isSelected) {
|
||||
return newItem.isSelected
|
||||
}
|
||||
return super.getChangePayload(oldItem, newItem)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,23 @@
|
||||
package org.koitharu.kotatsu.widget.shelf.model
|
||||
|
||||
import org.koitharu.kotatsu.list.ui.ListModelDiffCallback
|
||||
import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||
|
||||
data class CategoryItem(
|
||||
val id: Long,
|
||||
val name: String?,
|
||||
val isSelected: Boolean
|
||||
)
|
||||
) : ListModel {
|
||||
|
||||
override fun areItemsTheSame(other: ListModel): Boolean {
|
||||
return other is CategoryItem && other.id == id
|
||||
}
|
||||
|
||||
override fun getChangePayload(previousState: ListModel): Any? {
|
||||
return if (previousState is CategoryItem && previousState.isSelected != isSelected) {
|
||||
ListModelDiffCallback.PAYLOAD_CHECKED_CHANGED
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user