-
Notifications
You must be signed in to change notification settings - Fork 5
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
Allow selecting different variant options, respect available for sale #181
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,7 @@ import androidx.compose.ui.unit.Dp | |
import androidx.compose.ui.unit.dp | ||
import com.shopify.checkout_sdk_mobile_buy_integration_sample.R | ||
import com.shopify.checkout_sdk_mobile_buy_integration_sample.common.ID | ||
import com.shopify.checkout_sdk_mobile_buy_integration_sample.common.components.MoneyText | ||
import com.shopify.checkout_sdk_mobile_buy_integration_sample.common.components.MoneyRangeText | ||
import com.shopify.checkout_sdk_mobile_buy_integration_sample.common.components.RemoteImage | ||
import com.shopify.checkout_sdk_mobile_buy_integration_sample.common.ui.theme.defaultProductImageHeight | ||
import com.shopify.checkout_sdk_mobile_buy_integration_sample.common.ui.theme.defaultProductImageHeightLg | ||
|
@@ -79,9 +79,11 @@ fun ProductCollectionProduct( | |
maxLines = 1, | ||
) | ||
|
||
MoneyText( | ||
currency = product.priceRange.maxVariantPrice.currencyCode, | ||
price = product.priceRange.maxVariantPrice.amount, | ||
MoneyRangeText( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be a range for products with differently priced variants |
||
fromPrice = product.priceRange.minVariantPrice.amount, | ||
fromCurrencyCode = product.priceRange.minVariantPrice.currencyCode, | ||
toPrice = product.priceRange.maxVariantPrice.amount, | ||
toCurrencyCode = product.priceRange.maxVariantPrice.currencyCode, | ||
style = MaterialTheme.typography.bodyMedium, | ||
color = textColor, | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright 2023-present, Shopify Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
package com.shopify.checkout_sdk_mobile_buy_integration_sample.products.product | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.material3.ButtonDefaults | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.OutlinedButton | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.text.style.TextDecoration | ||
import androidx.compose.ui.unit.dp | ||
import com.shopify.checkout_sdk_mobile_buy_integration_sample.common.components.BodyMedium | ||
import com.shopify.checkout_sdk_mobile_buy_integration_sample.common.components.BodySmall | ||
import com.shopify.checkout_sdk_mobile_buy_integration_sample.products.product.data.ProductVariantOptionDetails | ||
|
||
@Composable | ||
fun OptionSelector( | ||
availableOptions: Map<String, List<ProductVariantOptionDetails>>, | ||
selectedOptions: Map<String, String>, | ||
onSelected: (name: String, value: String) -> Unit, | ||
) { | ||
availableOptions.forEach { (optionName, optionValues) -> | ||
Column(verticalArrangement = Arrangement.spacedBy(4.dp)) { | ||
|
||
BodySmall(optionName) | ||
|
||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { | ||
optionValues.forEach { optionDetails -> | ||
val isSelected = selectedOptions[optionName] == optionDetails.name | ||
|
||
OutlinedButton( | ||
onClick = { onSelected(optionName, optionDetails.name) }, | ||
enabled = optionDetails.availableForSale, | ||
colors = if (isSelected) { | ||
ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.onBackground) | ||
} else { | ||
ButtonDefaults.outlinedButtonColors(containerColor = Color.Unspecified) | ||
} | ||
) { | ||
BodyMedium( | ||
optionDetails.name, | ||
color = if (isSelected) MaterialTheme.colorScheme.background else MaterialTheme.colorScheme.onBackground, | ||
textDecoration = if (optionDetails.availableForSale) TextDecoration.None else TextDecoration.LineThrough, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,6 @@ import androidx.compose.ui.res.stringResource | |
import androidx.compose.ui.unit.dp | ||
import com.shopify.checkout_sdk_mobile_buy_integration_sample.R | ||
import com.shopify.checkout_sdk_mobile_buy_integration_sample.common.components.BodyMedium | ||
import com.shopify.checkout_sdk_mobile_buy_integration_sample.common.components.BodySmall | ||
import com.shopify.checkout_sdk_mobile_buy_integration_sample.common.components.Header2 | ||
import com.shopify.checkout_sdk_mobile_buy_integration_sample.common.components.MoneyText | ||
import com.shopify.checkout_sdk_mobile_buy_integration_sample.common.components.ProgressIndicator | ||
|
@@ -119,19 +118,23 @@ fun ProductView( | |
) | ||
|
||
Column(verticalArrangement = Arrangement.spacedBy(5.dp)) { | ||
// TODO deal with a variable amount of variants | ||
val variant = product.variants!!.first() | ||
val variant = productUIState.selectedVariant | ||
MoneyText(variant.price.currencyCode, variant.price.amount) | ||
BodySmall( | ||
stringResource(id = R.string.product_taxes_included) | ||
) | ||
} | ||
|
||
OptionSelector( | ||
availableOptions = productUIState.availableOptions, | ||
selectedOptions = productUIState.selectedVariant.selectedOptions.associate { it.name to it.value } | ||
) { name, value -> | ||
productViewModel.updateSelectedOption(name, value) | ||
} | ||
|
||
QuantitySelector(enabled = true, quantity = productUIState.addQuantityAmount) { quantity -> | ||
productViewModel.setAddQuantityAmount(quantity) | ||
} | ||
|
||
AddToCartButton( | ||
enabled = productUIState.selectedVariant.availableForSale, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. disable add to cart if the selected variant is not available for sale |
||
loading = productUIState.isAddingToCart, | ||
modifier = Modifier.fillMaxWidth() | ||
) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ class ProductRepository( | |
|
||
suspend fun getProduct(productId: ID): Product { | ||
return suspendCoroutine { continuation -> | ||
client.fetchProduct(productId = productId, numVariants = 1, { result -> | ||
client.fetchProduct(productId = productId, numVariants = 20, { result -> | ||
val product = result.data?.product | ||
if (product == null) { | ||
continuation.resumeWith(Result.failure(RuntimeException("Failed to fetch product"))) | ||
|
@@ -52,14 +52,14 @@ class ProductRepository( | |
if (products == null) { | ||
continuation.resumeWith(Result.failure(RuntimeException("Failed to fetch products"))) | ||
} else { | ||
val products = Products( | ||
val result = Products( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. small fix for the var name |
||
products = products.edges.map { it.node.toLocal() }, | ||
pageInfo = PageInfo( | ||
startCursor = products.pageInfo.startCursor, | ||
endCursor = products.pageInfo.endCursor, | ||
) | ||
) | ||
continuation.resumeWith(Result.success(products)) | ||
continuation.resumeWith(Result.success(result)) | ||
} | ||
}, { exception -> | ||
continuation.resumeWith(Result.failure(exception)) | ||
|
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.
display selected options on the cart page
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.
What happens with products without
variantDescription
?edit:
nvm, it's a non-nullable attribute