|
|
package com.example.pizzaorder
|
|
|
|
|
|
import android.content.Context
|
|
|
import android.content.Intent
|
|
|
import androidx.annotation.StringRes
|
|
|
import androidx.compose.foundation.layout.Arrangement
|
|
|
import androidx.compose.foundation.layout.Column
|
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
|
|
import androidx.compose.foundation.layout.padding
|
|
|
import androidx.compose.material.icons.Icons
|
|
|
import androidx.compose.material.icons.automirrored.filled.ArrowBack
|
|
|
import androidx.compose.material.icons.filled.MoreVert
|
|
|
import androidx.compose.material3.AlertDialog
|
|
|
import androidx.compose.material3.Button
|
|
|
import androidx.compose.material3.ButtonDefaults
|
|
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
|
|
import androidx.compose.material3.HorizontalDivider
|
|
|
import androidx.compose.material3.Icon
|
|
|
import androidx.compose.material3.IconButton
|
|
|
import androidx.compose.material3.MaterialTheme
|
|
|
import androidx.compose.material3.ModalBottomSheet
|
|
|
import androidx.compose.material3.Scaffold
|
|
|
import androidx.compose.material3.Text
|
|
|
import androidx.compose.material3.TopAppBar
|
|
|
import androidx.compose.material3.TopAppBarDefaults
|
|
|
import androidx.compose.material3.rememberModalBottomSheetState
|
|
|
import androidx.compose.runtime.Composable
|
|
|
import androidx.compose.runtime.collectAsState
|
|
|
import androidx.compose.runtime.getValue
|
|
|
import androidx.compose.runtime.mutableStateOf
|
|
|
import androidx.compose.runtime.remember
|
|
|
import androidx.compose.runtime.rememberCoroutineScope
|
|
|
import androidx.compose.runtime.setValue
|
|
|
import androidx.compose.ui.Modifier
|
|
|
import androidx.compose.ui.graphics.Color
|
|
|
import androidx.compose.ui.res.dimensionResource
|
|
|
import androidx.compose.ui.res.stringResource
|
|
|
import androidx.compose.ui.unit.dp
|
|
|
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.pizzaorder.ui.BurgerScreen
|
|
|
import com.example.pizzaorder.ui.ForgotPasswordScreen
|
|
|
import com.example.pizzaorder.ui.LoginScreen
|
|
|
import com.example.pizzaorder.ui.OrderPlacedScreen
|
|
|
import com.example.pizzaorder.ui.OrderSummaryScreen
|
|
|
import com.example.pizzaorder.ui.OrderViewModel
|
|
|
import com.example.pizzaorder.ui.PizzaScreen
|
|
|
import com.example.pizzaorder.ui.SignUpScreen
|
|
|
import com.example.pizzaorder.ui.StartOrderScreen
|
|
|
import com.google.firebase.auth.FirebaseAuth
|
|
|
|
|
|
enum class PizzaScreenPages(@StringRes val title: Int) {
|
|
|
Login(title = R.string.login),
|
|
|
SignUp(title = R.string.signup),
|
|
|
ForgotPassword(title = R.string.forgot_password),
|
|
|
Start(title = R.string.start_order),
|
|
|
Pizza(title = R.string.choose_pizza_type),
|
|
|
Burger(title = R.string.choose_burger_type),
|
|
|
Pickup(title = R.string.choose_pickup_date),
|
|
|
Summary(title = R.string.order_summary),
|
|
|
OrderPlaced(title = R.string.order_placed)
|
|
|
}
|
|
|
|
|
|
@OptIn(ExperimentalMaterial3Api::class)
|
|
|
@Composable
|
|
|
fun PizzaAppBar(
|
|
|
currentScreen: PizzaScreenPages,
|
|
|
canNavigateBack: Boolean,
|
|
|
navigateUp: () -> Unit,
|
|
|
onMenuClicked: () -> Unit,
|
|
|
modifier: Modifier = Modifier
|
|
|
) {
|
|
|
TopAppBar(
|
|
|
title = { Text(stringResource(currentScreen.title)) },
|
|
|
colors = TopAppBarDefaults.mediumTopAppBarColors(
|
|
|
containerColor = MaterialTheme.colorScheme.primaryContainer
|
|
|
),
|
|
|
modifier = modifier,
|
|
|
navigationIcon = {
|
|
|
if (canNavigateBack) {
|
|
|
IconButton(onClick = navigateUp) {
|
|
|
Icon(
|
|
|
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
|
|
|
contentDescription = stringResource(R.string.back_button)
|
|
|
)
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
actions = {
|
|
|
IconButton(onClick = onMenuClicked) {
|
|
|
Icon(
|
|
|
imageVector = Icons.Default.MoreVert,
|
|
|
contentDescription = stringResource(R.string.menu_button)
|
|
|
)
|
|
|
}
|
|
|
}
|
|
|
)
|
|
|
}
|
|
|
|
|
|
@OptIn(ExperimentalMaterial3Api::class)
|
|
|
@Composable
|
|
|
fun PizzaApp(
|
|
|
viewModel: OrderViewModel = viewModel(),
|
|
|
navController: NavHostController = rememberNavController()
|
|
|
) {
|
|
|
val backStackEntry by navController.currentBackStackEntryAsState()
|
|
|
val currentScreen = PizzaScreenPages.valueOf(
|
|
|
backStackEntry?.destination?.route ?: PizzaScreenPages.Start.name
|
|
|
)
|
|
|
val isAuthenticated = FirebaseAuth.getInstance().currentUser != null
|
|
|
|
|
|
var showBottomSheet by remember { mutableStateOf(false) }
|
|
|
var showLoginDialog by remember { mutableStateOf(false) }
|
|
|
val sheetState = rememberModalBottomSheetState()
|
|
|
val scope = rememberCoroutineScope()
|
|
|
|
|
|
|
|
|
if (showLoginDialog) {
|
|
|
AlertDialog(
|
|
|
onDismissRequest = {showLoginDialog = false},
|
|
|
title = { Text("Login Required") },
|
|
|
text = { Text("You need to log in to access this feature.") },
|
|
|
confirmButton = {
|
|
|
Button(onClick = {showLoginDialog = false}) {
|
|
|
Text("OK")
|
|
|
}
|
|
|
}
|
|
|
)
|
|
|
}
|
|
|
|
|
|
if (showBottomSheet) {
|
|
|
ModalBottomSheet(
|
|
|
onDismissRequest = { showBottomSheet = false },
|
|
|
sheetState = sheetState
|
|
|
) {
|
|
|
MenuContent(
|
|
|
onHomeClicked = {
|
|
|
showBottomSheet = false
|
|
|
if (isAuthenticated) {
|
|
|
navController.navigate(PizzaScreenPages.Start.name)
|
|
|
} else {
|
|
|
showLoginDialog = true
|
|
|
}
|
|
|
|
|
|
},
|
|
|
onOrdersClicked = {
|
|
|
showBottomSheet = false
|
|
|
if (isAuthenticated) {
|
|
|
navController.navigate(PizzaScreenPages.Summary.name)
|
|
|
} else {
|
|
|
showLoginDialog = true
|
|
|
}
|
|
|
|
|
|
},
|
|
|
onLogoutClicked = {
|
|
|
FirebaseAuth.getInstance().signOut()
|
|
|
showBottomSheet = false
|
|
|
navController.navigate(PizzaScreenPages.Login.name) {
|
|
|
popUpTo(PizzaScreenPages.Login.name) { inclusive = true }
|
|
|
}
|
|
|
}
|
|
|
)
|
|
|
}
|
|
|
}
|
|
|
Scaffold(
|
|
|
topBar = {
|
|
|
PizzaAppBar(
|
|
|
currentScreen = currentScreen,
|
|
|
canNavigateBack = navController.previousBackStackEntry != null,
|
|
|
navigateUp = { navController.navigateUp() },
|
|
|
onMenuClicked = { showBottomSheet = true }
|
|
|
)
|
|
|
},
|
|
|
bottomBar = {
|
|
|
|
|
|
}
|
|
|
) { innerPadding ->
|
|
|
val uiState by viewModel.uiState.collectAsState()
|
|
|
|
|
|
NavHost(
|
|
|
navController = navController,
|
|
|
startDestination = if (isAuthenticated) PizzaScreenPages.Start.name else PizzaScreenPages.Login.name,
|
|
|
modifier = Modifier.padding(innerPadding)
|
|
|
) {
|
|
|
composable(route = PizzaScreenPages.Login.name) {
|
|
|
LoginScreen(
|
|
|
navController = navController,
|
|
|
modifier = Modifier
|
|
|
.fillMaxSize()
|
|
|
.padding(dimensionResource(R.dimen.padding_medium))
|
|
|
)
|
|
|
}
|
|
|
composable(route = PizzaScreenPages.SignUp.name) {
|
|
|
SignUpScreen(
|
|
|
navController = navController,
|
|
|
modifier = Modifier
|
|
|
.fillMaxSize()
|
|
|
.padding(dimensionResource(R.dimen.padding_medium))
|
|
|
)
|
|
|
}
|
|
|
composable(route = PizzaScreenPages.ForgotPassword.name) {
|
|
|
ForgotPasswordScreen(
|
|
|
navController = navController,
|
|
|
modifier = Modifier
|
|
|
.fillMaxSize()
|
|
|
.padding(dimensionResource(R.dimen.padding_medium))
|
|
|
)
|
|
|
}
|
|
|
composable(route = PizzaScreenPages.Start.name) {
|
|
|
StartOrderScreen(
|
|
|
navController = navController,
|
|
|
modifier = Modifier.fillMaxSize())
|
|
|
}
|
|
|
|
|
|
composable(route = PizzaScreenPages.Pizza.name) {
|
|
|
PizzaScreen(
|
|
|
viewModel = viewModel,
|
|
|
navController = navController)
|
|
|
}
|
|
|
|
|
|
composable(route = PizzaScreenPages.Burger.name) {
|
|
|
BurgerScreen(
|
|
|
viewModel = viewModel,
|
|
|
navController = navController)
|
|
|
}
|
|
|
composable(route = PizzaScreenPages.Summary.name) {
|
|
|
OrderSummaryScreen(
|
|
|
viewModel = viewModel,
|
|
|
onBack = { navController.popBackStack() },
|
|
|
onPlace = { navController.navigate(PizzaScreenPages.OrderPlaced.name) })
|
|
|
}
|
|
|
composable(route = PizzaScreenPages.OrderPlaced.name) {
|
|
|
OrderPlacedScreen(
|
|
|
onBack = {
|
|
|
viewModel.clearOrder()
|
|
|
navController.navigate(PizzaScreenPages.Start.name) }
|
|
|
)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Composable
|
|
|
fun MenuContent(
|
|
|
onHomeClicked: () -> Unit,
|
|
|
onOrdersClicked: () -> Unit,
|
|
|
onLogoutClicked: () -> Unit
|
|
|
) {
|
|
|
Column(
|
|
|
modifier = Modifier
|
|
|
.fillMaxWidth()
|
|
|
.padding(16.dp),
|
|
|
verticalArrangement = Arrangement.spacedBy(16.dp)
|
|
|
) {
|
|
|
Text(
|
|
|
text = "Menu",
|
|
|
style = MaterialTheme.typography.titleMedium,
|
|
|
modifier = Modifier.padding(bottom = 8.dp)
|
|
|
)
|
|
|
HorizontalDivider()
|
|
|
Button(
|
|
|
onClick = onHomeClicked,
|
|
|
modifier = Modifier.fillMaxWidth()
|
|
|
) {
|
|
|
Text("Home")
|
|
|
}
|
|
|
Button(
|
|
|
onClick = onOrdersClicked,
|
|
|
modifier = Modifier.fillMaxWidth()
|
|
|
) {
|
|
|
Text("Orders")
|
|
|
}
|
|
|
Button(
|
|
|
onClick = onLogoutClicked,
|
|
|
modifier = Modifier.fillMaxWidth(),
|
|
|
colors = ButtonDefaults.buttonColors(containerColor = Color.Red)
|
|
|
) {
|
|
|
Text("Logout")
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|