Добавил роль, добавил 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,6 +64,7 @@ fun BikeList(
Scaffold(
topBar = {},
floatingActionButton = {
if(getUser?.role == "admin") {
Box(
contentAlignment = Alignment.Center
) {
@ -83,8 +84,10 @@ fun BikeList(
}
}
}
}
) { 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,7 +210,9 @@ private fun BikeListItem(
)
// Добавляем пустое пространство для разделения текста и кнопок
Spacer(modifier = Modifier.weight(1f))
if (getUser?.role == "admin") {
IconButton(
onClick = { onEditClick(bike.uid) },
modifier = Modifier.size(24.dp)
@ -208,6 +223,7 @@ private fun BikeListItem(
tint = MaterialTheme.colorScheme.secondary,
)
}
}
IconButton(
onClick = { onClick(bike.uid) },
modifier = Modifier.size(24.dp)
@ -218,6 +234,7 @@ private fun BikeListItem(
tint = MaterialTheme.colorScheme.secondary,
)
}
if (getUser?.role == "admin") {
IconButton(
onClick = { onDeleteClick(bike) },
modifier = Modifier.size(24.dp)
@ -229,6 +246,7 @@ private fun BikeListItem(
)
}
}
}
AnimatedVisibility(visible = isExpanded) { // Анимированная видимость для дополнительной информации
Text(
text = bike.description,

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,7 +125,7 @@ fun BikeView(
.weight(1f) // Занимает доступное пространство
.padding(top = 8.dp, bottom = 8.dp)
)
if (getUser?.role == "admin") {
IconButton(
onClick = {
val route = Screen.ItemEdit.route.replace("{id}", 0.toString())
@ -136,6 +142,7 @@ fun BikeView(
)
}
}
}
if (bikeUiState.bikeWithItems != null) {
ItemList(viewModel, navController, currentUserViewModel = currentUserViewModel)
}

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()) {
@ -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,6 +135,7 @@ fun ItemList(
tint = MaterialTheme.colorScheme.onPrimary
)
}
if (getUser?.role == "admin") {
IconButton(
onClick = {
coroutineScope.launch {
@ -151,5 +157,6 @@ fun ItemList(
}
}
}
}
}

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,