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.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
*/
class CopyText(
private val clipboardService: ClipboardService,
private val translator: TranslatorScope,
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(
text: String,
hidden: Boolean,
type: Type = Type.VALUE,
) {
clipboardService.setPrimaryClip(text, concealed = hidden)
if (!clipboardService.hasCopyNotification()) {
val message = ToastMessage(
title = "Copied a value",
title = translator.translate(type.res),
text = text.takeUnless { hidden },
)
onMessage(message)

View File

@ -785,6 +785,7 @@ private suspend fun FlowCollector<VaultViewItem>.emitEmail(
this += copyText.FlatItemAction(
title = scope.translate(Res.strings.copy),
value = email,
type = CopyText.Type.EMAIL,
)
}
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.relays.api.EmailRelay
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.GetEmailRelays
import com.artemchep.keyguard.common.usecase.GetPassword
@ -1226,6 +1227,7 @@ fun produceGeneratorState(
this += copyItemFactory.FlatItemAction(
title = translate(Res.strings.copy),
value = password,
type = CopyText.Type.PASSWORD,
)
}
val actions = kotlin.run {
@ -1263,6 +1265,7 @@ fun produceGeneratorState(
copyItemFactory::copy
.partially1(password)
.partially1(false)
.partially1(CopyText.Type.PASSWORD)
} else {
null
},

View File

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

View File

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

View File

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

View File

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

View File

@ -317,10 +317,28 @@
<string name="copy_cardholder_name">Copy cardholder name</string>
<string name="copy_expiration_year">Copy expiration year</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_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_open_keyguard">Open Keyguard</string>