вход с учетом ролей

This commit is contained in:
Татьяна Артамонова 2023-12-27 01:02:01 +04:00
parent 5dde1465e2
commit 778ee2feac
5 changed files with 42 additions and 529 deletions

View File

@ -1,25 +1,18 @@
package ru.ulstu.`is`.airticketrentservice.navigation package ru.ulstu.`is`.airticketrentservice.navigation
import android.content.res.Configuration
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar import androidx.compose.material3.NavigationBar
import androidx.compose.material3.Surface
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
@ -28,21 +21,22 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController import androidx.navigation.NavController
import androidx.navigation.NavDestination import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hierarchy import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import ru.ulstu.`is`.airticketrentservice.R import ru.ulstu.`is`.airticketrentservice.R
import ru.ulstu.`is`.airticketrentservice.ui.theme.AirTicketRentServiceTheme import ru.ulstu.`is`.airticketrentservice.viewModel.AppViewModelProvider
import ru.ulstu.`is`.airticketrentservice.viewModel.CurrentUserViewModel
@Composable @Composable
fun BottomBar(navController: NavHostController){ fun BottomBar(navController: NavHostController, currentUserViewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory)){
val screens = listOf( val screens = listOf(
BottomBarScreen.MainPage, BottomBarScreen.MainPage,
BottomBarScreen.Profile, BottomBarScreen.Profile,
@ -65,28 +59,31 @@ fun BottomBar(navController: NavHostController){
.clip(shape = RoundedCornerShape(20.dp)) .clip(shape = RoundedCornerShape(20.dp))
) { ) {
screens.forEach { screen -> screens.forEach { screen ->
currentUserViewModel.user?.role?.let {
AddItem( AddItem(
screen = screen, screen = screen,
currentDestination = currentDestination, currentDestination = currentDestination,
navController = navController navController = navController,
userRole = it
) )
} }
} }
} }
} }
}
} }
@Composable @Composable
fun RowScope.AddItem( fun RowScope.AddItem(
screen: BottomBarScreen, screen: BottomBarScreen,
currentDestination: NavDestination?, currentDestination: NavDestination?,
navController: NavController navController: NavController,
userRole: String
){ ){
val isAdmin = userRole == "Admin"
val selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true val selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true
val background = if (selected) Color(0xFFFFFFFF) else Color.Transparent val background = if (selected) Color(0xFFFFFFFF) else Color.Transparent
if (isAdmin || screen != BottomBarScreen.Admin) {
Box( Box(
modifier = Modifier modifier = Modifier
.height(40.dp) .height(40.dp)
@ -100,7 +97,7 @@ fun RowScope.AddItem(
}) })
) { ) {
Row( Row(
modifier = Modifier.padding(start = 47.dp, end = 46.dp), modifier = Modifier.padding(start = 47.dp, end = 46.dp, top = 10.dp),
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
) { ) {
Icon( Icon(
@ -110,4 +107,7 @@ fun RowScope.AddItem(
) )
} }
} }
} else {
Text(text = "Недостаточно прав", fontSize = 8.sp, modifier = Modifier.fillMaxWidth().padding(top = 10.dp))
}
} }

View File

