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