Skip to content

Commit fb2ab99

Browse files
committed
Avoid recreating Coupons on Project instances to keep observers working on updates
1 parent a12893d commit fb2ab99

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## [0.50.3]
5+
6+
### Fixed
7+
- Interrupted coupons observer
8+
49
## [0.50.2]
510

611
### Fixed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ allprojects {
3131
}
3232

3333
project.ext {
34-
sdkVersion='0.50.2'
34+
sdkVersion='0.50.3'
3535
versionCode=1
3636

3737
compileSdkVersion=31

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.snabble.sdk
22

3+
import android.os.Looper
34
import android.os.Parcelable
45
import androidx.annotation.Keep
56
import androidx.lifecycle.LiveData
@@ -69,9 +70,8 @@ enum class CouponSource {
6970
}
7071

7172
class Coupons (
72-
coupons: List<Coupon>,
7373
private val project: Project
74-
) : Iterable<Coupon>, LiveData<List<Coupon>>(coupons) {
74+
) : Iterable<Coupon>, LiveData<List<Coupon>>() {
7575
val source: LiveData<CouponSource> = MutableLiveData(CouponSource.Bundled)
7676
val isLoading: LiveData<Boolean> = MutableLiveData(false)
7777

@@ -97,16 +97,19 @@ class Coupons (
9797

9898
fun update() {
9999
if (isLoading.value == true) return
100-
// make an implicit cast with an extension function
101-
fun <T> LiveData<T>.postValue(value: T) {
102-
(this as? MutableLiveData<T>)?.postValue(value)
100+
fun <T> LiveData<T>.setAsap(value: T) {
101+
if (Looper.getMainLooper().thread.id == Thread.currentThread().id) {
102+
(this as MutableLiveData<T>).value = value
103+
} else {
104+
(this as MutableLiveData<T>).postValue(value)
105+
}
103106
}
104107
project.urls["coupons"]?.let { path ->
105-
isLoading.postValue(true)
108+
isLoading.setAsap(true)
106109
val newsUrl = Snabble.getInstance().absoluteUrl(path)
107110

108111
if (newsUrl == null) {
109-
postValue(emptyList())
112+
setProjectCoupons(emptyList())
110113
return
111114
}
112115

@@ -117,8 +120,8 @@ class Coupons (
117120
couponCall.enqueue(object : Callback {
118121
override fun onFailure(call: Call, e: IOException) {
119122
postValue(emptyList())
120-
isLoading.postValue(false)
121-
source.postValue(CouponSource.Online)
123+
isLoading.setAsap(false)
124+
source.setAsap(CouponSource.Online)
122125
}
123126

124127
override fun onResponse(call: Call, response: Response) {
@@ -128,14 +131,23 @@ class Coupons (
128131
postValue(localizedResponse.coupons.filter { coupon ->
129132
coupon.isValid
130133
})
131-
source.postValue(CouponSource.Online)
134+
source.setAsap(CouponSource.Online)
132135
}
133-
isLoading.postValue(false)
136+
isLoading.setAsap(false)
134137
}
135138
})
136139
}
137140
}
138141

142+
// Visibility for Project class. Used for setting the data asap if on main thread
143+
internal fun setProjectCoupons(coupons: List<Coupon>) {
144+
if (Looper.getMainLooper().thread.id == Thread.currentThread().id) {
145+
value = coupons
146+
} else {
147+
postValue(coupons)
148+
}
149+
}
150+
139151
@Keep
140152
private data class CouponResponse(
141153
val coupons: List<Coupon>

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public class Project {
7676
private PriceOverrideTemplate[] priceOverrideTemplates;
7777
private String[] searchableTemplates;
7878
private PriceFormatter priceFormatter;
79-
private Coupons coupons;
79+
private final Coupons coupons;
8080
private Map<String, String> texts;
8181

8282
private int maxOnlinePaymentLimit;
@@ -89,6 +89,7 @@ public class Project {
8989

9090
Project(JsonObject jsonObject) throws IllegalArgumentException {
9191
snabble = Snabble.getInstance();
92+
coupons = new Coupons(this);
9293

9394
parse(jsonObject);
9495

@@ -317,7 +318,7 @@ void parse(JsonObject jsonObject) {
317318
Logger.e("Could not parse coupons");
318319
}
319320

320-
this.coupons = new Coupons(couponList, this);
321+
this.coupons.setProjectCoupons$core_debug(couponList); // call internal setProjectCoupons
321322

322323
notifyUpdate();
323324
}

0 commit comments

Comments
 (0)