Skip to content

Commit cb57f61

Browse files
authored
Merge pull request #129 from icerockdev/cancelable
Release 0.10.1
2 parents 7c92e79 + b757290 commit cb57f61

File tree

39 files changed

+393
-223
lines changed

39 files changed

+393
-223
lines changed

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ This is a Kotlin Multiplatform library that provides architecture components of
5353
- kotlin 1.4.31
5454
- 0.9.2
5555
- 0.10.0
56+
- 0.10.1
5657

5758
## Installation
5859
root build.gradle
@@ -67,15 +68,15 @@ allprojects {
6768
project build.gradle
6869
```groovy
6970
dependencies {
70-
commonMainApi("dev.icerock.moko:mvvm-core:0.10.0") // only ViewModel, EventsDispatcher, Dispatchers.UI
71-
commonMainApi("dev.icerock.moko:mvvm-livedata:0.10.0") // api mvvm-core, LiveData and extensions
72-
androidMainApi("dev.icerock.moko:mvvm-livedata-material:0.10.0") // api mvvm-livedata, Material library android extensions
73-
androidMainApi("dev.icerock.moko:mvvm-livedata-glide:0.10.0") // api mvvm-livedata, Glide library android extensions
74-
androidMainApi("dev.icerock.moko:mvvm-livedata-swiperefresh:0.10.0") // api mvvm-livedata, SwipeRefreshLayout library android extensions
75-
commonMainApi("dev.icerock.moko:mvvm-state:0.10.0") // api mvvm-livedata, ResourceState class and extensions
76-
androidMainApi("dev.icerock.moko:mvvm-databinding:0.10.0") // api mvvm-livedata, DataBinding support for Android
77-
androidMainApi("dev.icerock.moko:mvvm-viewbinding:0.10.0") // api mvvm-livedata, ViewBinding support for Android
78-
commonTestImplementation("dev.icerock.moko:mvvm-test:0.10.0") // test utilities
71+
commonMainApi("dev.icerock.moko:mvvm-core:0.10.1") // only ViewModel, EventsDispatcher, Dispatchers.UI
72+
commonMainApi("dev.icerock.moko:mvvm-livedata:0.10.1") // api mvvm-core, LiveData and extensions
73+
androidMainApi("dev.icerock.moko:mvvm-livedata-material:0.10.1") // api mvvm-livedata, Material library android extensions
74+
androidMainApi("dev.icerock.moko:mvvm-livedata-glide:0.10.1") // api mvvm-livedata, Glide library android extensions
75+
androidMainApi("dev.icerock.moko:mvvm-livedata-swiperefresh:0.10.1") // api mvvm-livedata, SwipeRefreshLayout library android extensions
76+
commonMainApi("dev.icerock.moko:mvvm-state:0.10.1") // api mvvm-livedata, ResourceState class and extensions
77+
androidMainApi("dev.icerock.moko:mvvm-databinding:0.10.1") // api mvvm-livedata, DataBinding support for Android
78+
androidMainApi("dev.icerock.moko:mvvm-viewbinding:0.10.1") // api mvvm-livedata, ViewBinding support for Android
79+
commonTestImplementation("dev.icerock.moko:mvvm-test:0.10.1") // test utilities
7980
}
8081
```
8182

@@ -90,15 +91,15 @@ kotlin {
9091
else -> throw IllegalArgumentException()
9192
}
9293
binaries.withType(org.jetbrains.kotlin.gradle.plugin.mpp.Framework::class.java).all {
93-
export("dev.icerock.moko:mvvm-$arch:0.10.0")
94+
export("dev.icerock.moko:mvvm-$arch:0.10.1")
9495
}
9596
}
9697
}
9798
```
9899

99100
On iOS, in addition to the Kotlin library add in Podfile
100101
```ruby
101-
pod 'MultiPlatformLibraryMvvm', :git => 'https://github.com/icerockdev/moko-mvvm.git', :tag => 'release/0.10.0'
102+
pod 'MultiPlatformLibraryMvvm', :git => 'https://github.com/icerockdev/moko-mvvm.git', :tag => 'release/0.10.1'
102103
```
103104
**`MultiPlatformLibraryMvvm` CocoaPod requires that the framework compiled from Kotlin be named
104105
`MultiPlatformLibrary` and be connected as a CocoaPod `MultiPlatformLibrary`.

buildSrc/src/main/kotlin/Deps.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ object Deps {
1414
private const val coroutinesVersion = "1.4.2"
1515
private const val mokoResourcesVersion = "0.15.1"
1616
private const val mokoTestVersion = "0.2.1"
17-
const val mokoMvvmVersion = "0.10.0"
17+
const val mokoMvvmVersion = "0.10.1"
1818

1919
object Android {
2020
const val compileSdk = 29

mvvm-livedata-glide/api/mvvm-livedata-glide.api

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ public final class dev/icerock/moko/mvvm/livedata/glide/BuildConfig {
66
}
77

88
public final class dev/icerock/moko/mvvm/livedata/glide/GlideImageViewBindingsKt {
9-
public static final fun bindToImageViewSrc (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/ImageView;Landroid/graphics/drawable/Drawable;Landroid/graphics/drawable/Drawable;)V
10-
public static final fun bindToImageViewSrc (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/ImageView;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)V
11-
public static synthetic fun bindToImageViewSrc$default (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/ImageView;Landroid/graphics/drawable/Drawable;Landroid/graphics/drawable/Drawable;ILjava/lang/Object;)V
12-
public static synthetic fun bindToImageViewSrc$default (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/ImageView;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
9+
public static final fun bindToImageViewSrc (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/ImageView;Landroid/graphics/drawable/Drawable;Landroid/graphics/drawable/Drawable;)Ldev/icerock/moko/mvvm/livedata/Closeable;
10+
public static final fun bindToImageViewSrc (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/ImageView;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ldev/icerock/moko/mvvm/livedata/Closeable;
11+
public static synthetic fun bindToImageViewSrc$default (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/ImageView;Landroid/graphics/drawable/Drawable;Landroid/graphics/drawable/Drawable;ILjava/lang/Object;)Ldev/icerock/moko/mvvm/livedata/Closeable;
12+
public static synthetic fun bindToImageViewSrc$default (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/ImageView;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ldev/icerock/moko/mvvm/livedata/Closeable;
1313
}
1414

mvvm-livedata-glide/src/main/kotlin/dev/icerock/moko/mvvm/livedata/glide/GlideImageViewBindings.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.lifecycle.LifecycleOwner
1010
import com.bumptech.glide.Glide
1111
import com.bumptech.glide.RequestBuilder
1212
import com.bumptech.glide.RequestManager
13+
import dev.icerock.moko.mvvm.livedata.Closeable
1314
import dev.icerock.moko.mvvm.livedata.LiveData
1415
import dev.icerock.moko.mvvm.utils.bindNotNull
1516

@@ -18,8 +19,8 @@ fun LiveData<String>.bindToImageViewSrc(
1819
imageView: ImageView,
1920
requestManager: (RequestManager.() -> Unit)? = null,
2021
requestBuilder: (RequestBuilder<Drawable>.() -> Unit)? = null
21-
) {
22-
bindNotNull(lifecycleOwner) { url ->
22+
): Closeable {
23+
return bindNotNull(lifecycleOwner) { url ->
2324
Glide.with(imageView)
2425
.also { requestManager?.invoke(it) }
2526
.load(url)
@@ -33,8 +34,8 @@ fun LiveData<String>.bindToImageViewSrc(
3334
imageView: ImageView,
3435
loadingPlaceholder: Drawable? = null,
3536
errorPlaceholder: Drawable? = null
36-
) {
37-
bindToImageViewSrc(
37+
): Closeable {
38+
return bindToImageViewSrc(
3839
lifecycleOwner = lifecycleOwner,
3940
imageView = imageView,
4041
requestBuilder = {

mvvm-livedata-material/api/mvvm-livedata-material.api

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public final class dev/icerock/moko/mvvm/livedata/material/BuildConfig {
66
}
77

88
public final class dev/icerock/moko/mvvm/livedata/material/TextInputLayoutBindingsKt {
9-
public static final fun bindToTextInputLayoutErrorString (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Lcom/google/android/material/textfield/TextInputLayout;)V
10-
public static final fun bindToTextInputLayoutErrorStringDesc (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Lcom/google/android/material/textfield/TextInputLayout;)V
9+
public static final fun bindToTextInputLayoutErrorString (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Lcom/google/android/material/textfield/TextInputLayout;)Ldev/icerock/moko/mvvm/livedata/Closeable;
10+
public static final fun bindToTextInputLayoutErrorStringDesc (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Lcom/google/android/material/textfield/TextInputLayout;)Ldev/icerock/moko/mvvm/livedata/Closeable;
1111
}
1212

mvvm-livedata-material/src/main/kotlin/dev/icerock/moko/mvvm/livedata/material/TextInputLayoutBindings.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package dev.icerock.moko.mvvm.livedata.material
66

77
import androidx.lifecycle.LifecycleOwner
88
import com.google.android.material.textfield.TextInputLayout
9+
import dev.icerock.moko.mvvm.livedata.Closeable
910
import dev.icerock.moko.mvvm.livedata.LiveData
1011
import dev.icerock.moko.mvvm.utils.bindNotNull
1112
import dev.icerock.moko.resources.desc.StringDesc
@@ -14,15 +15,15 @@ import dev.icerock.moko.resources.desc.StringDesc
1415
fun LiveData<String>.bindToTextInputLayoutError(
1516
lifecycleOwner: LifecycleOwner,
1617
textInputLayout: TextInputLayout
17-
) {
18-
bindNotNull(lifecycleOwner) { textInputLayout.error = it }
18+
): Closeable {
19+
return bindNotNull(lifecycleOwner) { textInputLayout.error = it }
1920
}
2021

2122
@JvmName("bindToTextInputLayoutErrorStringDesc")
2223
fun LiveData<StringDesc>.bindToTextInputLayoutError(
2324
lifecycleOwner: LifecycleOwner,
2425
textInputLayout: TextInputLayout
25-
) {
26+
): Closeable {
2627
val context = textInputLayout.context
27-
bindNotNull(lifecycleOwner) { textInputLayout.error = it.toString(context) }
28+
return bindNotNull(lifecycleOwner) { textInputLayout.error = it.toString(context) }
2829
}

mvvm-livedata-swiperefresh/api/mvvm-livedata-swiperefresh.api

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public final class dev/icerock/moko/mvvm/livedata/swiperefresh/BuildConfig {
66
}
77

88
public final class dev/icerock/moko/mvvm/livedata/swiperefresh/SwipeRefreshBindingsKt {
9-
public static final fun bindToSwipeRefreshLayoutRefreshing (Ldev/icerock/moko/mvvm/livedata/MutableLiveData;Landroidx/lifecycle/LifecycleOwner;Landroidx/swiperefreshlayout/widget/SwipeRefreshLayout;)V
9+
public static final fun bindToSwipeRefreshLayoutRefreshing (Ldev/icerock/moko/mvvm/livedata/MutableLiveData;Landroidx/lifecycle/LifecycleOwner;Landroidx/swiperefreshlayout/widget/SwipeRefreshLayout;)Ldev/icerock/moko/mvvm/livedata/Closeable;
1010
public static final fun setRefreshAction (Landroidx/swiperefreshlayout/widget/SwipeRefreshLayout;Lkotlin/jvm/functions/Function1;)V
1111
public static final fun setRefreshAction (Landroidx/swiperefreshlayout/widget/SwipeRefreshLayout;Lkotlinx/coroutines/CoroutineScope;Lkotlin/jvm/functions/Function1;)V
1212
}

mvvm-livedata-swiperefresh/src/main/kotlin/dev/icerock/moko/mvvm/livedata/swiperefresh/SwipeRefreshBindings.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package dev.icerock.moko.mvvm.livedata.swiperefresh
66

77
import androidx.lifecycle.LifecycleOwner
88
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
9+
import dev.icerock.moko.mvvm.livedata.Closeable
910
import dev.icerock.moko.mvvm.livedata.MutableLiveData
1011
import dev.icerock.moko.mvvm.utils.bindNotNull
1112
import kotlinx.coroutines.CoroutineScope
@@ -14,10 +15,16 @@ import kotlinx.coroutines.launch
1415
fun MutableLiveData<Boolean>.bindToSwipeRefreshLayoutRefreshing(
1516
lifecycleOwner: LifecycleOwner,
1617
swipeRefreshLayout: SwipeRefreshLayout
17-
) {
18-
bindNotNull(lifecycleOwner) { swipeRefreshLayout.isRefreshing = it }
18+
): Closeable {
19+
val readCloseable = bindNotNull(lifecycleOwner) { swipeRefreshLayout.isRefreshing = it }
1920

2021
swipeRefreshLayout.setOnRefreshListener { value = true }
22+
23+
val writeCloseable = Closeable {
24+
swipeRefreshLayout.setOnRefreshListener(null)
25+
}
26+
27+
return readCloseable + writeCloseable
2128
}
2229

2330
fun SwipeRefreshLayout.setRefreshAction(

mvvm-livedata/api/mvvm-livedata.api

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,30 @@ public final class dev/icerock/moko/mvvm/livedata/BuildConfig {
66
}
77

88
public final class dev/icerock/moko/mvvm/livedata/CheckBoxBindingsKt {
9-
public static final fun bindToCheckBoxChecked (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/CheckBox;)V
10-
public static final fun bindTwoWayToCheckBoxChecked (Ldev/icerock/moko/mvvm/livedata/MutableLiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/CheckBox;)V
9+
public static final fun bindToCheckBoxChecked (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/CheckBox;)Ldev/icerock/moko/mvvm/livedata/Closeable;
10+
public static final fun bindTwoWayToCheckBoxChecked (Ldev/icerock/moko/mvvm/livedata/MutableLiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/CheckBox;)Ldev/icerock/moko/mvvm/livedata/Closeable;
11+
}
12+
13+
public abstract interface class dev/icerock/moko/mvvm/livedata/Closeable {
14+
public static final field Companion Ldev/icerock/moko/mvvm/livedata/Closeable$Companion;
15+
public abstract fun close ()V
16+
public abstract fun plus (Ldev/icerock/moko/mvvm/livedata/Closeable;)Ldev/icerock/moko/mvvm/livedata/Closeable;
17+
}
18+
19+
public final class dev/icerock/moko/mvvm/livedata/Closeable$Companion {
20+
public final fun invoke (Lkotlin/jvm/functions/Function0;)Ldev/icerock/moko/mvvm/livedata/Closeable;
21+
}
22+
23+
public final class dev/icerock/moko/mvvm/livedata/Closeable$DefaultImpls {
24+
public static fun plus (Ldev/icerock/moko/mvvm/livedata/Closeable;Ldev/icerock/moko/mvvm/livedata/Closeable;)Ldev/icerock/moko/mvvm/livedata/Closeable;
1125
}
1226

1327
public final class dev/icerock/moko/mvvm/livedata/DebounceLiveDataKt {
1428
public static final fun debounce (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlinx/coroutines/CoroutineScope;J)Ldev/icerock/moko/mvvm/livedata/LiveData;
1529
}
1630

1731
public final class dev/icerock/moko/mvvm/livedata/EditTextBindingsKt {
18-
public static final fun bindTwoWayToEditTextText (Ldev/icerock/moko/mvvm/livedata/MutableLiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/EditText;)V
32+
public static final fun bindTwoWayToEditTextText (Ldev/icerock/moko/mvvm/livedata/MutableLiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/EditText;)Ldev/icerock/moko/mvvm/livedata/Closeable;
1933
}
2034

2135
public class dev/icerock/moko/mvvm/livedata/LiveData {
@@ -49,6 +63,10 @@ public final class dev/icerock/moko/mvvm/livedata/LiveDataExtKt {
4963
public static final fun revert (Ldev/icerock/moko/mvvm/livedata/LiveData;)Ldev/icerock/moko/mvvm/livedata/LiveData;
5064
}
5165

66+
public final class dev/icerock/moko/mvvm/livedata/LiveDataKt {
67+
public static final fun addCloseableObserver (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function1;)Ldev/icerock/moko/mvvm/livedata/Closeable;
68+
}
69+
5270
public final class dev/icerock/moko/mvvm/livedata/LiveDataTransformsKt {
5371
public static final fun flatMap (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function1;)Ldev/icerock/moko/mvvm/livedata/LiveData;
5472
public static final fun flatMapBuffered (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function2;)Ldev/icerock/moko/mvvm/livedata/LiveData;
@@ -86,8 +104,8 @@ public final class dev/icerock/moko/mvvm/livedata/MutableLiveDataExtKt {
86104
}
87105

88106
public final class dev/icerock/moko/mvvm/livedata/TextViewBindingsKt {
89-
public static final fun bindToTextViewTextString (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/TextView;)V
90-
public static final fun bindToTextViewTextStringDesc (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/TextView;)V
107+
public static final fun bindToTextViewTextString (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/TextView;)Ldev/icerock/moko/mvvm/livedata/Closeable;
108+
public static final fun bindToTextViewTextStringDesc (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/TextView;)Ldev/icerock/moko/mvvm/livedata/Closeable;
91109
}
92110

93111
public final class dev/icerock/moko/mvvm/livedata/ThrowableLiveDataExtKt {
@@ -96,13 +114,13 @@ public final class dev/icerock/moko/mvvm/livedata/ThrowableLiveDataExtKt {
96114
}
97115

98116
public final class dev/icerock/moko/mvvm/livedata/ViewBindingsKt {
99-
public static final fun bindToViewEnabled (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/view/View;)V
100-
public static final fun bindToViewVisibleOrGone (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/view/View;)V
101-
public static final fun bindToViewVisibleOrInvisible (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/view/View;)V
117+
public static final fun bindToViewEnabled (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/view/View;)Ldev/icerock/moko/mvvm/livedata/Closeable;
118+
public static final fun bindToViewVisibleOrGone (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/view/View;)Ldev/icerock/moko/mvvm/livedata/Closeable;
119+
public static final fun bindToViewVisibleOrInvisible (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/view/View;)Ldev/icerock/moko/mvvm/livedata/Closeable;
102120
}
103121

104122
public final class dev/icerock/moko/mvvm/utils/LiveDataExtKt {
105-
public static final fun bind (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Lkotlin/jvm/functions/Function1;)V
106-
public static final fun bindNotNull (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Lkotlin/jvm/functions/Function1;)V
123+
public static final fun bind (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Lkotlin/jvm/functions/Function1;)Ldev/icerock/moko/mvvm/livedata/Closeable;
124+
public static final fun bindNotNull (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Lkotlin/jvm/functions/Function1;)Ldev/icerock/moko/mvvm/livedata/Closeable;
107125
}
108126

mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/livedata/CheckBoxBindings.kt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,37 @@
55
package dev.icerock.moko.mvvm.livedata
66

77
import android.widget.CheckBox
8+
import android.widget.CompoundButton
89
import androidx.lifecycle.LifecycleOwner
910
import dev.icerock.moko.mvvm.utils.bindNotNull
1011

11-
fun LiveData<Boolean>.bindToCheckBoxChecked(lifecycleOwner: LifecycleOwner, checkBox: CheckBox) {
12-
bindNotNull(lifecycleOwner) { checkBox.isChecked = it }
12+
fun LiveData<Boolean>.bindToCheckBoxChecked(
13+
lifecycleOwner: LifecycleOwner,
14+
checkBox: CheckBox
15+
): Closeable {
16+
return bindNotNull(lifecycleOwner) { checkBox.isChecked = it }
1317
}
1418

1519
fun MutableLiveData<Boolean>.bindTwoWayToCheckBoxChecked(
1620
lifecycleOwner: LifecycleOwner,
1721
checkBox: CheckBox
18-
) {
19-
bindNotNull(lifecycleOwner) { value ->
22+
): Closeable {
23+
val readCloseable = bindNotNull(lifecycleOwner) { value ->
2024
if (checkBox.isChecked == value) return@bindNotNull
2125

2226
checkBox.isChecked = value
2327
}
2428

25-
checkBox.setOnCheckedChangeListener { _, isChecked ->
26-
if (value == isChecked) return@setOnCheckedChangeListener
29+
val checkListener = CompoundButton.OnCheckedChangeListener { _, isChecked ->
30+
if (value == isChecked) return@OnCheckedChangeListener
2731

2832
value = isChecked
2933
}
34+
checkBox.setOnCheckedChangeListener(checkListener)
35+
36+
val writeCloseable = Closeable {
37+
checkBox.setOnCheckedChangeListener(null)
38+
}
39+
40+
return readCloseable + writeCloseable
3041
}

0 commit comments

Comments
 (0)