From 36dccc845b1821fccacc93fe1ede85a1b5f0422d Mon Sep 17 00:00:00 2001 From: tzugen Date: Sun, 20 Jun 2021 08:24:14 +0200 Subject: [PATCH] Use ListAdapter class to calculate Diff off the main thread --- .../moire/ultrasonic/domain/GenericEntry.kt | 14 ++++++- .../ultrasonic/fragment/GenericRowAdapter.kt | 41 ++++++++----------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/core/domain/src/main/kotlin/org/moire/ultrasonic/domain/GenericEntry.kt b/core/domain/src/main/kotlin/org/moire/ultrasonic/domain/GenericEntry.kt index 194408e6..37bd863f 100644 --- a/core/domain/src/main/kotlin/org/moire/ultrasonic/domain/GenericEntry.kt +++ b/core/domain/src/main/kotlin/org/moire/ultrasonic/domain/GenericEntry.kt @@ -1,7 +1,19 @@ package org.moire.ultrasonic.domain abstract class GenericEntry { - // TODO Should be non-null! + // TODO: Should be non-null! abstract val id: String? open val name: String? = null + + // These are just a formality and will never be called, + // because Kotlin data classes will have autogenerated equals() and hashCode() functions + override operator fun equals(other: Any?): Boolean { + return this === other + } + + override fun hashCode(): Int { + var result = id?.hashCode() ?: 0 + result = 31 * result + (name?.hashCode() ?: 0) + return result + } } diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/GenericRowAdapter.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/GenericRowAdapter.kt index 9e52e4e8..b825fcac 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/GenericRowAdapter.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/GenericRowAdapter.kt @@ -17,6 +17,7 @@ import android.widget.PopupMenu import android.widget.RelativeLayout import android.widget.TextView import androidx.recyclerview.widget.DiffUtil +import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.RecyclerView import org.moire.ultrasonic.R import org.moire.ultrasonic.data.ActiveServerProvider @@ -31,7 +32,8 @@ abstract class GenericRowAdapter( val onItemClick: (T) -> Unit, val onContextMenuClick: (MenuItem, T) -> Boolean, private val onMusicFolderUpdate: (String?) -> Unit -) : RecyclerView.Adapter() { +) : ListAdapter(GenericDiffCallback()) { + open var itemList: List = listOf() protected abstract val layout: Int protected abstract val contextMenuLayout: Int @@ -46,10 +48,8 @@ abstract class GenericRowAdapter( * using DiffUtil to efficiently calculate the minimum required changes.. */ open fun setData(data: List) { - val callback = DiffUtilCallback(itemList, data) - val result = DiffUtil.calculateDiff(callback) + submitList(data) itemList = data - result.dispatchUpdatesTo(this) } /** @@ -138,29 +138,20 @@ abstract class GenericRowAdapter( var coverArtId: String? = null } - /** - * Calculates the differences between data sets - */ - open class DiffUtilCallback( - private val oldList: List, - private val newList: List - ) : DiffUtil.Callback() { - override fun getOldListSize(): Int { - return oldList.size - } - override fun getNewListSize(): Int { - return newList.size - } - override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { - return oldList[oldItemPosition].id == newList[newItemPosition].id - } - override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { - return oldList[oldItemPosition] == newList[newItemPosition] - } - } - companion object { internal const val TYPE_HEADER = 0 internal const val TYPE_ITEM = 1 + + /** + * Calculates the differences between data sets + */ + class GenericDiffCallback : DiffUtil.ItemCallback() { + override fun areContentsTheSame(oldItem: T, newItem: T): Boolean { + return oldItem == newItem + } + override fun areItemsTheSame(oldItem: T, newItem: T): Boolean { + return oldItem.id == newItem.id + } + } } }