Skip to content

Commit c65faf1

Browse files
authored
Merge pull request #14593 from woocommerce/issue/woomob-1280-woo-poslocal-catalog-use-new-pos-specific-product-model-step-8
POS Product Migration Step 8
2 parents 07ee3f6 + 0ae7ac6 commit c65faf1

File tree

6 files changed

+69
-73
lines changed

6 files changed

+69
-73
lines changed

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/common/data/WooPosVariationMapper.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import com.google.gson.Gson
44
import com.google.gson.JsonSyntaxException
55
import com.google.gson.reflect.TypeToken
66
import com.woocommerce.android.R
7-
import com.woocommerce.android.model.Product
87
import com.woocommerce.android.model.ProductVariation
98
import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVersion2
109
import com.woocommerce.android.util.WooLog
@@ -118,7 +117,7 @@ class WooPosVariationMapper @Inject constructor(
118117
}
119118
}
120119

121-
fun getName(variation: WooPosVariation, parentProduct: Product? = null): String {
120+
fun getName(variation: WooPosVariation, parentProduct: WooPosProductModelVersion2? = null): String {
122121
return parentProduct?.variationEnabledAttributes?.joinToString(" - ") { attribute ->
123122
val option = variation.attributes.firstOrNull { it.name == attribute.name }
124123
option?.option ?: "Any ${attribute.name}"
@@ -172,5 +171,5 @@ fun WooPosVariation.getNameForPOS(
172171
resourceProvider: ResourceProvider,
173172
): String = mapper.getNameForPOS(this, parentProduct, resourceProvider)
174173

175-
fun WooPosVariation.getName(mapper: WooPosVariationMapper, parentProduct: Product? = null): String =
174+
fun WooPosVariation.getName(mapper: WooPosVariationMapper, parentProduct: WooPosProductModelVersion2? = null): String =
176175
mapper.getName(this, parentProduct)

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/items/search/WooPosItemsSearchEmptyStateRepository.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.woocommerce.android.ui.woopos.home.items.search
22

3-
import com.woocommerce.android.model.Product
43
import com.woocommerce.android.ui.woopos.common.data.WooPosPopularProductsProvider
4+
import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVersion2
55
import com.woocommerce.android.ui.woopos.util.datastore.WooPosPreferencesRepository
66
import kotlinx.coroutines.flow.first
77
import javax.inject.Inject
@@ -10,7 +10,7 @@ class WooPosItemsSearchEmptyStateRepository @Inject constructor(
1010
private val preferencesRepository: WooPosPreferencesRepository,
1111
private val popularProductsProvider: WooPosPopularProductsProvider,
1212
) {
13-
suspend fun getPopularItems(): List<Product> = popularProductsProvider.getPopularProducts()
13+
suspend fun getPopularItems(): List<WooPosProductModelVersion2> = popularProductsProvider.getPopularProducts()
1414

1515
suspend fun getLastSearches(): List<String> = preferencesRepository.recentProductSearches.first()
1616

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/items/search/WooPosItemsSearchViewModel.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package com.woocommerce.android.ui.woopos.home.items.search
22

33
import androidx.lifecycle.ViewModel
44
import androidx.lifecycle.viewModelScope
5-
import com.woocommerce.android.model.Product
6-
import com.woocommerce.android.ui.products.ProductType
5+
import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVersion2
76
import com.woocommerce.android.ui.woopos.home.ChildToParentEvent
87
import com.woocommerce.android.ui.woopos.home.ChildToParentEvent.SearchEvent.RecentSearchSelected
98
import com.woocommerce.android.ui.woopos.home.ParentToChildrenEvent
@@ -148,7 +147,7 @@ class WooPosItemsSearchViewModel @Inject constructor(
148147
}
149148

150149
childToParentEventSender.sendToParent(ChildToParentEvent.SearchEvent.Started)
151-
var result: Result<List<Product>>
150+
var result: Result<List<WooPosProductModelVersion2>>
152151
val searchTimeMillis = measureTimeMillis {
153152
result = dataSource.searchRemoteProducts(query)
154153
}
@@ -287,7 +286,7 @@ class WooPosItemsSearchViewModel @Inject constructor(
287286
}
288287
}
289288

290-
private suspend fun List<Product>.toContentState(
289+
private suspend fun List<WooPosProductModelVersion2>.toContentState(
291290
searchQuery: String,
292291
paginationState: WooPosPaginationState = WooPosPaginationState.None,
293292
) = WooPosItemsSearchViewState.Content(
@@ -296,21 +295,21 @@ class WooPosItemsSearchViewModel @Inject constructor(
296295
paginationState = paginationState,
297296
)
298297

299-
private suspend fun Product.toViewModelProduct(): WooPosItemSelectionViewState.Product =
300-
if (productType == ProductType.VARIABLE) {
298+
private suspend fun WooPosProductModelVersion2.toViewModelProduct(): WooPosItemSelectionViewState.Product =
299+
if (type == WooPosProductModelVersion2.WooPosProductType.VARIABLE) {
301300
WooPosItemSelectionViewState.Product.Variable(
302301
id = this.remoteId,
303302
name = this.name,
304-
price = priceFormat(this.price),
303+
price = priceFormat(this.pricing.displayPrice),
305304
imageUrl = this.firstImageUrl,
306-
numOfVariations = this.numVariations,
305+
numOfVariations = this.variationIds.size,
307306
variationIds = this.variationIds
308307
)
309308
} else {
310309
WooPosItemSelectionViewState.Product.Simple(
311310
id = this.remoteId,
312311
name = this.name,
313-
price = priceFormat(this.price),
312+
price = priceFormat(this.pricing.displayPrice),
314313
imageUrl = this.firstImageUrl,
315314
)
316315
}

WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/items/search/WooPosItemsSearchViewModelTest.kt

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.woocommerce.android.ui.woopos.home.items.search
22

33
import app.cash.turbine.test
4-
import com.woocommerce.android.ui.products.ProductTestUtils
4+
import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVersion2
55
import com.woocommerce.android.ui.woopos.home.ChildToParentEvent
66
import com.woocommerce.android.ui.woopos.home.ParentToChildrenEvent
77
import com.woocommerce.android.ui.woopos.home.WooPosChildrenToParentEventSender
@@ -13,6 +13,7 @@ import com.woocommerce.android.ui.woopos.util.WooPosCoroutineTestRule
1313
import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsEvent
1414
import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsEventConstant
1515
import com.woocommerce.android.ui.woopos.util.format.WooPosFormatPrice
16+
import com.woocommerce.android.ui.woopos.util.generateWooPosProduct
1617
import kotlinx.coroutines.ExperimentalCoroutinesApi
1718
import kotlinx.coroutines.delay
1819
import kotlinx.coroutines.flow.emptyFlow
@@ -51,11 +52,11 @@ class WooPosItemsSearchViewModelTest {
5152
private val mockAnalyticsTracker: WooPosItemsSearchAnalyticsTracker = mock()
5253

5354
private val defaultQuery = "test query"
54-
private val defaultProduct = ProductTestUtils.generateProduct(
55+
private val defaultProduct = generateWooPosProduct(
5556
productId = 1,
5657
productName = "Test Product",
5758
amount = "10.0",
58-
productType = "simple"
59+
productType = WooPosProductModelVersion2.WooPosProductType.SIMPLE
5960
)
6061

6162
@Before
@@ -280,13 +281,12 @@ class WooPosItemsSearchViewModelTest {
280281
runTest {
281282
// GIVEN
282283
val variableProduct =
283-
ProductTestUtils.generateProduct(
284+
generateWooPosProduct(
284285
productId = 1,
285286
productName = "Variable Product",
286287
amount = "10.0",
287-
productType = "variable",
288-
isVariable = true,
289-
variationIds = "[101,102,103]"
288+
productType = WooPosProductModelVersion2.WooPosProductType.VARIABLE,
289+
variationIds = listOf(101L, 102L, 103L)
290290
)
291291

292292
mockSuccessfulSearch(defaultQuery, listOf(variableProduct))
@@ -312,11 +312,11 @@ class WooPosItemsSearchViewModelTest {
312312
@Test
313313
fun `given content state and more pages available, when end of list reached, then load more data`() = runTest {
314314
// GIVEN
315-
val additionalProduct = ProductTestUtils.generateProduct(
315+
val additionalProduct = generateWooPosProduct(
316316
productId = 2,
317317
productName = "Test Product 2",
318318
amount = "20.0",
319-
productType = "simple"
319+
productType = WooPosProductModelVersion2.WooPosProductType.SIMPLE
320320
)
321321

322322
mockSuccessfulSearch(defaultQuery, listOf(defaultProduct))
@@ -346,11 +346,11 @@ class WooPosItemsSearchViewModelTest {
346346
fun `given content state and more pages available, when end of list reached, then track ItemsNextPageLoaded event`() =
347347
runTest {
348348
// GIVEN
349-
val additionalProduct = ProductTestUtils.generateProduct(
349+
val additionalProduct = generateWooPosProduct(
350350
productId = 2,
351351
productName = "Test Product 2",
352352
amount = "20.0",
353-
productType = "simple"
353+
productType = WooPosProductModelVersion2.WooPosProductType.SIMPLE
354354
)
355355

356356
mockSuccessfulSearch(defaultQuery, listOf(defaultProduct))
@@ -393,18 +393,18 @@ class WooPosItemsSearchViewModelTest {
393393
@Test
394394
fun `given cached products, when search performed, then cached results shown while loading`() = runTest {
395395
// GIVEN
396-
val cachedProduct = ProductTestUtils.generateProduct(
396+
val cachedProduct = generateWooPosProduct(
397397
productId = 1,
398398
productName = "Cached Product",
399399
amount = "10.0",
400-
productType = "simple"
400+
productType = WooPosProductModelVersion2.WooPosProductType.SIMPLE
401401
)
402402

403-
val remoteProduct = ProductTestUtils.generateProduct(
403+
val remoteProduct = generateWooPosProduct(
404404
productId = 2,
405405
productName = "Remote Product",
406406
amount = "20.0",
407-
productType = "simple"
407+
productType = WooPosProductModelVersion2.WooPosProductType.SIMPLE
408408
)
409409

410410
mockCachedThenRemoteSearch(defaultQuery, cachedProduct, remoteProduct)
@@ -437,11 +437,11 @@ class WooPosItemsSearchViewModelTest {
437437
whenever(mockEmptyStateProvider.getLastSearches()).thenReturn(emptyList())
438438

439439
val products = listOf(
440-
ProductTestUtils.generateProduct(
440+
generateWooPosProduct(
441441
productId = 1,
442442
productName = "Test Product",
443443
amount = "10.0",
444-
productType = "simple"
444+
productType = WooPosProductModelVersion2.WooPosProductType.SIMPLE
445445
)
446446
)
447447

@@ -479,11 +479,11 @@ class WooPosItemsSearchViewModelTest {
479479
// GIVEN
480480
val query = "test query"
481481
val products = listOf(
482-
ProductTestUtils.generateProduct(
482+
generateWooPosProduct(
483483
productId = 1,
484484
productName = "Test Product",
485485
amount = "10.0",
486-
productType = "simple"
486+
productType = WooPosProductModelVersion2.WooPosProductType.SIMPLE
487487
)
488488
)
489489

@@ -517,11 +517,11 @@ class WooPosItemsSearchViewModelTest {
517517
// GIVEN
518518
val query = "test query"
519519
val products = listOf(
520-
ProductTestUtils.generateProduct(
520+
generateWooPosProduct(
521521
productId = 1,
522522
productName = "Test Product",
523523
amount = "10.0",
524-
productType = "simple"
524+
productType = WooPosProductModelVersion2.WooPosProductType.SIMPLE
525525
)
526526
)
527527

@@ -557,11 +557,11 @@ class WooPosItemsSearchViewModelTest {
557557
// GIVEN
558558
val query = "test query"
559559
val products = listOf(
560-
ProductTestUtils.generateProduct(
560+
generateWooPosProduct(
561561
productId = 1,
562562
productName = "Test Product",
563563
amount = "10.0",
564-
productType = "simple"
564+
productType = WooPosProductModelVersion2.WooPosProductType.SIMPLE
565565
)
566566
)
567567

@@ -784,7 +784,7 @@ class WooPosItemsSearchViewModelTest {
784784
)
785785
}
786786

787-
private fun mockSuccessfulSearch(query: String, products: List<com.woocommerce.android.model.Product>) {
787+
private fun mockSuccessfulSearch(query: String, products: List<WooPosProductModelVersion2>) {
788788
wheneverBlocking { mockDataSource.searchLocalProducts(query) }.thenReturn(emptyList())
789789
wheneverBlocking { mockDataSource.searchRemoteProducts(query) }.thenReturn(Result.success(products))
790790
whenever(mockParentToChildrenEventReceiver.events).thenReturn(
@@ -800,7 +800,7 @@ class WooPosItemsSearchViewModelTest {
800800
)
801801
}
802802

803-
private suspend fun mockSuccessfulPagination(query: String, products: List<com.woocommerce.android.model.Product>) {
803+
private suspend fun mockSuccessfulPagination(query: String, products: List<WooPosProductModelVersion2>) {
804804
whenever(mockDataSource.hasMorePages).thenReturn(true)
805805
whenever(mockDataSource.loadMore(query)).thenReturn(
806806
Result.success(products)
@@ -816,8 +816,8 @@ class WooPosItemsSearchViewModelTest {
816816

817817
private fun mockCachedThenRemoteSearch(
818818
query: String,
819-
cachedProduct: com.woocommerce.android.model.Product,
820-
remoteProduct: com.woocommerce.android.model.Product
819+
cachedProduct: WooPosProductModelVersion2,
820+
remoteProduct: WooPosProductModelVersion2
821821
) {
822822
wheneverBlocking { mockDataSource.searchLocalProducts(query) }.thenReturn(listOf(cachedProduct))
823823
wheneverBlocking {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.woocommerce.android.ui.woopos.util
2+
3+
import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVersion2
4+
5+
fun generateWooPosProduct(
6+
productId: Long = 1,
7+
parentId: Long? = null,
8+
productName: String = "Product 1",
9+
status: WooPosProductModelVersion2.WooPosProductStatus = WooPosProductModelVersion2.WooPosProductStatus.PUBLISH,
10+
amount: String = "10.0",
11+
productType: WooPosProductModelVersion2.WooPosProductType = WooPosProductModelVersion2.WooPosProductType.SIMPLE,
12+
isDownloadable: Boolean = false,
13+
images: List<WooPosProductModelVersion2.WooPosProductImage> = emptyList(),
14+
variationIds: List<Long> = emptyList(),
15+
) = WooPosProductModelVersion2(
16+
remoteId = productId,
17+
name = productName,
18+
pricing = WooPosProductModelVersion2.WooPosPricing.RegularPricing(amount.toBigDecimal()),
19+
type = productType,
20+
isDownloadable = isDownloadable,
21+
parentId = parentId,
22+
sku = "",
23+
globalUniqueId = "",
24+
status = status,
25+
description = "",
26+
shortDescription = "",
27+
lastModified = "",
28+
images = images,
29+
variationIds = variationIds,
30+
)

WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/util/WooPosProductUtils.kt

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)