@@ -65,6 +65,9 @@ class Coupons (
6565 coupons : List <Coupon >,
6666 private val project : Project
6767) : Iterable<Coupon>, LiveData<List<Coupon>>(coupons) {
68+ val source: LiveData <CouponSource > = MutableLiveData (CouponSource .Bundled )
69+ val isLoading: LiveData <Boolean > = MutableLiveData (false )
70+
6871 init {
6972 project.addOnUpdateListener {
7073 if (source.value == CouponSource .Bundled ) {
@@ -74,9 +77,6 @@ class Coupons (
7477 update()
7578 }
7679
77- val source: LiveData <CouponSource > = MutableLiveData (CouponSource .Bundled )
78- val isLoading: LiveData <Boolean > = MutableLiveData (false )
79-
8080 fun filter (type : CouponType ): List <Coupon > =
8181 value?.filter { it.type == type } ? : emptyList()
8282
@@ -90,7 +90,7 @@ class Coupons (
9090 get() = value?.size ? : 0
9191
9292 fun update () {
93- if (isLoading.value == true ) return
93+ if (isLoading.value == true ) return
9494 // make an implicit cast with an extension function
9595 fun <T > LiveData<T>.postValue (value : T ) {
9696 (this as ? MutableLiveData <T >)?.postValue(value)
@@ -105,8 +105,8 @@ class Coupons (
105105 }
106106
107107 val request = Request .Builder ()
108- .url(newsUrl)
109- .build()
108+ .url(newsUrl)
109+ .build()
110110 val couponCall = project.okHttpClient.newCall(request)
111111 couponCall.enqueue(object : Callback {
112112 override fun onFailure (call : Call , e : IOException ) {
@@ -118,7 +118,7 @@ class Coupons (
118118 override fun onResponse (call : Call , response : Response ) {
119119 if (response.isSuccessful) {
120120 val localizedResponse = GsonBuilder ().create()
121- .fromJson(response.body?.string(), CouponResponse ::class .java)
121+ .fromJson(response.body?.string(), CouponResponse ::class .java)
122122 postValue(localizedResponse.coupons.filter {
123123 it.image != null && it.validFrom != null && it.validUntil != null
124124 })
@@ -134,6 +134,6 @@ class Coupons (
134134
135135 @Keep
136136 private data class CouponResponse (
137- val coupons : List <Coupon >
137+ val coupons : List <Coupon >
138138 )
139139}
0 commit comments