Question
I fetch product details for listing and individual views. How can I add various items with unique quantities to the cart?
package com.example.shopapp.ui
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import com.example.shopapp.databinding.FragmentProductInfoBinding
import com.example.shopapp.viewmodel.ShopViewModel
class ProductInfoFragment : Fragment() {
private lateinit var binding: FragmentProductInfoBinding
private val shopViewModel: ShopViewModel by activityViewModels()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentProductInfoBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val item = arguments?.getParcelable("item")
binding.priceLabel.text = "$" + (item?.cost ?: "0")
binding.addQtdButton.setOnClickListener {
shopViewModel.increaseQuantity()
binding.qtdText.text = shopViewModel.currentQuantity.toString()
}
binding.addItemButton.setOnClickListener {
Toast.makeText(context, "Item added to cart", Toast.LENGTH_SHORT).show()
}
}
}
package com.example.shopapp.viewmodel
import androidx.lifecycle.ViewModel
class ShopViewModel : ViewModel() {
var currentQuantity: Int = 0
fun increaseQuantity() {
currentQuantity++
}
}
Hey everyone, I’ve been tinkering with similar scenarios and I’m curious how others are handling multiproduct carts in their apps. One approach I’ve been considering is rethinking the way our ViewModel handles items. Right now, we store just a simple quantity counter, but what if we introduced a data class, say CartItem, that wraps the details of each product along with its quantity? By maintaining a list or even a map of these objects, you could easily check if a product already exists in the cart and update its quantity, or add it as a fresh item if it’s missing. This might also let you handle things like updating prices or even checking stock levels more elegantly.
I wonder if anyone has dealt with similar challenges. Have you tried implementing a reactive update mechanism where changes in the cart are reflected throughout your UI in real time? Or maybe you even store cart data in a local database, like Room, to preserve state across app sessions? I’m really interested in what strategies others have found effective in similar projects. How do you ensure that your code remains maintainable as the cart operations become more complex? Let’s chat about it!
hey, i also struggled with this. i ended up creatng a list of cart items with each having its own quantity counter that updates via liveData. it may be a bit more complex but works well for reflecting real time changes. good luck!
I experimented by implementing a dedicated CartManager class which handles the logic of adding and updating products within a shopping cart. Rather than relying only on a single quantity variable in the ViewModel, I set up a data model to represent each unique product along with its respective quantity. This approach not only keeps the logic segregated from the UI layer but also allows for easier maintenance and future scalability. Using Kotlin flows for state updates, the UI remains synchronized with the cart’s content, ensuring a smooth user experience even when multiple items are involved.
Hey folks, I’ve been exploring different approaches to enhance the cart functionality in my Android app too. I ended up moving away from having just a single ViewModel with a simple counter, and instead built a small repository layer to handle cart operations in a more reactive way. I created a data model that not only stores the product info and quantity, but also handles events like price changes and stock updates. Using Kotlin’s StateFlows, the UI is instantly updated whenever something changes in the cart, which makes it feel much smoother. I wonder, has anyone tried combining this with local persistence solutions like Room and then syncing with a backend? I’m especially curious about how you’d ensure smooth state transitions and avoid conflicts when multiple items are updated simultaneously. Looking forward to hearing your thoughts!
hey, i tried a bit diff approach: using a mutable list inside the viewmodel to handle each cart entry individually. it simplified state management a bit, though i ranning into occasional sync issues. curious if any of u have tried something similar?