Skip to content

Commit 55f2c0b

Browse files
committed
Add SwatchesOption to FetchedProduct and UpdatedProduct
1 parent 3494239 commit 55f2c0b

File tree

7 files changed

+115
-35
lines changed

7 files changed

+115
-35
lines changed

src/main/kotlin/com/ecwid/apiclient/v3/ApiClientHelper.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,8 @@ private fun createProductOptionsPolymorphicType(): PolymorphicType<ProductOption
527527
"textfield" to ProductOption.TextFieldOption::class.java,
528528
"textarea" to ProductOption.TextAreaOption::class.java,
529529
"date" to ProductOption.DateOption::class.java,
530-
"files" to ProductOption.FilesOption::class.java
530+
"files" to ProductOption.FilesOption::class.java,
531+
"swatches" to ProductOption.SwatchesOption::class.java,
531532
)
532533
)
533534
}

src/main/kotlin/com/ecwid/apiclient/v3/converter/FetchedProduct.kt

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ fun FetchedProduct.ProductOption.toUpdated() = when (this) {
108108
is FetchedProduct.ProductOption.SelectOption -> toUpdated()
109109
is FetchedProduct.ProductOption.SizeOption -> toUpdated()
110110
is FetchedProduct.ProductOption.RadioOption -> toUpdated()
111+
is FetchedProduct.ProductOption.SwatchesOption -> toUpdated()
111112
is FetchedProduct.ProductOption.CheckboxOption -> toUpdated()
112113
is FetchedProduct.ProductOption.TextFieldOption -> toUpdated()
113114
is FetchedProduct.ProductOption.TextAreaOption -> toUpdated()
@@ -139,6 +140,14 @@ fun FetchedProduct.ProductOption.RadioOption.toUpdated() = UpdatedProduct.Produc
139140
required = required
140141
)
141142

143+
fun FetchedProduct.ProductOption.SwatchesOption.toUpdated() = UpdatedProduct.ProductOption.SwatchesOption(
144+
name = name,
145+
nameTranslated = nameTranslated,
146+
choices = choices.map { it.toUpdated() },
147+
defaultChoice = defaultChoice,
148+
required = required,
149+
)
150+
142151
fun FetchedProduct.ProductOption.CheckboxOption.toUpdated() = UpdatedProduct.ProductOption.CheckboxOption(
143152
name = name,
144153
nameTranslated = nameTranslated,
@@ -171,12 +180,28 @@ fun FetchedProduct.ProductOption.FilesOption.toUpdated() = UpdatedProduct.Produc
171180
required = required
172181
)
173182

174-
fun FetchedProduct.ProductOptionChoice.toUpdated() = UpdatedProduct.ProductOptionChoice(
175-
text = text,
176-
textTranslated = textTranslated,
177-
priceModifier = priceModifier,
178-
priceModifierType = priceModifierType
179-
)
183+
fun FetchedProduct.ProductOptionChoice.toUpdated(): UpdatedProduct.ProductOptionChoice {
184+
return when (this) {
185+
is FetchedProduct.ProductOptionChoice.SelectChoice ->
186+
UpdatedProduct.ProductOptionChoice.SelectChoice(
187+
text = text,
188+
textTranslated = textTranslated,
189+
priceModifier = priceModifier,
190+
priceModifierType = priceModifierType
191+
)
192+
193+
is FetchedProduct.ProductOptionChoice.SwatchChoice ->
194+
UpdatedProduct.ProductOptionChoice.SwatchChoice(
195+
text = text,
196+
textTranslated = textTranslated,
197+
priceModifier = priceModifier,
198+
priceModifierType = priceModifierType,
199+
hexCodes = hexCodes,
200+
imageId = imageId,
201+
)
202+
}
203+
204+
}
180205