@ -1,362 +0,0 @@
package ru.ulstu.`is`.airticketrentservice.navigation
import android.annotation.SuppressLint
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import androidx.navigation.navigation
import ru.ulstu.`is`.airticketrentservice.screen.RentList
import ru.ulstu.`is`.airticketrentservice.screen.UserEdit
import ru.ulstu.`is`.airticketrentservice.screen.FlightEdit
import ru.ulstu.`is`.airticketrentservice.R
import ru.ulstu.`is`.airticketrentservice.screen.Admin
import ru.ulstu.`is`.airticketrentservice.screen.FlightInfo
import ru.ulstu.`is`.airticketrentservice.screen.FlightList
import ru.ulstu.`is`.airticketrentservice.screen.FoundFlights
import ru.ulstu.`is`.airticketrentservice.screen.LoadScreen
import ru.ulstu.`is`.airticketrentservice.screen.MainPage
import ru.ulstu.`is`.airticketrentservice.screen.MyRents
import ru.ulstu.`is`.airticketrentservice.screen.Profile
import ru.ulstu.`is`.airticketrentservice.screen.RentEdit
import ru.ulstu.`is`.airticketrentservice.screen.TicketEdit
import ru.ulstu.`is`.airticketrentservice.screen.UserList
import ru.ulstu.`is`.airticketrentservice.screen.auth.Login
import ru.ulstu.`is`.airticketrentservice.screen.auth.Registration
import ru.ulstu.`is`.airticketrentservice.viewModel.AppViewModelProvider
import ru.ulstu.`is`.airticketrentservice.viewModel.CurrentUserViewModel
import ru.ulstu.`is`.airticketrentservice.viewModel.FlightEditViewModel
import ru.ulstu.`is`.airticketrentservice.viewModel.FlightListViewModel
import ru.ulstu.`is`.airticketrentservice.viewModel.LoginViewModel
import ru.ulstu.`is`.airticketrentservice.viewModel.RegistrationViewModel
import ru.ulstu.`is`.airticketrentservice.viewModel.RentEditViewModel
import ru.ulstu.`is`.airticketrentservice.viewModel.RentListViewModel
import ru.ulstu.`is`.airticketrentservice.viewModel.TicketEditViewModel
import ru.ulstu.`is`.airticketrentservice.viewModel.UserEditViewModel
import ru.ulstu.`is`.airticketrentservice.viewModel.UserListViewModel
//@OptIn(ExperimentalMaterial3Api::class)
//@Composable
//fun Topbar(
// navController: NavHostController,
// currentScreen: Screen?
//) {
// TopAppBar(
// colors = TopAppBarDefaults.topAppBarColors(
// containerColor = Color.Transparent,
// ),
// title = {},
// navigationIcon = {
// if (
// navController.previousBackStackEntry != null
// && (currentScreen == null || !currentScreen.showInBottomBar)
// ) {
// IconButton(onClick = { navController.navigateUp() }) {
// Icon(
// imageVector = Icons.Filled.ArrowBack,
// contentDescription = null,
// tint = colorResource(R.color.black)
// )
// }
// }
// },
// modifier = Modifier
// .padding(10.dp)
// .clip(RoundedCornerShape(20.dp))
// )
//}
//
//@Composable
//fun Navbar(
// navController: NavHostController,
// currentDestination: NavDestination?
//) {
// NavigationBar(
// Modifier
// .padding(10.dp)
// .clip(RoundedCornerShape(20.dp))) {
// Screen.bottomBarItems.forEach { screen ->
// NavigationBarItem(
// colors = NavigationBarItemDefaults.colors(colorResource(R.color.lightBlue)),
// icon = { Icon(screen.icon, contentDescription = null) },
// label = { Text(stringResource(screen.resourceId)) },
// selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
// onClick = {
// navController.navigate(screen.route) {
// popUpTo(navController.graph.findStartDestination().id) {
// saveState = true
// }
// launchSingleTop = true
// restoreState = true
// }
// }
// )
// }
// }
//}
//
//const val USERID_ARGUMENT="userId"
//@Composable
//fun RootNavGraph(
// navController: NavHostController,
// registrationViewModel: RegistrationViewModel= viewModel(factory = AppViewModelProvider.Factory),
// loginViewModel: LoginViewModel = viewModel(factory = AppViewModelProvider.Factory),
// currentUserViewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory),
//) {
// NavHost(
// navController = navController,
// route = Graph.ROOT,
// startDestination = Graph.AUTHENTICATION
// ) {
// authNavGraph(navController = navController, registrationViewModel, loginViewModel, currentUserViewModel)
// composable(route=Graph.MAIN,
// arguments = listOf(navArgument(USERID_ARGUMENT){
// type= NavType.StringType
// })){
// LoadScreen(
// navController = navController,
// currentUserViewModel = currentUserViewModel
// )
// }
// }
//}
//
//fun NavGraphBuilder.authNavGraph(navController: NavHostController, registrationViewModel: RegistrationViewModel, loginViewModel: LoginViewModel, currentUserViewModel: CurrentUserViewModel){
// navigation(
// route=Graph.AUTHENTICATION,
// startDestination = AuthScreen.Login.route
// ){
// composable(route=AuthScreen.Login.route){
// Login(navController = navController, Modifier, loginViewModel, currentUserViewModel)
// }
// composable(route=AuthScreen.Registration.route){
// Registration(navController = navController, Modifier, registrationViewModel, currentUserViewModel)
// }
// }
//}
//
//sealed class AuthScreen(val route: String){
// object Login : AuthScreen(route = "login")
// object Registration : AuthScreen(route="registration")
//}
//
//@Composable
//fun HomeNavGraph(
// navController: NavHostController,
// currentUserViewModel: CurrentUserViewModel
//) {
// NavHost(
// navController = navController,
// startDestination = Screen.MainPage.route
// ) {
// composable(Screen.MainPage.route) { MainPage(navController) }
// composable(Screen.RentList.route) { RentList(navController) }
// composable(Screen.FlightList.route) { FlightList(navController) }
// composable(Screen.Profile.route) { Profile(navController, currentUserViewModel) }
// composable(Screen.UserList.route) { UserList(navController) }
//// composable(Screen.Login.route) { Login(navController) }
// composable(Screen.MyRents.route) { MyRents(navController) }
//// composable(Screen.Registration.route) { Registration(navController) }
// composable(Screen.Admin.route) { Admin(navController) }
// composable(
// Screen.FlightInfo.route,
// arguments = listOf(navArgument("id") { type = NavType.IntType })
// ) {
// FlightInfo(navController)
// }
// composable(
// Screen.FoundFlights.route,
// arguments = listOf(
// navArgument("from") { type = NavType.StringType },
// navArgument("to") { type = NavType.StringType },
// navArgument("departureDate") { type = NavType.StringType }
// )
// ) {
// FoundFlights(navController)
// }
// composable(
// Screen.UserEdit.route,
// arguments = listOf(navArgument("id") { type = NavType.IntType })
// ) {
// UserEdit(navController)
// }
// composable(
// Screen.FlightEdit.route,
// arguments = listOf(navArgument("id") { type = NavType.IntType })
// ) {
// FlightEdit(navController)
// }
// composable(
// Screen.TicketEdit.route,
// arguments = listOf(navArgument("id") { type = NavType.IntType })
// ) {
// TicketEdit(navController)
// }
//// composable(
//// Screen.MyRents.route,
//// arguments = listOf(navArgument("id") { type = NavType.IntType })
//// ) {
//// MyRents(navController)
//// }
// composable(
// Screen.RentEdit.route,
// arguments = listOf(navArgument("id") { type = NavType.IntType })
// ) {
// RentEdit(navController)
// }
// }
//}
//
//object Graph{
// const val ROOT="root_graph"
// const val AUTHENTICATION="auth_graph"
// const val MAIN="main_graph/{$USERID_ARGUMENT}"
// fun passUserId(userId: String): String{
// return "main_graph/$userId"
// }
//}
//
////@RequiresApi(Build.VERSION_CODES.O)
////@Composable
////fun Navhost(
//// navController: NavHostController,
//// innerPadding: PaddingValues, modifier:
//// Modifier = Modifier,
//// currentUserViewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory),
//// loginViewModel: LoginViewModel = viewModel(factory = AppViewModelProvider.Factory),
//// registrationViewModel: RegistrationViewModel = viewModel(factory = AppViewModelProvider.Factory),
////) {
//// NavHost(
//// navController,
//// startDestination = Graph.AUTHENTICATION,
//// modifier.padding(innerPadding),
//// route = Graph.ROOT
//// ) {
//// authNavGraph(navController=navController,registrationViewModel,loginViewModel,currentUserViewModel)
//// composable(route=Graph.MAIN,
//// arguments = listOf(navArgument(USERID_ARGUMENT){
//// type= NavType.StringType
//// })
////// composable(Screen.RentList.route) { RentList(navController) }
////// composable(Screen.FlightList.route) { FlightList(navController) }
////// composable(Screen.Profile.route) { Profile(navController) }
////// composable(Screen.UserList.route) { UserList(navController) }
////// composable(Screen.Login.route) { Login(navController) }
////// composable(Screen.MyRents.route) { MyRents(navController) }
////// composable(Screen.Registration.route) { Registration(navController) }
////// composable(Screen.MainPage.route) { MainPage(navController) }
////// composable(Screen.Admin.route) { Admin(navController) }
////// composable(
////// Screen.TicketEdit.route,
////// arguments = listOf(navArgument("id") { type = NavType.IntType })
////// ) {
////// TicketEdit(navController)
////// }
////// composable(
////// Screen.FlightInfo.route,
////// arguments = listOf(navArgument("id") { type = NavType.IntType })
////// ) {
////// FlightInfo(navController)
////// }
////// composable(
////// Screen.FoundFlights.route,
////// arguments = listOf(
////// navArgument("from") { type = NavType.StringType },
////// navArgument("to") { type = NavType.StringType },
////// navArgument("departureDate") { type = NavType.StringType }
////// )
////// ) {
////// FoundFlights(navController)
////// }
////// composable(
////// Screen.UserEdit.route,
////// arguments = listOf(navArgument("id") { type = NavType.IntType })
////// ) {
////// UserEdit(navController)
////// }
////// composable(
////// Screen.FlightEdit.route,
////// arguments = listOf(navArgument("id") { type = NavType.IntType })
////// ) {
////// FlightEdit(navController)
////// }
//////// composable(
//////// Screen.MyRents.route,
//////// arguments = listOf(navArgument("id") { type = NavType.IntType })
//////// ) {
//////// MyRents(navController)
//////// }
////// composable(
////// Screen.RentEdit.route,
////// arguments = listOf(navArgument("id") { type = NavType.IntType })
////// ) {
////// RentEdit(navController)
////// }
//// }
////}
//
//@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
//@RequiresApi(Build.VERSION_CODES.O)
//@Composable
//fun MainNavbar() {
// val navController = rememberNavController()
// val navBackStackEntry by navController.currentBackStackEntryAsState()
// val currentDestination = navBackStackEntry?.destination
// val currentScreen = currentDestination?.route?.let { Screen.getItem(it) }
// val registrationViewModel: RegistrationViewModel= viewModel(factory = AppViewModelProvider.Factory)
// val loginViewModel: LoginViewModel = viewModel(factory = AppViewModelProvider.Factory)
// val currentUserViewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory)
//
// Scaffold(
// topBar = {
// Topbar(navController, currentScreen)
// },
// bottomBar = {
// if (currentScreen == null || currentScreen.showInBottomBar) {
// Navbar(navController, currentDestination)
// }
// }
// ) {
// RootNavGraph(navController, registrationViewModel, loginViewModel, currentUserViewModel)
// }
//}

