From ce8ae2538a3afe831ccf37b5a30af1f4da5bf31b Mon Sep 17 00:00:00 2001 From: Tangem Date: Thu, 30 Jan 2025 16:14:52 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../impl/DefaultRenameWalletComponent.kt | 35 ++++++++++++------- .../preview/PreviewRenameWalletComponent.kt | 3 +- .../walletsettings/entity/RenameWalletUM.kt | 4 ++- .../walletsettings/ui/RenameWalletDialog.kt | 5 ++- .../wallet/state/model/WalletAlertState.kt | 1 + 5 files changed, 33 insertions(+), 15 deletions(-) diff --git a/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/component/impl/DefaultRenameWalletComponent.kt b/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/component/impl/DefaultRenameWalletComponent.kt index 902d26f1ef..d079febfc3 100644 --- a/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/component/impl/DefaultRenameWalletComponent.kt +++ b/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/component/impl/DefaultRenameWalletComponent.kt @@ -10,6 +10,7 @@ import com.tangem.core.ui.extensions.wrappedList import com.tangem.core.ui.message.SnackbarMessage import com.tangem.domain.wallets.models.UpdateWalletError import com.tangem.domain.wallets.models.UserWalletId +import com.tangem.domain.wallets.usecase.GetWalletNamesUseCase import com.tangem.domain.wallets.usecase.RenameWalletUseCase import com.tangem.feature.walletsettings.component.RenameWalletComponent import com.tangem.feature.walletsettings.entity.RenameWalletUM @@ -26,17 +27,20 @@ import timber.log.Timber internal class DefaultRenameWalletComponent @AssistedInject constructor( @Assisted context: AppComponentContext, @Assisted private val params: RenameWalletComponent.Params, + getWalletNamesUseCase: GetWalletNamesUseCase, private val renameWalletUseCase: RenameWalletUseCase, ) : RenameWalletComponent, AppComponentContext by context { - private val currentWalletName = params.currentName + private val initialName = params.currentName + private val walletNames by lazy(getWalletNamesUseCase::invoke) private val stateFlow: MutableStateFlow = MutableStateFlow( value = RenameWalletUM( walletNameValue = TextFieldValue(text = params.currentName), - updateValue = ::updateValue, + onValueChange = ::updateValue, isConfirmEnabled = false, onConfirm = { renameWallet(params.userWalletId) }, + error = null, ), ) @@ -48,24 +52,31 @@ internal class DefaultRenameWalletComponent @AssistedInject constructor( override fun Dialog() { val model by stateFlow.collectAsStateWithLifecycle() - RenameWalletDialog( - model = model, - onDismiss = ::dismiss, - ) + RenameWalletDialog(model = model, onDismiss = ::dismiss) } private fun updateValue(value: TextFieldValue) { stateFlow.update { + val isNameAlreadyExists = initialName != value.text && walletNames.contains(value.text) + it.copy( walletNameValue = value, - isConfirmEnabled = value.text.isNotBlank() && value.text != currentWalletName, + isConfirmEnabled = value.text.isNotBlank() && + value.text != initialName && + !isNameAlreadyExists, + error = if (isNameAlreadyExists) { + resourceReference( + id = R.string.user_wallet_list_rename_popup_error_already_exists, + formatArgs = wrappedList(value.text), + ) + } else { + null + }, ) } } private fun renameWallet(userWalletId: UserWalletId) = componentScope.launch { - stateFlow.update { it.copy(isConfirmEnabled = false) } - val newName = stateFlow.value.walletNameValue val maybeError = renameWalletUseCase(userWalletId, newName.text).leftOrNull() @@ -76,14 +87,14 @@ internal class DefaultRenameWalletComponent @AssistedInject constructor( is UpdateWalletError.DataError -> resourceReference(id = R.string.common_unknown_error) is UpdateWalletError.NameAlreadyExists -> resourceReference( id = R.string.user_wallet_list_rename_popup_error_already_exists, - formatArgs = wrappedList(newName), + formatArgs = wrappedList(newName.text), ) } messageSender.send(message = SnackbarMessage(message)) + } else { + dismiss() } - - dismiss() } @AssistedFactory diff --git a/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/component/preview/PreviewRenameWalletComponent.kt b/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/component/preview/PreviewRenameWalletComponent.kt index fbcb3d6389..12834f5b0d 100644 --- a/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/component/preview/PreviewRenameWalletComponent.kt +++ b/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/component/preview/PreviewRenameWalletComponent.kt @@ -11,8 +11,9 @@ internal class PreviewRenameWalletComponent : RenameWalletComponent { private val previewState = RenameWalletUM( walletNameValue = TextFieldValue(text = "My Wallet"), isConfirmEnabled = false, - updateValue = {}, + onValueChange = {}, onConfirm = {}, + error = null, ) override fun dismiss() {} diff --git a/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/entity/RenameWalletUM.kt b/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/entity/RenameWalletUM.kt index 730cc1ee60..23549c9809 100644 --- a/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/entity/RenameWalletUM.kt +++ b/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/entity/RenameWalletUM.kt @@ -2,11 +2,13 @@ package com.tangem.feature.walletsettings.entity import androidx.compose.runtime.Immutable import androidx.compose.ui.text.input.TextFieldValue +import com.tangem.core.ui.extensions.TextReference @Immutable internal data class RenameWalletUM( val walletNameValue: TextFieldValue, - val updateValue: (value: TextFieldValue) -> Unit, + val onValueChange: (value: TextFieldValue) -> Unit, val isConfirmEnabled: Boolean, val onConfirm: () -> Unit, + val error: TextReference?, ) \ No newline at end of file diff --git a/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/ui/RenameWalletDialog.kt b/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/ui/RenameWalletDialog.kt index 0e6313e933..0d32c07b9e 100644 --- a/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/ui/RenameWalletDialog.kt +++ b/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/ui/RenameWalletDialog.kt @@ -8,6 +8,7 @@ import androidx.compose.ui.tooling.preview.Preview import com.tangem.core.ui.components.AdditionalTextInputDialogUM import com.tangem.core.ui.components.DialogButtonUM import com.tangem.core.ui.components.TextInputDialog +import com.tangem.core.ui.extensions.resolveReference import com.tangem.core.ui.extensions.stringResourceSafe import com.tangem.core.ui.res.TangemThemePreview import com.tangem.feature.walletsettings.component.preview.PreviewRenameWalletComponent @@ -31,9 +32,11 @@ internal fun RenameWalletDialog(model: RenameWalletUM, onDismiss: () -> Unit) { onClick = onDismiss, ), onDismissDialog = onDismiss, - onValueChange = model.updateValue, + onValueChange = model.onValueChange, textFieldParams = AdditionalTextInputDialogUM( label = stringResourceSafe(id = R.string.user_wallet_list_rename_popup_placeholder), + isError = model.error != null, + caption = model.error?.resolveReference(), ), ) } diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/model/WalletAlertState.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/model/WalletAlertState.kt index 5c8ab22a90..a55311e803 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/model/WalletAlertState.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/model/WalletAlertState.kt @@ -36,6 +36,7 @@ internal sealed interface WalletAlertState { override val onConfirmClick: (() -> Unit)?, ) : Basic() + // TODO: reuse RenameWalletComponent [REDACTED_JIRA] data class RenameWalletAlert( override val text: String, override val onConfirmClick: (String) -> Unit,