From 16a8e5b3ed1282b045c9b7ca9671c566969cfee9 Mon Sep 17 00:00:00 2001 From: ThomazFB Date: Wed, 11 Dec 2024 19:28:40 -0300 Subject: [PATCH] Add Loading and Error support to WooShippingCarrierPackageScreen --- .../ui/WooShippingCarrierPackageScreen.kt | 58 +++++++++++++++++-- .../ui/WooShippingSavedPackageScreen.kt | 1 + 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/ui/WooShippingCarrierPackageScreen.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/ui/WooShippingCarrierPackageScreen.kt index d0716d43590..d39f544cbbf 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/ui/WooShippingCarrierPackageScreen.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/ui/WooShippingCarrierPackageScreen.kt @@ -1,7 +1,6 @@ package com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui import androidx.annotation.DrawableRes -import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column @@ -15,7 +14,9 @@ import androidx.compose.foundation.layout.wrapContentHeight import androidx.compose.foundation.pager.HorizontalPager import androidx.compose.foundation.pager.PagerState import androidx.compose.foundation.pager.rememberPagerState +import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.verticalScroll import androidx.compose.material.Button import androidx.compose.material.Divider import androidx.compose.material.Icon @@ -40,23 +41,70 @@ import androidx.compose.ui.unit.dp import com.woocommerce.android.R import com.woocommerce.android.ui.compose.theme.WooThemeWithBackground import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel +import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.PredefinedPackagesState import com.woocommerce.android.ui.orders.wooshippinglabels.packages.components.WooShippingPackageListItem +import com.woocommerce.android.ui.orders.wooshippinglabels.packages.components.WooShippingPackageListItemSkeleton import kotlinx.coroutines.launch @Composable fun WooShippingCarrierPackageScreen(viewModel: WooShippingLabelPackageCreationViewModel) { val viewState by viewModel.viewState.observeAsState() WooShippingCarrierPackageScreen( - carrierPackages = viewState?.predefinedPackagesData?.carrierPackages ?: emptyMap(), + packageState = viewState?.predefinedPackagesState ?: PredefinedPackagesState.Waiting, isAddPackageEnabled = viewState?.predefinedPackagesData?.hasCarrierSelection ?: false, onPackageSelected = viewModel::onCarrierPackageSelected, onAddPackageClick = viewModel::onAddCarrierPackageClick ) } -@OptIn(ExperimentalFoundationApi::class) @Composable fun WooShippingCarrierPackageScreen( + modifier: Modifier = Modifier, + packageState: PredefinedPackagesState, + onPackageSelected: (PackageData, Boolean) -> Unit, + isAddPackageEnabled: Boolean = false, + onAddPackageClick: () -> Unit = {} +) { + when (packageState) { + is PredefinedPackagesState.Data -> { + WooShippingCarrierPackageContent( + modifier = modifier, + carrierPackages = packageState.carrierPackages, + onPackageSelected = onPackageSelected, + isAddPackageEnabled = isAddPackageEnabled, + onAddPackageClick = onAddPackageClick + ) + } + + is PredefinedPackagesState.Error -> { + Column( + horizontalAlignment = Alignment.CenterHorizontally, + modifier = modifier + .fillMaxSize() + .padding(16.dp) + .verticalScroll(rememberScrollState()) + ) { + Text(text = stringResource(id = R.string.woo_shipping_labels_package_creation_error)) + } + } + + is PredefinedPackagesState.Waiting -> { + Column( + modifier = modifier + .fillMaxSize() + .padding(16.dp) + .verticalScroll(rememberScrollState()) + ) { + WooShippingPackageListItemSkeleton() + WooShippingPackageListItemSkeleton() + WooShippingPackageListItemSkeleton() + } + } + } +} + +@Composable +fun WooShippingCarrierPackageContent( modifier: Modifier = Modifier, carrierPackages: Map>, onPackageSelected: (PackageData, Boolean) -> Unit, @@ -94,7 +142,6 @@ fun WooShippingCarrierPackageScreen( } } -@OptIn(ExperimentalFoundationApi::class) @Composable private fun CarrierTabRow( modifier: Modifier, @@ -137,7 +184,6 @@ private fun CarrierTabRow( } } -@OptIn(ExperimentalFoundationApi::class) @Composable private fun PackageListPager( modifier: Modifier, @@ -223,7 +269,7 @@ private fun CarrierLogo( @Composable fun WooShippingCarrierPackageScreenPreview() { WooThemeWithBackground { - WooShippingCarrierPackageScreen( + WooShippingCarrierPackageContent( carrierPackages = mapOf( Carrier.DHL to listOf( CarrierPackageGroup( diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/ui/WooShippingSavedPackageScreen.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/ui/WooShippingSavedPackageScreen.kt index 2ad5dc063fd..121a78d248d 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/ui/WooShippingSavedPackageScreen.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/ui/WooShippingSavedPackageScreen.kt @@ -65,6 +65,7 @@ fun WooShippingSavedPackageScreen( Text(text = stringResource(id = R.string.woo_shipping_labels_package_creation_error)) } } + is PredefinedPackagesState.Waiting -> { Column( modifier = modifier