Update CipherView subtitle generation (#730)

This commit is contained in:
Lucas Kivi
2024-01-23 13:33:30 -06:00
committed by Álison Fernandes
parent bc1f5cb020
commit 91df6b5e25
6 changed files with 418 additions and 44 deletions

View File

@@ -0,0 +1,70 @@
package com.x8bit.bitwarden.data.platform.util
import com.bitwarden.core.CardView
import com.bitwarden.core.CipherType
import com.bitwarden.core.CipherView
/**
* If someone has multiple AMEX cards, they tend to have the same last 4 digits. So we provide a
* fifth card number digit to allow users to differentiate between cards based on the subtitle.
*/
private const val AMEX_DIGITS_DISPLAYED: Int = 5
/**
* A normal number of card number digits to show in a subtitle for all non-AMEX cards.
*/
private const val CARD_DIGITS_DISPLAYED: Int = 4
/**
* The subtitle for a [CipherView] used to give extra information about a particular instance.
*/
val CipherView.subtitle: String?
get() = when (type) {
CipherType.LOGIN -> this.login?.username.orEmpty()
CipherType.SECURE_NOTE -> null
CipherType.CARD -> {
this
.card
?.let { cardView ->
when {
cardView.brand.isNullOrBlank() -> cardView.subtitleCardNumber
cardView.subtitleCardNumber.isNullOrBlank() -> cardView.brand
else -> "${cardView.brand}, *${cardView.subtitleCardNumber}"
}
}
}
CipherType.IDENTITY -> {
this
.identity
?.let { identityView ->
when {
identityView.firstName.isNullOrBlank() -> identityView.lastName
identityView.lastName.isNullOrBlank() -> identityView.firstName
else -> "${identityView.firstName} ${identityView.lastName}"
}
}
}
}
/**
* Get the card number as it should be shown in the subtitle.
*/
private val CardView.subtitleCardNumber: String?
get() {
val digitsDisplayedCount = if (this.number.isAmEx) {
AMEX_DIGITS_DISPLAYED
} else {
CARD_DIGITS_DISPLAYED
}
return this
.number
?.takeLast(digitsDisplayedCount)
}
/**
* American express cards start with "34" or "37". This function determine whether a string
* matches that amex standard.
*/
private val String?.isAmEx: Boolean
get() = this?.startsWith("34") == true || this?.startsWith("37") == true

View File

@@ -10,6 +10,7 @@ import com.bitwarden.core.FolderView
import com.bitwarden.core.SendType
import com.bitwarden.core.SendView
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.platform.util.subtitle
import com.x8bit.bitwarden.ui.platform.base.util.asText
import com.x8bit.bitwarden.ui.platform.base.util.removeDiacritics
import com.x8bit.bitwarden.ui.platform.components.model.IconData
@@ -171,26 +172,6 @@ private fun CipherView.toDisplayItem(
overflowOptions = emptyList(),
)
@Suppress("MagicNumber")
private val CipherView.subtitle: String?
get() = when (type) {
CipherType.LOGIN -> login?.username.orEmpty()
CipherType.SECURE_NOTE -> null
CipherType.CARD -> {
card
?.number
?.takeLast(4)
.orEmpty()
}
CipherType.IDENTITY -> {
identity
?.firstName
.orEmpty()
.plus(identity?.lastName.orEmpty())
}
}
private fun CipherView.toIconData(
baseIconUrl: String,
isIconLoadingDisabled: Boolean,

View File

@@ -8,6 +8,7 @@ import com.bitwarden.core.FolderView
import com.bitwarden.core.SendType
import com.bitwarden.core.SendView
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.platform.util.subtitle
import com.x8bit.bitwarden.ui.platform.components.model.IconData
import com.x8bit.bitwarden.ui.platform.util.toFormattedPattern
import com.x8bit.bitwarden.ui.tools.feature.send.util.toLabelIcons
@@ -211,26 +212,6 @@ private fun SendView.toDisplayItem(
overflowOptions = toOverflowActions(baseWebSendUrl = baseWebSendUrl),
)
@Suppress("MagicNumber")
private val CipherView.subtitle: String?
get() = when (type) {
CipherType.LOGIN -> login?.username.orEmpty()
CipherType.SECURE_NOTE -> null
CipherType.CARD -> {
card
?.number
?.takeLast(4)
.orEmpty()
}
CipherType.IDENTITY -> {
identity
?.firstName
.orEmpty()
.plus(identity?.lastName.orEmpty())
}
}
@get:DrawableRes
private val CipherType.iconRes: Int
get() = when (this) {