Skip to content

Commit e0567c1

Browse files
authored
Add stateflow for shopping cart (#137)
1 parent bb708f0 commit e0567c1

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

core/src/main/java/io/snabble/sdk/Project.kt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import io.snabble.sdk.utils.getIntOpt
2121
import io.snabble.sdk.utils.getString
2222
import io.snabble.sdk.utils.getStringListOpt
2323
import io.snabble.sdk.utils.getStringOpt
24+
import kotlinx.coroutines.flow.MutableStateFlow
25+
import kotlinx.coroutines.flow.StateFlow
26+
import kotlinx.coroutines.flow.asStateFlow
2427
import okhttp3.OkHttpClient
2528
import okhttp3.Request
2629
import org.apache.commons.lang3.LocaleUtils
@@ -290,11 +293,18 @@ class Project internal constructor(jsonObject: JsonObject) {
290293
private lateinit var shoppingCartStorage: ShoppingCartStorage
291294
private set
292295

296+
private val _shoppingCart = MutableStateFlow(ShoppingCart())
297+
298+
/**
299+
* Flow to observe the current users shopping cart
300+
*/
301+
val shoppingCartFlow: StateFlow<ShoppingCart> = _shoppingCart.asStateFlow()
302+
293303
/**
294304
* The users shopping cart
295305
*/
296-
lateinit var shoppingCart: ShoppingCart
297-
private set
306+
val shoppingCart: ShoppingCart
307+
get() = shoppingCartFlow.value
298308

299309
/**
300310
* Provides a list of active coupons
@@ -496,15 +506,15 @@ class Project internal constructor(jsonObject: JsonObject) {
496506
.addInterceptor(AcceptedLanguageInterceptor())
497507
.build()
498508

499-
shoppingCart = ShoppingCart(this)
509+
_shoppingCart.tryEmit(ShoppingCart(this))
500510

501511
shoppingCartStorage = ShoppingCartStorage(this)
502512

503-
checkout = Checkout(this, shoppingCart)
513+
checkout = Checkout(this, shoppingCartFlow.value)
504514

505-
productDatabase = ProductDatabase(this, shoppingCart, "$id.sqlite3", Snabble.config.generateSearchIndex)
515+
productDatabase = ProductDatabase(this, shoppingCartFlow.value, "$id.sqlite3", Snabble.config.generateSearchIndex)
506516

507-
events = Events(this, shoppingCart)
517+
events = Events(this, shoppingCartFlow.value)
508518

509519
assets = Assets(this)
510520

ui/src/main/java/io/snabble/sdk/ui/cart/PaymentSelectionHelper.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,21 @@ private void setProject(Project project) {
147147
if (project != null) {
148148
PaymentSelectionHelper.this.project = project;
149149

150-
if (cart != null) {
151-
cart.removeListener(shoppingCartListener);
152-
}
153-
154-
cart = project.getShoppingCart();
155-
cart.addListener(shoppingCartListener);
156-
150+
updateShoppingCart();
157151
update();
158152
}
159153
}
160154

155+
public void updateShoppingCart() {
156+
if (cart != null) {
157+
cart.removeListener(shoppingCartListener);
158+
}
159+
160+
cart = project.getShoppingCart();
161+
cart.addListener(shoppingCartListener);
162+
update();
163+
}
164+
161165
private void update() {
162166
updateGooglePayIsReadyToPay();
163167

0 commit comments

Comments
 (0)