From 5fa993392bea12670e8ccaf5ad7ed426d74c0503 Mon Sep 17 00:00:00 2001 From: cketti Date: Wed, 15 Mar 2023 15:20:35 +0100 Subject: [PATCH] Message Details: Use stable IDs for list items --- .../fsck/k9/ui/messagedetails/EmptyItem.kt | 25 ++++++++++++ .../messagedetails/MessageDetailsFragment.kt | 38 +++++++++++++------ .../main/res/values/message_details_ids.xml | 1 + 3 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 app/ui/legacy/src/main/java/com/fsck/k9/ui/messagedetails/EmptyItem.kt diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagedetails/EmptyItem.kt b/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagedetails/EmptyItem.kt new file mode 100644 index 000000000..30bcea9f6 --- /dev/null +++ b/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagedetails/EmptyItem.kt @@ -0,0 +1,25 @@ +package com.fsck.k9.ui.messagedetails + +import android.content.Context +import android.view.View +import android.view.ViewGroup +import com.fsck.k9.ui.R +import com.mikepenz.fastadapter.FastAdapter +import com.mikepenz.fastadapter.items.AbstractItem + +internal class EmptyItem : AbstractItem() { + override val type: Int = R.id.message_details_empty + override val layoutRes = 0 + + override fun createView(ctx: Context, parent: ViewGroup?): View { + return View(ctx) + } + + override fun getViewHolder(v: View) = ViewHolder(v) + + class ViewHolder(view: View) : FastAdapter.ViewHolder(view) { + override fun bindView(item: EmptyItem, payloads: List) = Unit + + override fun unbindView(item: EmptyItem) = Unit + } +} diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagedetails/MessageDetailsFragment.kt b/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagedetails/MessageDetailsFragment.kt index a6f5d20c5..98672c2b3 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagedetails/MessageDetailsFragment.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagedetails/MessageDetailsFragment.kt @@ -144,9 +144,7 @@ class MessageDetailsFragment : ToolbarBottomSheetDialogFragment() { val items = buildList { add(MessageDateItem(details.date ?: getString(R.string.message_details_missing_date))) - if (details.cryptoDetails != null) { - add(CryptoStatusItem(details.cryptoDetails)) - } + addCryptoStatus(details) addParticipants(details.from, R.string.message_details_from_section_title, appearance) addParticipants(details.sender, R.string.message_details_sender_section_title, appearance) @@ -158,14 +156,26 @@ class MessageDetailsFragment : ToolbarBottomSheetDialogFragment() { addParticipants(details.cc, R.string.message_details_cc_section_title, appearance) addParticipants(details.bcc, R.string.message_details_bcc_section_title, appearance) - if (details.folder != null) { - addFolderName(details.folder) - } + addFolderName(details.folder) + } + + // Use list index as stable identifier. This means changes to the list may only update existing items or add + // new items to the end of the list. Use place holder items (EmptyItem) if necessary. + items.forEachIndexed { index, item -> + item.identifier = index.toLong() } itemAdapter.setNewList(items) } + private fun MutableList.addCryptoStatus(details: MessageDetailsUi) { + if (details.cryptoDetails != null) { + add(CryptoStatusItem(details.cryptoDetails)) + } else { + add(EmptyItem()) + } + } + private fun MutableList.addParticipants( participants: List, @StringRes title: Int, @@ -188,12 +198,16 @@ class MessageDetailsFragment : ToolbarBottomSheetDialogFragment() { } } - private fun MutableList.addFolderName(folder: FolderInfoUi) { - val folderNameItem = FolderNameItem( - displayName = folder.displayName, - iconResourceId = folderIconProvider.getFolderIcon(folder.type), - ) - add(folderNameItem) + private fun MutableList.addFolderName(folder: FolderInfoUi?) { + if (folder != null) { + val folderNameItem = FolderNameItem( + displayName = folder.displayName, + iconResourceId = folderIconProvider.getFolderIcon(folder.type), + ) + add(folderNameItem) + } else { + add(EmptyItem()) + } } private val cryptoStatusClickEventHook = object : ClickEventHook() { diff --git a/app/ui/legacy/src/main/res/values/message_details_ids.xml b/app/ui/legacy/src/main/res/values/message_details_ids.xml index bc2f6e35f..82ef7db6e 100644 --- a/app/ui/legacy/src/main/res/values/message_details_ids.xml +++ b/app/ui/legacy/src/main/res/values/message_details_ids.xml @@ -6,4 +6,5 @@ +