-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* created a confirmation of placed order screen * moved checkout info to a ViewModel * passed shipping info to checkout confirmation * in progress * changed layout a bit and cleared cart when leaving the confirmation screen using lifecycle owner * fixed the inability to update payment and shipping info by removing an unnecessary remember * added email address to the confirmation screen
- Loading branch information
Showing
5 changed files
with
243 additions
and
45 deletions.
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() | ||
} | ||
} |