refactor(locale): Move Copied a value to translatable strings

This commit is contained in:
Artem Chepurnoy 2023-12-30 22:19:31 +02:00
parent bca0d1d965
commit daba003b20
No known key found for this signature in database
GPG Key ID: FAC37D0CF674043E
8 changed files with 68 additions and 6 deletions

View File

@ -2,22 +2,49 @@ package com.artemchep.keyguard.common.usecase
import com.artemchep.keyguard.common.model.ToastMessage import com.artemchep.keyguard.common.model.ToastMessage
import com.artemchep.keyguard.common.service.clipboard.ClipboardService import com.artemchep.keyguard.common.service.clipboard.ClipboardService
import com.artemchep.keyguard.feature.navigation.state.TranslatorScope
import com.artemchep.keyguard.res.Res
import dev.icerock.moko.resources.StringResource
/** /**
* @author Artem Chepurnyi * @author Artem Chepurnyi
*/ */
class CopyText( class CopyText(
private val clipboardService: ClipboardService, private val clipboardService: ClipboardService,
private val translator: TranslatorScope,
private val onMessage: (ToastMessage) -> Unit, private val onMessage: (ToastMessage) -> Unit,
) { ) {
enum class Type(
val res: StringResource,
) {
VALUE(Res.strings.copied_value),
URL(Res.strings.copied_url),
URI(Res.strings.copied_uri),
PACKAGE_NAME(Res.strings.copied_package_name),
PASSWORD(Res.strings.copied_password),
USERNAME(Res.strings.copied_username),
EMAIL(Res.strings.copied_email),
PHONE_NUMBER(Res.strings.copied_phone_number),
PASSPORT_NUMBER(Res.strings.copied_passport_number),
LICENSE_NUMBER(Res.strings.copied_license_number),
CARD_NUMBER(Res.strings.copied_card_number),
CARD_CARDHOLDER_NAME(Res.strings.copied_cardholder_name),
CARD_EXP_YEAR(Res.strings.copied_expiration_year),
CARD_EXP_MONTH(Res.strings.copied_expiration_month),
CARD_CVV(Res.strings.copied_cvv_code),
OTP(Res.strings.copied_otp_code),
OTP_SECRET(Res.strings.copied_otp_secret_code),
}
fun copy( fun copy(
text: String, text: String,
hidden: Boolean, hidden: Boolean,
type: Type = Type.VALUE,
) { ) {
clipboardService.setPrimaryClip(text, concealed = hidden) clipboardService.setPrimaryClip(text, concealed = hidden)
if (!clipboardService.hasCopyNotification()) { if (!clipboardService.hasCopyNotification()) {
val message = ToastMessage( val message = ToastMessage(
title = "Copied a value", title = translator.translate(type.res),
text = text.takeUnless { hidden }, text = text.takeUnless { hidden },
) )
onMessage(message) onMessage(message)

View File

@ -785,6 +785,7 @@ private suspend fun FlowCollector<VaultViewItem>.emitEmail(
this += copyText.FlatItemAction( this += copyText.FlatItemAction(
title = scope.translate(Res.strings.copy), title = scope.translate(Res.strings.copy),
value = email, value = email,
type = CopyText.Type.EMAIL,
) )
} }
section { section {

View File

@ -38,6 +38,7 @@ import com.artemchep.keyguard.common.model.isExpensive
import com.artemchep.keyguard.common.service.clipboard.ClipboardService import com.artemchep.keyguard.common.service.clipboard.ClipboardService
import com.artemchep.keyguard.common.service.relays.api.EmailRelay import com.artemchep.keyguard.common.service.relays.api.EmailRelay
import com.artemchep.keyguard.common.usecase.AddGeneratorHistory import com.artemchep.keyguard.common.usecase.AddGeneratorHistory
import com.artemchep.keyguard.common.usecase.CopyText
import com.artemchep.keyguard.common.usecase.GetCanWrite import com.artemchep.keyguard.common.usecase.GetCanWrite
import com.artemchep.keyguard.common.usecase.GetEmailRelays import com.artemchep.keyguard.common.usecase.GetEmailRelays
import com.artemchep.keyguard.common.usecase.GetPassword import com.artemchep.keyguard.common.usecase.GetPassword
@ -1226,6 +1227,7 @@ fun produceGeneratorState(
this += copyItemFactory.FlatItemAction( this += copyItemFactory.FlatItemAction(
title = translate(Res.strings.copy), title = translate(Res.strings.copy),
value = password, value = password,
type = CopyText.Type.PASSWORD,
) )
} }
val actions = kotlin.run { val actions = kotlin.run {
@ -1263,6 +1265,7 @@ fun produceGeneratorState(
copyItemFactory::copy copyItemFactory::copy
.partially1(password) .partially1(password)
.partially1(false) .partially1(false)
.partially1(CopyText.Type.PASSWORD)
} else { } else {
null null
}, },

View File

@ -196,7 +196,11 @@ fun VaultViewTotpBadge2(
.clickable(enabled = state is VaultViewTotpItemBadgeState.Success) { .clickable(enabled = state is VaultViewTotpItemBadgeState.Success) {
val code = (state as? VaultViewTotpItemBadgeState.Success) val code = (state as? VaultViewTotpItemBadgeState.Success)
?.codeRaw ?: return@clickable ?.codeRaw ?: return@clickable
copyText.copy(code, hidden = false) copyText.copy(
text = code,
hidden = false,
type = CopyText.Type.OTP,
)
} }
.padding( .padding(
start = 8.dp, start = 8.dp,

View File

@ -227,7 +227,11 @@ private suspend fun DSecret.createLogin(
getTotpCode(token) getTotpCode(token)
.toIO() .toIO()
.effectTap { code -> .effectTap { code ->
copy.copy(code.code, false) copy.copy(
text = code.code,
hidden = false,
type = CopyText.Type.OTP,
)
} }
.attempt() .attempt()
.launchIn(GlobalScope) .launchIn(GlobalScope)

View File

@ -7,5 +7,6 @@ fun RememberStateFlowScope.copy(
clipboardService: ClipboardService, clipboardService: ClipboardService,
) = CopyText( ) = CopyText(
clipboardService = clipboardService, clipboardService = clipboardService,
translator = this@copy,
onMessage = ::message, onMessage = ::message,
) )

View File

@ -190,6 +190,7 @@ fun CopyText.FlatItemAction(
title: String, title: String,
value: String, value: String,
hidden: Boolean = false, hidden: Boolean = false,
type: CopyText.Type = CopyText.Type.VALUE,
) = FlatItemAction( ) = FlatItemAction(
leading = leading, leading = leading,
icon = Icons.Outlined.ContentCopy, icon = Icons.Outlined.ContentCopy,
@ -197,8 +198,11 @@ fun CopyText.FlatItemAction(
text = value.takeUnless { hidden }, text = value.takeUnless { hidden },
type = FlatItemAction.Type.COPY, type = FlatItemAction.Type.COPY,
onClick = { onClick = {
println("Copying!") copy(
copy(value, hidden) text = value,
hidden = hidden,
type = type,
)
}, },
) )

View File

@ -317,10 +317,28 @@
<string name="copy_cardholder_name">Copy cardholder name</string> <string name="copy_cardholder_name">Copy cardholder name</string>
<string name="copy_expiration_year">Copy expiration year</string> <string name="copy_expiration_year">Copy expiration year</string>
<string name="copy_expiration_month">Copy expiration month</string> <string name="copy_expiration_month">Copy expiration month</string>
<string name="copy_cvv_code">Copy CVV code</string> <string name="copy_cvv_code">Copy security code</string>
<string name="copy_otp_code">Copy one-time password</string> <string name="copy_otp_code">Copy one-time password</string>
<string name="copy_otp_secret_code">Copy secret key</string> <string name="copy_otp_secret_code">Copy secret key</string>
<string name="copied_value">Copied a value</string>
<string name="copied_url">Copied a URL</string>
<string name="copied_uri">Copied a URI</string>
<string name="copied_package_name">Copied a package name</string>
<string name="copied_password">Copied a password</string>
<string name="copied_username">Copied a username</string>
<string name="copied_email">Copied an email</string>
<string name="copied_phone_number">Copied a phone number</string>
<string name="copied_passport_number">Copied a passport number</string>
<string name="copied_license_number">Copied a license number</string>
<string name="copied_card_number">Copied a card number</string>
<string name="copied_cardholder_name">Copied a cardholder name</string>
<string name="copied_expiration_year">Copied an expiration year</string>
<string name="copied_expiration_month">Copied an expiration month</string>
<string name="copied_cvv_code">Copied a security code</string>
<string name="copied_otp_code">Copied an one-time password</string>
<string name="copied_otp_secret_code">Copied a secret key</string>
<string name="autofill_unlock_keyguard">Unlock Keyguard</string> <string name="autofill_unlock_keyguard">Unlock Keyguard</string>
<string name="autofill_open_keyguard">Open Keyguard</string> <string name="autofill_open_keyguard">Open Keyguard</string>