Skip to content

Commit

Permalink
Merge pull request #13105 from woocommerce/issue/connecting-shipping-…
Browse files Browse the repository at this point in the history
…rates

Connecting shipping rates
  • Loading branch information
atorresveiga authored Dec 12, 2024
2 parents fb53ca1 + 023c863 commit bfc0203
Show file tree
Hide file tree
Showing 5 changed files with 412 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.woocommerce.android.ui.orders.wooshippinglabels

import com.woocommerce.android.R
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.datasource.PackageDAO
import kotlinx.coroutines.delay
import javax.inject.Inject
import kotlin.random.Random
@Suppress("MagicNumber")
class GetShippingRates @Inject constructor() {
private val cheapestComparator = Comparator<ShippingRateUI> { r1, r2 ->
r1.rate.substring(1).toBigDecimal().compareTo(r2.rate.substring(1).toBigDecimal())
}

private val fastestComparator = Comparator<ShippingRateUI> { r1, r2 ->
r1.deliveryDays.compareTo(r2.deliveryDays)
}

suspend operator fun invoke(
selectedPackage: PackageDAO,
sortOrder: ShippingSortOption
): Result<Map<Carrier, List<ShippingRateUI>>> {
delay(1_000)
val comparator = when (sortOrder) {
ShippingSortOption.CHEAPEST -> {
cheapestComparator
}

ShippingSortOption.FASTEST -> {
fastestComparator
}
}
val carriers = if (selectedPackage.isLetter) {
listOf(
Carrier(
id = "dhl",
name = "DHL Express",
logoRes = R.drawable.dhl_logo
),
Carrier(
id = "usps",
name = "USPS",
logoRes = R.drawable.usps_logo
)
)
} else {
listOf(
Carrier(
id = "dhl",
name = "DHL Express",
logoRes = R.drawable.dhl_logo
),
Carrier(
id = "usps",
name = "USPS",
logoRes = R.drawable.usps_logo
),
Carrier(
id = "ups",
name = "UPS",
logoRes = R.drawable.ups_logo
)
)
}

return Result.success(
carriers.associateWith {
generateRates(
it.name,
Random(0).nextInt(from = 3, until = 10)
).sortedWith(comparator)
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.compose.foundation.layout.sizeIn
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Colors
import androidx.compose.material.DropdownMenu
import androidx.compose.material.DropdownMenuItem
Expand Down Expand Up @@ -66,23 +67,24 @@ val Colors.selectedRateBackgroundColor: Color get() = if (isLight) Color(0xFFF2E

@Composable
internal fun ShippingRatesCard(
selected: ShippingRate?,
onSelectedChange: (ShippingRate) -> Unit = {},
shippingRates: Map<Carrier, List<ShippingRate>>,
selectedRate: ShippingRateUI?,
onSelectedChange: (ShippingRateUI) -> Unit,
shippingRates: Map<Carrier, List<ShippingRateUI>>,
signatureRequired: SignatureRequired?,
onSelectedSignatureChange: (SignatureRequired?) -> Unit,
signatureRequiredOptions: List<SignatureRequired>,
selectedSortOption: ShippingSortOption,
onSelectedRateSortOrderChanged: (ShippingSortOption) -> Unit,
modifier: Modifier = Modifier
) {
var selectedSortOption by remember { mutableStateOf(ShippingSortOption.CHEAPEST) }
Column(modifier = modifier) {
ShippingRatesHeader(
selectedSortOption = selectedSortOption,
onSortOptionSelected = { selectedSortOption = it },
onSortOptionSelected = onSelectedRateSortOrderChanged,
modifier = Modifier.padding(start = dimensionResource(R.dimen.major_100))
)
ShippingRates(
selected = selected,
selectedRate = selectedRate,
onSelectedChange = onSelectedChange,
shippingRates = shippingRates,
signatureRequired = signatureRequired,
Expand All @@ -100,15 +102,41 @@ private fun ShippingRatesCardPreview() {
val selected = rates.values.first().first()
WooThemeWithBackground {
ShippingRatesCard(
selected = selected,
selectedRate = selected,
shippingRates = generateShippingRates(),
signatureRequired = null,
onSelectedChange = {},
onSelectedSignatureChange = {},
signatureRequiredOptions = listOf(
SignatureRequired("Signature Required", "$10.00"),
SignatureRequired("Adult Signature Required", "$15.00")
)
),
selectedSortOption = ShippingSortOption.CHEAPEST,
onSelectedRateSortOrderChanged = {}
)
}
}

@Composable
internal fun ShippingRatesLoading(
selectedSortOption: ShippingSortOption,
onSelectedRateSortOrderChanged: (ShippingSortOption) -> Unit,
modifier: Modifier = Modifier,
) {
Column(modifier = modifier) {
ShippingRatesHeader(
selectedSortOption = selectedSortOption,
onSortOptionSelected = onSelectedRateSortOrderChanged,
modifier = Modifier.padding(start = dimensionResource(R.dimen.major_100))
)
Box(
modifier = Modifier
.fillMaxWidth()
.sizeIn(minHeight = 300.dp),
contentAlignment = Alignment.Center
) {
CircularProgressIndicator()
}
}
}

Expand Down Expand Up @@ -200,9 +228,9 @@ private fun SortingDropdownMenu(
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ShippingRates(
selected: ShippingRate?,
onSelectedChange: (ShippingRate) -> Unit = {},
shippingRates: Map<Carrier, List<ShippingRate>>,
selectedRate: ShippingRateUI?,
onSelectedChange: (ShippingRateUI) -> Unit,
shippingRates: Map<Carrier, List<ShippingRateUI>>,
signatureRequired: SignatureRequired?,
onSelectedSignatureChange: (SignatureRequired?) -> Unit,
signatureRequiredOptions: List<SignatureRequired>,
Expand Down Expand Up @@ -259,7 +287,7 @@ fun ShippingRates(
ShippingRateItem(
carrier = carrier,
shippingRate = rate,
isSelected = selected == rate,
isSelected = selectedRate == rate,
signatureRequired = signatureRequired,
onSelectedSignatureChange = onSelectedSignatureChange,
signatureRequiredOptions = signatureRequiredOptions,
Expand Down Expand Up @@ -288,7 +316,7 @@ private fun CarrierLogo(
@Composable
private fun ShippingRateItem(
carrier: Carrier,
shippingRate: ShippingRate,
shippingRate: ShippingRateUI,
isSelected: Boolean,
signatureRequired: SignatureRequired?,
onSelectedSignatureChange: (SignatureRequired?) -> Unit,
Expand Down Expand Up @@ -363,7 +391,7 @@ private fun ShippingRateItem(

private fun getShippingRateFormattedDescription(
context: Context,
shippingRate: ShippingRate
shippingRate: ShippingRateUI
): AnnotatedString {
return buildAnnotatedString {
withStyle(style = SpanStyle(fontWeight = FontWeight.Bold)) {
Expand All @@ -383,7 +411,7 @@ private fun getShippingRateFormattedDescription(

@Composable
private fun ShippingRateItemExpandedDescription(
shippingRate: ShippingRate,
shippingRate: ShippingRateUI,
signatureRequired: SignatureRequired?,
onSelectedSignatureChange: (SignatureRequired?) -> Unit,
signatureRequiredOptions: List<SignatureRequired>,
Expand Down Expand Up @@ -455,7 +483,7 @@ private fun SelectSignatureRequired(
}
}

fun ShippingRate.getEstimatedDays(context: Context): String {
fun ShippingRateUI.getEstimatedDays(context: Context): String {
return StringUtils.getQuantityString(
context = context,
quantity = deliveryDays,
Expand All @@ -464,7 +492,7 @@ fun ShippingRate.getEstimatedDays(context: Context): String {
)
}

fun ShippingRate.getIncludedOptions(context: Context): List<String> {
fun ShippingRateUI.getIncludedOptions(context: Context): List<String> {
val options = mutableListOf<String>()
if (tracking) {
val tracking = context.getString(
Expand Down Expand Up @@ -499,7 +527,7 @@ data class Carrier(
val logoRes: Int? = null,
)

data class ShippingRate(
data class ShippingRateUI(
val name: String,
val rate: String,
val currency: String,
Expand All @@ -514,7 +542,7 @@ data class SignatureRequired(
val amount: String,
)

fun generateShippingRates(): Map<Carrier, List<ShippingRate>> {
fun generateShippingRates(): Map<Carrier, List<ShippingRateUI>> {
val carriers = listOf(
Carrier(
id = "dhl",
Expand Down Expand Up @@ -551,11 +579,11 @@ fun generateShippingRates(): Map<Carrier, List<ShippingRate>> {
}
}

fun generateRates(carrier: String, number: Int): List<ShippingRate> {
fun generateRates(carrier: String, number: Int): List<ShippingRateUI> {
return List(number) {
ShippingRate(
ShippingRateUI(
name = "$carrier - Ground Advantage Express",
rate = "$${(it + 1) * 2}.00",
rate = "$${(it + 100) / (it + 1)}.00",
currency = "USD",
deliveryDays = it,
insurance = "$100.00",
Expand Down
Loading

0 comments on commit bfc0203

Please sign in to comment.