181206
fun FetchedProduct.ShippingSettings.toUpdated() = UpdatedProduct.ShippingSettings(
182207
type = type,

src/main/kotlin/com/ecwid/apiclient/v3/dto/product/enums/ProductOptionType.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ enum class ProductOptionType {
88
TEXTAREA,
99
DATE,
1010
FILES,
11-
SIZE
11+
SIZE,
12+
SWATCHES,
1213
}

src/main/kotlin/com/ecwid/apiclient/v3/dto/product/request/UpdatedProduct.kt

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ data class UpdatedProduct(
139139
override val required: Boolean? = null
140140
) : ProductOption(ProductOptionType.RADIO), ChoiceBased
141141

142+
data class SwatchesOption(
143+
override val name: String = "",
144+
override val nameTranslated: LocalizedValueMap? = null,
145+
override val choices: List<ProductOptionChoice> = listOf(),
146+
override val defaultChoice: Int? = null,
147+
override val required: Boolean? = null,
148+
val useImageAsSwatchSelector: Boolean = false,
149+
) : ProductOption(ProductOptionType.SWATCHES), ChoiceBased
150+
142151
data class CheckboxOption(
143152
override val name: String = "",
144153
override val nameTranslated: LocalizedValueMap? = null,
@@ -176,7 +185,7 @@ data class UpdatedProduct(
176185
fun createSelectOption(
177186
name: String = "",
178187
nameTranslated: LocalizedValueMap? = null,
179-
choices: List<ProductOptionChoice> = listOf(),
188+
choices: List<ProductOptionChoice.SelectChoice> = listOf(),
180189
defaultChoice: Int? = null,
181190
required: Boolean? = null
182191
) = SelectOption(
@@ -190,7 +199,7 @@ data class UpdatedProduct(
190199
fun createSizeOption(
191200
name: String = "",
192201
nameTranslated: LocalizedValueMap? = null,
193-
choices: List<ProductOptionChoice> = listOf(),
202+
choices: List<ProductOptionChoice.SelectChoice> = listOf(),
194203
defaultChoice: Int? = null,
195204
required: Boolean? = null
196205
) = SizeOption(
@@ -204,7 +213,7 @@ data class UpdatedProduct(
204213
fun createRadioOption(
205214
name: String = "",
206215
nameTranslated: LocalizedValueMap? = null,
207-
choices: List<ProductOptionChoice> = listOf(),
216+
choices: List<ProductOptionChoice.SelectChoice> = listOf(),
208217
defaultChoice: Int? = null,
209218
required: Boolean? = null
210219
) = RadioOption(
@@ -218,7 +227,7 @@ data class UpdatedProduct(
218227
fun createCheckboxOption(
219228
name: String = "",
220229
nameTranslated: LocalizedValueMap? = null,
221-
choices: List<ProductOptionChoice> = listOf(),
230+
choices: List<ProductOptionChoice.SelectChoice> = listOf(),
222231
defaultChoice: Int? = null,
223232
required: Boolean? = null
224233
) = CheckboxOption(
@@ -271,12 +280,28 @@ data class UpdatedProduct(
271280
}
272281
}
273282

274-
data class ProductOptionChoice(
275-
val text: String = "",
276-
val textTranslated: LocalizedValueMap? = null,
277-
val priceModifier: Double? = null,
278-
val priceModifierType: PriceModifierType? = null,
279-
)
283+
sealed class ProductOptionChoice {
284+
abstract val text: String
285+
abstract val textTranslated: LocalizedValueMap?
286+
abstract val priceModifier: Double?
287+
abstract val priceModifierType: PriceModifierType?
288+
289+
data class SelectChoice(
290+
override val text: String = "",
291+
override val textTranslated: LocalizedValueMap? = null,
292+
override val priceModifier: Double? = null,
293+
override val priceModifierType: PriceModifierType? = null,
294+
) : ProductOptionChoice()
295+
296+
data class SwatchChoice(
297+
override val text: String = "",
298+
override val textTranslated: LocalizedValueMap? = null,
299+
override val priceModifier: Double? = null,
300+
override val priceModifierType: PriceModifierType? = null,
301+
val hexCodes: List<String> = emptyList(),
302+
val imageId: String? = null,
303+
) : ProductOptionChoice()
304+
}
280305

281306
data class ShippingSettings(
282307
val type: ShippingSettingsType? = null,

src/main/kotlin/com/ecwid/apiclient/v3/dto/product/result/FetchedProduct.kt

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,15 @@ data class FetchedProduct(
240240
override val required: Boolean = false
241241
) : ProductOption(ProductOptionType.RADIO), ChoiceBased
242242

243+
data class SwatchesOption(
244+
override val name: String = "",
245+
override val nameTranslated: LocalizedValueMap? = null,
246+
override val choices: List<ProductOptionChoice> = listOf(),
247+
override val defaultChoice: Int = 0,
248+
override val required: Boolean = false,
249+
val useImageAsSwatchSelector: Boolean = false,
250+
) : ProductOption(ProductOptionType.SWATCHES), ChoiceBased
251+
243252
data class CheckboxOption(
244253
override val name: String = "",
245254
override val nameTranslated: LocalizedValueMap? = null,
@@ -273,12 +282,28 @@ data class FetchedProduct(
273282
) : ProductOption(ProductOptionType.FILES)
274283
}
275284

276-
data class ProductOptionChoice(
277-
val text: String = "",
278-
val textTranslated: LocalizedValueMap? = null,
279-
val priceModifier: Double = 0.0,
280-
val priceModifierType: PriceModifierType = PriceModifierType.ABSOLUTE
281-
)
285+
sealed class ProductOptionChoice {
286+
abstract val text: String
287+
abstract val textTranslated: LocalizedValueMap?
288+
abstract val priceModifier: Double
289+
abstract val priceModifierType: PriceModifierType
290+
291+
data class SelectChoice(
292+
override val text: String = "",
293+
override val textTranslated: LocalizedValueMap? = null,
294+
override val priceModifier: Double = 0.0,
295+
override val priceModifierType: PriceModifierType = PriceModifierType.ABSOLUTE,
296+
) : ProductOptionChoice()
297+
298+
data class SwatchChoice(
299+
override val text: String = "",
300+
override val textTranslated: LocalizedValueMap? = null,
301+
override val priceModifier: Double = 0.0,
302+
override val priceModifierType: PriceModifierType = PriceModifierType.ABSOLUTE,
303+
val hexCodes: List<String> = emptyList(),
304+
val imageId: String? = null,
305+
) : ProductOptionChoice()
306+
}
282307

283308
data class ShippingSettings(
284309
val type: ShippingSettingsType? = null,

src/test/kotlin/com/ecwid/apiclient/v3/entity/ProductsTest.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ class ProductsTest : BaseEntityTest() {
7878
ProductOption.createSelectOption(
7979
name = "Color",
8080
choices = listOf(
81-
ProductOptionChoice("Black"),
82-
ProductOptionChoice("White"),
83-
ProductOptionChoice("Yellow"),
84-
ProductOptionChoice("Red")
81+
ProductOptionChoice.SelectChoice("Black"),
82+
ProductOptionChoice.SelectChoice("White"),
83+
ProductOptionChoice.SelectChoice("Yellow"),
84+
ProductOptionChoice.SelectChoice("Red")
8585
),
8686
defaultChoice = 0,
8787
required = true
@@ -610,8 +610,8 @@ class ProductsTest : BaseEntityTest() {
610610
ProductOption.createSelectOption(
611611
name = "Color",
612612
choices = listOf(
613-
ProductOptionChoice("Red"),
614-
ProductOptionChoice("Green"),
613+
ProductOptionChoice.SelectChoice("Red"),
614+
ProductOptionChoice.SelectChoice("Green"),
615615
)
616616
)
617617
),
@@ -634,8 +634,8 @@ class ProductsTest : BaseEntityTest() {
634634
ProductOption.createSelectOption(
635635
name = "Color",
636636
choices = listOf(
637-
ProductOptionChoice("Yellow"),
638-
ProductOptionChoice("Green"),
637+
ProductOptionChoice.SelectChoice("Yellow"),
638+
ProductOptionChoice.SelectChoice("Green"),
639639
)
640640
)
641641
),
@@ -1563,9 +1563,9 @@ private fun generateProductFilesOption(): ProductOption.FilesOption {
15631563
)
15641564
}
15651565

1566-
private fun generateProductOptionChoice(): ProductOptionChoice {
1566+
private fun generateProductOptionChoice(): ProductOptionChoice.SelectChoice {
15671567
val enText = "Option choice " + randomAlphanumeric(8)
1568-
return ProductOptionChoice(
1568+
return ProductOptionChoice.SelectChoice(
15691569
text = enText,
15701570
textTranslated = LocalizedValueMap(
15711571
"ru" to "Выбор опции " + randomAlphanumeric(8),

src/test/kotlin/com/ecwid/apiclient/v3/rule/nullablepropertyrules/FetchedProductRules.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,12 @@ val fetchedProductNullablePropertyRules: List<NullablePropertyRule<*, *>> = list
130130
IgnoreNullable(FetchedProduct.ProductOption.RadioOption::nameTranslated),
131131
IgnoreNullable(FetchedProduct.ProductOption.SelectOption::nameTranslated),
132132
IgnoreNullable(FetchedProduct.ProductOption.SizeOption::nameTranslated),
133+
IgnoreNullable(FetchedProduct.ProductOption.SwatchesOption::nameTranslated),
133134
IgnoreNullable(FetchedProduct.ProductOption.TextAreaOption::nameTranslated),
134135
IgnoreNullable(FetchedProduct.ProductOption.TextFieldOption::nameTranslated),
135-
IgnoreNullable(FetchedProduct.ProductOptionChoice::textTranslated),
136+
IgnoreNullable(FetchedProduct.ProductOptionChoice.SelectChoice::textTranslated),
137+
IgnoreNullable(FetchedProduct.ProductOptionChoice.SwatchChoice::textTranslated),
138+
IgnoreNullable(FetchedProduct.ProductOptionChoice.SwatchChoice::imageId),
136139
IgnoreNullable(FetchedProduct.RelatedCategory::categoryId),
137140
IgnoreNullable(FetchedProduct.RelatedCategory::enabled),
138141
IgnoreNullable(FetchedProduct.RelatedCategory::productCount),

0 commit comments

Comments
 (0)