Message Details: Use stable IDs for list items

This commit is contained in:
cketti 2023-03-15 15:20:35 +01:00
parent 71c8fb11d9
commit 5fa993392b
3 changed files with 52 additions and 12 deletions

View file

@ -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<EmptyItem.ViewHolder>() {
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<EmptyItem>(view) {
override fun bindView(item: EmptyItem, payloads: List<Any>) = Unit
override fun unbindView(item: EmptyItem) = Unit
}
}

View file

@ -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<GenericItem>.addCryptoStatus(details: MessageDetailsUi) {
if (details.cryptoDetails != null) {
add(CryptoStatusItem(details.cryptoDetails))
} else {
add(EmptyItem())
}
}
private fun MutableList<GenericItem>.addParticipants(
participants: List<Participant>,
@StringRes title: Int,
@ -188,12 +198,16 @@ class MessageDetailsFragment : ToolbarBottomSheetDialogFragment() {
}
}
private fun MutableList<GenericItem>.addFolderName(folder: FolderInfoUi) {
val folderNameItem = FolderNameItem(
displayName = folder.displayName,
iconResourceId = folderIconProvider.getFolderIcon(folder.type),
)
add(folderNameItem)
private fun MutableList<GenericItem>.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<CryptoStatusItem>() {

View file

@ -6,4 +6,5 @@
<item type="id" name="message_details_participant"/>
<item type="id" name="message_details_folder_name"/>
<item type="id" name="message_details_divider"/>
<item type="id" name="message_details_empty"/>
</resources>