-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13105 from woocommerce/issue/connecting-shipping-…
…rates Connecting shipping rates
- Loading branch information
Showing
5 changed files
with
412 additions
and
82 deletions.
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 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 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
Oops, something went wrong.