Fix: correct update basket

This commit is contained in:
Emelyanov535 2023-12-13 22:51:51 +04:00
parent db454fe540
commit b416011a1d
8 changed files with 51 additions and 28 deletions

View File

@ -21,14 +21,14 @@ class RestBasketRepository(
service.createBasketSneaker(basketSneaker.toBasketSneakerRemote()) service.createBasketSneaker(basketSneaker.toBasketSneakerRemote())
} }
override fun getBasketWithSneakers(id: Int): Flow<List<Sneaker>> = runBlocking { override suspend fun getBasketWithSneakers(id: Int): Flow<List<Sneaker>> {
val sneakersRemoteList = service.getUserBasketSneakers(id) val sneakersRemoteList = service.getUserBasketSneakers(id)
val sneakersList = sneakersRemoteList.map { it.toSneaker() } val sneakersList = sneakersRemoteList.map { it.toSneaker() }
flowOf(sneakersList.toList()) return flowOf(sneakersList.toList())
} }
override suspend fun getUserBasketId(id: Int): Int { override suspend fun getUserBasketId(id: Int): Flow<Int> {
return service.getUserBasket(id) return flowOf(service.getUserBasket(id))
} }
override suspend fun getQuantity(basketId: Int, sneakerId: Int): Int? { override suspend fun getQuantity(basketId: Int, sneakerId: Int): Int? {

View File

@ -11,8 +11,8 @@ import kotlinx.coroutines.flow.Flow
interface BasketRepository { interface BasketRepository {
suspend fun insertBasketSneaker(basketSneaker: BasketSneakers) suspend fun insertBasketSneaker(basketSneaker: BasketSneakers)
fun getBasketWithSneakers(id: Int): Flow<List<Sneaker>> suspend fun getBasketWithSneakers(id: Int): Flow<List<Sneaker>>
suspend fun getUserBasketId(id: Int): Int suspend fun getUserBasketId(id: Int): Flow<Int>
// fun getAllBasket(): Flow<List<Basket>> // fun getAllBasket(): Flow<List<Basket>>
// suspend fun delete(basket: Basket) // suspend fun delete(basket: Basket)
// suspend fun createBasket(basket: Basket):Long // suspend fun createBasket(basket: Basket):Long

View File

@ -8,16 +8,20 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.example.android_programming.GlobalUser
import com.example.android_programming.model.BasketSneakers import com.example.android_programming.model.BasketSneakers
import com.example.android_programming.model.BasketWithSneakers import com.example.android_programming.model.BasketWithSneakers
import com.example.android_programming.businessLogic.repo.BasketRepository import com.example.android_programming.businessLogic.repo.BasketRepository
import com.example.android_programming.model.Sneaker import com.example.android_programming.model.Sneaker
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
@ -25,6 +29,9 @@ class BasketViewModel(private val basketRepository: BasketRepository): ViewModel
private val _quantityStateMap = mutableMapOf<Int, MutableStateFlow<Int>>() private val _quantityStateMap = mutableMapOf<Int, MutableStateFlow<Int>>()
private val _sneakerList = MutableStateFlow<List<Sneaker>>(emptyList())
val sneakerList: StateFlow<List<Sneaker>> = _sneakerList
fun getQuantityState(basketId: Int, sneakerId: Int): StateFlow<Int> { fun getQuantityState(basketId: Int, sneakerId: Int): StateFlow<Int> {
val quantityStateFlow = _quantityStateMap.getOrPut(sneakerId) { val quantityStateFlow = _quantityStateMap.getOrPut(sneakerId) {
MutableStateFlow(0) MutableStateFlow(0)
@ -50,17 +57,22 @@ class BasketViewModel(private val basketRepository: BasketRepository): ViewModel
basketRepository.insertBasketSneaker(basketSneakers) basketRepository.insertBasketSneaker(basketSneakers)
} }
} }
fun getBasketSneakers(userId: Int) : Flow<List<Sneaker>> { fun fetchBasketSneakers(userId: Int) {
return basketRepository.getBasketWithSneakers(userId) viewModelScope.launch {
basketRepository.getBasketWithSneakers(userId).collect {
_sneakerList.value = it
}
}
} }
suspend fun getUserBasketId(userId: Int) : Int{ suspend fun getUserBasketId(userId: Int) : Flow<Int>{
return basketRepository.getUserBasketId(userId) return basketRepository.getUserBasketId(userId)
} }
fun deleteSneakerFromBasket(basketId: Int, sneakerId: Int) = viewModelScope.launch { fun deleteSneakerFromBasket(basketId: Int, sneakerId: Int) = viewModelScope.launch {
basketRepository.removeSneakerFromBasket(basketId, sneakerId) basketRepository.removeSneakerFromBasket(basketId, sneakerId)
fetchBasketSneakers(GlobalUser.getInstance().getUser()?.userId!!)
} }
fun incrementQuantity(basketId: Int, sneakerId: Int) { fun incrementQuantity(basketId: Int, sneakerId: Int) {

View File

@ -62,8 +62,7 @@ class OrderViewModel(private val orderRepository: OrderRepository, private val b
for (sneaker in selectedItems.value.orEmpty()) { for (sneaker in selectedItems.value.orEmpty()) {
val userId = GlobalUser.getInstance().getUser()?.userId!! val userId = GlobalUser.getInstance().getUser()?.userId!!
val orderSneaker = basketRepository.getQuantity(userId, sneaker.sneakerId!!) val orderSneaker = OrderSneaker( orderId.toInt(), sneaker.sneakerId!!, 1)
?.let { OrderSneaker( orderId.toInt(), sneaker.sneakerId!!, it) }
if (orderSneaker != null) { if (orderSneaker != null) {
orderRepository.insertOrderSneaker(orderSneaker) orderRepository.insertOrderSneaker(orderSneaker)
} }

View File

@ -18,7 +18,11 @@ import androidx.compose.material.Text
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ShoppingCart import androidx.compose.material.icons.filled.ShoppingCart
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
@ -37,14 +41,16 @@ import com.example.android_programming.model.Sneaker
import com.example.android_programming.businessLogic.vmodel.AppViewModelProvider import com.example.android_programming.businessLogic.vmodel.AppViewModelProvider
import com.example.android_programming.businessLogic.vmodel.BasketViewModel import com.example.android_programming.businessLogic.vmodel.BasketViewModel
import com.google.gson.Gson import com.google.gson.Gson
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
@Composable @Composable
fun CardSneaker(item: Sneaker, navController: NavHostController, basketViewModel: BasketViewModel = viewModel(factory = AppViewModelProvider.Factory)) { fun CardSneaker(item: Sneaker, navController: NavHostController, basketViewModel: BasketViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
val maxWidth = (LocalConfiguration.current.screenWidthDp / 2).dp val maxWidth = (LocalConfiguration.current.screenWidthDp / 2).dp
val coroutineScope = rememberCoroutineScope()
Box( Box(
modifier = Modifier modifier = Modifier
.padding(4.dp) .padding(4.dp)
@ -100,12 +106,10 @@ fun CardSneaker(item: Sneaker, navController: NavHostController, basketViewModel
if(user == null){ if(user == null){
navController.navigate("login") navController.navigate("login")
}else{ }else{
basketViewModel.addToBasket(BasketSneakers(1, item.sneakerId!!, 1)) coroutineScope.launch {
// runBlocking { val userBasketId: Int = basketViewModel.getUserBasketId(user.userId!!).first()
// launch(Dispatchers.Default) { basketViewModel.addToBasket(BasketSneakers(userBasketId, item.sneakerId!!, 1))
// basketViewModel.addToBasket(BasketSneakers(basketViewModel.getUserBasketId(user.userId!!), item.sneakerId!!, 1)) }
// }
// }
} }
}, },
modifier = Modifier modifier = Modifier

View File

@ -24,6 +24,7 @@ import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
@ -42,6 +43,7 @@ import com.example.android_programming.businessLogic.vmodel.BasketViewModel
import com.example.android_programming.businessLogic.vmodel.OrderViewModel import com.example.android_programming.businessLogic.vmodel.OrderViewModel
import com.example.android_programming.model.BasketSneakers import com.example.android_programming.model.BasketSneakers
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
@ -50,6 +52,7 @@ import kotlinx.coroutines.runBlocking
fun CardSneakerLike(item: Sneaker, basketViewModel: BasketViewModel = viewModel(factory = AppViewModelProvider.Factory)) { fun CardSneakerLike(item: Sneaker, basketViewModel: BasketViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
val userId = GlobalUser.getInstance().getUser()?.userId!! val userId = GlobalUser.getInstance().getUser()?.userId!!
val quantityState by basketViewModel.getQuantityState(userId, item.sneakerId!!).collectAsState() val quantityState by basketViewModel.getQuantityState(userId, item.sneakerId!!).collectAsState()
val scope = rememberCoroutineScope()
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -85,7 +88,9 @@ fun CardSneakerLike(item: Sneaker, basketViewModel: BasketViewModel = viewModel(
contentColor = Color.White contentColor = Color.White
), ),
onClick = { onClick = {
basketViewModel.deleteSneakerFromBasket(1, item.sneakerId!!) scope.launch {
basketViewModel.deleteSneakerFromBasket(basketViewModel.getUserBasketId(GlobalUser.getInstance().getUser()?.userId!!).first(), item.sneakerId!!)
}
// runBlocking { // runBlocking {
// launch(Dispatchers.Default) { // launch(Dispatchers.Default) {
// basketViewModel.deleteSneakerFromBasket(basketViewModel.getUserBasketId(GlobalUser.getInstance().getUser()?.userId!!), item.sneakerId!!) // basketViewModel.deleteSneakerFromBasket(basketViewModel.getUserBasketId(GlobalUser.getInstance().getUser()?.userId!!), item.sneakerId!!)

View File

@ -15,6 +15,7 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource import androidx.compose.ui.res.colorResource
@ -30,12 +31,14 @@ import com.example.android_programming.businessLogic.vmodel.BasketViewModel
import com.example.android_programming.businessLogic.vmodel.OrderViewModel import com.example.android_programming.businessLogic.vmodel.OrderViewModel
import com.example.android_programming.model.BasketSneakers import com.example.android_programming.model.BasketSneakers
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
@Composable @Composable
fun OrderScreen(navHostController: NavHostController, basketViewModel: BasketViewModel = viewModel(factory = AppViewModelProvider.Factory), orderViewModel: OrderViewModel = viewModel(factory = AppViewModelProvider.Factory)) { fun OrderScreen(navHostController: NavHostController, basketViewModel: BasketViewModel = viewModel(factory = AppViewModelProvider.Factory), orderViewModel: OrderViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
val scope = rememberCoroutineScope()
Column( Column(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
@ -46,10 +49,11 @@ fun OrderScreen(navHostController: NavHostController, basketViewModel: BasketVie
DeliveryAddress(orderViewModel) DeliveryAddress(orderViewModel)
val userId = GlobalUser.getInstance().getUser()?.userId val userId = GlobalUser.getInstance().getUser()?.userId
if (userId != null) { if (userId != null) {
val sneakerList: List<Sneaker>? by basketViewModel.getBasketSneakers(userId).collectAsState(null) basketViewModel.fetchBasketSneakers(userId!!)
val sneakerList: List<Sneaker>? = basketViewModel.sneakerList.collectAsState(null).value
if (sneakerList != null) { if (sneakerList != null) {
orderViewModel.updateSelectedItems(sneakerList!!) orderViewModel.updateSelectedItems(sneakerList)
ShoppingList(sneakerList!!) ShoppingList(sneakerList)
SubTotal(orderViewModel) SubTotal(orderViewModel)
} }
} }
@ -61,9 +65,8 @@ fun OrderScreen(navHostController: NavHostController, basketViewModel: BasketVie
onClick = { onClick = {
if(GlobalUser.getInstance().getUser() != null){ if(GlobalUser.getInstance().getUser() != null){
orderViewModel.createOrder() orderViewModel.createOrder()
runBlocking { scope.launch {
launch(Dispatchers.Default) { basketViewModel.deleteAllSneakerFromBasket(basketViewModel.getUserBasketId(userId!!).first())
basketViewModel.deleteAllSneakerFromBasket(basketViewModel.getUserBasketId(userId!!)) }
} }
navHostController.navigate("home") navHostController.navigate("home")
}else{ }else{

View File

@ -77,9 +77,9 @@ abstract class AppDatabase : RoomDatabase() {
.addCallback(object : Callback() { .addCallback(object : Callback() {
override fun onCreate(db: SupportSQLiteDatabase) { override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db) super.onCreate(db)
CoroutineScope(Dispatchers.IO).launch { // CoroutineScope(Dispatchers.IO).launch {
populateDatabase() // populateDatabase()
} // }
} }
}) })
.fallbackToDestructiveMigration() .fallbackToDestructiveMigration()