diff --git a/app/src/main/java/com/example/cupcake/CupcakeScreen.kt b/app/src/main/java/com/example/cupcake/CupcakeScreen.kt index 53e047f..6c51554 100644 --- a/app/src/main/java/com/example/cupcake/CupcakeScreen.kt +++ b/app/src/main/java/com/example/cupcake/CupcakeScreen.kt @@ -15,6 +15,12 @@ */ package com.example.cupcake +import android.content.Context +import android.content.Intent +import androidx.annotation.StringRes +import androidx.compose.foundation.layout.fillMaxHeight +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.padding import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.ArrowBack import androidx.compose.material3.Icon @@ -28,23 +34,40 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue 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.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavHostController +import androidx.navigation.compose.NavHost +import androidx.navigation.compose.composable +import androidx.navigation.compose.currentBackStackEntryAsState import androidx.navigation.compose.rememberNavController +import com.example.cupcake.data.DataSource +import com.example.cupcake.ui.OrderSummaryScreen import com.example.cupcake.ui.OrderViewModel +import com.example.cupcake.ui.SelectOptionScreen +import com.example.cupcake.ui.StartOrderScreen /** * Composable that displays the topBar and displays back button if back navigation is possible. */ +enum class CupcakeScreen(@StringRes val title: Int) { + Start(title = R.string.app_name), + Flavor(title = R.string.choose_flavor), + Pickup(title = R.string.choose_pickup_date), + Summary(title = R.string.order_summary) +} + @Composable fun CupcakeAppBar( + currentScreen: CupcakeScreen, canNavigateBack: Boolean, navigateUp: () -> Unit, modifier: Modifier = Modifier ) { TopAppBar( - title = { Text(stringResource(id = R.string.app_name)) }, + title = { Text(stringResource(id = currentScreen.title)) }, colors = TopAppBarDefaults.mediumTopAppBarColors( containerColor = MaterialTheme.colorScheme.primaryContainer ), @@ -67,16 +90,94 @@ fun CupcakeApp( viewModel: OrderViewModel = viewModel(), navController: NavHostController = rememberNavController() ) { + val backStackEntry by navController.currentBackStackEntryAsState() + val currentScreen = CupcakeScreen.valueOf( + backStackEntry?.destination?.route ?: CupcakeScreen.Start.name + ) Scaffold( topBar = { CupcakeAppBar( - canNavigateBack = false, - navigateUp = { /* TODO: implement back navigation */ } + canNavigateBack = navController.previousBackStackEntry != null, + navigateUp = {navController.navigateUp() }, + currentScreen = currentScreen ) } ) { innerPadding -> val uiState by viewModel.uiState.collectAsState() + NavHost( + navController = navController, + startDestination = CupcakeScreen.Start.name, + modifier = Modifier.padding(innerPadding) + ) { + composable(CupcakeScreen.Start.name) { + StartOrderScreen( + quantityOptions = DataSource.quantityOptions, + onNextButtonClicked = { + viewModel.setQuantity(it) + navController.navigate(CupcakeScreen.Flavor.name) + }, + modifier = Modifier + .fillMaxSize() + .padding(dimensionResource(id = R.dimen.padding_medium)) + ) + } + composable(CupcakeScreen.Flavor.name) { + val context = LocalContext.current + SelectOptionScreen( + subtotal = uiState.price, + onNextButtonClicked = { navController.navigate(CupcakeScreen.Pickup.name) }, + options = DataSource.flavors.map{context.resources.getString(it)}, + onCancelButtonClicked = { cancelOrderAndBackToStart(navController, viewModel) }, + onSelectionChanged = { flavor -> + viewModel.setFlavor(flavor) + }, + modifier = Modifier + .fillMaxHeight() + ) + } + composable(CupcakeScreen.Pickup.name) { + SelectOptionScreen( + subtotal = uiState.price, + options = uiState.pickupOptions, + onCancelButtonClicked = { cancelOrderAndBackToStart(navController, viewModel) }, + onNextButtonClicked = { navController.navigate(CupcakeScreen.Summary.name) }, + onSelectionChanged = {viewModel.setDate(it)}, + modifier = Modifier.fillMaxHeight() + ) + } + composable(CupcakeScreen.Summary.name) { + val context = LocalContext.current + OrderSummaryScreen( + orderUiState = uiState, + modifier = Modifier.fillMaxHeight(), + onCancelButtonClicked = { cancelOrderAndBackToStart(navController, viewModel) }, + onSendButtonClicked = { subject, summary-> + shareOrder(context,summary,subject) + navController.navigate(CupcakeScreen.Start.name) + } + ) + } + } + } +} +private fun cancelOrderAndBackToStart(navController: NavHostController, viewModel: OrderViewModel) { + viewModel.resetOrder() + navController.popBackStack(CupcakeScreen.Start.name,false) +} + +private fun shareOrder(context: Context,summary:String,subject:String){ + val intent = Intent(Intent.ACTION_SEND).apply { + type = "text/plain" + putExtra(Intent.EXTRA_TEXT, summary) + putExtra(Intent.EXTRA_SUBJECT, subject) } + context.startActivity( + Intent.createChooser( + intent, + context.getString(R.string.new_cupcake_order) + ) + ) } + diff --git a/app/src/main/java/com/example/cupcake/ui/SelectOptionScreen.kt b/app/src/main/java/com/example/cupcake/ui/SelectOptionScreen.kt index 84f7718..cedeea4 100644 --- a/app/src/main/java/com/example/cupcake/ui/SelectOptionScreen.kt +++ b/app/src/main/java/com/example/cupcake/ui/SelectOptionScreen.kt @@ -52,6 +52,8 @@ fun SelectOptionScreen( subtotal: String, options: List, onSelectionChanged: (String) -> Unit = {}, + onCancelButtonClicked: () -> Unit = {}, + onNextButtonClicked: () -> Unit = {}, modifier: Modifier = Modifier ) { var selectedValue by rememberSaveable { mutableStateOf("") } @@ -105,7 +107,7 @@ fun SelectOptionScreen( ) { OutlinedButton( modifier = Modifier.weight(1f), - onClick = {} + onClick = {onCancelButtonClicked()} ) { Text(stringResource(R.string.cancel)) } @@ -113,7 +115,9 @@ fun SelectOptionScreen( modifier = Modifier.weight(1f), // the button is enabled when the user makes a selection enabled = selectedValue.isNotEmpty(), - onClick = {} + onClick = { + onNextButtonClicked() + } ) { Text(stringResource(R.string.next)) } diff --git a/app/src/main/java/com/example/cupcake/ui/StartOrderScreen.kt b/app/src/main/java/com/example/cupcake/ui/StartOrderScreen.kt index beb5368..d6d43c0 100644 --- a/app/src/main/java/com/example/cupcake/ui/StartOrderScreen.kt +++ b/app/src/main/java/com/example/cupcake/ui/StartOrderScreen.kt @@ -49,6 +49,7 @@ import com.example.cupcake.ui.theme.CupcakeTheme @Composable fun StartOrderScreen( quantityOptions: List>, + onNextButtonClicked: (Int) -> Unit, modifier: Modifier = Modifier ) { Column( @@ -83,7 +84,9 @@ fun StartOrderScreen( quantityOptions.forEach { item -> SelectQuantityButton( labelResourceId = item.first, - onClick = {} + onClick = { + onNextButtonClicked(item.second) + } ) } } @@ -114,6 +117,7 @@ fun StartOrderPreview() { CupcakeTheme { StartOrderScreen( quantityOptions = DataSource.quantityOptions, + onNextButtonClicked = {}, modifier = Modifier .fillMaxSize() .padding(dimensionResource(R.dimen.padding_medium)) diff --git a/app/src/main/java/com/example/cupcake/ui/SummaryScreen.kt b/app/src/main/java/com/example/cupcake/ui/SummaryScreen.kt index 29a283e..077a41d 100644 --- a/app/src/main/java/com/example/cupcake/ui/SummaryScreen.kt +++ b/app/src/main/java/com/example/cupcake/ui/SummaryScreen.kt @@ -48,6 +48,8 @@ import com.example.cupcake.ui.theme.CupcakeTheme @Composable fun OrderSummaryScreen( orderUiState: OrderUiState, + onCancelButtonClicked: () -> Unit = {}, + onSendButtonClicked: (String,String) -> Unit, modifier: Modifier = Modifier ) { val resources = LocalContext.current.resources @@ -103,13 +105,13 @@ fun OrderSummaryScreen( ) { Button( modifier = Modifier.fillMaxWidth(), - onClick = {} + onClick = {onSendButtonClicked(newOrder, orderSummary)} ) { Text(stringResource(R.string.send)) } OutlinedButton( modifier = Modifier.fillMaxWidth(), - onClick = {} + onClick = {onCancelButtonClicked()} ) { Text(stringResource(R.string.cancel)) } @@ -124,6 +126,7 @@ fun OrderSummaryPreview() { CupcakeTheme { OrderSummaryScreen( orderUiState = OrderUiState(0, "Test", "Test", "$300.00"), + onSendButtonClicked = { _: String, _: String ->}, modifier = Modifier.fillMaxHeight() ) }