Message Details: Show "me" as display name if there's only a single identity
This commit is contained in:
parent
54bba36fc0
commit
19ac95c9b2
3 changed files with 36 additions and 7 deletions
|
@ -20,5 +20,5 @@ val messageDetailsUiModule = module {
|
||||||
factory { ContactSettingsProvider() }
|
factory { ContactSettingsProvider() }
|
||||||
factory { AddToContactsLauncher() }
|
factory { AddToContactsLauncher() }
|
||||||
factory { ShowContactLauncher() }
|
factory { ShowContactLauncher() }
|
||||||
factory { createMessageDetailsParticipantFormatter(contactNameProvider = get()) }
|
factory { createMessageDetailsParticipantFormatter(contactNameProvider = get(), resources = get()) }
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
package com.fsck.k9.ui.messagedetails
|
package com.fsck.k9.ui.messagedetails
|
||||||
|
|
||||||
|
import android.content.res.Resources
|
||||||
import android.text.Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
|
import android.text.Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
|
||||||
import android.text.SpannableString
|
import android.text.SpannableString
|
||||||
import android.text.style.ForegroundColorSpan
|
import android.text.style.ForegroundColorSpan
|
||||||
import com.fsck.k9.Account
|
import com.fsck.k9.Account
|
||||||
|
import com.fsck.k9.Identity
|
||||||
import com.fsck.k9.K9
|
import com.fsck.k9.K9
|
||||||
import com.fsck.k9.helper.ContactNameProvider
|
import com.fsck.k9.helper.ContactNameProvider
|
||||||
import com.fsck.k9.mail.Address
|
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.
|
* 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 contactNameProvider: ContactNameProvider,
|
||||||
private val showContactNames: Boolean,
|
private val showContactNames: Boolean,
|
||||||
private val contactNameColor: Int?,
|
private val contactNameColor: Int?,
|
||||||
|
private val meText: String,
|
||||||
) : MessageDetailsParticipantFormatter {
|
) : MessageDetailsParticipantFormatter {
|
||||||
override fun getDisplayName(address: Address, account: Account): CharSequence? {
|
override fun getDisplayName(address: Address, account: Account): CharSequence? {
|
||||||
val identityDisplayName = account.findIdentity(address)?.name
|
val identity = account.findIdentity(address)
|
||||||
if (identityDisplayName != null) {
|
if (identity != null) {
|
||||||
return identityDisplayName
|
return getIdentityName(identity, account)
|
||||||
}
|
}
|
||||||
|
|
||||||
return if (showContactNames) {
|
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? {
|
private fun getContactNameOrNull(address: Address): CharSequence? {
|
||||||
val contactName = contactNameProvider.getNameForAddress(address.address) ?: return null
|
val contactName = contactNameProvider.getNameForAddress(address.address) ?: return null
|
||||||
|
|
||||||
|
@ -48,10 +60,12 @@ internal class RealMessageDetailsParticipantFormatter(
|
||||||
|
|
||||||
internal fun createMessageDetailsParticipantFormatter(
|
internal fun createMessageDetailsParticipantFormatter(
|
||||||
contactNameProvider: ContactNameProvider,
|
contactNameProvider: ContactNameProvider,
|
||||||
|
resources: Resources,
|
||||||
): MessageDetailsParticipantFormatter {
|
): MessageDetailsParticipantFormatter {
|
||||||
return RealMessageDetailsParticipantFormatter(
|
return RealMessageDetailsParticipantFormatter(
|
||||||
contactNameProvider = contactNameProvider,
|
contactNameProvider = contactNameProvider,
|
||||||
showContactNames = K9.isShowContactName,
|
showContactNames = K9.isShowContactName,
|
||||||
contactNameColor = if (K9.isChangeContactNameColor) K9.contactNameColor else null,
|
contactNameColor = if (K9.isChangeContactNameColor) K9.contactNameColor else null,
|
||||||
|
meText = resources.getString(R.string.message_view_me_text),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ import org.junit.Test
|
||||||
|
|
||||||
private const val IDENTITY_NAME = "Alice"
|
private const val IDENTITY_NAME = "Alice"
|
||||||
private const val IDENTITY_ADDRESS = "me@domain.example"
|
private const val IDENTITY_ADDRESS = "me@domain.example"
|
||||||
|
private const val ME_TEXT = "me"
|
||||||
|
|
||||||
class MessageDetailsParticipantFormatterTest : RobolectricTest() {
|
class MessageDetailsParticipantFormatterTest : RobolectricTest() {
|
||||||
private val contactNameProvider = object : ContactNameProvider {
|
private val contactNameProvider = object : ContactNameProvider {
|
||||||
|
@ -33,7 +34,19 @@ class MessageDetailsParticipantFormatterTest : RobolectricTest() {
|
||||||
private val participantFormatter = createParticipantFormatter()
|
private val participantFormatter = createParticipantFormatter()
|
||||||
|
|
||||||
@Test
|
@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)
|
val displayName = participantFormatter.getDisplayName(Address(IDENTITY_ADDRESS, "irrelevant"), account)
|
||||||
|
|
||||||
assertThat(displayName).isEqualTo(IDENTITY_NAME)
|
assertThat(displayName).isEqualTo(IDENTITY_NAME)
|
||||||
|
@ -47,18 +60,19 @@ class MessageDetailsParticipantFormatterTest : RobolectricTest() {
|
||||||
|
|
||||||
val displayName = participantFormatter.getDisplayName(Address(IDENTITY_ADDRESS, "Bob"), account)
|
val displayName = participantFormatter.getDisplayName(Address(IDENTITY_ADDRESS, "Bob"), account)
|
||||||
|
|
||||||
assertThat(displayName).isEqualTo("Bob")
|
assertThat(displayName).isEqualTo(ME_TEXT)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `identity and address without a display name`() {
|
fun `identity and address without a display name`() {
|
||||||
val account = Account("uuid").apply {
|
val account = Account("uuid").apply {
|
||||||
identities += Identity(name = null, email = IDENTITY_ADDRESS)
|
identities += Identity(name = null, email = IDENTITY_ADDRESS)
|
||||||
|
identities += Identity(name = "Another identity", email = "irrelevant@domain.example")
|
||||||
}
|
}
|
||||||
|
|
||||||
val displayName = participantFormatter.getDisplayName(Address(IDENTITY_ADDRESS), account)
|
val displayName = participantFormatter.getDisplayName(Address(IDENTITY_ADDRESS), account)
|
||||||
|
|
||||||
assertThat(displayName).isNull()
|
assertThat(displayName).isEqualTo(ME_TEXT)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -118,6 +132,7 @@ class MessageDetailsParticipantFormatterTest : RobolectricTest() {
|
||||||
contactNameProvider = contactNameProvider,
|
contactNameProvider = contactNameProvider,
|
||||||
showContactNames = showContactNames,
|
showContactNames = showContactNames,
|
||||||
contactNameColor = contactNameColor,
|
contactNameColor = contactNameColor,
|
||||||
|
meText = ME_TEXT,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue