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

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,6 +74,8 @@ fun Cart(
val cartUiState = viewModel.cartUiState val cartUiState = viewModel.cartUiState
var getUser by remember { mutableStateOf(currentUserViewModel.user) } var getUser by remember { mutableStateOf(currentUserViewModel.user) }
// Проверяем, есть ли у пользователя uid
if (getUser?.uid != null) {
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
getUser?.uid?.let { viewModel.refreshState(it) } getUser?.uid?.let { viewModel.refreshState(it) }
} }
@ -116,6 +120,26 @@ fun Cart(
} }
} }
) )
} 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,8 +51,13 @@ 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)
) { ) {
var getUser by remember { mutableStateOf(currentUserViewModel.user) }
// Проверяем, есть ли у пользователя uid
if (getUser?.uid != null) {
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
val bikePagingItems = viewModel.bikeListUiState.collectAsLazyPagingItems() val bikePagingItems = viewModel.bikeListUiState.collectAsLazyPagingItems()
@ -60,8 +65,7 @@ fun BikeList(
topBar = {}, topBar = {},
floatingActionButton = { floatingActionButton = {
Box( Box(
//modifier = Modifier.fillMaxWidth(), contentAlignment = Alignment.Center
contentAlignment = Alignment.Center // Center align the FloatingActionButton
) { ) {
FloatingActionButton( FloatingActionButton(
onClick = { onClick = {
@ -100,7 +104,11 @@ fun BikeList(
}, },
) )
} }
} else {
UnauthorizedScreen()
}
} }
@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,10 +48,14 @@ 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) }
// Проверяем, есть ли у пользователя uid
if (getUser?.uid != null) {
viewModel.setUserId(getUser?.uid!!)
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
val rentsUiState = viewModel.rentListUiState.collectAsLazyPagingItems() val rentsUiState = viewModel.rentListUiState.collectAsLazyPagingItems()
LazyColumn( LazyColumn(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
@ -95,6 +103,25 @@ fun RentList(
} }
} }
} }
} 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.")
}
} }
@Preview(name = "Light Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO) @Preview(name = "Light Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO)

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": [