Skip to content
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

added nav host and enabled navigation to different screens #105

Open
wants to merge 1 commit into
base: starter
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 107 additions & 4 deletions app/src/main/java/com/example/cupcake/CupcakeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,23 +34,39 @@ 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

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 that displays the topBar and displays back button if back navigation is possible.
*/
@Composable
fun CupcakeAppBar(
currentScreen: CupcakeScreen,
canNavigateBack: Boolean,
navigateUp: () -> Unit,
modifier: Modifier = Modifier
) {
TopAppBar(
title = { Text(stringResource(id = R.string.app_name)) },
title = { Text(stringResource(currentScreen.title)) },
colors = TopAppBarDefaults.mediumTopAppBarColors(
containerColor = MaterialTheme.colorScheme.primaryContainer
),
Expand All @@ -67,16 +89,97 @@ 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 */ }
currentScreen = currentScreen,
canNavigateBack = navController.previousBackStackEntry != null,
navigateUp = { navController.navigateUp() }
)
}
) { innerPadding ->
val uiState by viewModel.uiState.collectAsState()
NavHost(
navController = navController,
startDestination = CupcakeScreen.Start.name,
modifier = Modifier.padding(innerPadding)
) {
composable(route = CupcakeScreen.Start.name) {
StartOrderScreen(
onNextButtonClicked = {
viewModel.setQuantity(it)
navController.navigate(CupcakeScreen.Flavor.name)
},
quantityOptions = DataSource.quantityOptions,
modifier = Modifier
.fillMaxSize()
.padding(dimensionResource(R.dimen.padding_medium))
)
}
composable(route = CupcakeScreen.Flavor.name) {
val context = LocalContext.current
SelectOptionScreen(
subtotal = uiState.price,
options = DataSource.flavors.map { id -> context.resources.getString(id) },
onSelectionChanged = { viewModel.setFlavor(it) },
onNextButtonClicked = { navController.navigate(CupcakeScreen.Pickup.name) },
onCancelButtonClicked = {
cancelOrderAndNavigateToStart(viewModel, navController)
},
modifier = Modifier.fillMaxHeight()
)
}
composable(route = CupcakeScreen.Pickup.name) {
SelectOptionScreen(
subtotal = uiState.price,
onNextButtonClicked = { navController.navigate(CupcakeScreen.Summary.name) },
onCancelButtonClicked = {
cancelOrderAndNavigateToStart(viewModel, navController)
},
options = uiState.pickupOptions,
onSelectionChanged = { viewModel.setDate(it) },
modifier = Modifier.fillMaxHeight()
)
}
composable(route = CupcakeScreen.Summary.name) {
val context = LocalContext.current
OrderSummaryScreen(
orderUiState = uiState,
onCancelButtonClicked = {
cancelOrderAndNavigateToStart(viewModel, navController)
},
onSendButtonClicked = { subject: String, summary: String ->
shareOrder(context, subject = subject, summary = summary)
},
modifier = Modifier.fillMaxHeight()
)
}
}
}
}

private fun shareOrder(context: Context, subject: String, summary: String) {
val intent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_SUBJECT, subject)
putExtra(Intent.EXTRA_TEXT, summary)
}
context.startActivity(
Intent.createChooser(
intent,
context.getString(R.string.new_cupcake_order)
)
)
}

private fun cancelOrderAndNavigateToStart(
viewModel: OrderViewModel,
navController: NavHostController
) {
viewModel.resetOrder()
navController.popBackStack(CupcakeScreen.Start.name, inclusive = false)
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ fun SelectOptionScreen(
subtotal: String,
options: List<String>,
onSelectionChanged: (String) -> Unit = {},
onCancelButtonClicked: () -> Unit = {},
onNextButtonClicked: () -> Unit = {},
modifier: Modifier = Modifier
) {
var selectedValue by rememberSaveable { mutableStateOf("") }
Expand Down Expand Up @@ -105,15 +107,15 @@ fun SelectOptionScreen(
) {
OutlinedButton(
modifier = Modifier.weight(1f),
onClick = {}
onClick = onCancelButtonClicked
) {
Text(stringResource(R.string.cancel))
}
Button(
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))
}
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/example/cupcake/ui/StartOrderScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import com.example.cupcake.ui.theme.CupcakeTheme
*/
@Composable
fun StartOrderScreen(
onNextButtonClicked: (Int) -> Unit,
quantityOptions: List<Pair<Int, Int>>,
modifier: Modifier = Modifier
) {
Expand Down Expand Up @@ -83,7 +84,7 @@ fun StartOrderScreen(
quantityOptions.forEach { item ->
SelectQuantityButton(
labelResourceId = item.first,
onClick = {}
onClick = { onNextButtonClicked(item.second) }
)
}
}
Expand Down Expand Up @@ -113,6 +114,7 @@ fun SelectQuantityButton(
fun StartOrderPreview() {
CupcakeTheme {
StartOrderScreen(
onNextButtonClicked = {},
quantityOptions = DataSource.quantityOptions,
modifier = Modifier
.fillMaxSize()
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/com/example/cupcake/ui/SummaryScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Expand All @@ -124,6 +126,8 @@ fun OrderSummaryPreview() {
CupcakeTheme {
OrderSummaryScreen(
orderUiState = OrderUiState(0, "Test", "Test", "$300.00"),
onSendButtonClicked = { subject: String, summary: String -> },
onCancelButtonClicked = {},
modifier = Modifier.fillMaxHeight()
)
}
Expand Down