11package io.snabble.sdk.ui.cart.shoppingcart
22
33import androidx.lifecycle.ViewModel
4+ import androidx.lifecycle.asFlow
5+ import androidx.lifecycle.viewModelScope
46import io.snabble.sdk.PriceFormatter
7+ import io.snabble.sdk.Project
58import io.snabble.sdk.Snabble
69import io.snabble.sdk.checkout.LineItemType
710import io.snabble.sdk.shoppingcart.ShoppingCart
@@ -15,35 +18,65 @@ import io.snabble.sdk.ui.cart.shoppingcart.product.model.ProductItem
1518import io.snabble.sdk.ui.telemetry.Telemetry
1619import kotlinx.coroutines.flow.MutableStateFlow
1720import kotlinx.coroutines.flow.asStateFlow
21+ import kotlinx.coroutines.flow.collectLatest
22+ import kotlinx.coroutines.flow.filterNotNull
1823import kotlinx.coroutines.flow.update
24+ import kotlinx.coroutines.launch
1925
2026class ShoppingCartViewModel : ViewModel () {
2127
2228 private val _uiState = MutableStateFlow (UiState ())
2329 val uiState = _uiState .asStateFlow()
2430
25- private lateinit var cachedCart : ShoppingCart
31+ private var currentCart : ShoppingCart ? = null
2632 private var priceFormatter: PriceFormatter ? = null
33+ private var project: Project ? = null
2734
2835 private val simpleShoppingCartListener = object : SimpleShoppingCartListener () {
2936 override fun onChanged (cart : ShoppingCart ) {
37+ currentCart = cart
3038 updateUiState(cart)
3139 }
3240 }
3341
3442 // used to update the cart remote -> do not delete it
3543 fun updateCart () {
36- updateUiState(cachedCart)
44+ currentCart?. let { updateUiState(it) }
3745 }
3846
3947 init {
40- val project = Snabble .checkedInProject.value
41- val cart = Snabble .checkedInProject.value?.shoppingCart
42- cart?.addListener(simpleShoppingCartListener)
48+ project = Snabble .checkedInProject.value
4349 project?.let {
4450 priceFormatter = PriceFormatter (it)
45- updateUiState(it.shoppingCart)
51+ updateCart(it.shoppingCart)
52+ updateOnShoppingCartChange(it)
4653 }
54+ updateOnProjectChange()
55+ }
56+
57+ private fun updateOnShoppingCartChange (project : Project ) {
58+ viewModelScope.launch {
59+ project.shoppingCartFlow.collectLatest {
60+ updateCart(it)
61+ }
62+ }
63+ }
64+
65+ private fun updateOnProjectChange () {
66+ viewModelScope.launch {
67+ Snabble .checkedInProject.asFlow().filterNotNull().collectLatest {
68+ priceFormatter = PriceFormatter (it)
69+ updateCart(it.shoppingCart)
70+ updateOnShoppingCartChange(it)
71+ }
72+ }
73+ }
74+
75+ private fun updateCart (shoppingCart : ShoppingCart ) {
76+ currentCart?.removeListener(simpleShoppingCartListener)
77+ currentCart = shoppingCart
78+ currentCart?.addListener(simpleShoppingCartListener)
79+ updateUiState(shoppingCart)
4780 }
4881
4982 fun onEvent (event : Event ) {
@@ -54,9 +87,9 @@ class ShoppingCartViewModel : ViewModel() {
5487 }
5588
5689 private fun removeItemFromCart (item : ShoppingCart .Item ? , onSuccess : (index: Int ) -> Unit ) {
57- val index = cachedCart .indexOf(item)
90+ val index = currentCart? .indexOf(item) ? : return
5891 if (index != - 1 ) {
59- cachedCart .remove(index)
92+ currentCart? .remove(index)
6093 Telemetry .event(Telemetry .Event .DeletedFromCart , item?.product)
6194 onSuccess(index)
6295 }
@@ -68,8 +101,6 @@ class ShoppingCartViewModel : ViewModel() {
68101 }
69102
70103 private fun updateUiState (cart : ShoppingCart ) {
71- cachedCart = cart
72-
73104 val cartItems: MutableList <CartItem > = mutableListOf ()
74105 with (cart.filterNotNull()) {
75106 filter { it.type == ItemType .PRODUCT }.let { cartItems.addProducts(it) }
@@ -85,7 +116,7 @@ class ShoppingCartViewModel : ViewModel() {
85116 cartItems.updatePrices()
86117 cartItems.sortCartDiscountsToBottom()
87118
88- _uiState .update { it.copy(items = cartItems, totalCartPrice = cachedCart .totalPrice) }
119+ _uiState .update { it.copy(items = cartItems, totalCartPrice = currentCart? .totalPrice) }
89120 }
90121
91122 private fun MutableList<CartItem>.addDepositReturnItems (items : List <ShoppingCart .Item >) {
0 commit comments