Ура, адекватный пользователь сделан, осталось адекватную авторизацию или регистрацию сделать...
This commit is contained in:
parent
17a4568123
commit
68a276d549
@ -80,8 +80,9 @@ fun Cart(
|
||||
cartUiState = cartUiState,
|
||||
modifier = Modifier
|
||||
.padding(all = 10.dp),
|
||||
onSwipe = { item: ItemFromCart, user: Int ->
|
||||
onSwipe = { item: ItemFromCart ->
|
||||
coroutineScope.launch {
|
||||
getUser?.uid?.let {
|
||||
viewModel.removeFromCart(
|
||||
item = Item(
|
||||
uid = item.uid,
|
||||
@ -89,12 +90,14 @@ fun Cart(
|
||||
weight = item.weight,
|
||||
maxCount = 0,
|
||||
bikeId = item.bikeId
|
||||
), user = user
|
||||
), user = it
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
onChangeCount = { item: ItemFromCart, user: Int, count: Int ->
|
||||
onChangeCount = { item: ItemFromCart, count: Int ->
|
||||
coroutineScope.launch {
|
||||
getUser?.uid?.let {
|
||||
viewModel.updateFromCart(
|
||||
item = Item(
|
||||
uid = item.uid,
|
||||
@ -102,13 +105,14 @@ fun Cart(
|
||||
weight = item.weight,
|
||||
maxCount = 0,
|
||||
bikeId = item.bikeId
|
||||
), userId = user, count = count, availableCount = item.availableCount
|
||||
), userId = it, count = count, availableCount = item.availableCount
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
onAddToRent = { items: List<ItemFromCart>, user: Int ->
|
||||
onAddToRent = { items: List<ItemFromCart> ->
|
||||
coroutineScope.launch {
|
||||
viewModel.addToRent(items = items, userId = user)
|
||||
getUser?.uid?.let { viewModel.addToRent(items = items, userId = it) }
|
||||
}
|
||||
}
|
||||
)
|
||||
@ -119,9 +123,9 @@ fun Cart(
|
||||
private fun Cart(
|
||||
cartUiState: CartUiState,
|
||||
modifier: Modifier,
|
||||
onSwipe: (ItemFromCart, Int) -> Unit,
|
||||
onChangeCount: (ItemFromCart, Int, Int) -> Unit,
|
||||
onAddToRent: (List<ItemFromCart>, Int) -> Unit
|
||||
onSwipe: (ItemFromCart) -> Unit,
|
||||
onChangeCount: (ItemFromCart, Int) -> Unit,
|
||||
onAddToRent: (List<ItemFromCart>) -> Unit
|
||||
) {
|
||||
LazyColumn(
|
||||
modifier = modifier
|
||||
@ -132,7 +136,7 @@ private fun Cart(
|
||||
)
|
||||
|
||||
if (dismissState.isDismissed(direction = DismissDirection.EndToStart)) {
|
||||
onSwipe(item, 1)
|
||||
onSwipe(item)
|
||||
}
|
||||
|
||||
SwipeToDelete(
|
||||
@ -146,7 +150,7 @@ private fun Cart(
|
||||
Spacer(modifier = Modifier.weight(1f))
|
||||
|
||||
Button(
|
||||
onClick = { onAddToRent(cartUiState.itemList, 1) },
|
||||
onClick = { onAddToRent(cartUiState.itemList) },
|
||||
modifier = Modifier
|
||||
.padding(16.dp)
|
||||
.fillMaxWidth()
|
||||
@ -159,7 +163,7 @@ private fun Cart(
|
||||
private fun SwipeToDelete(
|
||||
dismissState: DismissState,
|
||||
item: ItemFromCart,
|
||||
onChangeCount: (ItemFromCart, Int, Int) -> Unit,
|
||||
onChangeCount: (ItemFromCart, Int) -> Unit,
|
||||
) {
|
||||
SwipeToDismiss(
|
||||
state = dismissState,
|
||||
@ -211,7 +215,7 @@ private fun SwipeToDelete(
|
||||
private fun ItemListItem(
|
||||
item: ItemFromCart,
|
||||
modifier: Modifier = Modifier,
|
||||
onChangeCount: (ItemFromCart, Int, Int) -> Unit,
|
||||
onChangeCount: (ItemFromCart, Int) -> Unit,
|
||||
) {
|
||||
var currentCount by remember { mutableStateOf(item.count) }
|
||||
|
||||
@ -269,7 +273,7 @@ private fun ItemListItem(
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
IconButton(
|
||||
onClick = { onChangeCount(item, 1, --currentCount) }
|
||||
onClick = { onChangeCount(item, --currentCount) }
|
||||
) {
|
||||
Icon(
|
||||
imageVector = ImageVector.vectorResource(id = R.drawable.minus),
|
||||
@ -288,7 +292,6 @@ private fun ItemListItem(
|
||||
onClick = {
|
||||
onChangeCount(
|
||||
item,
|
||||
1,
|
||||
if (currentCount != item.availableCount) ++currentCount else currentCount
|
||||
)
|
||||
}
|
||||
|
@ -186,7 +186,7 @@ fun Navhost(
|
||||
modifier.padding(innerPadding)
|
||||
) {
|
||||
composable(Screen.BikeList.route) { BikeList(navController) }
|
||||
composable(Screen.RentList.route) { RentList(navController, 1) }
|
||||
composable(Screen.RentList.route) { RentList(navController, currentUserViewModel = currentUserViewModel) }
|
||||
composable(Screen.Cart.route) { Cart(currentUserViewModel = currentUserViewModel) }
|
||||
composable(Screen.UserProfile.route) { UserProfile(isDarkTheme, dataStore, currentUserViewModel = currentUserViewModel) }
|
||||
composable(
|
||||
|
@ -19,7 +19,11 @@ import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
@ -36,9 +40,12 @@ import kotlinx.coroutines.launch
|
||||
@Composable
|
||||
fun RentList(
|
||||
navController: NavController?,
|
||||
userId: Int?,
|
||||
viewModel: RentListViewModel = viewModel(factory = AppViewModelProvider.Factory)
|
||||
viewModel: RentListViewModel = viewModel(factory = AppViewModelProvider.Factory),
|
||||
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(
|
||||
@ -98,7 +105,7 @@ fun RentListPreview() {
|
||||
Surface(
|
||||
color = MaterialTheme.colorScheme.background
|
||||
) {
|
||||
RentList(navController = null, 1)
|
||||
RentList(navController = null)
|
||||
}
|
||||
}
|
||||
}
|
@ -5,15 +5,27 @@ import androidx.paging.PagingData
|
||||
import com.example.myapplication.database.entities.model.Rent
|
||||
import com.example.myapplication.database.entities.repository.RentRepository
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
|
||||
class RentListViewModel(
|
||||
private val rentRepository: RentRepository
|
||||
) : ViewModel() {
|
||||
val rentListUiState: Flow<PagingData<Rent>> = rentRepository.getAllRents(1)
|
||||
private val _userId = MutableStateFlow<Int?>(null)
|
||||
val userId: StateFlow<Int?> get() = _userId
|
||||
|
||||
val rentListUiState: Flow<PagingData<Rent>> = userId.flatMapLatest { userId ->
|
||||
rentRepository.getAllRents(userId)
|
||||
}
|
||||
|
||||
suspend fun deleteRent(rent: Rent) {
|
||||
rentRepository.deleteRent(rent)
|
||||
}
|
||||
|
||||
fun setUserId(userId: Int) {
|
||||
_userId.value = userId
|
||||
}
|
||||
}
|
||||
|
||||
data class RentListUiState(val rentList: List<Rent> = listOf())
|
@ -20,19 +20,7 @@
|
||||
{
|
||||
"id": 2,
|
||||
"login": "hello",
|
||||
"password": "hello",
|
||||
"items": [
|
||||
{
|
||||
"id": 6,
|
||||
"count": 1,
|
||||
"bikeId": 3
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"count": 1,
|
||||
"bikeId": 1
|
||||
}
|
||||
]
|
||||
"password": "hello"
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
@ -57,14 +45,7 @@
|
||||
{
|
||||
"id": 7,
|
||||
"login": "1",
|
||||
"password": "1",
|
||||
"items": [
|
||||
{
|
||||
"id": 4,
|
||||
"count": 1,
|
||||
"bikeId": 2
|
||||
}
|
||||
]
|
||||
"password": "1"
|
||||
},
|
||||
{
|
||||
"id": 8,
|
||||
@ -74,28 +55,8 @@
|
||||
],
|
||||
"rents": [
|
||||
{
|
||||
"id": 2,
|
||||
"userId": 1,
|
||||
"items": [
|
||||
{
|
||||
"id": 1,
|
||||
"dateTime": 2026,
|
||||
"frozenWeight": 26.6,
|
||||
"count": 1,
|
||||
"bikeId": 1
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"dateTime": 2027,
|
||||
"frozenWeight": 24.6,
|
||||
"count": 2,
|
||||
"bikeId": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"userId": 7,
|
||||
"id": 6,
|
||||
"userId": 2,
|
||||
"items": [
|
||||
{
|
||||
"id": 1,
|
||||
@ -103,52 +64,18 @@
|
||||
"frozenWeight": 26.6,
|
||||
"count": 2,
|
||||
"bikeId": 1
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"dateTime": 2023,
|
||||
"frozenWeight": 26.3,
|
||||
"count": 1,
|
||||
"bikeId": 2
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"userId": 7,
|
||||
"id": 8,
|
||||
"userId": 2,
|
||||
"items": [
|
||||
{
|
||||
"id": 1,
|
||||
"dateTime": 2021,
|
||||
"frozenWeight": 26.6,
|
||||
"id": 4,
|
||||
"dateTime": 2023,
|
||||
"frozenWeight": 26.3,
|
||||
"count": 2,
|
||||
"bikeId": 1
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"dateTime": 2023,
|
||||
"frozenWeight": 26.3,
|
||||
"count": 1,
|
||||
"bikeId": 2
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"userId": 7,
|
||||
"items": [
|
||||
{
|
||||
"id": 1,
|
||||
"dateTime": 2021,
|
||||
"frozenWeight": 26.6,
|
||||
"count": 3,
|
||||
"bikeId": 1
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"dateTime": 2023,
|
||||
"frozenWeight": 26.3,
|
||||
"count": 1,
|
||||
"bikeId": 2
|
||||
}
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user