BIT-1215: Improve handling of avatar initials for missing names (#308)

This commit is contained in:
Brian Yencho 2023-12-02 13:03:33 -06:00 committed by Álison Fernandes
parent a4e11af784
commit 79ca73ec00
2 changed files with 27 additions and 9 deletions

View file

@ -4,6 +4,7 @@ import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.ui.platform.components.model.AccountSummary
import java.util.Locale
/**
* Given the [AccountSummary], returns the first two "initials" found when looking at the
@ -12,17 +13,21 @@ import com.x8bit.bitwarden.ui.platform.components.model.AccountSummary
* Ex:
* - "First Last" -> "FL"
* - "First Second Last" -> "FS"
* - `null` -> ".."
* - "First" -> "FI"
* - name is `null`, email is "test@bitwarden.com" -> "TE"
*/
val AccountSummary.initials: String
get() = this
.name
?.let {
it.split(" ")
get() {
val names = this.name.orEmpty().split(" ")
return if (names.size >= 2) {
names
.take(2)
.joinToString(separator = "") { it.first().toString() }
} else {
(this.name ?: this.email).take(2)
}
?: ".."
.uppercase(Locale.getDefault())
}
/**
* Drawable resource to display for the given [AccountSummary].

View file

@ -12,7 +12,7 @@ class AccountSummaryExtensionsTest {
@Suppress("MaxLineLength")
@Test
fun `initials should return the starting letters of the first two words in the name if present`() {
fun `initials should return the starting letters of the first two words for a multi-word name`() {
assertEquals(
"FS",
mockk<AccountSummary>() {
@ -23,11 +23,24 @@ class AccountSummaryExtensionsTest {
}
@Test
fun `initials should return a default value if the name is not present`() {
fun `initials should return the first two letters of the name for a single word name`() {
assertEquals(
"..",
"FI",
mockk<AccountSummary>() {
every { name } returns "First"
}
.initials,
)
}
@Suppress("MaxLineLength")
@Test
fun `initials should return the first two letters of the user's email if the name is not present`() {
assertEquals(
"TE",
mockk<AccountSummary>() {
every { name } returns null
every { email } returns "test@bitwarden.com"
}
.initials,
)