Enable stable ids again

now we have stable ids, sorta
This commit is contained in:
Naveen 2022-11-19 06:08:01 +05:30
parent 4295e5cf55
commit 6cc8277694
2 changed files with 23 additions and 0 deletions

View File

@ -67,6 +67,7 @@ class ThreadAdapter(
init {
setupDragListener(true)
setHasStableIds(true)
}
override fun getActionMenuId() = R.menu.cab_thread
@ -142,6 +143,13 @@ class ThreadAdapter(
bindViewHolder(holder)
}
override fun getItemId(position: Int): Long {
return when (val item = getItem(position)) {
is Message -> Message.getStableId(item)
else -> item.hashCode().toLong()
}
}
override fun getItemViewType(position: Int): Int {
return when (val item = getItem(position)) {
is ThreadLoading -> THREAD_LOADING

View File

@ -29,6 +29,20 @@ data class Message(
fun millis() = date * 1000L
companion object {
fun getStableId(message: Message): Long {
var result = message.id.hashCode()
result = 31 * result + message.body.hashCode()
result = 32 * result + message.date.hashCode()
result = 31 * result + message.threadId.hashCode()
result = 31 * result + message.isMMS.hashCode()
result = 31 * result + (message.attachment?.hashCode() ?: 0)
result = 31 * result + message.senderName.hashCode()
result = 31 * result + message.senderPhotoUri.hashCode()
result = 31 * result + message.isScheduled.hashCode()
return result.toLong()
}
fun areItemsTheSame(old: Message, new: Message): Boolean {
return old.id == new.id
}
@ -36,6 +50,7 @@ data class Message(
fun areContentsTheSame(old: Message, new: Message): Boolean {
return old.body == new.body &&
old.threadId == new.threadId &&
old.date == new.date &&
old.isMMS == new.isMMS &&
old.attachment == new.attachment &&
old.senderName == new.senderName &&