135 lines
5.0 KiB
Kotlin

/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.cupcake.ui
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Divider
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import com.example.cupcake.R
import com.example.cupcake.data.OrderUiState
import com.example.cupcake.ui.components.FormattedPriceLabel
import com.example.cupcake.ui.theme.CupcakeTheme
/**
* This composable expects [orderUiState] that represents the order state, [onCancelButtonClicked]
* lambda that triggers canceling the order and passes the final order to [onSendButtonClicked]
* lambda
*/
@Composable
fun OrderSummaryScreen(
orderUiState: OrderUiState,
onCancelButtonClicked: () -> Unit,
onSendButtonClicked: (String, String) -> Unit,
modifier: Modifier = Modifier
) {
val resources = LocalContext.current.resources
val numberOfCupcakes = resources.getQuantityString(
R.plurals.cupcakes,
orderUiState.quantity,
orderUiState.quantity
)
//Load and format a string resource with the parameters.
val orderSummary = stringResource(
R.string.order_details,
numberOfCupcakes,
orderUiState.flavor,
orderUiState.date,
orderUiState.quantity
)
val newOrder = stringResource(R.string.new_cupcake_order)
//Create a list of order summary to display
val items = listOf(
// Summary line 1: display selected quantity
Pair(stringResource(R.string.quantity), numberOfCupcakes),
// Summary line 2: display selected flavor
Pair(stringResource(R.string.flavor), orderUiState.flavor),
// Summary line 3: display selected pickup date
Pair(stringResource(R.string.pickup_date), orderUiState.date)
)
Column(
modifier = modifier,
verticalArrangement = Arrangement.SpaceBetween
) {
Column(
modifier = Modifier.padding(dimensionResource(R.dimen.padding_medium)),
verticalArrangement = Arrangement.spacedBy(dimensionResource(R.dimen.padding_small))
) {
items.forEach { item ->
Text(item.first.uppercase())
Text(text = item.second, fontWeight = FontWeight.Bold)
Divider(thickness = dimensionResource(R.dimen.thickness_divider))
}
Spacer(modifier = Modifier.height(dimensionResource(R.dimen.padding_small)))
FormattedPriceLabel(
subtotal = orderUiState.price,
modifier = Modifier.align(Alignment.End)
)
}
Row(
modifier = Modifier.padding(dimensionResource(R.dimen.padding_medium))
) {
Column(
verticalArrangement = Arrangement.spacedBy(dimensionResource(R.dimen.padding_small))
) {
Button(
modifier = Modifier.fillMaxWidth(),
onClick = { onSendButtonClicked(newOrder, orderSummary) }
) {
Text(stringResource(R.string.send))
}
OutlinedButton(
modifier = Modifier.fillMaxWidth(),
onClick = onCancelButtonClicked
) {
Text(stringResource(R.string.cancel))
}
}
}
}
}
@Preview
@Composable
fun OrderSummaryPreview() {
CupcakeTheme {
OrderSummaryScreen(
orderUiState = OrderUiState(0, "Test", "Test", "$300.00"),
onSendButtonClicked = { subject: String, summary: String -> },
onCancelButtonClicked = {},
modifier = Modifier.fillMaxHeight()
)
}
}