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 } }