Skip to content

Commit

Permalink
Merge pull request #13364 from woocommerce/issue/12439-validation-wit…
Browse files Browse the repository at this point in the history
…h-type-delay

Validation with delay
  • Loading branch information
atorresveiga authored Jan 28, 2025
2 parents 52880bd + b8727c1 commit 0e696fa
Show file tree
Hide file tree
Showing 6 changed files with 401 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.woocommerce.android.ui.orders.wooshippinglabels.address

import com.woocommerce.android.R
import com.woocommerce.android.viewmodel.ResourceProvider
import javax.inject.Inject

class AddressValidationHelper @Inject constructor(
private val resourceProvider: ResourceProvider
) {
fun validateAtLeastOneOf(vararg values: String): String? {
return if (values.all { it.isEmpty() || it.isBlank() }) {
resourceProvider.getString(R.string.woo_shipping_field_required_error)
} else {
null
}
}

fun validateFieldRequired(value: String): String? {
return if (value.isEmpty() || value.isBlank()) {
resourceProvider.getString(R.string.woo_shipping_field_required_error)
} else {
null
}
}

fun validateUSCustomsPhone(value: String): String? {
return when {
value.isEmpty() || value.isBlank() -> resourceProvider.getString(R.string.woo_shipping_field_required_error)
value.replace(Regex("^1|[^\\d]"), "").length != US_PHONE_NUMBER_LENGTH -> {
resourceProvider.getString(R.string.shipping_label_destination_address_phone_invalid)
}

else -> null
}
}

fun validateCustomsPhone(value: String): String? {
return when {
value.isEmpty() || value.isBlank() -> resourceProvider.getString(R.string.woo_shipping_field_required_error)
value.contains(Regex("\\d")).not() -> {
resourceProvider.getString(R.string.shipping_label_destination_address_phone_invalid)
}
else -> null
}
}

companion object {
private const val US_PHONE_NUMBER_LENGTH = 10
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -38,9 +39,41 @@ import com.woocommerce.android.R
import com.woocommerce.android.extensions.isNotNullOrEmpty
import com.woocommerce.android.ui.compose.component.Toolbar
import com.woocommerce.android.ui.orders.wooshippinglabels.RoundedCornerBoxWithBorder
import com.woocommerce.android.ui.orders.wooshippinglabels.address.origin.EditableAddress
import com.woocommerce.android.ui.orders.wooshippinglabels.address.origin.WooShippingEditOriginViewModel

@Composable
fun WooShippingEditAddressScreen(
viewModel: WooShippingEditOriginViewModel,
modifier: Modifier = Modifier
) {
when (val viewState = viewModel.viewState.collectAsState().value) {
is WooShippingEditOriginViewModel.EditAddressViewState.DataState -> {
WooShippingEditAddressScreen(
editableAddress = viewState.editableAddress,
onNameChange = viewModel::onNameChange,
onCompanyChange = viewModel::onCompanyChange,
onAddressChange = viewModel::onAddressChange,
onCityChange = viewModel::onCityChange,
onPostalCodeChange = viewModel::onPostalCodeChange,
onEmailChange = viewModel::onEmailChange,
onPhoneChange = viewModel::onPhoneChange,
modifier = modifier
)
}
}
}

@Composable
fun WooShippingEditAddressScreen(
editableAddress: EditableAddress,
onNameChange: (String) -> Unit,
onCompanyChange: (String) -> Unit,
onAddressChange: (String) -> Unit,
onCityChange: (String) -> Unit,
onPostalCodeChange: (String) -> Unit,
onEmailChange: (String) -> Unit,
onPhoneChange: (String) -> Unit,
modifier: Modifier = Modifier
) {
Scaffold(
Expand All @@ -60,8 +93,9 @@ fun WooShippingEditAddressScreen(
) {
RoundedBorderTextFieldWithLabel(
label = "${stringResource(id = R.string.woo_shipping_label_name)} *",
text = "",
onTextChange = {},
text = editableAddress.name.value,
error = editableAddress.name.error,
onTextChange = onNameChange,
)

var isExpanded by remember { mutableStateOf(false) }
Expand All @@ -74,35 +108,37 @@ fun WooShippingEditAddressScreen(
) {
RoundedBorderTextFieldWithLabel(
label = stringResource(id = R.string.woo_shipping_label_company),
text = "",
onTextChange = {},
text = editableAddress.company.value,
onTextChange = onCompanyChange,
modifier = Modifier.padding(top = 8.dp)
)
}

RoundedBorderDropDownWithLabel(
label = "${stringResource(id = R.string.woo_shipping_label_country)} *",
text = "United States",
text = editableAddress.country,
modifier = Modifier.padding(top = 24.dp),
onClick = {}
)
RoundedBorderTextFieldWithLabel(
label = "${stringResource(id = R.string.woo_shipping_label_address)} *",
text = "",
onTextChange = {},
text = editableAddress.address.value,
error = editableAddress.address.error,
onTextChange = onAddressChange,
modifier = Modifier.padding(top = 8.dp)
)
RoundedBorderTextFieldWithLabel(
label = "${stringResource(id = R.string.woo_shipping_label_city)} *",
text = "",
onTextChange = {},
text = editableAddress.city.value,
error = editableAddress.city.error,
onTextChange = onCityChange,
modifier = Modifier.padding(top = 8.dp)
)

Row {
RoundedBorderDropDownWithLabel(
label = "${stringResource(id = R.string.woo_shipping_label_state)} *",
text = "United States",
text = editableAddress.state,
modifier = Modifier
.padding(top = 8.dp)
.weight(1f),
Expand All @@ -111,9 +147,10 @@ fun WooShippingEditAddressScreen(
Spacer(modifier = Modifier.size(8.dp))
RoundedBorderTextFieldWithLabel(
label = "${stringResource(id = R.string.woo_shipping_label_post_code)} *",
text = "",
text = editableAddress.postalCode.value,
error = editableAddress.postalCode.error,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
onTextChange = {},
onTextChange = onPostalCodeChange,
modifier = Modifier
.padding(top = 8.dp)
.weight(1f)
Expand All @@ -122,14 +159,18 @@ fun WooShippingEditAddressScreen(

RoundedBorderTextFieldWithLabel(
label = "${stringResource(id = R.string.woo_shipping_label_email)} *",
text = "",
onTextChange = {},
text = editableAddress.email.value,
error = editableAddress.email.error,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email),
onTextChange = onEmailChange,
modifier = Modifier.padding(top = 32.dp)
)
RoundedBorderTextFieldWithLabel(
label = "${stringResource(id = R.string.woo_shipping_label_phone)} *",
text = "",
onTextChange = {},
text = editableAddress.phone.value,
error = editableAddress.phone.error,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Phone),
onTextChange = onPhoneChange,
modifier = Modifier.padding(top = 8.dp)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.woocommerce.android.ui.orders.wooshippinglabels.address.origin

import android.os.Bundle
import android.view.LayoutInflater
import android.view.Surface
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.layout.fillMaxSize
Expand All @@ -12,6 +11,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed
import androidx.compose.ui.unit.dp
import androidx.fragment.app.viewModels
import com.woocommerce.android.ui.base.BaseFragment
import com.woocommerce.android.ui.compose.theme.WooThemeWithBackground
import com.woocommerce.android.ui.main.AppBarStatus
Expand All @@ -21,6 +21,8 @@ import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint
class WooShippingEditOriginAddressFragment : BaseFragment() {

private val viewModel: WooShippingEditOriginViewModel by viewModels()

override val activityAppBarStatus: AppBarStatus = AppBarStatus.Hidden

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
Expand All @@ -30,6 +32,7 @@ class WooShippingEditOriginAddressFragment : BaseFragment() {
WooThemeWithBackground {
Surface {
WooShippingEditAddressScreen(
viewModel = viewModel,
modifier = Modifier
.fillMaxSize()
.padding(16.dp)
Expand Down
Loading

0 comments on commit 0e696fa

Please sign in to comment.