Refactor adapters

This commit is contained in:
Koitharu
2023-07-06 06:53:23 +03:00
parent 7908eb1441
commit 394479192b
43 changed files with 249 additions and 434 deletions

View File

@@ -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)
}
}
}

View File

@@ -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
}
}
}