Update the item count in column Android jetpack compose

84 Views Asked by At

Product List Composable :

@Composable
fun DisplayProducts(productResult: ApiResult<GetProduct>) {
    when (productResult) {
        is ApiResult.Loading -> {
            // Loading state code for products
        }

        is ApiResult.Error -> {
            // Handle product loading error
        }

        is ApiResult.Success -> {
            val productList = productResult.data?.Product
                Column(
                modifier = Modifier
                    .fillMaxSize()
                    .verticalScroll(rememberScrollState()) // Add verticalScroll
            ) {
                productList?.forEachIndexed { index,product  ->
                    ProductItem(product, index)
                }
            }
        }
    }
}

Product Item Composable :

    @Composable
fun ProductItem(product: Product, index: Int) {
    
    val addProductViewModel = hiltViewModel<AddProductVM>()
    val addProductResult by addProductViewModel.addProductList.collectAsState()

    val productSelectedIndex = remember { mutableIntStateOf(0) }

    Card(
        modifier = Modifier
        shape = RoundedCornerShape(8.dp)
    ) {

            Column() {
                Row{
                    Box(
                        modifier = Modifier
                            .weight(1f)
                            .wrapContentHeight()
                            .border(1.dp, Color.Black, shape = RoundedCornerShape(5.dp)),
                        contentAlignment = Alignment.Center// Add border with corner radius
                    ) {
                        Text(text = product.variants[0].measurement +" "+ product.variants[0].measurement_unit_name,
                            fontSize = 15.sp,
                            modifier = Modifier.padding(12.dp,4.dp,12.dp,4.dp))
                    }
                    Spacer(modifier = Modifier.size(16.dp))
                    Box(
                        modifier = Modifier,
                        contentAlignment = Alignment.Center
                    ) {
                        if (product.variants[0].cart_count > 0) {
                            Row {
                                Text(text = "-",
                                    fontSize = 15.sp,
                                    modifier = Modifier.padding(8.dp))
                                Spacer(modifier = Modifier.size(16.dp))
                                Text(text = product.variants[0].cart_count.toString(),
                                    fontSize = 15.sp,
                                    modifier = Modifier.padding(8.dp))
                                Spacer(modifier = Modifier.size(16.dp))
                                Text(text = "+",
                                    fontSize = 15.sp,
                                    modifier = Modifier.padding(8.dp)
                                        .clickable {
                                            productSelectedIndex.intValue = index
                                            product.variants[0].cart_count=+1
                                            addProductViewModel.addProduct(product.variants[0].product_id.toString(),
                                                                           product.variants[0].cart_count.toString(),
                                                                           product.variants[0].id.toString())
                                            Log.e(
                                                "selectedProduct",
                                                "selected Product offset : ${productSelectedIndex.intValue}"
                                            )
                                        })
                                    }
                        } else {
                            Text(
                                text = "Add To Cart", // "Add To Cart" text
                                fontSize = 14.sp,
                                modifier = Modifier
                                    .background(Color.Blue,shape = RoundedCornerShape(5.dp) )
                                    .padding(12.dp,4.dp,12.dp,4.dp)
                                    .clickable {
                                        productSelectedIndex.intValue = index
                                        product.variants[0].cart_count=+1
                                        addProductViewModel.addProduct(product.variants[0].product_id.toString(),
                                            product.variants[0].cart_count.toString(),
                                            product.variants[0].id.toString())
                                        Log.e(
                                            "selectedProduct",
                                            "selected Product offset : $index"
                                        )
                                      }
                                 },
                            )
                        }
                    }
                }
            }
        }
    }
}

AddProductResult :

    @Composable
fun AddProductResult(addProductResult: ApiResult<AddProduct>) {

    when (addProductResult) {
        is ApiResult.Loading -> {
            
        }

        is ApiResult.Error -> {
            // Handle product loading error
        }

        is ApiResult.Success -> {
            val Result = addProductResult.data?.error
            if(Result == true) {
                Log.e("productapicall", "failure error: " + addProductResult.error)
            } else {
                Log.e("productapicall", "failure error: " + addProductResult.error)
            }
        }
    }
}

ViewModel :

    @HiltViewModel
class AddProductVM @Inject constructor(private val apiService: ApiService,
                                       private val defaultDispatcher: CoroutineDispatcher
): ViewModel() {

    private val _addProductList= MutableStateFlow<ApiResult<AddProduct>>(ApiResult.Loading())
    val addProductList= _addProductList.asStateFlow()

    fun addProduct(productId:String,qty:String,productVariantId:String){
        viewModelScope.launch {
            apiService.addProduct(productId,qty,productVariantId)
                .flowOn(defaultDispatcher)
                .catch {
                    _addProductList.value= ApiResult.Error(it.message ?: "Something went wrong")
                }
                .collect{
                    _addProductList.value=it
                }
        }
    }
}

When I click Add to Cart text, I make API call and get the result in AddProductResult.

if ( val Result = addProductResult.data?.error == false) , I need to update the text value by 1 also to hide the Add to Cart text and visible the Row in box and show the count value in text

How to get the API result and update the only selectable product count?

This is my ScreenShot This is my ScreenShot

0

There are 0 best solutions below