From 99e191b85dc09d0ae7fb72a6a38c540cfc90a0c0 Mon Sep 17 00:00:00 2001 From: Fabian Bender Date: Wed, 19 Feb 2025 11:34:03 +0100 Subject: [PATCH 1/2] fix missing tracking of the checked in Project and the current shopping cart --- .../shoppingcart/ShoppingCartViewModel.kt | 53 +++++++++++++++---- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/ShoppingCartViewModel.kt b/ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/ShoppingCartViewModel.kt index ff1cf4508..5e7d22d94 100644 --- a/ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/ShoppingCartViewModel.kt +++ b/ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/ShoppingCartViewModel.kt @@ -1,7 +1,10 @@ package io.snabble.sdk.ui.cart.shoppingcart import androidx.lifecycle.ViewModel +import androidx.lifecycle.asFlow +import androidx.lifecycle.viewModelScope import io.snabble.sdk.PriceFormatter +import io.snabble.sdk.Project import io.snabble.sdk.Snabble import io.snabble.sdk.checkout.LineItemType import io.snabble.sdk.shoppingcart.ShoppingCart @@ -15,35 +18,65 @@ import io.snabble.sdk.ui.cart.shoppingcart.product.model.ProductItem import io.snabble.sdk.ui.telemetry.Telemetry import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.flow.update +import kotlinx.coroutines.launch class ShoppingCartViewModel : ViewModel() { private val _uiState = MutableStateFlow(UiState()) val uiState = _uiState.asStateFlow() - private lateinit var cachedCart: ShoppingCart + private var currentCart: ShoppingCart? = null private var priceFormatter: PriceFormatter? = null + private var project: Project? = null private val simpleShoppingCartListener = object : SimpleShoppingCartListener() { override fun onChanged(cart: ShoppingCart) { + currentCart = cart updateUiState(cart) } } // used to update the cart remote -> do not delete it fun updateCart() { - updateUiState(cachedCart) + currentCart?.let { updateUiState(it) } } init { - val project = Snabble.checkedInProject.value - val cart = Snabble.checkedInProject.value?.shoppingCart - cart?.addListener(simpleShoppingCartListener) + project = Snabble.checkedInProject.value project?.let { priceFormatter = PriceFormatter(it) - updateUiState(it.shoppingCart) + updateCart(it.shoppingCart) + updateOnShoppingCartChange(it) } + updateOnProjectChange() + } + + private fun updateOnShoppingCartChange(project: Project) { + viewModelScope.launch { + project.shoppingCartFlow.collectLatest { + updateCart(it) + } + } + } + + private fun updateOnProjectChange() { + viewModelScope.launch { + Snabble.checkedInProject.asFlow().filterNotNull().collectLatest { + priceFormatter = PriceFormatter(it) + updateCart(it.shoppingCart) + updateOnShoppingCartChange(it) + } + } + } + + private fun updateCart(shoppingCart: ShoppingCart) { + currentCart?.removeListener(simpleShoppingCartListener) + currentCart = shoppingCart + currentCart?.addListener(simpleShoppingCartListener) + updateUiState(shoppingCart) } fun onEvent(event: Event) { @@ -54,9 +87,9 @@ class ShoppingCartViewModel : ViewModel() { } private fun removeItemFromCart(item: ShoppingCart.Item?, onSuccess: (index: Int) -> Unit) { - val index = cachedCart.indexOf(item) + val index = currentCart?.indexOf(item) ?: return if (index != -1) { - cachedCart.remove(index) + currentCart?.remove(index) Telemetry.event(Telemetry.Event.DeletedFromCart, item?.product) onSuccess(index) } @@ -68,8 +101,6 @@ class ShoppingCartViewModel : ViewModel() { } private fun updateUiState(cart: ShoppingCart) { - cachedCart = cart - val cartItems: MutableList = mutableListOf() with(cart.filterNotNull()) { filter { it.type == ItemType.PRODUCT }.let { cartItems.addProducts(it) } @@ -85,7 +116,7 @@ class ShoppingCartViewModel : ViewModel() { cartItems.updatePrices() cartItems.sortCartDiscountsToBottom() - _uiState.update { it.copy(items = cartItems, totalCartPrice = cachedCart.totalPrice) } + _uiState.update { it.copy(items = cartItems, totalCartPrice = currentCart?.totalPrice) } } private fun MutableList.addDepositReturnItems(items: List) { From d64fad105463e6dbee4e703da755b66199c5f20e Mon Sep 17 00:00:00 2001 From: Fabian Bender Date: Wed, 19 Feb 2025 11:40:43 +0100 Subject: [PATCH 2/2] add missing CHANGELOG.md entry --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d3373a20..d1570c96b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ All notable changes to this project will be documented in this file. ### Changed ### Removed ### Fixed + +## [0.80.8] +### Fixed +* ui: Fix missing updates for the shopping cart (APPS-2163) + +## [0.80.7] +### Fixed * core: fix payment validation ## [0.80.6]