Теперь, обязательн надо авторизоваться, пабеда

This commit is contained in:
Кашин Максим 2023-12-23 03:31:18 +04:00
parent 68a276d549
commit 361caca3c8
5 changed files with 200 additions and 128 deletions

View File

@ -13,6 +13,7 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize 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.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
@ -48,6 +49,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.vectorResource import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
@ -72,50 +74,72 @@ fun Cart(
val cartUiState = viewModel.cartUiState val cartUiState = viewModel.cartUiState
var getUser by remember { mutableStateOf(currentUserViewModel.user) } var getUser by remember { mutableStateOf(currentUserViewModel.user) }
LaunchedEffect(Unit) { // Проверяем, есть ли у пользователя uid
getUser?.uid?.let { viewModel.refreshState(it) } if (getUser?.uid != null) {
} LaunchedEffect(Unit) {
getUser?.uid?.let { viewModel.refreshState(it) }
Cart(
cartUiState = cartUiState,
modifier = Modifier
.padding(all = 10.dp),
onSwipe = { item: ItemFromCart ->
coroutineScope.launch {
getUser?.uid?.let {
viewModel.removeFromCart(
item = Item(
uid = item.uid,
dateTime = item.dateTime,
weight = item.weight,
maxCount = 0,
bikeId = item.bikeId
), user = it
)
}
}
},
onChangeCount = { item: ItemFromCart, count: Int ->
coroutineScope.launch {
getUser?.uid?.let {
viewModel.updateFromCart(
item = Item(
uid = item.uid,
dateTime = item.dateTime,
weight = item.weight,
maxCount = 0,
bikeId = item.bikeId
), userId = it, count = count, availableCount = item.availableCount
)
}
}
},
onAddToRent = { items: List<ItemFromCart> ->
coroutineScope.launch {
getUser?.uid?.let { viewModel.addToRent(items = items, userId = it) }
}
} }
)
Cart(
cartUiState = cartUiState,
modifier = Modifier
.padding(all = 10.dp),
onSwipe = { item: ItemFromCart ->
coroutineScope.launch {
getUser?.uid?.let {
viewModel.removeFromCart(
item = Item(
uid = item.uid,
dateTime = item.dateTime,
weight = item.weight,
maxCount = 0,
bikeId = item.bikeId
), user = it
)
}
}
},
onChangeCount = { item: ItemFromCart, count: Int ->
coroutineScope.launch {
getUser?.uid?.let {
viewModel.updateFromCart(
item = Item(
uid = item.uid,
dateTime = item.dateTime,
weight = item.weight,
maxCount = 0,
bikeId = item.bikeId
), userId = it, count = count, availableCount = item.availableCount
)
}
}
},
onAddToRent = { items: List<ItemFromCart> ->
coroutineScope.launch {
getUser?.uid?.let { viewModel.addToRent(items = items, userId = it) }
}
}
)
} else {
// Пользователь не авторизован, показываем экран с сообщением
UnauthorizedScreen()
}
}
@Composable
fun UnauthorizedScreen() {
// Экран с сообщением о неудачной авторизации
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text("Oops! Something went wrong.", fontWeight = FontWeight.Bold)
Spacer(modifier = Modifier.height(8.dp))
Text("Please try again later.")
}
} }
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)

View File

