Skip to content

Commit

Permalink
Fixed address book field state handling (#1625)
Browse files Browse the repository at this point in the history
  • Loading branch information
Milan-Cerovsky authored Oct 9, 2024
1 parent 1e275fe commit 2345a40
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,47 +32,65 @@ 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 }
}
)
}

@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 }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,46 +43,61 @@ 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 }
}
)
}

@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 }
}
Expand Down

0 comments on commit 2345a40

Please sign in to comment.