From 361caca3c82eec91987de6da9d5f344fc57a9974 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=88=D0=B8=D0=BD=20=D0=9C=D0=B0=D0=BA=D1=81?= =?UTF-8?q?=D0=B8=D0=BC?= Date: Sat, 23 Dec 2023 03:31:18 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A2=D0=B5=D0=BF=D0=B5=D1=80=D1=8C,=20=D0=BE?= =?UTF-8?q?=D0=B1=D1=8F=D0=B7=D0=B0=D1=82=D0=B5=D0=BB=D1=8C=D0=BD=20=D0=BD?= =?UTF-8?q?=D0=B0=D0=B4=D0=BE=20=D0=B0=D0=B2=D1=82=D0=BE=D1=80=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=D1=81=D1=8F,=20=D0=BF=D0=B0?= =?UTF-8?q?=D0=B1=D0=B5=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/myapplication/composeui/Cart.kt | 110 ++++++++++------- .../composeui/navigation/MainNavbar.kt | 2 +- .../database/entities/composeui/BikeList.kt | 92 ++++++++------- .../database/entities/composeui/RentList.kt | 111 +++++++++++------- server/data.json | 13 ++ 5 files changed, 200 insertions(+), 128 deletions(-) diff --git a/app/src/main/java/com/example/myapplication/composeui/Cart.kt b/app/src/main/java/com/example/myapplication/composeui/Cart.kt index 8da94de..7b7d5c4 100644 --- a/app/src/main/java/com/example/myapplication/composeui/Cart.kt +++ b/app/src/main/java/com/example/myapplication/composeui/Cart.kt @@ -13,6 +13,7 @@ import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size 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.vector.ImageVector import androidx.compose.ui.res.vectorResource +import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel @@ -72,50 +74,72 @@ fun Cart( val cartUiState = viewModel.cartUiState var getUser by remember { mutableStateOf(currentUserViewModel.user) } - 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 -> - coroutineScope.launch { - getUser?.uid?.let { viewModel.addToRent(items = items, userId = it) } - } + // Проверяем, есть ли у пользователя uid + 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 -> + 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) diff --git a/app/src/main/java/com/example/myapplication/composeui/navigation/MainNavbar.kt b/app/src/main/java/com/example/myapplication/composeui/navigation/MainNavbar.kt index 4dad969..745898b 100644 --- a/app/src/main/java/com/example/myapplication/composeui/navigation/MainNavbar.kt +++ b/app/src/main/java/com/example/myapplication/composeui/navigation/MainNavbar.kt @@ -185,7 +185,7 @@ fun Navhost( startDestination = Screen.BikeList.route, 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.Cart.route) { Cart(currentUserViewModel = currentUserViewModel) } composable(Screen.UserProfile.route) { UserProfile(isDarkTheme, dataStore, currentUserViewModel = currentUserViewModel) } diff --git a/app/src/main/java/com/example/myapplication/database/entities/composeui/BikeList.kt b/app/src/main/java/com/example/myapplication/database/entities/composeui/BikeList.kt index c5bace7..5704198 100644 --- a/app/src/main/java/com/example/myapplication/database/entities/composeui/BikeList.kt +++ b/app/src/main/java/com/example/myapplication/database/entities/composeui/BikeList.kt @@ -51,56 +51,64 @@ import kotlinx.coroutines.launch @Composable fun BikeList( navController: NavController, - viewModel: BikeListViewModel = viewModel(factory = AppViewModelProvider.Factory) + viewModel: BikeListViewModel = viewModel(factory = AppViewModelProvider.Factory), + currentUserViewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory) ) { - val coroutineScope = rememberCoroutineScope() - val bikePagingItems = viewModel.bikeListUiState.collectAsLazyPagingItems() + var getUser by remember { mutableStateOf(currentUserViewModel.user) } - Scaffold( - topBar = {}, - floatingActionButton = { - Box( - //modifier = Modifier.fillMaxWidth(), - contentAlignment = Alignment.Center // Center align the FloatingActionButton - ) { - 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 + // Проверяем, есть ли у пользователя uid + if (getUser?.uid != null) { + val coroutineScope = rememberCoroutineScope() + val bikePagingItems = viewModel.bikeListUiState.collectAsLazyPagingItems() + + Scaffold( + topBar = {}, + floatingActionButton = { + 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( + 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 -> - 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) - }, - ) + } else { + UnauthorizedScreen() } } + @Composable private fun BikeList( modifier: Modifier = Modifier, diff --git a/app/src/main/java/com/example/myapplication/database/entities/composeui/RentList.kt b/app/src/main/java/com/example/myapplication/database/entities/composeui/RentList.kt index cfff48c..facf349 100644 --- a/app/src/main/java/com/example/myapplication/database/entities/composeui/RentList.kt +++ b/app/src/main/java/com/example/myapplication/database/entities/composeui/RentList.kt @@ -5,9 +5,12 @@ import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.shape.RoundedCornerShape @@ -26,6 +29,7 @@ import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel @@ -44,56 +48,79 @@ fun RentList( currentUserViewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory) ) { var getUser by remember { mutableStateOf(currentUserViewModel.user) } - getUser?.uid?.let { viewModel.setUserId(it) } - val coroutineScope = rememberCoroutineScope() - val rentsUiState = viewModel.rentListUiState.collectAsLazyPagingItems() - LazyColumn( - modifier = Modifier - .fillMaxSize() - .padding(all = 10.dp) - ) { - items( - count = rentsUiState.itemCount, - key = rentsUiState.itemKey(), - contentType = rentsUiState.itemContentType() - ) { index -> - val rent = rentsUiState[index] - val rentId = Screen.RentView.route.replace("{id}", rent!!.uid.toString()) - Box( - modifier = Modifier - .fillMaxWidth() - .padding(all = 10.dp) - .clickable { navController?.navigate(rentId) } - .background( - color = MaterialTheme.colorScheme.primary, - shape = RoundedCornerShape(16.dp) - ) - ) { - Row( + // Проверяем, есть ли у пользователя uid + if (getUser?.uid != null) { + viewModel.setUserId(getUser?.uid!!) + + val coroutineScope = rememberCoroutineScope() + val rentsUiState = viewModel.rentListUiState.collectAsLazyPagingItems() + + LazyColumn( + modifier = Modifier + .fillMaxSize() + .padding(all = 10.dp) + ) { + items( + count = rentsUiState.itemCount, + key = rentsUiState.itemKey(), + contentType = rentsUiState.itemContentType() + ) { index -> + val rent = rentsUiState[index] + val rentId = Screen.RentView.route.replace("{id}", rent!!.uid.toString()) + Box( 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 = "Удалить" + .padding(all = 10.dp) + .clickable { navController?.navigate(rentId) } + .background( + color = MaterialTheme.colorScheme.primary, + shape = RoundedCornerShape(16.dp) ) + ) { + 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.") } } diff --git a/server/data.json b/server/data.json index c9ad22b..84f3584 100644 --- a/server/data.json +++ b/server/data.json @@ -79,6 +79,19 @@ "bikeId": 2 } ] + }, + { + "id": 9, + "userId": 7, + "items": [ + { + "id": 4, + "dateTime": 2023, + "frozenWeight": 26.3, + "count": 3, + "bikeId": 2 + } + ] } ], "items": [