-
Notifications
You must be signed in to change notification settings - Fork 136
Connecting shipping rates #13105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Connecting shipping rates #13105
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ae1cda8
add mock use case
ee6b4a0
refactor shipping rate section
4374130
UI for shipping rate states
8a3c535
connect UI with shipping rates from use case
470b928
refactor card elevation
b256baa
fi detekt warning
063540b
add unit tests
023c863
Merge branch 'trunk' into issue/connecting-shipping-rates
atorresveiga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
...e/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/GetShippingRates.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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, | ||
|
|
@@ -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() | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -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>, | ||
|
|
@@ -259,7 +287,7 @@ fun ShippingRates( | |
| ShippingRateItem( | ||
| carrier = carrier, | ||
| shippingRate = rate, | ||
| isSelected = selected == rate, | ||
| isSelected = selectedRate == rate, | ||
| signatureRequired = signatureRequired, | ||
| onSelectedSignatureChange = onSelectedSignatureChange, | ||
| signatureRequiredOptions = signatureRequiredOptions, | ||
|
|
@@ -288,7 +316,7 @@ private fun CarrierLogo( | |
| @Composable | ||
| private fun ShippingRateItem( | ||
| carrier: Carrier, | ||
| shippingRate: ShippingRate, | ||
| shippingRate: ShippingRateUI, | ||
| isSelected: Boolean, | ||
| signatureRequired: SignatureRequired?, | ||
| onSelectedSignatureChange: (SignatureRequired?) -> Unit, | ||
|
|
@@ -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)) { | ||
|
|
@@ -383,7 +411,7 @@ private fun getShippingRateFormattedDescription( | |
|
|
||
| @Composable | ||
| private fun ShippingRateItemExpandedDescription( | ||
| shippingRate: ShippingRate, | ||
| shippingRate: ShippingRateUI, | ||
| signatureRequired: SignatureRequired?, | ||
| onSelectedSignatureChange: (SignatureRequired?) -> Unit, | ||
| signatureRequiredOptions: List<SignatureRequired>, | ||
|
|
@@ -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, | ||
|
|
@@ -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( | ||
|
|
@@ -499,7 +527,7 @@ data class Carrier( | |
| val logoRes: Int? = null, | ||
| ) | ||
|
|
||
| data class ShippingRate( | ||
| data class ShippingRateUI( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renaming ShippingRate -> ShippingRateUI. The new model will be added in the next PR |
||
| val name: String, | ||
| val rate: String, | ||
| val currency: String, | ||
|
|
@@ -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", | ||
|
|
@@ -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", | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This use case will be updated in the next PR to fetch real data, for now, I'm skipping unit testing it