Skip to content

Commit e1eef54

Browse files
authored
Fix missing updates for the shopping cart (Apps-2163) (#244)
1 parent 2da11ab commit e1eef54

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ All notable changes to this project will be documented in this file.
66
### Changed
77
### Removed
88
### Fixed
9+
10+
## [0.80.8]
11+
### Fixed
12+
* ui: Fix missing updates for the shopping cart (APPS-2163)
13+
14+
## [0.80.7]
15+
### Fixed
916
* core: fix payment validation
1017

1118
## [0.80.6]

ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/ShoppingCartViewModel.kt

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package io.snabble.sdk.ui.cart.shoppingcart
22

33
import androidx.lifecycle.ViewModel
4+
import androidx.lifecycle.asFlow
5+
import androidx.lifecycle.viewModelScope
46
import io.snabble.sdk.PriceFormatter
7+
import io.snabble.sdk.Project
58
import io.snabble.sdk.Snabble
69
import io.snabble.sdk.checkout.LineItemType
710
import io.snabble.sdk.shoppingcart.ShoppingCart
@@ -15,35 +18,65 @@ import io.snabble.sdk.ui.cart.shoppingcart.product.model.ProductItem
1518
import io.snabble.sdk.ui.telemetry.Telemetry
1619
import kotlinx.coroutines.flow.MutableStateFlow
1720
import kotlinx.coroutines.flow.asStateFlow
21+
import kotlinx.coroutines.flow.collectLatest
22+
import kotlinx.coroutines.flow.filterNotNull
1823
import kotlinx.coroutines.flow.update
24+
import kotlinx.coroutines.launch
1925

2026
class 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

Comments
 (0)