View File

@ -1,76 +0,0 @@
package ru.ulstu.`is`.airticketrentservice.navigation
import androidx.annotation.StringRes
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.AdminPanelSettings
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Search
import androidx.compose.ui.graphics.vector.ImageVector
import ru.ulstu.`is`.airticketrentservice.R
//enum class Screen(
// val route: String,
// @StringRes val resourceId: Int,
// val icon: ImageVector = Icons.Filled.Favorite,
// val showInBottomBar: Boolean = true
//) {
// Admin(
// "admin", R.string.rent_view_title, Icons.Filled.AdminPanelSettings
// ),
// FlightEdit(
// "flight-edit/{id}", R.string.ticket_view_title, showInBottomBar = false
// ),
// FlightInfo(
// "flight-info/{id}", R.string.ticket_view_title, showInBottomBar = false
// ),
// FlightList(
// "flight-list", R.string.ticket_main_title, showInBottomBar = false
// ),
// FoundFlights(
// "found-flights/{from}-{to}-{departureDate}", R.string.ticket_main_title, showInBottomBar = false
// ),
// MainPage(
// "main", R.string.rent_view_title, Icons.Filled.Search
// ),
// MyRents(
// "my-rents", R.string.ticket_view_title, showInBottomBar = false
// ),
// Profile(
// "profile", R.string.rent_view_title, Icons.Filled.AccountCircle
// ),
// RentEdit(
// "rent-edit/{id}", R.string.ticket_view_title, showInBottomBar = false
// ),
// RentList(
// "rent-list", R.string.ticket_main_title, showInBottomBar = false
// ),
// TicketEdit(
// "ticket-edit/{id}", R.string.ticket_view_title, showInBottomBar = false
// ),
// UserEdit(
// "user-edit/{id}", R.string.ticket_view_title, showInBottomBar = false
// ),
// UserList(
// "user-list", R.string.ticket_main_title, showInBottomBar = false
// ),
// Login(
// "login", R.string.rent_view_title, showInBottomBar = false
// ),
// Registration(
// "registration", R.string.rent_view_title, showInBottomBar = false
// );
//
// companion object {
// val bottomBarItems = listOf(
// MainPage,
// Profile,
// Admin
// )
//
// fun getItem(route: String): Screen? {
// val findRoute = route.split("/").first()
// return values().find { value -> value.route.startsWith(findRoute) }
// }
// }
//}

