-
Notifications
You must be signed in to change notification settings - Fork 136
[POS][Local Catalog] WooPosVariation #14564
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
malinajirka
merged 22 commits into
trunk
from
woomob-1244-woo-poslocal-catalog-use-new-pos-specific-variation-model-in
Sep 8, 2025
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
5ac8671
Create POS-specific variation model
samiuelson 24f6350
Migrate POS to WooPosVariation
samiuelson f10a079
Satisfy detekt's complaints
samiuelson 62474bf
Remove comment
samiuelson f5af9f9
Handle null attributes
samiuelson bf1f27a
Update tests
samiuelson 4d83ab3
Merge branch 'trunk' into woomob-1244-woo-poslocal-catalog-use-new-po…
samiuelson 74ad241
Clean up code
samiuelson 5095fb6
Revert WooPosGetVariationById
samiuelson abc8221
Map WCProductVariationModel → WooPosVariation
samiuelson 64a0510
Remove unnecessary try-catch
samiuelson b4997b7
Satisfy detekt's complaints
samiuelson 2c8b8fb
Make Gson instance a singleton
samiuelson e2dc6d0
Satisfy detekt's complaints
samiuelson 43257a1
Merge branch 'trunk' into woomob-1244-woo-poslocal-catalog-use-new-po…
samiuelson ed12b16
Fix tests
samiuelson d12819b
Fix tests
samiuelson a4c3903
Fix tests
samiuelson cf5cecd
Log JsonSyntaxException
samiuelson abba849
Extract JSON parsing and data mapping to the mapper class
samiuelson 2dc7ce9
Update tests
samiuelson 4ae74f9
Merge branch 'trunk' into woomob-1244-woo-poslocal-catalog-use-new-po…
samiuelson 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
29 changes: 29 additions & 0 deletions
29
WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/common/data/WooPosVariation.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,29 @@ | ||
| package com.woocommerce.android.ui.woopos.common.data | ||
|
|
||
| import java.math.BigDecimal | ||
|
|
||
| /** | ||
| * POS-specific product variation model containing only the fields needed for POS functionality. | ||
| * This model provides better performance and cleaner separation compared to the general ProductVariation model. | ||
| */ | ||
| data class WooPosVariation( | ||
| val remoteVariationId: Long, | ||
| val remoteProductId: Long, | ||
| val globalUniqueId: String, | ||
| val price: BigDecimal?, | ||
| val image: WooPosVariationImage?, | ||
| val attributes: List<WooPosVariationAttribute>, | ||
| val isVisible: Boolean, | ||
| val isDownloadable: Boolean | ||
| ) { | ||
|
|
||
| data class WooPosVariationImage( | ||
| val source: String | ||
| ) | ||
|
|
||
| data class WooPosVariationAttribute( | ||
| val id: Long?, | ||
| val name: String?, | ||
| val option: String? | ||
| ) | ||
| } | ||
175 changes: 175 additions & 0 deletions
175
...ce/src/main/kotlin/com/woocommerce/android/ui/woopos/common/data/WooPosVariationMapper.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,175 @@ | ||
| package com.woocommerce.android.ui.woopos.common.data | ||
|
|
||
| import com.google.gson.Gson | ||
| import com.google.gson.JsonSyntaxException | ||
| import com.google.gson.reflect.TypeToken | ||
| import com.woocommerce.android.R | ||
| import com.woocommerce.android.model.Product | ||
| import com.woocommerce.android.model.ProductVariation | ||
| import com.woocommerce.android.util.WooLog | ||
| import com.woocommerce.android.viewmodel.ResourceProvider | ||
| import org.wordpress.android.fluxc.model.WCProductVariationModel | ||
| import org.wordpress.android.fluxc.persistence.entity.pos.WCPosVariationModel | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| /** | ||
| * Mapper class responsible for all WooPosVariation conversions and transformations. | ||
| * This centralizes all variation mapping logic, JSON parsing, and name generation. | ||
| */ | ||
| @Singleton | ||
| class WooPosVariationMapper @Inject constructor( | ||
| private val gson: Gson | ||
| ) { | ||
| fun fromProductVariation(productVariation: ProductVariation): WooPosVariation { | ||
| return WooPosVariation( | ||
| remoteVariationId = productVariation.remoteVariationId, | ||
| remoteProductId = productVariation.remoteProductId, | ||
| globalUniqueId = productVariation.globalUniqueId, | ||
| price = productVariation.price, | ||
| image = productVariation.image?.let { WooPosVariation.WooPosVariationImage(it.source) }, | ||
| attributes = productVariation.attributes.map { | ||
| WooPosVariation.WooPosVariationAttribute( | ||
| id = it.id, | ||
| name = it.name, | ||
| option = it.option | ||
| ) | ||
| }, | ||
| isVisible = productVariation.isVisible, | ||
| isDownloadable = productVariation.isDownloadable | ||
| ) | ||
| } | ||
|
|
||
| @Suppress("SwallowedException") | ||
| fun fromWCProductVariationModel(model: WCProductVariationModel): WooPosVariation { | ||
| val attributesList = model.attributeList?.map { attribute -> | ||
| WooPosVariation.WooPosVariationAttribute( | ||
| id = attribute.id, | ||
| name = attribute.name, | ||
| option = attribute.option | ||
| ) | ||
| } ?: emptyList() | ||
|
|
||
| val imageModel = try { | ||
| if (model.image.isNotEmpty()) model.getImageModel() else null | ||
| } catch (e: JsonSyntaxException) { | ||
| WooLog.w( | ||
| WooLog.T.POS, | ||
| "Failed to parse image from JSON attributes for variation " + | ||
| "${model.remoteVariationId.value}: ${e.message}" | ||
| ) | ||
| null | ||
| } | ||
|
|
||
| return WooPosVariation( | ||
| remoteVariationId = model.remoteVariationId.value, | ||
| remoteProductId = model.remoteProductId.value, | ||
| globalUniqueId = model.globalUniqueId, | ||
| price = model.price.toBigDecimalOrNull(), | ||
| image = imageModel?.src?.let { WooPosVariation.WooPosVariationImage(it) }, | ||
| attributes = attributesList, | ||
| isVisible = model.status == "publish", | ||
| isDownloadable = model.downloadable | ||
| ) | ||
| } | ||
|
|
||
| @Suppress("SwallowedException") | ||
| fun fromWCPosVariationModel(model: WCPosVariationModel): WooPosVariation { | ||
| val attributesList = parseAttributesJson(model.attributesJson) | ||
|
|
||
| return WooPosVariation( | ||
| remoteVariationId = model.remoteVariationId.value, | ||
| remoteProductId = model.remoteProductId.value, | ||
| globalUniqueId = model.globalUniqueId, | ||
| price = model.price.toBigDecimalOrNull(), | ||
| image = if (model.imageUrl.isNotEmpty()) WooPosVariation.WooPosVariationImage(model.imageUrl) else null, | ||
| attributes = attributesList, | ||
| isVisible = model.status == "publish", | ||
| isDownloadable = model.downloadable | ||
| ) | ||
| } | ||
|
|
||
| fun getNameForPOS( | ||
| variation: WooPosVariation, | ||
| parentProduct: Product? = null, | ||
| resourceProvider: ResourceProvider, | ||
| ): String { | ||
| return parentProduct?.variationEnabledAttributes?.joinToString(", ") { attribute -> | ||
| val option = variation.attributes.firstOrNull { it.name == attribute.name } | ||
| if (option?.option != null) { | ||
| "${attribute.name}: ${option.option}" | ||
| } else { | ||
| resourceProvider.getString( | ||
| R.string.woopos_variations_any_variation, | ||
| attribute.name | ||
| ) | ||
| } | ||
| } ?: variation.attributes.joinToString(", ") { attribute -> | ||
| when { | ||
| attribute.option != null && attribute.name != null -> "${attribute.name}: ${attribute.option}" | ||
| attribute.option != null -> attribute.option | ||
| attribute.name != null -> resourceProvider.getString( | ||
| R.string.woopos_variations_any_variation, | ||
| attribute.name | ||
| ) | ||
| else -> "" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun getName(variation: WooPosVariation, parentProduct: Product? = null): String { | ||
| return parentProduct?.variationEnabledAttributes?.joinToString(" - ") { attribute -> | ||
| val option = variation.attributes.firstOrNull { it.name == attribute.name } | ||
| option?.option ?: "Any ${attribute.name}" | ||
| } ?: variation.attributes.mapNotNull { it.option }.joinToString(" - ") | ||
| } | ||
|
|
||
| /** | ||
| * Parses JSON string containing variation attributes into a list of WooPosVariationAttribute objects. | ||
| * | ||
| * @param attributesJson The JSON string to parse | ||
| * @return List of parsed attributes, or empty list if parsing fails | ||
| */ | ||
| private fun parseAttributesJson(attributesJson: String): List<WooPosVariation.WooPosVariationAttribute> { | ||
| if (attributesJson.isEmpty()) return emptyList() | ||
|
|
||
| return try { | ||
| val type = object : TypeToken<List<AttributeJsonItem>>() {}.type | ||
| val items: List<AttributeJsonItem> = gson.fromJson(attributesJson, type) | ||
| items.map { item -> | ||
| WooPosVariation.WooPosVariationAttribute( | ||
| id = item.id, | ||
| name = item.name, | ||
| option = item.option | ||
| ) | ||
| } | ||
| } catch (e: JsonSyntaxException) { | ||
| WooLog.w(WooLog.T.POS, "Failed to parse attributes JSON: ${e.message}") | ||
| emptyList() | ||
| } | ||
| } | ||
|
|
||
| private data class AttributeJsonItem( | ||
| val id: Long?, | ||
| val name: String?, | ||
| val option: String? | ||
| ) | ||
| } | ||
|
|
||
| fun ProductVariation.toWooPosVariation(mapper: WooPosVariationMapper): WooPosVariation = | ||
| mapper.fromProductVariation(this) | ||
|
|
||
| fun WCProductVariationModel.toWooPosVariation(mapper: WooPosVariationMapper): WooPosVariation = | ||
| mapper.fromWCProductVariationModel(this) | ||
|
|
||
| fun WCPosVariationModel.toWooPosVariation(mapper: WooPosVariationMapper): WooPosVariation = | ||
| mapper.fromWCPosVariationModel(this) | ||
|
|
||
| fun WooPosVariation.getNameForPOS( | ||
| mapper: WooPosVariationMapper, | ||
| parentProduct: Product? = null, | ||
| resourceProvider: ResourceProvider, | ||
| ): String = mapper.getNameForPOS(this, parentProduct, resourceProvider) | ||
|
|
||
| fun WooPosVariation.getName(mapper: WooPosVariationMapper, parentProduct: Product? = null): String = | ||
| mapper.getName(this, parentProduct) |
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
4 changes: 2 additions & 2 deletions
4
...mmerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierResult.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
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
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 is technically enough for now, but we'll likely need to have access to both sale and regular prices quite soon, so I'd consider including them similarly to how we do it in the product model. Having said that, feel free to leave it as is.
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'd leave these props for now and update the model when we need them.