Refactor adapters
parent
7908eb1441
commit
394479192b
@ -1,32 +1,15 @@
|
|||||||
package org.koitharu.kotatsu.bookmarks.ui.adapter
|
package org.koitharu.kotatsu.bookmarks.ui.adapter
|
||||||
|
|
||||||
import androidx.lifecycle.LifecycleOwner
|
import androidx.lifecycle.LifecycleOwner
|
||||||
import androidx.recyclerview.widget.DiffUtil
|
|
||||||
import coil.ImageLoader
|
import coil.ImageLoader
|
||||||
import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter
|
|
||||||
import org.koitharu.kotatsu.bookmarks.domain.Bookmark
|
import org.koitharu.kotatsu.bookmarks.domain.Bookmark
|
||||||
|
import org.koitharu.kotatsu.core.ui.BaseListAdapter
|
||||||
import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
|
import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
|
||||||
|
|
||||||
class BookmarksAdapter(
|
class BookmarksAdapter(
|
||||||
coil: ImageLoader,
|
coil: ImageLoader,
|
||||||
lifecycleOwner: LifecycleOwner,
|
lifecycleOwner: LifecycleOwner,
|
||||||
clickListener: OnListItemClickListener<Bookmark>,
|
clickListener: OnListItemClickListener<Bookmark>,
|
||||||
) : AsyncListDifferDelegationAdapter<Bookmark>(
|
) : BaseListAdapter<Bookmark>(
|
||||||
DiffCallback(),
|
|
||||||
bookmarkListAD(coil, lifecycleOwner, clickListener),
|
bookmarkListAD(coil, lifecycleOwner, clickListener),
|
||||||
) {
|
)
|
||||||
|
|
||||||
private class DiffCallback : DiffUtil.ItemCallback<Bookmark>() {
|
|
||||||
|
|
||||||
override fun areItemsTheSame(oldItem: Bookmark, newItem: Bookmark): Boolean {
|
|
||||||
return oldItem.manga.id == newItem.manga.id &&
|
|
||||||
oldItem.chapterId == newItem.chapterId &&
|
|
||||||
oldItem.page == newItem.page
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun areContentsTheSame(oldItem: Bookmark, newItem: Bookmark): Boolean {
|
|
||||||
return oldItem.imageUrl == newItem.imageUrl
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,44 +1,16 @@
|
|||||||
package org.koitharu.kotatsu.favourites.ui.categories.select.adapter
|
package org.koitharu.kotatsu.favourites.ui.categories.select.adapter
|
||||||
|
|
||||||
import androidx.recyclerview.widget.DiffUtil
|
import org.koitharu.kotatsu.core.ui.BaseListAdapter
|
||||||
import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter
|
|
||||||
import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
|
import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
|
||||||
import org.koitharu.kotatsu.favourites.ui.categories.select.model.CategoriesHeaderItem
|
|
||||||
import org.koitharu.kotatsu.favourites.ui.categories.select.model.MangaCategoryItem
|
import org.koitharu.kotatsu.favourites.ui.categories.select.model.MangaCategoryItem
|
||||||
import org.koitharu.kotatsu.list.ui.model.ListModel
|
import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||||
|
|
||||||
class MangaCategoriesAdapter(
|
class MangaCategoriesAdapter(
|
||||||
clickListener: OnListItemClickListener<MangaCategoryItem>,
|
clickListener: OnListItemClickListener<MangaCategoryItem>,
|
||||||
) : AsyncListDifferDelegationAdapter<ListModel>(DiffCallback()) {
|
) : BaseListAdapter<ListModel>() {
|
||||||
|
|
||||||
init {
|
init {
|
||||||
delegatesManager.addDelegate(mangaCategoryAD(clickListener))
|
delegatesManager.addDelegate(mangaCategoryAD(clickListener))
|
||||||
.addDelegate(categoriesHeaderAD())
|
.addDelegate(categoriesHeaderAD())
|
||||||
}
|
}
|
||||||
|
|
||||||
private class DiffCallback : DiffUtil.ItemCallback<ListModel>() {
|
|
||||||
override fun areItemsTheSame(
|
|
||||||
oldItem: ListModel,
|
|
||||||
newItem: ListModel,
|
|
||||||
): Boolean = when {
|
|
||||||
oldItem is MangaCategoryItem && newItem is MangaCategoryItem -> oldItem.id == newItem.id
|
|
||||||
oldItem is CategoriesHeaderItem && newItem is CategoriesHeaderItem -> oldItem == newItem
|
|
||||||
else -> false
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun areContentsTheSame(
|
|
||||||
oldItem: ListModel,
|
|
||||||
newItem: ListModel,
|
|
||||||
): Boolean = oldItem == newItem
|
|
||||||
|
|
||||||
override fun getChangePayload(
|
|
||||||
oldItem: ListModel,
|
|
||||||
newItem: ListModel,
|
|
||||||
): Any? {
|
|
||||||
if (oldItem is MangaCategoryItem && newItem is MangaCategoryItem && oldItem.isChecked != newItem.isChecked) {
|
|
||||||
return newItem.isChecked
|
|
||||||
}
|
|
||||||
return super.getChangePayload(oldItem, newItem)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,52 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.filter.ui
|
|
||||||
|
|
||||||
import androidx.recyclerview.widget.DiffUtil
|
|
||||||
import org.koitharu.kotatsu.filter.ui.model.FilterItem
|
|
||||||
import org.koitharu.kotatsu.list.ui.model.ListHeader
|
|
||||||
import org.koitharu.kotatsu.list.ui.model.ListModel
|
|
||||||
|
|
||||||
class FilterDiffCallback : DiffUtil.ItemCallback<ListModel>() {
|
|
||||||
|
|
||||||
override fun areItemsTheSame(oldItem: ListModel, newItem: ListModel): Boolean {
|
|
||||||
return when {
|
|
||||||
oldItem === newItem -> true
|
|
||||||
oldItem.javaClass != newItem.javaClass -> false
|
|
||||||
oldItem is ListHeader && newItem is ListHeader -> {
|
|
||||||
oldItem == newItem
|
|
||||||
}
|
|
||||||
|
|
||||||
oldItem is FilterItem.Tag && newItem is FilterItem.Tag -> {
|
|
||||||
oldItem.tag == newItem.tag
|
|
||||||
}
|
|
||||||
|
|
||||||
oldItem is FilterItem.Sort && newItem is FilterItem.Sort -> {
|
|
||||||
oldItem.order == newItem.order
|
|
||||||
}
|
|
||||||
|
|
||||||
oldItem is FilterItem.Error && newItem is FilterItem.Error -> {
|
|
||||||
oldItem.textResId == newItem.textResId
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun areContentsTheSame(oldItem: ListModel, newItem: ListModel): Boolean {
|
|
||||||
return oldItem == newItem
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getChangePayload(oldItem: ListModel, newItem: ListModel): Any? {
|
|
||||||
val hasPayload = when {
|
|
||||||
oldItem is FilterItem.Tag && newItem is FilterItem.Tag -> {
|
|
||||||
oldItem.isChecked != newItem.isChecked
|
|
||||||
}
|
|
||||||
|
|
||||||
oldItem is FilterItem.Sort && newItem is FilterItem.Sort -> {
|
|
||||||
oldItem.isSelected != newItem.isSelected
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> false
|
|
||||||
}
|
|
||||||
return if (hasPayload) Unit else super.getChangePayload(oldItem, newItem)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,48 +1,22 @@
|
|||||||
package org.koitharu.kotatsu.scrobbling.common.ui.config.adapter
|
package org.koitharu.kotatsu.scrobbling.common.ui.config.adapter
|
||||||
|
|
||||||
import androidx.lifecycle.LifecycleOwner
|
import androidx.lifecycle.LifecycleOwner
|
||||||
import androidx.recyclerview.widget.DiffUtil
|
|
||||||
import coil.ImageLoader
|
import coil.ImageLoader
|
||||||
import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter
|
import org.koitharu.kotatsu.core.ui.BaseListAdapter
|
||||||
import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
|
import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
|
||||||
import org.koitharu.kotatsu.list.ui.adapter.emptyStateListAD
|
import org.koitharu.kotatsu.list.ui.adapter.emptyStateListAD
|
||||||
import org.koitharu.kotatsu.list.ui.model.EmptyState
|
|
||||||
import org.koitharu.kotatsu.list.ui.model.ListModel
|
import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||||
import org.koitharu.kotatsu.scrobbling.common.domain.model.ScrobblingInfo
|
import org.koitharu.kotatsu.scrobbling.common.domain.model.ScrobblingInfo
|
||||||
import org.koitharu.kotatsu.scrobbling.common.domain.model.ScrobblingStatus
|
|
||||||
|
|
||||||
class ScrobblingMangaAdapter(
|
class ScrobblingMangaAdapter(
|
||||||
clickListener: OnListItemClickListener<ScrobblingInfo>,
|
clickListener: OnListItemClickListener<ScrobblingInfo>,
|
||||||
coil: ImageLoader,
|
coil: ImageLoader,
|
||||||
lifecycleOwner: LifecycleOwner,
|
lifecycleOwner: LifecycleOwner,
|
||||||
) : AsyncListDifferDelegationAdapter<ListModel>(DiffCallback()) {
|
) : BaseListAdapter<ListModel>() {
|
||||||
|
|
||||||
init {
|
init {
|
||||||
delegatesManager.addDelegate(scrobblingMangaAD(clickListener, coil, lifecycleOwner))
|
delegatesManager.addDelegate(scrobblingMangaAD(clickListener, coil, lifecycleOwner))
|
||||||
.addDelegate(scrobblingHeaderAD())
|
.addDelegate(scrobblingHeaderAD())
|
||||||
.addDelegate(emptyStateListAD(coil, lifecycleOwner, null))
|
.addDelegate(emptyStateListAD(coil, lifecycleOwner, null))
|
||||||
}
|
}
|
||||||
|
|
||||||
private class DiffCallback : DiffUtil.ItemCallback<ListModel>() {
|
|
||||||
|
|
||||||
override fun areItemsTheSame(oldItem: ListModel, newItem: ListModel): Boolean {
|
|
||||||
return when {
|
|
||||||
oldItem is ScrobblingInfo && newItem is ScrobblingInfo -> {
|
|
||||||
oldItem.targetId == newItem.targetId && oldItem.mangaId == newItem.mangaId
|
|
||||||
}
|
|
||||||
|
|
||||||
oldItem is ScrobblingStatus && newItem is ScrobblingStatus -> {
|
|
||||||
oldItem.ordinal == newItem.ordinal
|
|
||||||
}
|
|
||||||
|
|
||||||
oldItem is EmptyState && newItem is EmptyState -> true
|
|
||||||
|
|
||||||
else -> false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun areContentsTheSame(oldItem: ListModel, newItem: ListModel): Boolean {
|
|
||||||
return oldItem == newItem
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,27 +1,13 @@
|
|||||||
package org.koitharu.kotatsu.settings.onboard.adapter
|
package org.koitharu.kotatsu.settings.onboard.adapter
|
||||||
|
|
||||||
import androidx.recyclerview.widget.DiffUtil
|
import org.koitharu.kotatsu.core.ui.BaseListAdapter
|
||||||
import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter
|
|
||||||
import org.koitharu.kotatsu.settings.onboard.model.SourceLocale
|
import org.koitharu.kotatsu.settings.onboard.model.SourceLocale
|
||||||
|
|
||||||
class SourceLocalesAdapter(
|
class SourceLocalesAdapter(
|
||||||
listener: SourceLocaleListener,
|
listener: SourceLocaleListener,
|
||||||
) : AsyncListDifferDelegationAdapter<SourceLocale>(DiffCallback()) {
|
) : BaseListAdapter<SourceLocale>() {
|
||||||
|
|
||||||
init {
|
init {
|
||||||
delegatesManager.addDelegate(sourceLocaleAD(listener))
|
delegatesManager.addDelegate(sourceLocaleAD(listener))
|
||||||
}
|
}
|
||||||
|
|
||||||
private class DiffCallback : DiffUtil.ItemCallback<SourceLocale>() {
|
|
||||||
|
|
||||||
override fun areItemsTheSame(
|
|
||||||
oldItem: SourceLocale,
|
|
||||||
newItem: SourceLocale,
|
|
||||||
): Boolean = oldItem.key == newItem.key
|
|
||||||
|
|
||||||
override fun areContentsTheSame(
|
|
||||||
oldItem: SourceLocale,
|
|
||||||
newItem: SourceLocale,
|
|
||||||
): Boolean = oldItem == newItem
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,44 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.settings.sources.adapter
|
|
||||||
|
|
||||||
import androidx.recyclerview.widget.DiffUtil
|
|
||||||
import org.koitharu.kotatsu.settings.sources.model.SourceConfigItem
|
|
||||||
import org.koitharu.kotatsu.settings.sources.model.SourceConfigItem.EmptySearchResult
|
|
||||||
import org.koitharu.kotatsu.settings.sources.model.SourceConfigItem.Header
|
|
||||||
import org.koitharu.kotatsu.settings.sources.model.SourceConfigItem.LocaleGroup
|
|
||||||
import org.koitharu.kotatsu.settings.sources.model.SourceConfigItem.SourceItem
|
|
||||||
|
|
||||||
class SourceConfigDiffCallback : DiffUtil.ItemCallback<SourceConfigItem>() {
|
|
||||||
|
|
||||||
override fun areItemsTheSame(oldItem: SourceConfigItem, newItem: SourceConfigItem): Boolean {
|
|
||||||
return when {
|
|
||||||
oldItem.javaClass != newItem.javaClass -> false
|
|
||||||
oldItem is LocaleGroup && newItem is LocaleGroup -> {
|
|
||||||
oldItem.localeId == newItem.localeId
|
|
||||||
}
|
|
||||||
|
|
||||||
oldItem is SourceItem && newItem is SourceItem -> {
|
|
||||||
oldItem.source == newItem.source
|
|
||||||
}
|
|
||||||
|
|
||||||
oldItem is Header && newItem is Header -> {
|
|
||||||
oldItem.titleResId == newItem.titleResId
|
|
||||||
}
|
|
||||||
|
|
||||||
oldItem == EmptySearchResult && newItem == EmptySearchResult -> {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
oldItem is SourceConfigItem.Tip && newItem is SourceConfigItem.Tip -> {
|
|
||||||
oldItem.key == newItem.key
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun areContentsTheSame(oldItem: SourceConfigItem, newItem: SourceConfigItem): Boolean {
|
|
||||||
return oldItem == newItem
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getChangePayload(oldItem: SourceConfigItem, newItem: SourceConfigItem) = Unit
|
|
||||||
}
|
|
||||||
@ -1,32 +1,11 @@
|
|||||||
package org.koitharu.kotatsu.settings.tracker.categories
|
package org.koitharu.kotatsu.settings.tracker.categories
|
||||||
|
|
||||||
import androidx.recyclerview.widget.DiffUtil
|
|
||||||
import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter
|
|
||||||
import org.koitharu.kotatsu.core.model.FavouriteCategory
|
import org.koitharu.kotatsu.core.model.FavouriteCategory
|
||||||
|
import org.koitharu.kotatsu.core.ui.BaseListAdapter
|
||||||
import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
|
import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
|
||||||
|
|
||||||
class TrackerCategoriesConfigAdapter(
|
class TrackerCategoriesConfigAdapter(
|
||||||
listener: OnListItemClickListener<FavouriteCategory>,
|
listener: OnListItemClickListener<FavouriteCategory>,
|
||||||
) : AsyncListDifferDelegationAdapter<FavouriteCategory>(DiffCallback()) {
|
) : BaseListAdapter<FavouriteCategory>(
|
||||||
|
trackerCategoryAD(listener),
|
||||||
init {
|
)
|
||||||
delegatesManager.addDelegate(trackerCategoryAD(listener))
|
|
||||||
}
|
|
||||||
|
|
||||||
class DiffCallback : DiffUtil.ItemCallback<FavouriteCategory>() {
|
|
||||||
|
|
||||||
override fun areItemsTheSame(oldItem: FavouriteCategory, newItem: FavouriteCategory): Boolean {
|
|
||||||
return oldItem.id == newItem.id
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun areContentsTheSame(oldItem: FavouriteCategory, newItem: FavouriteCategory): Boolean {
|
|
||||||
return oldItem.isTrackingEnabled == newItem.isTrackingEnabled && oldItem.title == newItem.title
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getChangePayload(oldItem: FavouriteCategory, newItem: FavouriteCategory): Any? {
|
|
||||||
return if (oldItem.isTrackingEnabled == newItem.isTrackingEnabled) {
|
|
||||||
super.getChangePayload(oldItem, newItem)
|
|
||||||
} else Unit
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,33 +1,14 @@
|
|||||||
package org.koitharu.kotatsu.widget.shelf.adapter
|
package org.koitharu.kotatsu.widget.shelf.adapter
|
||||||
|
|
||||||
import androidx.recyclerview.widget.DiffUtil
|
import org.koitharu.kotatsu.core.ui.BaseListAdapter
|
||||||
import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter
|
|
||||||
import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
|
import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
|
||||||
import org.koitharu.kotatsu.widget.shelf.model.CategoryItem
|
import org.koitharu.kotatsu.widget.shelf.model.CategoryItem
|
||||||
|
|
||||||
class CategorySelectAdapter(
|
class CategorySelectAdapter(
|
||||||
clickListener: OnListItemClickListener<CategoryItem>
|
clickListener: OnListItemClickListener<CategoryItem>
|
||||||
) : AsyncListDifferDelegationAdapter<CategoryItem>(DiffCallback()) {
|
) : BaseListAdapter<CategoryItem>() {
|
||||||
|
|
||||||
init {
|
init {
|
||||||
delegatesManager.addDelegate(categorySelectItemAD(clickListener))
|
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
|
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(
|
data class CategoryItem(
|
||||||
val id: Long,
|
val id: Long,
|
||||||
val name: String?,
|
val name: String?,
|
||||||
val isSelected: Boolean
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue