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

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.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<ItemFromCart> ->
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<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)

View File

@ -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) }

View File

@ -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,

View File

@ -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.")
}
}

View File

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