Message Details: Show "me" as display name if there's only a single identity

This commit is contained in:
cketti 2023-02-17 17:51:13 +01:00
parent 54bba36fc0
commit 19ac95c9b2
3 changed files with 36 additions and 7 deletions

View file

@ -20,5 +20,5 @@ val messageDetailsUiModule = module {
factory { ContactSettingsProvider() }
factory { AddToContactsLauncher() }
factory { ShowContactLauncher() }
factory { createMessageDetailsParticipantFormatter(contactNameProvider = get()) }
factory { createMessageDetailsParticipantFormatter(contactNameProvider = get(), resources = get()) }
}

View file

@ -1,12 +1,15 @@
package com.fsck.k9.ui.messagedetails
import android.content.res.Resources
import android.text.Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import com.fsck.k9.Account
import com.fsck.k9.Identity
import com.fsck.k9.K9
import com.fsck.k9.helper.ContactNameProvider
import com.fsck.k9.mail.Address
import com.fsck.k9.ui.R
/**
* Get the display name for a participant to be shown in the message details screen.
@ -19,11 +22,12 @@ internal class RealMessageDetailsParticipantFormatter(
private val contactNameProvider: ContactNameProvider,
private val showContactNames: Boolean,
private val contactNameColor: Int?,
private val meText: String,
) : MessageDetailsParticipantFormatter {
override fun getDisplayName(address: Address, account: Account): CharSequence? {
val identityDisplayName = account.findIdentity(address)?.name
if (identityDisplayName != null) {
return identityDisplayName
val identity = account.findIdentity(address)
if (identity != null) {
return getIdentityName(identity, account)
}
return if (showContactNames) {
@ -33,6 +37,14 @@ internal class RealMessageDetailsParticipantFormatter(
}
}
private fun getIdentityName(identity: Identity, account: Account): String {
return if (account.identities.size == 1) {
meText
} else {
identity.description ?: identity.name ?: meText
}
}
private fun getContactNameOrNull(address: Address): CharSequence? {
val contactName = contactNameProvider.getNameForAddress(address.address) ?: return null
@ -48,10 +60,12 @@ internal class RealMessageDetailsParticipantFormatter(
internal fun createMessageDetailsParticipantFormatter(
contactNameProvider: ContactNameProvider,
resources: Resources,
): MessageDetailsParticipantFormatter {
return RealMessageDetailsParticipantFormatter(
contactNameProvider = contactNameProvider,
showContactNames = K9.isShowContactName,
contactNameColor = if (K9.isChangeContactNameColor) K9.contactNameColor else null,
meText = resources.getString(R.string.message_view_me_text),
)
}

View file

@ -14,6 +14,7 @@ import org.junit.Test
private const val IDENTITY_NAME = "Alice"
private const val IDENTITY_ADDRESS = "me@domain.example"
private const val ME_TEXT = "me"
class MessageDetailsParticipantFormatterTest : RobolectricTest() {
private val contactNameProvider = object : ContactNameProvider {
@ -33,7 +34,19 @@ class MessageDetailsParticipantFormatterTest : RobolectricTest() {
private val participantFormatter = createParticipantFormatter()
@Test
fun `identity address`() {
fun `identity address with single identity`() {
val displayName = participantFormatter.getDisplayName(Address(IDENTITY_ADDRESS, "irrelevant"), account)
assertThat(displayName).isEqualTo(ME_TEXT)
}
@Test
fun `identity address with multiple identities`() {
val account = Account("uuid").apply {
identities += Identity(name = IDENTITY_NAME, email = IDENTITY_ADDRESS)
identities += Identity(name = "Another identity", email = "irrelevant@domain.example")
}
val displayName = participantFormatter.getDisplayName(Address(IDENTITY_ADDRESS, "irrelevant"), account)
assertThat(displayName).isEqualTo(IDENTITY_NAME)
@ -47,18 +60,19 @@ class MessageDetailsParticipantFormatterTest : RobolectricTest() {
val displayName = participantFormatter.getDisplayName(Address(IDENTITY_ADDRESS, "Bob"), account)
assertThat(displayName).isEqualTo("Bob")
assertThat(displayName).isEqualTo(ME_TEXT)
}
@Test
fun `identity and address without a display name`() {
val account = Account("uuid").apply {
identities += Identity(name = null, email = IDENTITY_ADDRESS)
identities += Identity(name = "Another identity", email = "irrelevant@domain.example")
}
val displayName = participantFormatter.getDisplayName(Address(IDENTITY_ADDRESS), account)
assertThat(displayName).isNull()
assertThat(displayName).isEqualTo(ME_TEXT)
}
@Test
@ -118,6 +132,7 @@ class MessageDetailsParticipantFormatterTest : RobolectricTest() {
contactNameProvider = contactNameProvider,
showContactNames = showContactNames,
contactNameColor = contactNameColor,
meText = ME_TEXT,
)
}
}