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].