Добавил роль, добавил UI ограничения по ролям

This commit is contained in:
Кашин Максим 2023-12-24 18:39:23 +04:00
parent 54ee3a0a18
commit 3fe3d41253
4 changed files with 115 additions and 70 deletions

View File

@ -64,27 +64,30 @@ fun BikeList(
Scaffold(
topBar = {},
floatingActionButton = {
Box(
contentAlignment = Alignment.Center
) {
FloatingActionButton(
onClick = {
val route = Screen.BikeEdit.route.replace("{id}", 0.toString())
navController.navigate(route)
},
modifier = Modifier.fillMaxWidth(0.92f).align(Alignment.BottomCenter),
containerColor = MaterialTheme.colorScheme.primary
if(getUser?.role == "admin") {
Box(
contentAlignment = Alignment.Center
) {
Icon(
Icons.Filled.Add,
"Добавить",
tint = MaterialTheme.colorScheme.onPrimary
)
FloatingActionButton(
onClick = {
val route = Screen.BikeEdit.route.replace("{id}", 0.toString())
navController.navigate(route)
},
modifier = Modifier.fillMaxWidth(0.92f).align(Alignment.BottomCenter),
containerColor = MaterialTheme.colorScheme.primary
) {
Icon(
Icons.Filled.Add,
"Добавить",
tint = MaterialTheme.colorScheme.onPrimary
)
}
}
}
}
) { innerPadding ->
BikeList(
currentUserViewModel = currentUserViewModel,
modifier = Modifier
.padding(innerPadding)
.fillMaxSize(),
@ -115,8 +118,11 @@ private fun BikeList(
pagingBike: LazyPagingItems<Bike>,
onClick: (uid: Int) -> Unit,
onDeleteClick: (bike: Bike) -> Unit,
onEditClick: (bike: Int) -> Unit
onEditClick: (bike: Int) -> Unit,
currentUserViewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory)
) {
var getUser by remember { mutableStateOf(currentUserViewModel.user) }
Column(
modifier = modifier
) {
@ -124,12 +130,13 @@ private fun BikeList(
modifier = Modifier
.fillMaxSize()
.padding(all = 10.dp)
.padding(bottom = 72.dp) // Добавим отступ внизу
.then(if (getUser?.role == "admin") Modifier.padding(bottom = 72.dp) else Modifier)
) {
items(pagingBike.itemCount) { index ->
val bike = pagingBike[index]
if (bike != null) {
BikeListItem(
currentUserViewModel = currentUserViewModel,
bike = bike,
modifier = Modifier
.padding(vertical = 7.dp)
@ -151,21 +158,24 @@ private fun BikeList(
@Composable
private fun BikeListItem(
currentUserViewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory),
bike: Bike,
modifier: Modifier = Modifier,
onDeleteClick: (bike: Bike) -> Unit,
onClick: (uid: Int) -> Unit,
onEditClick: (bike: Int) -> Unit
) {
var getUser by remember { mutableStateOf(currentUserViewModel.user) }
var isExpanded by remember { mutableStateOf(false) } // Состояние для определения, раскрыта ли дополнительная информация о велосипеде
Box(
modifier = modifier
.clip(RoundedCornerShape(8.dp))
//.background(Color.White)
.shadow(4.dp)
.padding(8.dp)
.clickable { isExpanded = !isExpanded } // Добавляем обработчик нажатия для раскрытия/скрытия дополнительной информации
.clickable {
isExpanded = !isExpanded
} // Добавляем обработчик нажатия для раскрытия/скрытия дополнительной информации
) {
Column(Modifier.padding(8.dp)) {
Row(
@ -177,7 +187,10 @@ private fun BikeListItem(
modifier = Modifier
.size(90.dp)
.padding(4.dp)
.border(width = 1.dp, color = MaterialTheme.colorScheme.secondary) // Добавляем рамку
.border(
width = 1.dp,
color = MaterialTheme.colorScheme.secondary
) // Добавляем рамку
) {
Image(
bitmap = BitmapFactory.decodeByteArray(
@ -197,16 +210,19 @@ private fun BikeListItem(
)
// Добавляем пустое пространство для разделения текста и кнопок
Spacer(modifier = Modifier.weight(1f))
IconButton(
onClick = { onEditClick(bike.uid) },
modifier = Modifier.size(24.dp)
) {
Icon(
imageVector = Icons.Default.Edit,
contentDescription = "Редактировать",
tint = MaterialTheme.colorScheme.secondary,
)
if (getUser?.role == "admin") {
IconButton(
onClick = { onEditClick(bike.uid) },
modifier = Modifier.size(24.dp)
) {
Icon(
imageVector = Icons.Default.Edit,
contentDescription = "Редактировать",
tint = MaterialTheme.colorScheme.secondary,
)
}
}
IconButton(
onClick = { onClick(bike.uid) },
@ -218,15 +234,17 @@ private fun BikeListItem(
tint = MaterialTheme.colorScheme.secondary,
)
}
IconButton(
onClick = { onDeleteClick(bike) },
modifier = Modifier.size(24.dp)
) {
Icon(
imageVector = Icons.Default.Delete,
contentDescription = "Удалить",
tint = MaterialTheme.colorScheme.secondary,
)
if (getUser?.role == "admin") {
IconButton(
onClick = { onDeleteClick(bike) },
modifier = Modifier.size(24.dp)
) {
Icon(
imageVector = Icons.Default.Delete,
contentDescription = "Удалить",
tint = MaterialTheme.colorScheme.secondary,
)
}
}
}
AnimatedVisibility(visible = isExpanded) { // Анимированная видимость для дополнительной информации

View File

@ -20,6 +20,10 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
@ -39,6 +43,8 @@ fun BikeView(
viewModel: BikeViewModel = viewModel(factory = AppViewModelProvider.Factory),
currentUserViewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory)
) {
var getUser by remember { mutableStateOf(currentUserViewModel.user) }
val bikeUiState = viewModel.bikeUiState
LaunchedEffect(Unit) {
@ -119,21 +125,22 @@ fun BikeView(
.weight(1f) // Занимает доступное пространство
.padding(top = 8.dp, bottom = 8.dp)
)
IconButton(
onClick = {
val route = Screen.ItemEdit.route.replace("{id}", 0.toString())
.replace(
"{bikeId}",
bikeUiState.bikeWithItems?.bike?.uid.toString()
)
navController.navigate(route)
if (getUser?.role == "admin") {
IconButton(
onClick = {
val route = Screen.ItemEdit.route.replace("{id}", 0.toString())
.replace(
"{bikeId}",
bikeUiState.bikeWithItems?.bike?.uid.toString()
)
navController.navigate(route)
}
) {
Icon(
imageVector = Icons.Filled.Add,
contentDescription = "Добавить сеанс",
)
}
) {
Icon(
imageVector = Icons.Filled.Add,
contentDescription = "Добавить сеанс",
)
}
}
if (bikeUiState.bikeWithItems != null) {

View File

@ -52,7 +52,7 @@ fun ItemList(
val bikeWithItems = bikeWithItemsViewModel.bikeUiState.bikeWithItems!!
var getUser by remember { mutableStateOf(currentUserViewModel.user) }
Log.d("Пожалуйста, работай","${getUser?.uid}")
Log.d("Пожалуйста, работай", "${getUser?.uid}")
LazyColumn {
if (bikeWithItems.items.isEmpty()) {
@ -73,7 +73,7 @@ fun ItemList(
val dateFormatter = DateTimeFormatter.ofPattern("yyyy")
val formattedDate = dateFormatter.format(item.dateTime)
Column {
/* Text(
/* Text(
text = formattedDate,
color = MaterialTheme.colorScheme.onBackground,
)*/
@ -118,7 +118,12 @@ fun ItemList(
onClick = {
coroutineScope.launch {
if (item.availableCount != 0) {
getUser?.uid?.let { viewModel.addItemInCart(userId = it,itemId = item.uid) }
getUser?.uid?.let {
viewModel.addItemInCart(
userId = it,
itemId = item.uid
)
}
}
}
},
@ -130,20 +135,22 @@ fun ItemList(
tint = MaterialTheme.colorScheme.onPrimary
)
}
IconButton(
onClick = {
coroutineScope.launch {
viewModel.deleteItem(item = item)
bikeWithItemsViewModel.refreshState()
}
},
) {
Icon(
imageVector = Icons.Default.Delete,
contentDescription = null,
modifier = Modifier.size(24.dp),
tint = MaterialTheme.colorScheme.onPrimary
)
if (getUser?.role == "admin") {
IconButton(
onClick = {
coroutineScope.launch {
viewModel.deleteItem(item = item)
bikeWithItemsViewModel.refreshState()
}
},
) {
Icon(
imageVector = Icons.Default.Delete,
contentDescription = null,
modifier = Modifier.size(24.dp),
tint = MaterialTheme.colorScheme.onPrimary
)
}
}
}
}

View File

@ -148,6 +148,19 @@
"bikeId": 3
}
]
},
{
"id": 11,
"userId": 10,
"items": [
{
"id": 6,
"dateTime": 2020,
"frozenWeight": 23.6,
"count": 1,
"bikeId": 3
}
]
}
],
"items": [
@ -57774,7 +57787,7 @@
},
{
"id": 3,
"name": "не велосипед ",
"name": "не велосипед 5",
"description": "очень дорогие вложении с уважением Александр и я не могу найти в интернете и я не могу найти в интернете и я не смогу быть у вас есть вопросы пишите ",
"image": [
-1,