Опа, пофикисили отображение

This commit is contained in:
Кашин Максим 2023-12-23 02:15:16 +04:00
parent f76e0ff450
commit 17a4568123
4 changed files with 33 additions and 11 deletions

View File

@ -55,6 +55,7 @@ import com.example.myapplication.R
import com.example.myapplication.database.entities.composeui.AppViewModelProvider import com.example.myapplication.database.entities.composeui.AppViewModelProvider
import com.example.myapplication.database.entities.composeui.CartUiState import com.example.myapplication.database.entities.composeui.CartUiState
import com.example.myapplication.database.entities.composeui.CartViewModel import com.example.myapplication.database.entities.composeui.CartViewModel
import com.example.myapplication.database.entities.composeui.CurrentUserViewModel
import com.example.myapplication.database.entities.model.Item import com.example.myapplication.database.entities.model.Item
import com.example.myapplication.database.entities.model.ItemFromCart import com.example.myapplication.database.entities.model.ItemFromCart
import com.example.myapplication.ui.theme.PmudemoTheme import com.example.myapplication.ui.theme.PmudemoTheme
@ -64,13 +65,15 @@ import org.threeten.bp.format.DateTimeFormatter
@Composable @Composable
fun Cart( fun Cart(
viewModel: CartViewModel = viewModel(factory = AppViewModelProvider.Factory) viewModel: CartViewModel = viewModel(factory = AppViewModelProvider.Factory),
currentUserViewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory)
) { ) {
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
val cartUiState = viewModel.cartUiState val cartUiState = viewModel.cartUiState
var getUser by remember { mutableStateOf(currentUserViewModel.user) }
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
viewModel.refreshState() getUser?.uid?.let { viewModel.refreshState(it) }
} }
Cart( Cart(

View File

@ -187,7 +187,7 @@ fun Navhost(
) { ) {
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, 1) }
composable(Screen.Cart.route) { Cart() } 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(
Screen.BikeEdit.route, Screen.BikeEdit.route,

View File

@ -21,12 +21,12 @@ class CartViewModel(
private val rentItemRepository: RentItemRepository, private val rentItemRepository: RentItemRepository,
private val userRepository: UserRepository, private val userRepository: UserRepository,
) : ViewModel() { ) : ViewModel() {
private val userUid: Int = 1 //private val userUid: Int = 1
var cartUiState by mutableStateOf(CartUiState()) var cartUiState by mutableStateOf(CartUiState())
private set private set
suspend fun refreshState() { suspend fun refreshState(userId: Int) {
val cart = userRepository.getCartByUser(userUid) val cart = userRepository.getCartByUser(userId)
cartUiState = CartUiState(cart) cartUiState = CartUiState(cart)
} }
@ -45,12 +45,12 @@ class CartViewModel(
) )
} }
userItemRepository.deleteUserItems(userId) userItemRepository.deleteUserItems(userId)
refreshState() refreshState(userId)
} }
suspend fun removeFromCart(user: Int, item: Item, count: Int = 1) { suspend fun removeFromCart(user: Int, item: Item, count: Int = 1) {
userItemRepository.deleteUserItem(UserItemCrossRef(user, item.uid, count)) userItemRepository.deleteUserItem(UserItemCrossRef(user, item.uid, count))
refreshState() refreshState(user)
} }
suspend fun updateFromCart(userId: Int, item: Item, count: Int, availableCount: Int) suspend fun updateFromCart(userId: Int, item: Item, count: Int, availableCount: Int)
@ -62,7 +62,7 @@ class CartViewModel(
if (count > availableCount) if (count > availableCount)
return false return false
userItemRepository.updateUserItem(UserItemCrossRef(userId, item.uid, count)) userItemRepository.updateUserItem(UserItemCrossRef(userId, item.uid, count))
refreshState() refreshState(userId)
return true return true
} }
} }

View File

@ -20,7 +20,19 @@
{ {
"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,
@ -45,7 +57,14 @@
{ {
"id": 7, "id": 7,
"login": "1", "login": "1",
"password": "1" "password": "1",
"items": [
{
"id": 4,
"count": 1,
"bikeId": 2
}
]
}, },
{ {
"id": 8, "id": 8,