diff --git a/app/src/main/java/com/example/myapplication/composeui/Cart.kt b/app/src/main/java/com/example/myapplication/composeui/Cart.kt index 261cb4e..5e49ff1 100644 --- a/app/src/main/java/com/example/myapplication/composeui/Cart.kt +++ b/app/src/main/java/com/example/myapplication/composeui/Cart.kt @@ -55,6 +55,7 @@ import com.example.myapplication.R import com.example.myapplication.database.entities.composeui.AppViewModelProvider import com.example.myapplication.database.entities.composeui.CartUiState 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.ItemFromCart import com.example.myapplication.ui.theme.PmudemoTheme @@ -64,13 +65,15 @@ import org.threeten.bp.format.DateTimeFormatter @Composable fun Cart( - viewModel: CartViewModel = viewModel(factory = AppViewModelProvider.Factory) + viewModel: CartViewModel = viewModel(factory = AppViewModelProvider.Factory), + currentUserViewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory) ) { val coroutineScope = rememberCoroutineScope() val cartUiState = viewModel.cartUiState + var getUser by remember { mutableStateOf(currentUserViewModel.user) } LaunchedEffect(Unit) { - viewModel.refreshState() + getUser?.uid?.let { viewModel.refreshState(it) } } Cart( diff --git a/app/src/main/java/com/example/myapplication/composeui/navigation/MainNavbar.kt b/app/src/main/java/com/example/myapplication/composeui/navigation/MainNavbar.kt index 99fd961..adb55ed 100644 --- a/app/src/main/java/com/example/myapplication/composeui/navigation/MainNavbar.kt +++ b/app/src/main/java/com/example/myapplication/composeui/navigation/MainNavbar.kt @@ -187,7 +187,7 @@ fun Navhost( ) { composable(Screen.BikeList.route) { BikeList(navController) } 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.BikeEdit.route, diff --git a/app/src/main/java/com/example/myapplication/database/entities/composeui/CartViewModel.kt b/app/src/main/java/com/example/myapplication/database/entities/composeui/CartViewModel.kt index 40e3627..0319e44 100644 --- a/app/src/main/java/com/example/myapplication/database/entities/composeui/CartViewModel.kt +++ b/app/src/main/java/com/example/myapplication/database/entities/composeui/CartViewModel.kt @@ -21,12 +21,12 @@ class CartViewModel( private val rentItemRepository: RentItemRepository, private val userRepository: UserRepository, ) : ViewModel() { - private val userUid: Int = 1 + //private val userUid: Int = 1 var cartUiState by mutableStateOf(CartUiState()) private set - suspend fun refreshState() { - val cart = userRepository.getCartByUser(userUid) + suspend fun refreshState(userId: Int) { + val cart = userRepository.getCartByUser(userId) cartUiState = CartUiState(cart) } @@ -45,12 +45,12 @@ class CartViewModel( ) } userItemRepository.deleteUserItems(userId) - refreshState() + refreshState(userId) } suspend fun removeFromCart(user: Int, item: Item, count: Int = 1) { userItemRepository.deleteUserItem(UserItemCrossRef(user, item.uid, count)) - refreshState() + refreshState(user) } suspend fun updateFromCart(userId: Int, item: Item, count: Int, availableCount: Int) @@ -62,7 +62,7 @@ class CartViewModel( if (count > availableCount) return false userItemRepository.updateUserItem(UserItemCrossRef(userId, item.uid, count)) - refreshState() + refreshState(userId) return true } } diff --git a/server/data.json b/server/data.json index ab5cdff..ca08969 100644 --- a/server/data.json +++ b/server/data.json @@ -20,7 +20,19 @@ { "id": 2, "login": "hello", - "password": "hello" + "password": "hello", + "items": [ + { + "id": 6, + "count": 1, + "bikeId": 3 + }, + { + "id": 1, + "count": 1, + "bikeId": 1 + } + ] }, { "id": 3, @@ -45,7 +57,14 @@ { "id": 7, "login": "1", - "password": "1" + "password": "1", + "items": [ + { + "id": 4, + "count": 1, + "bikeId": 2 + } + ] }, { "id": 8,