View File

@ -1,49 +0,0 @@
package ru.ulstu.`is`.airticketrentservice.navigation
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import ru.ulstu.`is`.airticketrentservice.R
//@OptIn(ExperimentalMaterial3Api::class)
//@Composable
//fun TopBar(navController: NavHostController,
// currentScreen: BottomBarScreen?) {
// TopAppBar(
// colors = TopAppBarDefaults.topAppBarColors(
// containerColor = Color.Transparent,
// ),
// title = {},
// actions = {},
// navigationIcon = {
// if (
// navController.previousBackStackEntry != null
// && (currentScreen == null)
// ) {
// IconButton(onClick = { navController.navigateUp() }) {
// Icon(
// imageVector = Icons.Filled.ArrowBack,
// contentDescription = null,
// tint = colorResource(R.color.black)
// )
// }
// }
// },
// modifier = Modifier
// .padding(10.dp)
// .clip(RoundedCornerShape(20.dp))
// )
//}

View File

@ -229,7 +229,7 @@ fun LoadScreen(navController: NavHostController = rememberNavController(),
rentListViewModel: RentListViewModel = viewModel(factory = AppViewModelProvider.Factory) rentListViewModel: RentListViewModel = viewModel(factory = AppViewModelProvider.Factory)
){ ){
Scaffold( Scaffold(
bottomBar = {BottomBar(navController = navController) }, bottomBar = {BottomBar(navController = navController, currentUserViewModel) },
) { ) {
Modifier Modifier
.padding(it) .padding(it)