From 2345a40c3b4cef9c587d1de44e00cff7b123bddd Mon Sep 17 00:00:00 2001 From: Milan Date: Wed, 9 Oct 2024 11:10:01 +0200 Subject: [PATCH] Fixed address book field state handling (#1625) --- .../contact/viewmodel/AddContactViewModel.kt | 74 ++++++++++++------- .../viewmodel/UpdateContactViewModel.kt | 69 ++++++++++------- 2 files changed, 88 insertions(+), 55 deletions(-) diff --git a/ui-lib/src/main/java/co/electriccoin/zcash/ui/screen/contact/viewmodel/AddContactViewModel.kt b/ui-lib/src/main/java/co/electriccoin/zcash/ui/screen/contact/viewmodel/AddContactViewModel.kt index 9437e5788..1ce2d9ea8 100644 --- a/ui-lib/src/main/java/co/electriccoin/zcash/ui/screen/contact/viewmodel/AddContactViewModel.kt +++ b/ui-lib/src/main/java/co/electriccoin/zcash/ui/screen/contact/viewmodel/AddContactViewModel.kt @@ -32,23 +32,31 @@ class AddContactViewModel( private val contactName = MutableStateFlow("") @OptIn(ExperimentalCoroutinesApi::class) - private val contactAddressState = + private val contactAddressError = contactAddress.mapLatest { address -> + if (address.isEmpty()) { + null + } else { + when (validateContactAddress(address)) { + ValidateContactAddressUseCase.Result.Invalid -> + stringRes(R.string.contact_address_error_invalid) + ValidateContactAddressUseCase.Result.NotUnique -> + stringRes(R.string.contact_address_error_not_unique) + + ValidateContactAddressUseCase.Result.Valid -> null + } + } + }.stateIn( + scope = viewModelScope, + started = SharingStarted.WhileSubscribed(ANDROID_STATE_FLOW_TIMEOUT), + initialValue = null + ) + + private val contactAddressState = + combine(contactAddress, contactAddressError) { address, contactAddressError -> TextFieldState( value = stringRes(address), - error = - if (address.isEmpty()) { - null - } else { - when (validateContactAddress(address)) { - ValidateContactAddressUseCase.Result.Invalid -> - stringRes(R.string.contact_address_error_invalid) - ValidateContactAddressUseCase.Result.NotUnique -> - stringRes(R.string.contact_address_error_not_unique) - - ValidateContactAddressUseCase.Result.Valid -> null - } - }, + error = contactAddressError, onValueChange = { newValue -> contactAddress.update { newValue } } @@ -56,23 +64,33 @@ class AddContactViewModel( } @OptIn(ExperimentalCoroutinesApi::class) - private val contactNameState = + private val contactNameError = contactName.mapLatest { name -> + if (name.isEmpty()) { + null + } else { + when (validateContactName(name)) { + ValidateContactNameUseCase.Result.TooLong -> + stringRes(R.string.contact_name_error_too_long) + + ValidateContactNameUseCase.Result.NotUnique -> + stringRes(R.string.contact_name_error_not_unique) + + ValidateContactNameUseCase.Result.Valid -> + null + } + } + }.stateIn( + scope = viewModelScope, + started = SharingStarted.WhileSubscribed(ANDROID_STATE_FLOW_TIMEOUT), + initialValue = null + ) + + private val contactNameState = + combine(contactName, contactNameError) { name, contactNameError -> TextFieldState( value = stringRes(name), - error = - if (name.isEmpty()) { - null - } else { - when (validateContactName(name)) { - ValidateContactNameUseCase.Result.TooLong -> - stringRes(R.string.contact_name_error_too_long) - ValidateContactNameUseCase.Result.NotUnique -> - stringRes(R.string.contact_name_error_not_unique) - ValidateContactNameUseCase.Result.Valid -> - null - } - }, + error = contactNameError, onValueChange = { newValue -> contactName.update { newValue } } diff --git a/ui-lib/src/main/java/co/electriccoin/zcash/ui/screen/contact/viewmodel/UpdateContactViewModel.kt b/ui-lib/src/main/java/co/electriccoin/zcash/ui/screen/contact/viewmodel/UpdateContactViewModel.kt index 6b654c990..f0fa73f3a 100644 --- a/ui-lib/src/main/java/co/electriccoin/zcash/ui/screen/contact/viewmodel/UpdateContactViewModel.kt +++ b/ui-lib/src/main/java/co/electriccoin/zcash/ui/screen/contact/viewmodel/UpdateContactViewModel.kt @@ -43,23 +43,32 @@ class UpdateContactViewModel( private val isLoadingContact = MutableStateFlow(true) @OptIn(ExperimentalCoroutinesApi::class) - private val contactAddressState = + private val contactAddressError = contactAddress.mapLatest { address -> + if (address.isEmpty()) { + null + } else { + when (validateContactAddress(address = address, exclude = contact)) { + ValidateContactAddressUseCase.Result.Invalid -> + stringRes(R.string.contact_address_error_invalid) + + ValidateContactAddressUseCase.Result.NotUnique -> + stringRes(R.string.contact_address_error_not_unique) + + ValidateContactAddressUseCase.Result.Valid -> null + } + } + }.stateIn( + scope = viewModelScope, + started = SharingStarted.WhileSubscribed(ANDROID_STATE_FLOW_TIMEOUT), + initialValue = null + ) + + private val contactAddressState = + combine(contactAddress, contactAddressError) { address, contactAddressError -> TextFieldState( value = stringRes(address), - error = - if (address.isEmpty()) { - null - } else { - when (validateContactAddress(address = address, exclude = contact)) { - ValidateContactAddressUseCase.Result.Invalid -> - stringRes(R.string.contact_address_error_invalid) - ValidateContactAddressUseCase.Result.NotUnique -> - stringRes(R.string.contact_address_error_not_unique) - - ValidateContactAddressUseCase.Result.Valid -> null - } - }, + error = contactAddressError, onValueChange = { newValue -> contactAddress.update { newValue } } @@ -67,22 +76,28 @@ class UpdateContactViewModel( } @OptIn(ExperimentalCoroutinesApi::class) - private val contactNameState = + private val contactNameError = contactName.mapLatest { name -> + if (name.isEmpty()) { + null + } else { + when (validateContactName(name = name, exclude = contact)) { + ValidateContactNameUseCase.Result.TooLong -> + stringRes(R.string.contact_name_error_too_long) + + ValidateContactNameUseCase.Result.NotUnique -> + stringRes(R.string.contact_name_error_not_unique) + + ValidateContactNameUseCase.Result.Valid -> null + } + } + } + + private val contactNameState = + combine(contactName, contactNameError) { name, contactNameError -> TextFieldState( value = stringRes(name), - error = - if (name.isEmpty()) { - null - } else { - when (validateContactName(name = name, exclude = contact)) { - ValidateContactNameUseCase.Result.TooLong -> - stringRes(R.string.contact_name_error_too_long) - ValidateContactNameUseCase.Result.NotUnique -> - stringRes(R.string.contact_name_error_not_unique) - ValidateContactNameUseCase.Result.Valid -> null - } - }, + error = contactNameError, onValueChange = { newValue -> contactName.update { newValue } }