Skip to content

Commit 1a99148

Browse files
committed
Allow setting coupon source when view is not yet attached
1 parent 72bb545 commit 1a99148

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

ui/src/main/java/io/snabble/sdk/ui/coupon/CouponOverviewView.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ class CouponOverviewView @JvmOverloads constructor(
3838
private var keyLineRight: Int
3939
var isInEmptyState = true
4040
private set
41+
var couponSource: LiveData<List<CouponItem>> = CouponManager.withCurrentProject()
42+
set(value) {
43+
field = value
44+
if (isAttachedToWindow) {
45+
ensureAdapterExists().setCouponSource(couponSource)
46+
}
47+
}
4148

4249
init {
4350
keyLineLeft = padding.left
@@ -52,7 +59,9 @@ class CouponOverviewView @JvmOverloads constructor(
5259

5360
override fun onAttachedToWindow() {
5461
super.onAttachedToWindow()
55-
setCouponSource(CouponManager.withCurrentProject())
62+
recyclerView.adapter = ensureAdapterExists().apply {
63+
setCouponSource(couponSource)
64+
}
5665
}
5766

5867
fun interface EmptyStageChangeListener {
@@ -67,12 +76,6 @@ class CouponOverviewView @JvmOverloads constructor(
6776
ensureAdapterExists().emptyStateListener -= listener
6877
}
6978

70-
fun setCouponSource(coupons: LiveData<List<CouponItem>>) {
71-
recyclerView.adapter = ensureAdapterExists().apply {
72-
setCouponSource(coupons)
73-
}
74-
}
75-
7679
private fun ensureAdapterExists(): CouponsAdapter {
7780
if (adapter == null) {
7881
adapter = CouponsAdapter(requireNotNull(findViewTreeLifecycleOwner()), keyLineLeft, keyLineRight).apply {
@@ -177,7 +180,7 @@ class CouponOverviewView @JvmOverloads constructor(
177180
itemView.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
178181
return
179182
}
180-
183+
181184
item.coupon?.let { coupon ->
182185
cardView.setCardBackgroundColor(coupon.backgroundColor)
183186
background.loadImage(coupon.image?.bestResolutionUrl)

0 commit comments

Comments
 (0)