-
Notifications
You must be signed in to change notification settings - Fork 42
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
Demo app: Order confirmation #598
Merged
breedx-splk
merged 7 commits into
open-telemetry:main
from
magda-woj:checkout-confirmation
Sep 18, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6d3363b
created a confirmation of placed order screen
magda-woj de997f1
moved checkout info to a ViewModel
magda-woj c27b86b
passed shipping info to checkout confirmation
magda-woj 3f91d7a
in progress
magda-woj 37fdc29
changed layout a bit and cleared cart when leaving the confirmation s…
magda-woj 9227524
fixed the inability to update payment and shipping info by removing a…
magda-woj b5059c4
added email address to the confirmation screen
magda-woj 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 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 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
141 changes: 141 additions & 0 deletions
141
demo-app/src/main/java/io/opentelemetry/android/demo/shop/ui/cart/CheckoutConfirmation.kt
This file contains 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,141 @@ | ||
package io.opentelemetry.android.demo.shop.ui.cart | ||
|
||
import androidx.compose.foundation.layout.* | ||
import androidx.compose.foundation.rememberScrollState | ||
import androidx.compose.foundation.verticalScroll | ||
import androidx.compose.material3.* | ||
import androidx.compose.runtime.* | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalLifecycleOwner | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleEventObserver | ||
import io.opentelemetry.android.demo.shop.ui.products.ProductCard | ||
import java.util.Locale | ||
|
||
@Composable | ||
fun CheckoutConfirmationScreen( | ||
cartViewModel: CartViewModel, | ||
checkoutInfoViewModel: CheckoutInfoViewModel | ||
) { | ||
val lifecycleOwner = LocalLifecycleOwner.current | ||
|
||
DisposableEffect(lifecycleOwner) { | ||
val observer = LifecycleEventObserver { _, event -> | ||
if (event == Lifecycle.Event.ON_PAUSE || event == Lifecycle.Event.ON_STOP) { | ||
cartViewModel.clearCart() | ||
} | ||
} | ||
lifecycleOwner.lifecycle.addObserver(observer) | ||
onDispose { | ||
lifecycleOwner.lifecycle.removeObserver(observer) | ||
} | ||
} | ||
|
||
val shippingInfo = checkoutInfoViewModel.shippingInfo | ||
|
||
Column( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.padding(16.dp) | ||
.verticalScroll(rememberScrollState()) | ||
) { | ||
Text( | ||
text = "Your order is complete!", | ||
fontSize = 24.sp, | ||
textAlign = TextAlign.Center, | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(bottom = 16.dp) | ||
) | ||
|
||
Text( | ||
text = "We've sent a confirmation email to ${shippingInfo.email}.", | ||
fontSize = 18.sp, | ||
textAlign = TextAlign.Center, | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(bottom = 32.dp) | ||
) | ||
|
||
val cartItems = cartViewModel.cartItems.collectAsState().value | ||
cartItems.forEach { cartItem -> | ||
Row( | ||
modifier = Modifier | ||
.fillMaxWidth(), | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
ProductCard( | ||
product = cartItem.product, | ||
onProductClick = {}, | ||
modifier = Modifier | ||
.width(300.dp) | ||
.height(170.dp), | ||
isNarrow = true | ||
) | ||
Column( | ||
modifier = Modifier | ||
.fillMaxHeight(), | ||
verticalArrangement = Arrangement.Center | ||
) { | ||
Text( | ||
text = "Quantity: ${cartItem.quantity}", | ||
fontSize = 12.sp, | ||
modifier = Modifier | ||
.padding(horizontal = 8.dp) | ||
) | ||
|
||
Text( | ||
text = "Total: \$${String.format(Locale.US, "%.2f", cartItem.totalPrice())}", | ||
fontSize = 14.sp, | ||
modifier = Modifier | ||
.padding(8.dp), | ||
) | ||
} | ||
} | ||
} | ||
|
||
Text( | ||
text = "Total Price: \$${String.format(Locale.US, "%.2f", cartViewModel.getTotalPrice())}", | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(top = 16.dp, bottom = 16.dp), | ||
textAlign = TextAlign.End | ||
) | ||
|
||
Card( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(vertical = 16.dp), | ||
) { | ||
Column( | ||
modifier = Modifier.padding(16.dp), | ||
verticalArrangement = Arrangement.spacedBy(8.dp) | ||
) { | ||
Text( | ||
text = "Shipping Data", | ||
fontWeight = FontWeight.Bold | ||
) | ||
Text( | ||
text = "Street: ${shippingInfo.streetAddress}", | ||
) | ||
Text( | ||
text = "City: ${shippingInfo.city}", | ||
) | ||
Text( | ||
text = "State: ${shippingInfo.state}", | ||
) | ||
Text( | ||
text = "Zip Code: ${shippingInfo.zipCode}", | ||
) | ||
Text( | ||
text = "Country: ${shippingInfo.country}", | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains 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
53 changes: 53 additions & 0 deletions
53
demo-app/src/main/java/io/opentelemetry/android/demo/shop/ui/cart/CheckoutInfoViewModel.kt
This file contains 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,53 @@ | ||
package io.opentelemetry.android.demo.shop.ui.cart | ||
|
||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.lifecycle.ViewModel | ||
|
||
data class ShippingInfo( | ||
var email: String = "[email protected]", | ||
var streetAddress: String = "1600 Amphitheatre Parkway", | ||
var zipCode: String = "94043", | ||
var city: String = "Mountain View", | ||
var state: String = "CA", | ||
var country: String = "United States" | ||
) { | ||
fun isComplete(): Boolean { | ||
return arrayOf(email, streetAddress, zipCode, city, state, country) | ||
.all { it.isNotBlank() } | ||
} | ||
} | ||
|
||
data class PaymentInfo( | ||
var creditCardNumber: String = "4432-8015-6152-0454", | ||
var expiryMonth: String = "01", | ||
var expiryYear: String = "2030", | ||
var cvv: String = "137" | ||
) { | ||
fun isComplete(): Boolean { | ||
return arrayOf(creditCardNumber, expiryMonth, expiryYear, cvv) | ||
.all { it.isNotBlank() } | ||
} | ||
} | ||
|
||
class CheckoutInfoViewModel : ViewModel() { | ||
|
||
var shippingInfo by mutableStateOf(ShippingInfo()) | ||
private set | ||
|
||
var paymentInfo by mutableStateOf(PaymentInfo()) | ||
private set | ||
|
||
fun updateShippingInfo(newShippingInfo: ShippingInfo) { | ||
shippingInfo = newShippingInfo | ||
} | ||
|
||
fun updatePaymentInfo(newPaymentInfo: PaymentInfo) { | ||
paymentInfo = newPaymentInfo | ||
} | ||
|
||
fun canProceedToCheckout(): Boolean { | ||
return shippingInfo.isComplete() && paymentInfo.isComplete() | ||
} | ||
} |
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.
Ideally these would be translatable strings, but for now it's perfectly fine to just have them coded.