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

Feat : add navigation to app #115

Open
wants to merge 2 commits 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
107 changes: 104 additions & 3 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,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
),
Expand All @@ -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)
)
)
}

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,17 @@ 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
6 changes: 5 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 @@ -49,6 +49,7 @@ import com.example.cupcake.ui.theme.CupcakeTheme
@Composable
fun StartOrderScreen(
quantityOptions: List<Pair<Int, Int>>,
onNextButtonClicked: (Int) -> Unit,
modifier: Modifier = Modifier
) {
Column(
Expand Down Expand Up @@ -83,7 +84,9 @@ fun StartOrderScreen(
quantityOptions.forEach { item ->
SelectQuantityButton(
labelResourceId = item.first,
onClick = {}
onClick = {
onNextButtonClicked(item.second)
}
)
}
}
Expand Down Expand Up @@ -114,6 +117,7 @@ fun StartOrderPreview() {
CupcakeTheme {
StartOrderScreen(
quantityOptions = DataSource.quantityOptions,
onNextButtonClicked = {},
modifier = Modifier
.fillMaxSize()
.padding(dimensionResource(R.dimen.padding_medium))
Expand Down
7 changes: 5 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,7 @@ fun OrderSummaryPreview() {
CupcakeTheme {
OrderSummaryScreen(
orderUiState = OrderUiState(0, "Test", "Test", "$300.00"),
onSendButtonClicked = { _: String, _: String ->},
modifier = Modifier.fillMaxHeight()
)
}
Expand Down