Skip to content

Commit 9c44f97

Browse files
authored
Merge pull request #14856 from woocommerce/issue/woomob-1334-woo-poslocal-catalog-run-both-products-and-variations-within-v2-step2
Reduce code duplication in WooPosSyncAction
2 parents be188ce + 84f1594 commit 9c44f97

File tree

9 files changed

+273
-358
lines changed

9 files changed

+273
-358
lines changed

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosLocalCatalogSyncRepository.kt

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.woocommerce.android.ui.woopos.localcatalog
22

33
import com.woocommerce.android.ui.woopos.common.util.WooPosLogWrapper
4-
import com.woocommerce.android.ui.woopos.localcatalog.WooPosSyncProductsAction.WooPosSyncProductsResult
5-
import com.woocommerce.android.ui.woopos.localcatalog.WooPosSyncVariationsAction.WooPosSyncVariationsResult
64
import com.woocommerce.android.ui.woopos.util.datastore.WooPosPreferencesRepository
75
import com.woocommerce.android.ui.woopos.util.datastore.WooPosSyncTimestampManager
86
import com.woocommerce.android.util.CoroutineDispatchers
@@ -95,20 +93,20 @@ class WooPosLocalCatalogSyncRepository @Inject constructor(
9593
}
9694

9795
val productSyncResult = syncProducts(site, modifiedAfterGmt, pageSize, maxPages)
98-
if (productSyncResult is WooPosSyncProductsResult.Failed) {
96+
if (productSyncResult is WooPosSyncResult.Failed) {
9997
return productSyncResult.toPosLocalCatalogSyncFailure()
10098
}
10199

102100
val variationSyncResult = syncVariations(site, modifiedAfterGmt, pageSize, maxPages)
103-
if (variationSyncResult is WooPosSyncVariationsResult.Failed) {
101+
if (variationSyncResult is WooPosSyncResult.Failed) {
104102
return variationSyncResult.toPosLocalCatalogSyncFailure()
105103
}
106104

107105
val syncDuration = System.currentTimeMillis() - startTime
108106

109107
return PosLocalCatalogSyncResult.Success(
110-
productsSynced = (productSyncResult as WooPosSyncProductsResult.Success).productsSynced,
111-
variationsSynced = (variationSyncResult as WooPosSyncVariationsResult.Success).variationsSynced,
108+
productsSynced = (productSyncResult as WooPosSyncResult.Success).syncedCount,
109+
variationsSynced = (variationSyncResult as WooPosSyncResult.Success).syncedCount,
112110
syncDurationMs = syncDuration
113111
)
114112
}
@@ -118,10 +116,10 @@ class WooPosLocalCatalogSyncRepository @Inject constructor(
118116
modifiedAfterGmt: String?,
119117
pageSize: Int,
120118
maxPages: Int
121-
): WooPosSyncProductsResult {
119+
): WooPosSyncResult {
122120
val result = posSyncAction.syncProducts(site, modifiedAfterGmt, pageSize, maxPages)
123121

124-
if (result is WooPosSyncProductsResult.Success) {
122+
if (result is WooPosSyncResult.Success) {
125123
result.serverDate?.let { serverDate ->
126124
syncTimestampManager.parseTimestampFromApi(serverDate)?.let { timestamp ->
127125
syncTimestampManager.storeProductsLastSyncTimestamp(timestamp)
@@ -138,10 +136,10 @@ class WooPosLocalCatalogSyncRepository @Inject constructor(
138136
modifiedAfterGmt: String?,
139137
pageSize: Int,
140138
maxPages: Int
141-
): WooPosSyncVariationsResult {
139+
): WooPosSyncResult {
142140
val result = posSyncAction.syncVariations(site, modifiedAfterGmt, pageSize, maxPages)
143141

144-
if (result is WooPosSyncVariationsResult.Success) {
142+
if (result is WooPosSyncResult.Success) {
145143
result.serverDate?.let { serverDate ->
146144
syncTimestampManager.parseTimestampFromApi(serverDate)?.let { timestamp ->
147145
syncTimestampManager.storeVariationsLastSyncTimestamp(timestamp)
@@ -160,34 +158,18 @@ class WooPosLocalCatalogSyncRepository @Inject constructor(
160158
posLocalCatalogStore.getVariationCount(LocalId(site.id)).getOrElse { 0 }
161159
}
162160

163-
private fun WooPosSyncProductsResult.Failed.toPosLocalCatalogSyncFailure(): PosLocalCatalogSyncResult.Failure {
161+
private fun WooPosSyncResult.Failed.toPosLocalCatalogSyncFailure(): PosLocalCatalogSyncResult.Failure {
164162
return when (this) {
165-
is WooPosSyncProductsResult.Failed.CatalogTooLarge -> {
163+
is WooPosSyncResult.Failed.CatalogTooLarge -> {
166164
PosLocalCatalogSyncResult.Failure.CatalogTooLarge(
167-
error = "Product catalog too large: $totalPages pages exceed maximum of $maxPages pages",
165+
error = "Catalog too large: $totalPages pages exceed maximum of $maxPages pages",
168166
totalPages = totalPages,
169167
maxPages = maxPages
170168
)
171169
}
172170

173-
else -> {
174-
PosLocalCatalogSyncResult.Failure.UnexpectedError(error)
175-
}
176-
}
177-
}
178-
179-
private fun WooPosSyncVariationsResult.Failed.toPosLocalCatalogSyncFailure(): PosLocalCatalogSyncResult.Failure {
180-
return when (this) {
181-
is WooPosSyncVariationsResult.Failed.CatalogTooLarge -> {
182-
PosLocalCatalogSyncResult.Failure.CatalogTooLarge(
183-
error = "Variations catalog too large: $totalPages pages exceed maximum of $maxPages pages",
184-
totalPages = totalPages,
185-
maxPages = maxPages
186-
)
187-
}
188-
189-
else -> {
190-
PosLocalCatalogSyncResult.Failure.UnexpectedError(error)
171+
is WooPosSyncResult.Failed.UnexpectedError -> {
172+
PosLocalCatalogSyncResult.Failure.UnexpectedError(errorMessage)
191173
}
192174
}
193175
}

0 commit comments

Comments
 (0)