-
Notifications
You must be signed in to change notification settings - Fork 134
[Shipping Labels] Cache carrier packages data #14541
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
hichamboushaba
merged 22 commits into
trunk
from
issue/WOOMOB-887-cache-carrier-packages-data
Sep 10, 2025
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
e5ad229
Add WooShippingPackagesEntity
irfano d31ed1e
Bump WCAndroidDatabase version to 60 and add WooShippingPackagesEntity
irfano f59ddc6
Add Shipping Packages methods to WooShippingDao
irfano 446d0bc
Refactor WooShippingLabelPackageMapper to use FluxC entities
irfano 696cd34
Inject WooShippingDao into WooShippingLabelPackageRepository
irfano 3c613f9
Update WooShippingLabelPackageRepository to save fetched packages
irfano b29a566
Rename `fetchAllStorePackages` to `fetchShippingPackages`
irfano 9eda96f
Add new FetchShippingPackages use case to adapt data model changes
irfano e165382
Remove `FetchPackagesFromStore` and use new `FetchShippingPackages`
irfano 7384ecc
Remove PackageDAOs in favor of using WooShippingPackageEntity
irfano b0cf524
Add ObserveShippingPackages
irfano ed433de
Use ObserveShippingPackages in WooShippingLabelPackageCreationViewModel
irfano 948c7e1
Fix unit tests related to shipment packages
irfano bf8450e
Add new test to ObserveShippingPackagesTest
irfano 55893ae
Add delete and get methods for packages to WooShippingDao
irfano fcdecd2
Update package cache after saving/deleting packages
irfano 0f87ddb
Update WooShippingLabelPackageRepository tests
irfano 2b4d5c5
Add release note for shipping labels package caching
irfano 5e13f7f
Merge branch 'trunk' into issue/WOOMOB-887-cache-carrier-packages-data
irfano 2cfd82d
Merge branch 'trunk' into issue/WOOMOB-887-cache-carrier-packages-data
irfano 146ebde
Merge branch 'trunk' into issue/WOOMOB-887-cache-carrier-packages-data
irfano 33ce680
Remove unused `deleteShippingPackages` function
irfano 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
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
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
67 changes: 0 additions & 67 deletions
67
...ommerce/android/ui/orders/wooshippinglabels/packages/datasource/FetchPackagesFromStore.kt
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
...commerce/android/ui/orders/wooshippinglabels/packages/datasource/FetchShippingPackages.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,21 @@ | ||
package com.woocommerce.android.ui.orders.wooshippinglabels.packages.datasource | ||
|
||
import com.woocommerce.android.tools.SelectedSite | ||
import org.wordpress.android.fluxc.persistence.entity.WooShippingPackagesEntity | ||
import javax.inject.Inject | ||
|
||
class FetchShippingPackages @Inject constructor( | ||
private val packageRepository: WooShippingLabelPackageRepository, | ||
private val selectedSite: SelectedSite | ||
) { | ||
suspend operator fun invoke(): Result<WooShippingPackagesEntity> { | ||
val response = packageRepository.fetchShippingPackages(selectedSite.get()) | ||
val result = response.model | ||
return if (response.isError || result == null) { | ||
val message = response.error?.type?.name ?: "Unknown error" | ||
Result.failure(Exception(message)) | ||
} else { | ||
Result.success(result) | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...mmerce/android/ui/orders/wooshippinglabels/packages/datasource/ObserveShippingPackages.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,88 @@ | ||
package com.woocommerce.android.ui.orders.wooshippinglabels.packages.datasource | ||
|
||
import com.woocommerce.android.tools.SelectedSite | ||
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.PackagesState | ||
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.Carrier | ||
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.CarrierPackageGroup | ||
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.PackageData | ||
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.StoreOptionsForPackages | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.transformLatest | ||
import kotlinx.coroutines.flow.withIndex | ||
import org.wordpress.android.fluxc.persistence.entity.WooShippingPackagesEntity | ||
import org.wordpress.android.fluxc.persistence.entity.WooShippingPackagesEntity.CarrierPackageGroups | ||
import org.wordpress.android.fluxc.persistence.entity.WooShippingPackagesEntity.StoreOptions | ||
import javax.inject.Inject | ||
|
||
class ObserveShippingPackages @Inject constructor( | ||
private val selectedSite: SelectedSite, | ||
private val packageRepository: WooShippingLabelPackageRepository, | ||
private val fetchShippingPackages: FetchShippingPackages | ||
) { | ||
@OptIn(ExperimentalCoroutinesApi::class) | ||
operator fun invoke(): Flow<PackagesState> = packageRepository.observeShippingPackages(selectedSite.get()) | ||
.withIndex() | ||
.transformLatest { (index, entity) -> | ||
val isFirst = index == 0 | ||
when { | ||
isFirst && entity == null -> { | ||
val result = fetchShippingPackages() | ||
if (result.isFailure) { | ||
emit(PackagesState.Error(result.exceptionOrNull()?.message ?: "Unknown error")) | ||
} | ||
} | ||
|
||
entity == null -> emit(PackagesState.Error()) | ||
else -> { | ||
emit(entity.toPackagesState()) | ||
if (isFirst) { | ||
fetchShippingPackages() | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun StoreOptions.toStoreOptionsForPackages() = StoreOptionsForPackages( | ||
currencySymbol = currencySymbol ?: StoreOptionsForPackages.DEFAULT.currencySymbol, | ||
dimensionUnit = dimensionUnit ?: StoreOptionsForPackages.DEFAULT.dimensionUnit, | ||
weightUnit = weightUnit ?: StoreOptionsForPackages.DEFAULT.weightUnit, | ||
originCountry = originCountry ?: StoreOptionsForPackages.DEFAULT.originCountry | ||
) | ||
|
||
private fun WooShippingPackagesEntity.filterCarrierData(): Map<Carrier, List<CarrierPackageGroup>> = | ||
buildMap { | ||
carrierPackageGroups.parseCarrierData(WooShippingPackagesEntity.CarrierType.USPS) | ||
.takeIf { it.isNotEmpty() } | ||
?.let { packageGroups -> put(Carrier.USPS, packageGroups) } | ||
|
||
carrierPackageGroups.parseCarrierData(WooShippingPackagesEntity.CarrierType.DHL) | ||
.takeIf { it.isNotEmpty() } | ||
?.let { packageGroups -> put(Carrier.DHL, packageGroups) } | ||
|
||
carrierPackageGroups.parseCarrierData(WooShippingPackagesEntity.CarrierType.UPS) | ||
.takeIf { it.isNotEmpty() } | ||
?.let { packageGroups -> put(Carrier.UPS, packageGroups) } | ||
} | ||
|
||
private fun List<CarrierPackageGroups>.parseCarrierData( | ||
carrierType: WooShippingPackagesEntity.CarrierType | ||
) = find { it.carrierType == carrierType }?.let { | ||
it.packageGroups?.mapNotNull { group -> | ||
group.description?.let { description -> | ||
CarrierPackageGroup( | ||
groupName = description, | ||
packages = group.packages?.map { packageItem -> | ||
PackageData.fromPackageEntity(packageItem) | ||
}.orEmpty() | ||
) | ||
} | ||
} | ||
} ?: emptyList() | ||
|
||
private fun WooShippingPackagesEntity.toPackagesState() = PackagesState.Data( | ||
storeOptions = storeOptions.toStoreOptionsForPackages(), | ||
savedPackages = savedPackages.map { PackageData.fromPackageEntity(it) }, | ||
carrierPackages = filterCarrierData() | ||
) | ||
} |
46 changes: 0 additions & 46 deletions
46
...in/com/woocommerce/android/ui/orders/wooshippinglabels/packages/datasource/PackageDAOs.kt
This file was deleted.
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.
np, I'm not sure this is needed, since we already use the usercase
ObserveShippingPackages
for the accessing the packages, I thinkObserveShippingPackages
can call the repository directly, and it would be easier to follow.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.
ObserveShippingPackages
has a different logic: it uses cached data while also fetching new data.FetchShippingPackages
is used for retry functionality to force a fresh fetch, and it’s a very small use case.If we switch to
ObserveShippingPackages
, we’d need to add aforceFetch
parameter, adjust the logic, and probably rename the use case. I think keeping it as is would be simpler. But let me know if I’ve misunderstood your request.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.
I think I wasn't clear here, I meant deleting this use case completely, and update
ObserveShippingPackages
to use the repository directly (calling WooShippingLabelPackageRepository#fetchShippingPackages directly).This use case doesn't have any additional logic; it just maps the response, which normally should be the repository role.