@ -185,7 +185,7 @@ fun Navhost(
startDestination = Screen.BikeList.route, startDestination = Screen.BikeList.route,
modifier.padding(innerPadding) modifier.padding(innerPadding)
) { ) {
composable(Screen.BikeList.route) { BikeList(navController) } composable(Screen.BikeList.route) { BikeList(navController, currentUserViewModel = currentUserViewModel) }
composable(Screen.RentList.route) { RentList(navController, currentUserViewModel = currentUserViewModel) } composable(Screen.RentList.route) { RentList(navController, currentUserViewModel = currentUserViewModel) }
composable(Screen.Cart.route) { Cart(currentUserViewModel = currentUserViewModel) } composable(Screen.Cart.route) { Cart(currentUserViewModel = currentUserViewModel) }
composable(Screen.UserProfile.route) { UserProfile(isDarkTheme, dataStore, currentUserViewModel = currentUserViewModel) } composable(Screen.UserProfile.route) { UserProfile(isDarkTheme, dataStore, currentUserViewModel = currentUserViewModel) }

View File

@ -51,56 +51,64 @@ import kotlinx.coroutines.launch
@Composable @Composable
fun BikeList( fun BikeList(
navController: NavController, navController: NavController,
viewModel: BikeListViewModel = viewModel(factory = AppViewModelProvider.Factory) viewModel: BikeListViewModel = viewModel(factory = AppViewModelProvider.Factory),
currentUserViewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory)
) { ) {
val coroutineScope = rememberCoroutineScope() var getUser by remember { mutableStateOf(currentUserViewModel.user) }
val bikePagingItems = viewModel.bikeListUiState.collectAsLazyPagingItems()
Scaffold( // Проверяем, есть ли у пользователя uid
topBar = {}, if (getUser?.uid != null) {
floatingActionButton = { val coroutineScope = rememberCoroutineScope()
Box( val bikePagingItems = viewModel.bikeListUiState.collectAsLazyPagingItems()
//modifier = Modifier.fillMaxWidth(),
contentAlignment = Alignment.Center // Center align the FloatingActionButton Scaffold(
) { topBar = {},
FloatingActionButton( floatingActionButton = {
onClick = { Box(
val route = Screen.BikeEdit.route.replace("{id}", 0.toString()) contentAlignment = Alignment.Center
navController.navigate(route)
},
modifier = Modifier.fillMaxWidth(0.92f).align(Alignment.BottomCenter),
containerColor = MaterialTheme.colorScheme.primary
) { ) {
Icon( FloatingActionButton(
Icons.Filled.Add, onClick = {
"Добавить", val route = Screen.BikeEdit.route.replace("{id}", 0.toString())
tint = MaterialTheme.colorScheme.onPrimary 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(
modifier = Modifier
.padding(innerPadding)
.fillMaxSize(),
pagingBike = bikePagingItems,
onClick = { uid: Int ->
val route = Screen.BikeView.route.replace("{id}", uid.toString())
navController.navigate(route)
},
onDeleteClick = { bike: Bike ->
coroutineScope.launch {
viewModel.deleteBike(bike)
}
},
onEditClick = { uid: Int ->
val route = Screen.BikeEdit.route.replace("{id}", uid.toString())
navController.navigate(route)
},
)
} }
) { innerPadding -> } else {
BikeList( UnauthorizedScreen()
modifier = Modifier
.padding(innerPadding)
.fillMaxSize(),
pagingBike = bikePagingItems,
onClick = { uid: Int ->
val route = Screen.BikeView.route.replace("{id}", uid.toString())
navController.navigate(route)
},
onDeleteClick = { bike: Bike ->
coroutineScope.launch {
viewModel.deleteBike(bike)
}
},
onEditClick = { uid: Int ->
val route = Screen.BikeEdit.route.replace("{id}", uid.toString())
navController.navigate(route)
},
)
} }
} }
@Composable @Composable
private fun BikeList( private fun BikeList(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,

View File

@ -5,9 +5,12 @@ 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.Column
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize 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.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
@ -26,6 +29,7 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
@ -44,56 +48,79 @@ fun RentList(
currentUserViewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory) currentUserViewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory)
) { ) {
var getUser by remember { mutableStateOf(currentUserViewModel.user) } var getUser by remember { mutableStateOf(currentUserViewModel.user) }
getUser?.uid?.let { viewModel.setUserId(it) }
val coroutineScope = rememberCoroutineScope() // Проверяем, есть ли у пользователя uid
val rentsUiState = viewModel.rentListUiState.collectAsLazyPagingItems() if (getUser?.uid != null) {
LazyColumn( viewModel.setUserId(getUser?.uid!!)
modifier = Modifier
.fillMaxSize() val coroutineScope = rememberCoroutineScope()
.padding(all = 10.dp) val rentsUiState = viewModel.rentListUiState.collectAsLazyPagingItems()
) {
items( LazyColumn(
count = rentsUiState.itemCount, modifier = Modifier
key = rentsUiState.itemKey(), .fillMaxSize()
contentType = rentsUiState.itemContentType() .padding(all = 10.dp)
) { index -> ) {
val rent = rentsUiState[index] items(
val rentId = Screen.RentView.route.replace("{id}", rent!!.uid.toString()) count = rentsUiState.itemCount,
Box( key = rentsUiState.itemKey(),
modifier = Modifier contentType = rentsUiState.itemContentType()
.fillMaxWidth() ) { index ->
.padding(all = 10.dp) val rent = rentsUiState[index]
.clickable { navController?.navigate(rentId) } val rentId = Screen.RentView.route.replace("{id}", rent!!.uid.toString())
.background( Box(
color = MaterialTheme.colorScheme.primary,
shape = RoundedCornerShape(16.dp)
)
) {
Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(8.dp), .padding(all = 10.dp)
verticalAlignment = Alignment.CenterVertically, .clickable { navController?.navigate(rentId) }
horizontalArrangement = Arrangement.SpaceBetween .background(
) { color = MaterialTheme.colorScheme.primary,
Text("Аренда №${rent.uid}", color = MaterialTheme.colorScheme.onPrimary) shape = RoundedCornerShape(16.dp)
IconButton(
onClick = {
coroutineScope.launch {
viewModel.deleteRent(rent!!)
}
}
) {
Icon(
Icons.Default.Delete,
tint = MaterialTheme.colorScheme.onPrimary,
contentDescription = "Удалить"
) )
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Text("Аренда №${rent.uid}", color = MaterialTheme.colorScheme.onPrimary)
IconButton(
onClick = {
coroutineScope.launch {
viewModel.deleteRent(rent!!)
}
}
) {
Icon(
Icons.Default.Delete,
tint = MaterialTheme.colorScheme.onPrimary,
contentDescription = "Удалить"
)
}
} }
} }
} }
} }
} else {
// Пользователь не авторизован, показываем экран с сообщением
UnauthorizedScreen()
}
}
@Composable
fun UnauthorizedScreen() {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text("Oops! Something went wrong.", fontWeight = FontWeight.Bold)
Spacer(modifier = Modifier.height(8.dp))
Text("Please try again later.")
} }
} }

View File

@ -79,6 +79,19 @@
"bikeId": 2 "bikeId": 2
} }
] ]
},
{
"id": 9,
"userId": 7,
"items": [
{
"id": 4,
"dateTime": 2023,
"frozenWeight": 26.3,
"count": 3,
"bikeId": 2
}
]
} }
], ],
"items": [ "items": [