Feature: complete Basket realization
This commit is contained in:
parent
7990af8e0b
commit
76ef28b66c
@ -94,6 +94,12 @@ interface BackendService {
|
||||
@Path("sneakerId") sneakerId: Int,
|
||||
)
|
||||
|
||||
@GET("basket/getSneaker/{basketId}/{sneakerId}")
|
||||
suspend fun getSneaker(
|
||||
@Path("basketId") basketId: Int,
|
||||
@Path("sneakerId") sneakerId: Int,
|
||||
): Boolean
|
||||
|
||||
companion object {
|
||||
private const val BASE_URL = "https://59k4pfj3-8080.euw.devtunnels.ms/api/"
|
||||
|
||||
|
@ -2,6 +2,7 @@ package com.example.android_programming.api.repository
|
||||
|
||||
import com.example.android_programming.api.BackendService
|
||||
import com.example.android_programming.api.model.SneakerRemote
|
||||
import com.example.android_programming.api.model.toBasketSneaker
|
||||
import com.example.android_programming.api.model.toBasketSneakerRemote
|
||||
import com.example.android_programming.api.model.toSneaker
|
||||
import com.example.android_programming.businessLogic.repo.BasketRepository
|
||||
@ -25,7 +26,7 @@ class RestBasketRepository(
|
||||
return flowOf(sneakersList.toList())
|
||||
}
|
||||
|
||||
override suspend fun getUserOrder(id: Int): Int {
|
||||
override suspend fun getUserBasketId(id: Int): Int {
|
||||
return service.getUserBasket(id)
|
||||
}
|
||||
|
||||
@ -65,9 +66,9 @@ class RestBasketRepository(
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override suspend fun getSneaker(basketId: Int, sneakerId: Int): BasketSneakers? {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
override suspend fun existSneaker(basketId: Int, sneakerId: Int): Boolean {
|
||||
return service.getSneaker(basketId, sneakerId)
|
||||
}
|
||||
//
|
||||
// override suspend fun getTotalPriceForUser(userId: Int): Double? {
|
||||
// TODO("Not yet implemented")
|
||||
|
@ -12,7 +12,7 @@ import kotlinx.coroutines.flow.Flow
|
||||
interface BasketRepository {
|
||||
suspend fun insertBasketSneaker(basketSneaker: BasketSneakers)
|
||||
suspend fun getBasketWithSneakers(id: Int): Flow<List<Sneaker>>
|
||||
suspend fun getUserOrder(id: Int): Int
|
||||
suspend fun getUserBasketId(id: Int): Int
|
||||
// fun getAllBasket(): Flow<List<Basket>>
|
||||
// suspend fun delete(basket: Basket)
|
||||
// suspend fun createBasket(basket: Basket):Long
|
||||
@ -21,6 +21,6 @@ interface BasketRepository {
|
||||
suspend fun incrementSneakerQuantity(basketId: Int, sneakerId: Int)
|
||||
suspend fun decrementSneakerQuantity(basketId: Int, sneakerId: Int)
|
||||
suspend fun getQuantity(basketId: Int, sneakerId: Int): Int?
|
||||
// suspend fun getSneaker(basketId: Int, sneakerId: Int): BasketSneakers?
|
||||
suspend fun existSneaker(basketId: Int, sneakerId: Int): Boolean
|
||||
// suspend fun getTotalPriceForUser(userId: Int): Double?
|
||||
}
|
@ -12,12 +12,14 @@ import com.example.android_programming.model.BasketSneakers
|
||||
import com.example.android_programming.model.BasketWithSneakers
|
||||
import com.example.android_programming.businessLogic.repo.BasketRepository
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
class BasketViewModel(private val basketRepository: BasketRepository): ViewModel() {
|
||||
|
||||
@ -26,9 +28,6 @@ class BasketViewModel(private val basketRepository: BasketRepository): ViewModel
|
||||
private val _sneakerList = MutableStateFlow<List<Sneaker>>(emptyList())
|
||||
val sneakerList: StateFlow<List<Sneaker>> = _sneakerList.asStateFlow()
|
||||
|
||||
private val _basketId = MutableLiveData<Int>()
|
||||
val basketId: LiveData<Int> get() = _basketId
|
||||
|
||||
fun getQuantityState(basketId: Int, sneakerId: Int): StateFlow<Int> {
|
||||
val quantityStateFlow = _quantityStateMap.getOrPut(sneakerId) {
|
||||
MutableStateFlow(0)
|
||||
@ -41,21 +40,19 @@ class BasketViewModel(private val basketRepository: BasketRepository): ViewModel
|
||||
|
||||
return quantityStateFlow
|
||||
}
|
||||
//
|
||||
// suspend fun isSneakerInBasket(basketId: Int, sneakerId: Int): Boolean {
|
||||
// return basketRepository.getSneaker(basketId, sneakerId) != null
|
||||
// }
|
||||
fun addToBasket(basketSneakers: BasketSneakers) = viewModelScope.launch {
|
||||
basketRepository.insertBasketSneaker(basketSneakers)
|
||||
// val isSneakerInBasket = isSneakerInBasket(basketSneakers.basketId, basketSneakers.sneakerId)
|
||||
//
|
||||
// if (isSneakerInBasket) {
|
||||
// incrementQuantity(basketSneakers.basketId, basketSneakers.sneakerId)
|
||||
// } else {
|
||||
// basketRepository.insertBasketSneaker(basketSneakers)
|
||||
// }
|
||||
|
||||
suspend fun isSneakerInBasket(basketId: Int, sneakerId: Int): Boolean {
|
||||
return basketRepository.existSneaker(basketId, sneakerId)
|
||||
}
|
||||
fun addToBasket(basketSneakers: BasketSneakers) = viewModelScope.launch {
|
||||
val isSneakerInBasket = isSneakerInBasket(basketSneakers.basketId, basketSneakers.sneakerId)
|
||||
|
||||
if (isSneakerInBasket) {
|
||||
incrementQuantity(basketSneakers.basketId, basketSneakers.sneakerId)
|
||||
} else {
|
||||
basketRepository.insertBasketSneaker(basketSneakers)
|
||||
}
|
||||
}
|
||||
//
|
||||
fun fetchBasketSneakers(userId: Int) {
|
||||
viewModelScope.launch {
|
||||
basketRepository.getBasketWithSneakers(userId).collect {
|
||||
@ -64,12 +61,9 @@ class BasketViewModel(private val basketRepository: BasketRepository): ViewModel
|
||||
}
|
||||
}
|
||||
|
||||
// fun getUserBasketId(userId: Int) {
|
||||
// viewModelScope.async {
|
||||
// val basket = basketRepository.getUserOrder(userId)
|
||||
// _basketId.value = basket
|
||||
// }.await()
|
||||
// }
|
||||
suspend fun getUserBasketId(userId: Int) : Int{
|
||||
return basketRepository.getUserBasketId(userId)
|
||||
}
|
||||
|
||||
//
|
||||
// fun deleteSneakerFromBasket(basketId: Int, sneakerId: Int) = viewModelScope.launch {
|
||||
|
@ -37,6 +37,9 @@ import com.example.android_programming.model.Sneaker
|
||||
import com.example.android_programming.businessLogic.vmodel.AppViewModelProvider
|
||||
import com.example.android_programming.businessLogic.vmodel.BasketViewModel
|
||||
import com.google.gson.Gson
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
@Composable
|
||||
fun CardSneaker(item: Sneaker, navController: NavHostController, basketViewModel: BasketViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
|
||||
@ -97,9 +100,11 @@ fun CardSneaker(item: Sneaker, navController: NavHostController, basketViewModel
|
||||
if(user == null){
|
||||
navController.navigate("login")
|
||||
}else{
|
||||
// basketViewModel.getUserBasketId(user.userId!!)
|
||||
// val userBasketId = basketViewModel.basketId.value!!
|
||||
// basketViewModel.addToBasket(BasketSneakers(userBasketId, item.sneakerId!!, 1))
|
||||
runBlocking {
|
||||
launch(Dispatchers.Default) {
|
||||
basketViewModel.addToBasket(BasketSneakers(basketViewModel.getUserBasketId(user.userId!!), item.sneakerId!!, 1))
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
modifier = Modifier
|
||||
|
Loading…
Reference in New Issue
Block a user