From be15f87e05e922c17d78d006c5b35520fc418697 Mon Sep 17 00:00:00 2001 From: Andrey Dorofeev Date: Fri, 30 Apr 2021 14:05:05 +0700 Subject: [PATCH 1/2] Added Cancelable implementation for livedata --- README.md | 23 ++++---- buildSrc/src/main/kotlin/Deps.kt | 2 +- .../api/mvvm-livedata-glide.api | 8 +-- .../livedata/glide/GlideImageViewBindings.kt | 9 +-- .../api/mvvm-livedata-material.api | 4 +- .../material/TextInputLayoutBindings.kt | 9 +-- .../api/mvvm-livedata-swiperefresh.api | 2 +- .../swiperefresh/SwipeRefreshBindings.kt | 11 +++- mvvm-livedata/api/mvvm-livedata.api | 59 ++++++++++++++----- .../moko/mvvm/livedata/CheckBoxBindings.kt | 23 ++++++-- .../moko/mvvm/livedata/EditTextBindings.kt | 10 +++- .../moko/mvvm/livedata/TextViewBindings.kt | 14 +++-- .../moko/mvvm/livedata/ViewBindings.kt | 21 +++++-- .../icerock/moko/mvvm/utils/LiveDataExt.kt | 18 ++++-- .../icerock/moko/mvvm/livedata/Closeable.kt | 26 ++++++++ .../icerock/moko/mvvm/livedata/LiveData.kt | 5 ++ .../moko/mvvm/binding/UIButtonBinding.kt | 26 ++++---- .../moko/mvvm/binding/UILabelBinding.kt | 9 +-- .../moko/mvvm/binding/UISwitchBinding.kt | 9 +-- .../moko/mvvm/binding/UITextFieldBinding.kt | 31 +++++----- .../moko/mvvm/binding/UITextViewBinding.kt | 29 +++++---- .../moko/mvvm/binding/UIViewBinding.kt | 5 +- .../moko/mvvm/livedata/UIButtonBindings.kt | 12 ++-- .../moko/mvvm/livedata/UIControlBindings.kt | 16 ++--- .../moko/mvvm/livedata/UILabelBindings.kt | 8 +-- .../moko/mvvm/livedata/UIResponderBindings.kt | 25 ++++---- .../moko/mvvm/livedata/UISwitchBindings.kt | 12 ++-- .../moko/mvvm/livedata/UITextFieldBindings.kt | 16 ++--- .../moko/mvvm/livedata/UITextViewBindings.kt | 28 +++++---- .../moko/mvvm/livedata/UIViewBindings.kt | 22 +++++-- .../icerock/moko/mvvm/utils/LiveDataExt.kt | 7 ++- .../mvvm/utils/NSNotificationCenterExt.kt | 9 ++- .../icerock/moko/mvvm/utils/UIControlExt.kt | 29 +++++---- .../swift/Core/UIButton+LiveData.swift | 28 +++++---- .../iosMain/swift/Core/UILabel+LiveData.swift | 14 +++-- .../swift/Core/UISwitch+LiveData.swift | 10 ++-- .../swift/Core/UITextField+LiveData.swift | 31 ++++++---- .../swift/Core/UITextView+LiveData.swift | 10 ++-- .../iosMain/swift/Core/UIView+LiveData.swift | 7 ++- 39 files changed, 409 insertions(+), 228 deletions(-) create mode 100644 mvvm-livedata/src/commonMain/kotlin/dev/icerock/moko/mvvm/livedata/Closeable.kt diff --git a/README.md b/README.md index 217d05db..f54431e8 100755 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ This is a Kotlin Multiplatform library that provides architecture components of - kotlin 1.4.31 - 0.9.2 - 0.10.0 + - 0.10.1 ## Installation root build.gradle @@ -67,15 +68,15 @@ allprojects { project build.gradle ```groovy dependencies { - commonMainApi("dev.icerock.moko:mvvm-core:0.10.0") // only ViewModel, EventsDispatcher, Dispatchers.UI - commonMainApi("dev.icerock.moko:mvvm-livedata:0.10.0") // api mvvm-core, LiveData and extensions - androidMainApi("dev.icerock.moko:mvvm-livedata-material:0.10.0") // api mvvm-livedata, Material library android extensions - androidMainApi("dev.icerock.moko:mvvm-livedata-glide:0.10.0") // api mvvm-livedata, Glide library android extensions - androidMainApi("dev.icerock.moko:mvvm-livedata-swiperefresh:0.10.0") // api mvvm-livedata, SwipeRefreshLayout library android extensions - commonMainApi("dev.icerock.moko:mvvm-state:0.10.0") // api mvvm-livedata, ResourceState class and extensions - androidMainApi("dev.icerock.moko:mvvm-databinding:0.10.0") // api mvvm-livedata, DataBinding support for Android - androidMainApi("dev.icerock.moko:mvvm-viewbinding:0.10.0") // api mvvm-livedata, ViewBinding support for Android - commonTestImplementation("dev.icerock.moko:mvvm-test:0.10.0") // test utilities + commonMainApi("dev.icerock.moko:mvvm-core:0.10.1") // only ViewModel, EventsDispatcher, Dispatchers.UI + commonMainApi("dev.icerock.moko:mvvm-livedata:0.10.1") // api mvvm-core, LiveData and extensions + androidMainApi("dev.icerock.moko:mvvm-livedata-material:0.10.1") // api mvvm-livedata, Material library android extensions + androidMainApi("dev.icerock.moko:mvvm-livedata-glide:0.10.1") // api mvvm-livedata, Glide library android extensions + androidMainApi("dev.icerock.moko:mvvm-livedata-swiperefresh:0.10.1") // api mvvm-livedata, SwipeRefreshLayout library android extensions + commonMainApi("dev.icerock.moko:mvvm-state:0.10.1") // api mvvm-livedata, ResourceState class and extensions + androidMainApi("dev.icerock.moko:mvvm-databinding:0.10.1") // api mvvm-livedata, DataBinding support for Android + androidMainApi("dev.icerock.moko:mvvm-viewbinding:0.10.1") // api mvvm-livedata, ViewBinding support for Android + commonTestImplementation("dev.icerock.moko:mvvm-test:0.10.1") // test utilities } ``` @@ -90,7 +91,7 @@ kotlin { else -> throw IllegalArgumentException() } binaries.withType(org.jetbrains.kotlin.gradle.plugin.mpp.Framework::class.java).all { - export("dev.icerock.moko:mvvm-$arch:0.10.0") + export("dev.icerock.moko:mvvm-$arch:0.10.1") } } } @@ -98,7 +99,7 @@ kotlin { On iOS, in addition to the Kotlin library add in Podfile ```ruby -pod 'MultiPlatformLibraryMvvm', :git => 'https://github.com/icerockdev/moko-mvvm.git', :tag => 'release/0.10.0' +pod 'MultiPlatformLibraryMvvm', :git => 'https://github.com/icerockdev/moko-mvvm.git', :tag => 'release/0.10.1' ``` **`MultiPlatformLibraryMvvm` CocoaPod requires that the framework compiled from Kotlin be named `MultiPlatformLibrary` and be connected as a CocoaPod `MultiPlatformLibrary`. diff --git a/buildSrc/src/main/kotlin/Deps.kt b/buildSrc/src/main/kotlin/Deps.kt index f2189080..1e0b415a 100755 --- a/buildSrc/src/main/kotlin/Deps.kt +++ b/buildSrc/src/main/kotlin/Deps.kt @@ -14,7 +14,7 @@ object Deps { private const val coroutinesVersion = "1.4.2" private const val mokoResourcesVersion = "0.15.1" private const val mokoTestVersion = "0.2.1" - const val mokoMvvmVersion = "0.10.0" + const val mokoMvvmVersion = "0.10.1" object Android { const val compileSdk = 29 diff --git a/mvvm-livedata-glide/api/mvvm-livedata-glide.api b/mvvm-livedata-glide/api/mvvm-livedata-glide.api index 927befa1..e1f170f8 100644 --- a/mvvm-livedata-glide/api/mvvm-livedata-glide.api +++ b/mvvm-livedata-glide/api/mvvm-livedata-glide.api @@ -6,9 +6,9 @@ public final class dev/icerock/moko/mvvm/livedata/glide/BuildConfig { } public final class dev/icerock/moko/mvvm/livedata/glide/GlideImageViewBindingsKt { - 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 - 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 - 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 - 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 + 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; + 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; + 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; + 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; } diff --git a/mvvm-livedata-glide/src/main/kotlin/dev/icerock/moko/mvvm/livedata/glide/GlideImageViewBindings.kt b/mvvm-livedata-glide/src/main/kotlin/dev/icerock/moko/mvvm/livedata/glide/GlideImageViewBindings.kt index 1f5a4e8b..0f1e6d62 100644 --- a/mvvm-livedata-glide/src/main/kotlin/dev/icerock/moko/mvvm/livedata/glide/GlideImageViewBindings.kt +++ b/mvvm-livedata-glide/src/main/kotlin/dev/icerock/moko/mvvm/livedata/glide/GlideImageViewBindings.kt @@ -10,6 +10,7 @@ import androidx.lifecycle.LifecycleOwner import com.bumptech.glide.Glide import com.bumptech.glide.RequestBuilder import com.bumptech.glide.RequestManager +import dev.icerock.moko.mvvm.livedata.Closeable import dev.icerock.moko.mvvm.livedata.LiveData import dev.icerock.moko.mvvm.utils.bindNotNull @@ -18,8 +19,8 @@ fun LiveData.bindToImageViewSrc( imageView: ImageView, requestManager: (RequestManager.() -> Unit)? = null, requestBuilder: (RequestBuilder.() -> Unit)? = null -) { - bindNotNull(lifecycleOwner) { url -> +): Closeable { + return bindNotNull(lifecycleOwner) { url -> Glide.with(imageView) .also { requestManager?.invoke(it) } .load(url) @@ -33,8 +34,8 @@ fun LiveData.bindToImageViewSrc( imageView: ImageView, loadingPlaceholder: Drawable? = null, errorPlaceholder: Drawable? = null -) { - bindToImageViewSrc( +): Closeable { + return bindToImageViewSrc( lifecycleOwner = lifecycleOwner, imageView = imageView, requestBuilder = { diff --git a/mvvm-livedata-material/api/mvvm-livedata-material.api b/mvvm-livedata-material/api/mvvm-livedata-material.api index e19d5288..8531129c 100644 --- a/mvvm-livedata-material/api/mvvm-livedata-material.api +++ b/mvvm-livedata-material/api/mvvm-livedata-material.api @@ -6,7 +6,7 @@ public final class dev/icerock/moko/mvvm/livedata/material/BuildConfig { } public final class dev/icerock/moko/mvvm/livedata/material/TextInputLayoutBindingsKt { - public static final fun bindToTextInputLayoutErrorString (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Lcom/google/android/material/textfield/TextInputLayout;)V - public static final fun bindToTextInputLayoutErrorStringDesc (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Lcom/google/android/material/textfield/TextInputLayout;)V + 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; + 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; } diff --git a/mvvm-livedata-material/src/main/kotlin/dev/icerock/moko/mvvm/livedata/material/TextInputLayoutBindings.kt b/mvvm-livedata-material/src/main/kotlin/dev/icerock/moko/mvvm/livedata/material/TextInputLayoutBindings.kt index a108a560..39704896 100644 --- a/mvvm-livedata-material/src/main/kotlin/dev/icerock/moko/mvvm/livedata/material/TextInputLayoutBindings.kt +++ b/mvvm-livedata-material/src/main/kotlin/dev/icerock/moko/mvvm/livedata/material/TextInputLayoutBindings.kt @@ -6,6 +6,7 @@ package dev.icerock.moko.mvvm.livedata.material import androidx.lifecycle.LifecycleOwner import com.google.android.material.textfield.TextInputLayout +import dev.icerock.moko.mvvm.livedata.Closeable import dev.icerock.moko.mvvm.livedata.LiveData import dev.icerock.moko.mvvm.utils.bindNotNull import dev.icerock.moko.resources.desc.StringDesc @@ -14,15 +15,15 @@ import dev.icerock.moko.resources.desc.StringDesc fun LiveData.bindToTextInputLayoutError( lifecycleOwner: LifecycleOwner, textInputLayout: TextInputLayout -) { - bindNotNull(lifecycleOwner) { textInputLayout.error = it } +): Closeable { + return bindNotNull(lifecycleOwner) { textInputLayout.error = it } } @JvmName("bindToTextInputLayoutErrorStringDesc") fun LiveData.bindToTextInputLayoutError( lifecycleOwner: LifecycleOwner, textInputLayout: TextInputLayout -) { +): Closeable { val context = textInputLayout.context - bindNotNull(lifecycleOwner) { textInputLayout.error = it.toString(context) } + return bindNotNull(lifecycleOwner) { textInputLayout.error = it.toString(context) } } diff --git a/mvvm-livedata-swiperefresh/api/mvvm-livedata-swiperefresh.api b/mvvm-livedata-swiperefresh/api/mvvm-livedata-swiperefresh.api index 1661b3f6..4c4c35af 100644 --- a/mvvm-livedata-swiperefresh/api/mvvm-livedata-swiperefresh.api +++ b/mvvm-livedata-swiperefresh/api/mvvm-livedata-swiperefresh.api @@ -6,7 +6,7 @@ public final class dev/icerock/moko/mvvm/livedata/swiperefresh/BuildConfig { } public final class dev/icerock/moko/mvvm/livedata/swiperefresh/SwipeRefreshBindingsKt { - public static final fun bindToSwipeRefreshLayoutRefreshing (Ldev/icerock/moko/mvvm/livedata/MutableLiveData;Landroidx/lifecycle/LifecycleOwner;Landroidx/swiperefreshlayout/widget/SwipeRefreshLayout;)V + public static final fun bindToSwipeRefreshLayoutRefreshing (Ldev/icerock/moko/mvvm/livedata/MutableLiveData;Landroidx/lifecycle/LifecycleOwner;Landroidx/swiperefreshlayout/widget/SwipeRefreshLayout;)Ldev/icerock/moko/mvvm/livedata/Closeable; public static final fun setRefreshAction (Landroidx/swiperefreshlayout/widget/SwipeRefreshLayout;Lkotlin/jvm/functions/Function1;)V public static final fun setRefreshAction (Landroidx/swiperefreshlayout/widget/SwipeRefreshLayout;Lkotlinx/coroutines/CoroutineScope;Lkotlin/jvm/functions/Function1;)V } diff --git a/mvvm-livedata-swiperefresh/src/main/kotlin/dev/icerock/moko/mvvm/livedata/swiperefresh/SwipeRefreshBindings.kt b/mvvm-livedata-swiperefresh/src/main/kotlin/dev/icerock/moko/mvvm/livedata/swiperefresh/SwipeRefreshBindings.kt index dbb1941b..5f9ebf8e 100644 --- a/mvvm-livedata-swiperefresh/src/main/kotlin/dev/icerock/moko/mvvm/livedata/swiperefresh/SwipeRefreshBindings.kt +++ b/mvvm-livedata-swiperefresh/src/main/kotlin/dev/icerock/moko/mvvm/livedata/swiperefresh/SwipeRefreshBindings.kt @@ -6,6 +6,7 @@ package dev.icerock.moko.mvvm.livedata.swiperefresh import androidx.lifecycle.LifecycleOwner import androidx.swiperefreshlayout.widget.SwipeRefreshLayout +import dev.icerock.moko.mvvm.livedata.Closeable import dev.icerock.moko.mvvm.livedata.MutableLiveData import dev.icerock.moko.mvvm.utils.bindNotNull import kotlinx.coroutines.CoroutineScope @@ -14,10 +15,16 @@ import kotlinx.coroutines.launch fun MutableLiveData.bindToSwipeRefreshLayoutRefreshing( lifecycleOwner: LifecycleOwner, swipeRefreshLayout: SwipeRefreshLayout -) { - bindNotNull(lifecycleOwner) { swipeRefreshLayout.isRefreshing = it } +): Closeable { + val readCloseable = bindNotNull(lifecycleOwner) { swipeRefreshLayout.isRefreshing = it } swipeRefreshLayout.setOnRefreshListener { value = true } + + val writeCloseable = Closeable { + swipeRefreshLayout.setOnRefreshListener(null) + } + + return readCloseable + writeCloseable } fun SwipeRefreshLayout.setRefreshAction( diff --git a/mvvm-livedata/api/mvvm-livedata.api b/mvvm-livedata/api/mvvm-livedata.api index 1b5bb408..c332eb27 100644 --- a/mvvm-livedata/api/mvvm-livedata.api +++ b/mvvm-livedata/api/mvvm-livedata.api @@ -6,8 +6,22 @@ public final class dev/icerock/moko/mvvm/livedata/BuildConfig { } public final class dev/icerock/moko/mvvm/livedata/CheckBoxBindingsKt { - public static final fun bindToCheckBoxChecked (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/CheckBox;)V - public static final fun bindTwoWayToCheckBoxChecked (Ldev/icerock/moko/mvvm/livedata/MutableLiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/CheckBox;)V + public static final fun bindToCheckBoxChecked (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/CheckBox;)Ldev/icerock/moko/mvvm/livedata/Closeable; + public static final fun bindTwoWayToCheckBoxChecked (Ldev/icerock/moko/mvvm/livedata/MutableLiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/CheckBox;)Ldev/icerock/moko/mvvm/livedata/Closeable; +} + +public abstract interface class dev/icerock/moko/mvvm/livedata/Closeable { + public static final field Companion Ldev/icerock/moko/mvvm/livedata/Closeable$Companion; + public abstract fun close ()V + public abstract fun plus (Ldev/icerock/moko/mvvm/livedata/Closeable;)Ldev/icerock/moko/mvvm/livedata/Closeable; +} + +public final class dev/icerock/moko/mvvm/livedata/Closeable$Companion { + public final fun invoke (Lkotlin/jvm/functions/Function0;)Ldev/icerock/moko/mvvm/livedata/Closeable; +} + +public final class dev/icerock/moko/mvvm/livedata/Closeable$DefaultImpls { + public static fun plus (Ldev/icerock/moko/mvvm/livedata/Closeable;Ldev/icerock/moko/mvvm/livedata/Closeable;)Ldev/icerock/moko/mvvm/livedata/Closeable; } public final class dev/icerock/moko/mvvm/livedata/DebounceLiveDataKt { @@ -15,15 +29,23 @@ public final class dev/icerock/moko/mvvm/livedata/DebounceLiveDataKt { } public final class dev/icerock/moko/mvvm/livedata/EditTextBindingsKt { - public static final fun bindTwoWayToEditTextText (Ldev/icerock/moko/mvvm/livedata/MutableLiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/EditText;)V + public static final fun bindTwoWayToEditTextText (Ldev/icerock/moko/mvvm/livedata/MutableLiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/EditText;)Ldev/icerock/moko/mvvm/livedata/Closeable; +} + +public final class dev/icerock/moko/mvvm/livedata/FlatMapLiveDataKt { + public static final fun flatMap (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function1;)Ldev/icerock/moko/mvvm/livedata/LiveData; } public class dev/icerock/moko/mvvm/livedata/LiveData { - public fun (Landroidx/lifecycle/LiveData;)V + public fun (Ljava/lang/Object;)V public final fun addObserver (Lkotlin/jvm/functions/Function1;)V - protected final fun getArchLiveData ()Landroidx/lifecycle/LiveData; + protected final fun changeValue (Ljava/lang/Object;)V + protected final fun getArchLiveData ()Landroidx/lifecycle/MutableLiveData; public fun getValue ()Ljava/lang/Object; + public final fun isActive ()Z public fun ld ()Landroidx/lifecycle/LiveData; + protected fun onActive ()V + protected fun onInactive ()V public final fun removeObserver (Lkotlin/jvm/functions/Function1;)V } @@ -49,17 +71,25 @@ public final class dev/icerock/moko/mvvm/livedata/LiveDataExtKt { public static final fun revert (Ldev/icerock/moko/mvvm/livedata/LiveData;)Ldev/icerock/moko/mvvm/livedata/LiveData; } +public final class dev/icerock/moko/mvvm/livedata/LiveDataKt { + public static final fun addCloseableObserver (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function1;)Ldev/icerock/moko/mvvm/livedata/Closeable; +} + public final class dev/icerock/moko/mvvm/livedata/LiveDataTransformsKt { - public static final fun flatMap (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function1;)Ldev/icerock/moko/mvvm/livedata/LiveData; public static final fun flatMapBuffered (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function2;)Ldev/icerock/moko/mvvm/livedata/LiveData; - public static final fun map (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function1;)Ldev/icerock/moko/mvvm/livedata/LiveData; public static final fun mapBuffered (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function2;)Ldev/icerock/moko/mvvm/livedata/LiveData; public static final fun mergeWith (Ldev/icerock/moko/mvvm/livedata/LiveData;Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function2;)Ldev/icerock/moko/mvvm/livedata/MediatorLiveData; } +public final class dev/icerock/moko/mvvm/livedata/MapLiveDataKt { + public static final fun map (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function1;)Ldev/icerock/moko/mvvm/livedata/LiveData; +} + public class dev/icerock/moko/mvvm/livedata/MediatorLiveData : dev/icerock/moko/mvvm/livedata/MutableLiveData { public fun (Ljava/lang/Object;)V public final fun addSource (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function1;)V + protected fun onActive ()V + protected fun onInactive ()V } public final class dev/icerock/moko/mvvm/livedata/MediatorLiveDataExtKt { @@ -71,7 +101,6 @@ public final class dev/icerock/moko/mvvm/livedata/MediatorLiveDataExtKt { } public class dev/icerock/moko/mvvm/livedata/MutableLiveData : dev/icerock/moko/mvvm/livedata/LiveData { - public fun (Landroidx/lifecycle/MutableLiveData;)V public fun (Ljava/lang/Object;)V public fun getValue ()Ljava/lang/Object; public synthetic fun ld ()Landroidx/lifecycle/LiveData; @@ -86,8 +115,8 @@ public final class dev/icerock/moko/mvvm/livedata/MutableLiveDataExtKt { } public final class dev/icerock/moko/mvvm/livedata/TextViewBindingsKt { - public static final fun bindToTextViewTextString (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/TextView;)V - public static final fun bindToTextViewTextStringDesc (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/TextView;)V + public static final fun bindToTextViewTextString (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/TextView;)Ldev/icerock/moko/mvvm/livedata/Closeable; + public static final fun bindToTextViewTextStringDesc (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/TextView;)Ldev/icerock/moko/mvvm/livedata/Closeable; } public final class dev/icerock/moko/mvvm/livedata/ThrowableLiveDataExtKt { @@ -96,13 +125,13 @@ public final class dev/icerock/moko/mvvm/livedata/ThrowableLiveDataExtKt { } public final class dev/icerock/moko/mvvm/livedata/ViewBindingsKt { - public static final fun bindToViewEnabled (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/view/View;)V - public static final fun bindToViewVisibleOrGone (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/view/View;)V - public static final fun bindToViewVisibleOrInvisible (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/view/View;)V + public static final fun bindToViewEnabled (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/view/View;)Ldev/icerock/moko/mvvm/livedata/Closeable; + public static final fun bindToViewVisibleOrGone (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/view/View;)Ldev/icerock/moko/mvvm/livedata/Closeable; + public static final fun bindToViewVisibleOrInvisible (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/view/View;)Ldev/icerock/moko/mvvm/livedata/Closeable; } public final class dev/icerock/moko/mvvm/utils/LiveDataExtKt { - public static final fun bind (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Lkotlin/jvm/functions/Function1;)V - public static final fun bindNotNull (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Lkotlin/jvm/functions/Function1;)V + public static final fun bind (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Lkotlin/jvm/functions/Function1;)Ldev/icerock/moko/mvvm/livedata/Closeable; + public static final fun bindNotNull (Ldev/icerock/moko/mvvm/livedata/LiveData;Landroidx/lifecycle/LifecycleOwner;Lkotlin/jvm/functions/Function1;)Ldev/icerock/moko/mvvm/livedata/Closeable; } diff --git a/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/livedata/CheckBoxBindings.kt b/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/livedata/CheckBoxBindings.kt index 4d11f60e..ffb657ca 100644 --- a/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/livedata/CheckBoxBindings.kt +++ b/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/livedata/CheckBoxBindings.kt @@ -5,26 +5,37 @@ package dev.icerock.moko.mvvm.livedata import android.widget.CheckBox +import android.widget.CompoundButton import androidx.lifecycle.LifecycleOwner import dev.icerock.moko.mvvm.utils.bindNotNull -fun LiveData.bindToCheckBoxChecked(lifecycleOwner: LifecycleOwner, checkBox: CheckBox) { - bindNotNull(lifecycleOwner) { checkBox.isChecked = it } +fun LiveData.bindToCheckBoxChecked( + lifecycleOwner: LifecycleOwner, + checkBox: CheckBox +): Closeable { + return bindNotNull(lifecycleOwner) { checkBox.isChecked = it } } fun MutableLiveData.bindTwoWayToCheckBoxChecked( lifecycleOwner: LifecycleOwner, checkBox: CheckBox -) { - bindNotNull(lifecycleOwner) { value -> +): Closeable { + val readCloseable = bindNotNull(lifecycleOwner) { value -> if (checkBox.isChecked == value) return@bindNotNull checkBox.isChecked = value } - checkBox.setOnCheckedChangeListener { _, isChecked -> - if (value == isChecked) return@setOnCheckedChangeListener + val checkListener = CompoundButton.OnCheckedChangeListener { _, isChecked -> + if (value == isChecked) return@OnCheckedChangeListener value = isChecked } + checkBox.setOnCheckedChangeListener(checkListener) + + val writeCloseable = Closeable { + checkBox.setOnCheckedChangeListener(null) + } + + return readCloseable + writeCloseable } diff --git a/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/livedata/EditTextBindings.kt b/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/livedata/EditTextBindings.kt index 49628076..64575427 100644 --- a/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/livedata/EditTextBindings.kt +++ b/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/livedata/EditTextBindings.kt @@ -13,8 +13,8 @@ import dev.icerock.moko.mvvm.utils.bindNotNull fun MutableLiveData.bindTwoWayToEditTextText( lifecycleOwner: LifecycleOwner, editText: EditText -) { - bindNotNull(lifecycleOwner) { +): Closeable { + val readCloseable = bindNotNull(lifecycleOwner) { if (editText.text.toString() == it) return@bindNotNull editText.setText(it) @@ -34,4 +34,10 @@ fun MutableLiveData.bindTwoWayToEditTextText( override fun afterTextChanged(s: Editable?) = Unit } editText.addTextChangedListener(watcher) + + val writeCloseable = Closeable { + editText.removeTextChangedListener(watcher) + } + + return readCloseable + writeCloseable } diff --git a/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/livedata/TextViewBindings.kt b/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/livedata/TextViewBindings.kt index 34902c47..565746d3 100644 --- a/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/livedata/TextViewBindings.kt +++ b/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/livedata/TextViewBindings.kt @@ -10,12 +10,18 @@ import dev.icerock.moko.mvvm.utils.bindNotNull import dev.icerock.moko.resources.desc.StringDesc @JvmName("bindToTextViewTextString") -fun LiveData.bindToTextViewText(lifecycleOwner: LifecycleOwner, textView: TextView) { - bindNotNull(lifecycleOwner) { textView.text = it } +fun LiveData.bindToTextViewText( + lifecycleOwner: LifecycleOwner, + textView: TextView +): Closeable { + return bindNotNull(lifecycleOwner) { textView.text = it } } @JvmName("bindToTextViewTextStringDesc") -fun LiveData.bindToTextViewText(lifecycleOwner: LifecycleOwner, textView: TextView) { +fun LiveData.bindToTextViewText( + lifecycleOwner: LifecycleOwner, + textView: TextView +): Closeable { val context = textView.context - bindNotNull(lifecycleOwner) { textView.text = it.toString(context) } + return bindNotNull(lifecycleOwner) { textView.text = it.toString(context) } } diff --git a/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/livedata/ViewBindings.kt b/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/livedata/ViewBindings.kt index 256ce489..0bac4baa 100644 --- a/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/livedata/ViewBindings.kt +++ b/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/livedata/ViewBindings.kt @@ -8,20 +8,29 @@ import android.view.View import androidx.lifecycle.LifecycleOwner import dev.icerock.moko.mvvm.utils.bindNotNull -fun LiveData.bindToViewVisibleOrGone(lifecycleOwner: LifecycleOwner, view: View) { - bindNotNull(lifecycleOwner) { value -> +fun LiveData.bindToViewVisibleOrGone( + lifecycleOwner: LifecycleOwner, + view: View +): Closeable { + return bindNotNull(lifecycleOwner) { value -> if (value) view.visibility = View.VISIBLE else view.visibility = View.GONE } } -fun LiveData.bindToViewVisibleOrInvisible(lifecycleOwner: LifecycleOwner, view: View) { - bindNotNull(lifecycleOwner) { value -> +fun LiveData.bindToViewVisibleOrInvisible( + lifecycleOwner: LifecycleOwner, + view: View +): Closeable { + return bindNotNull(lifecycleOwner) { value -> if (value) view.visibility = View.VISIBLE else view.visibility = View.INVISIBLE } } -fun LiveData.bindToViewEnabled(lifecycleOwner: LifecycleOwner, view: View) { - bindNotNull(lifecycleOwner) { view.isEnabled = it } +fun LiveData.bindToViewEnabled( + lifecycleOwner: LifecycleOwner, + view: View +): Closeable { + return bindNotNull(lifecycleOwner) { view.isEnabled = it } } diff --git a/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/utils/LiveDataExt.kt b/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/utils/LiveDataExt.kt index 661eaf4a..2d14d0a2 100644 --- a/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/utils/LiveDataExt.kt +++ b/mvvm-livedata/src/androidMain/kotlin/dev/icerock/moko/mvvm/utils/LiveDataExt.kt @@ -6,18 +6,26 @@ package dev.icerock.moko.mvvm.utils import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.Observer +import dev.icerock.moko.mvvm.livedata.Closeable import dev.icerock.moko.mvvm.livedata.LiveData -fun LiveData.bind(lifecycleOwner: LifecycleOwner, observer: (T?) -> Unit) { +fun LiveData.bind(lifecycleOwner: LifecycleOwner, observer: (T?) -> Unit): Closeable { observer(value) - this.ld().observe(lifecycleOwner, Observer { value -> + val androidObserver = Observer { value -> observer(value) - }) + } + val androidLiveData = this.ld() + + androidLiveData.observe(lifecycleOwner, androidObserver) + + return Closeable { + androidLiveData.removeObserver(androidObserver) + } } -fun LiveData.bindNotNull(lifecycleOwner: LifecycleOwner, observer: (T) -> Unit) { - bind(lifecycleOwner) { value -> +fun LiveData.bindNotNull(lifecycleOwner: LifecycleOwner, observer: (T) -> Unit): Closeable { + return bind(lifecycleOwner) { value -> if (value == null) return@bind observer(value) diff --git a/mvvm-livedata/src/commonMain/kotlin/dev/icerock/moko/mvvm/livedata/Closeable.kt b/mvvm-livedata/src/commonMain/kotlin/dev/icerock/moko/mvvm/livedata/Closeable.kt new file mode 100644 index 00000000..62541b40 --- /dev/null +++ b/mvvm-livedata/src/commonMain/kotlin/dev/icerock/moko/mvvm/livedata/Closeable.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2021 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + +package dev.icerock.moko.mvvm.livedata + +interface Closeable { + fun close() + + operator fun plus(other: Closeable): Closeable { + return Closeable { + this.close() + other.close() + } + } + + companion object { + operator fun invoke(block: () -> Unit): Closeable { + return object : Closeable { + override fun close() { + block() + } + } + } + } +} diff --git a/mvvm-livedata/src/commonMain/kotlin/dev/icerock/moko/mvvm/livedata/LiveData.kt b/mvvm-livedata/src/commonMain/kotlin/dev/icerock/moko/mvvm/livedata/LiveData.kt index a9038623..250330f4 100644 --- a/mvvm-livedata/src/commonMain/kotlin/dev/icerock/moko/mvvm/livedata/LiveData.kt +++ b/mvvm-livedata/src/commonMain/kotlin/dev/icerock/moko/mvvm/livedata/LiveData.kt @@ -11,3 +11,8 @@ expect open class LiveData { fun removeObserver(observer: (T) -> Unit) } + +fun LiveData.addCloseableObserver(observer: (T) -> Unit): Closeable { + addObserver(observer) + return Closeable { removeObserver(observer) } +} diff --git a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UIButtonBinding.kt b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UIButtonBinding.kt index 065859f0..b492b591 100644 --- a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UIButtonBinding.kt +++ b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UIButtonBinding.kt @@ -4,12 +4,13 @@ package dev.icerock.moko.mvvm.binding +import dev.icerock.moko.mvvm.livedata.Closeable import dev.icerock.moko.mvvm.livedata.LiveData import dev.icerock.moko.mvvm.livedata.bindBoolToButtonImage -import dev.icerock.moko.mvvm.livedata.bindStringDescToButtonTitle -import dev.icerock.moko.mvvm.livedata.bindStringToButtonTitle import dev.icerock.moko.mvvm.livedata.bindBoolToControlEnabled import dev.icerock.moko.mvvm.livedata.bindBoolToViewBackgroundColor +import dev.icerock.moko.mvvm.livedata.bindStringDescToButtonTitle +import dev.icerock.moko.mvvm.livedata.bindStringToButtonTitle import dev.icerock.moko.resources.desc.StringDesc import platform.UIKit.UIButton import platform.UIKit.UIColor @@ -20,30 +21,33 @@ fun UIButton.bindEnabled( liveData: LiveData, enabledColor: UIColor? = null, disabledColor: UIColor? = null -) { - liveData.bindBoolToControlEnabled(control = this) +): Closeable { + val enabledCloseable = liveData.bindBoolToControlEnabled(control = this) if (enabledColor != null && disabledColor != null) { - liveData.bindBoolToViewBackgroundColor( + val backgroundCloseable = liveData.bindBoolToViewBackgroundColor( view = this, trueColor = enabledColor, falseColor = disabledColor ) + return enabledCloseable + backgroundCloseable + } else { + return enabledCloseable } } @Deprecated("use LiveData.bindToButtonTitle extension") fun UIButton.bindTitle( liveData: LiveData -) { - liveData.bindStringToButtonTitle(button = this) +): Closeable { + return liveData.bindStringToButtonTitle(button = this) } @Deprecated("use LiveData.bindToButtonTitle extension") fun UIButton.bindTitle( liveData: LiveData -) { - liveData.bindStringDescToButtonTitle(button = this) +): Closeable { + return liveData.bindStringDescToButtonTitle(button = this) } @Deprecated("use LiveData.bindToButtonImage extension") @@ -51,8 +55,8 @@ fun UIButton.bindImages( liveData: LiveData, trueImage: UIImage, falseImage: UIImage -) { - liveData.bindBoolToButtonImage( +): Closeable { + return liveData.bindBoolToButtonImage( button = this, trueImage = trueImage, falseImage = falseImage diff --git a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UILabelBinding.kt b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UILabelBinding.kt index b846fc73..7a8501c2 100644 --- a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UILabelBinding.kt +++ b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UILabelBinding.kt @@ -4,6 +4,7 @@ package dev.icerock.moko.mvvm.binding +import dev.icerock.moko.mvvm.livedata.Closeable import dev.icerock.moko.mvvm.livedata.LiveData import dev.icerock.moko.mvvm.livedata.bindStringToLabelText import dev.icerock.moko.mvvm.livedata.map @@ -14,14 +15,14 @@ import platform.UIKit.UILabel fun UILabel.bindText( liveData: LiveData, formatter: ((String) -> String) = { it } -) { - liveData.map(formatter).bindStringToLabelText(label = this) +): Closeable { + return liveData.map(formatter).bindStringToLabelText(label = this) } @Deprecated("use LiveData.bindToLabelText extension") fun UILabel.bindText( liveData: LiveData, formatter: ((String) -> String) = { it } -) { - liveData.map { it.localized() }.map(formatter).bindStringToLabelText(label = this) +): Closeable { + return liveData.map { it.localized() }.map(formatter).bindStringToLabelText(label = this) } diff --git a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UISwitchBinding.kt b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UISwitchBinding.kt index a48bc57f..7107e2f3 100644 --- a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UISwitchBinding.kt +++ b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UISwitchBinding.kt @@ -4,6 +4,7 @@ package dev.icerock.moko.mvvm.binding +import dev.icerock.moko.mvvm.livedata.Closeable import dev.icerock.moko.mvvm.livedata.LiveData import dev.icerock.moko.mvvm.livedata.MutableLiveData import dev.icerock.moko.mvvm.livedata.bindBoolToSwitchOn @@ -11,11 +12,11 @@ import dev.icerock.moko.mvvm.livedata.bindBoolTwoWayToSwitchOn import platform.UIKit.UISwitch @Deprecated("use LiveData.bindToSwitchOn extension") -fun UISwitch.bindValue(liveData: LiveData) { - liveData.bindBoolToSwitchOn(switch = this) +fun UISwitch.bindValue(liveData: LiveData): Closeable { + return liveData.bindBoolToSwitchOn(switch = this) } @Deprecated("use LiveData.bindTwoWayToSwitchOn extension") -fun UISwitch.bindValueTwoWay(liveData: MutableLiveData) { - liveData.bindBoolTwoWayToSwitchOn(switch = this) +fun UISwitch.bindValueTwoWay(liveData: MutableLiveData): Closeable { + return liveData.bindBoolTwoWayToSwitchOn(switch = this) } diff --git a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UITextFieldBinding.kt b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UITextFieldBinding.kt index 2f7a6828..93dd792c 100644 --- a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UITextFieldBinding.kt +++ b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UITextFieldBinding.kt @@ -4,31 +4,32 @@ package dev.icerock.moko.mvvm.binding +import dev.icerock.moko.mvvm.livedata.Closeable import dev.icerock.moko.mvvm.livedata.LiveData import dev.icerock.moko.mvvm.livedata.MutableLiveData -import dev.icerock.moko.mvvm.livedata.bindBoolToResponderFocus -import dev.icerock.moko.mvvm.livedata.bindStringToTextFieldText +import dev.icerock.moko.mvvm.livedata.bindBoolToViewFocus import dev.icerock.moko.mvvm.livedata.bindBoolTwoWayToControlFocus +import dev.icerock.moko.mvvm.livedata.bindStringToTextFieldText import dev.icerock.moko.mvvm.livedata.map +import dev.icerock.moko.mvvm.utils.setEventHandler import dev.icerock.moko.resources.desc.StringDesc import platform.UIKit.UIControlEventEditingChanged import platform.UIKit.UITextField -import dev.icerock.moko.mvvm.utils.setEventHandler @Deprecated("use LiveData.bindToTextFieldText extension") fun UITextField.bindText( liveData: LiveData, formatter: ((String) -> String)? = null -) { - liveData.map { formatter?.invoke(it) ?: it }.bindStringToTextFieldText(textField = this) +): Closeable { + return liveData.map { formatter?.invoke(it) ?: it }.bindStringToTextFieldText(textField = this) } @Deprecated("use LiveData.bindToTextFieldText extension") fun UITextField.bindText( liveData: LiveData, formatter: ((String) -> String)? = null -) { - liveData +): Closeable { + return liveData .map { it.localized() } .map { formatter?.invoke(it) ?: it } .bindStringToTextFieldText(textField = this) @@ -39,10 +40,10 @@ fun UITextField.bindTextTwoWay( liveData: MutableLiveData, formatter: ((String) -> String)? = null, reverseFormatter: ((String) -> String)? = null -) { - bindText(liveData, formatter) +): Closeable { + val readCloseable = bindText(liveData, formatter) - setEventHandler(UIControlEventEditingChanged) { + val writeCloseable = setEventHandler(UIControlEventEditingChanged) { val newText = this.text.orEmpty() val newFormattedText = reverseFormatter?.invoke(newText) ?: newText @@ -50,14 +51,16 @@ fun UITextField.bindTextTwoWay( liveData.value = newFormattedText } + + return readCloseable + writeCloseable } @Deprecated("use LiveData.bindToControlFocus extension") -fun UITextField.bindFocus(liveData: LiveData) { - liveData.bindBoolToResponderFocus(responder = this) +fun UITextField.bindFocus(liveData: LiveData): Closeable { + return liveData.bindBoolToViewFocus(view = this) } @Deprecated("use LiveData. liveData.bindTwoWayToControlFocus(control = this)\n extension") -fun UITextField.bindFocusTwoWay(liveData: MutableLiveData) { - liveData.bindBoolTwoWayToControlFocus(control = this) +fun UITextField.bindFocusTwoWay(liveData: MutableLiveData): Closeable { + return liveData.bindBoolTwoWayToControlFocus(control = this) } diff --git a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UITextViewBinding.kt b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UITextViewBinding.kt index 0c2cad26..fcfc48b3 100644 --- a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UITextViewBinding.kt +++ b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UITextViewBinding.kt @@ -4,11 +4,12 @@ package dev.icerock.moko.mvvm.binding +import dev.icerock.moko.mvvm.livedata.Closeable import dev.icerock.moko.mvvm.livedata.LiveData import dev.icerock.moko.mvvm.livedata.MutableLiveData -import dev.icerock.moko.mvvm.livedata.bindBoolToResponderFocus -import dev.icerock.moko.mvvm.livedata.bindStringToTextViewText +import dev.icerock.moko.mvvm.livedata.bindBoolToViewFocus import dev.icerock.moko.mvvm.livedata.bindBoolTwoWayToTextViewFocus +import dev.icerock.moko.mvvm.livedata.bindStringToTextViewText import dev.icerock.moko.mvvm.livedata.map import dev.icerock.moko.mvvm.utils.setEventHandler import dev.icerock.moko.resources.desc.StringDesc @@ -20,16 +21,16 @@ import platform.UIKit.UITextViewTextDidChangeNotification fun UITextView.bindText( liveData: LiveData, formatter: ((String) -> String)? = null -) { - liveData.map { formatter?.invoke(it) ?: it }.bindStringToTextViewText(textView = this) +): Closeable { + return liveData.map { formatter?.invoke(it) ?: it }.bindStringToTextViewText(textView = this) } @Deprecated("use LiveData.bindToTextViewText extension") fun UITextView.bindText( liveData: LiveData, formatter: ((String) -> String)? = null -) { - liveData +): Closeable { + return liveData .map { it.localized() } .map { formatter?.invoke(it) ?: it } .bindStringToTextViewText(textView = this) @@ -40,10 +41,10 @@ fun UITextView.bindTextTwoWay( liveData: MutableLiveData, formatter: ((String) -> String)? = null, reverseFormatter: ((String) -> String)? = null -) { - bindText(liveData, formatter) +): Closeable { + val readCloseable = bindText(liveData, formatter) - NSNotificationCenter.defaultCenter.setEventHandler( + val writeCloseable = NSNotificationCenter.defaultCenter.setEventHandler( notification = UITextViewTextDidChangeNotification, ref = this ) { @@ -54,14 +55,16 @@ fun UITextView.bindTextTwoWay( liveData.value = newFormattedText } + + return readCloseable + writeCloseable } @Deprecated("use LiveData.bindToResponderFocus extension") -fun UITextView.bindFocus(liveData: LiveData) { - liveData.bindBoolToResponderFocus(responder = this) +fun UITextView.bindFocus(liveData: LiveData): Closeable { + return liveData.bindBoolToViewFocus(view = this) } @Deprecated("use LiveData.bindTwoWayToTextViewFocus extension") -fun UITextView.bindFocusTwoWay(liveData: MutableLiveData) { - liveData.bindBoolTwoWayToTextViewFocus(textView = this) +fun UITextView.bindFocusTwoWay(liveData: MutableLiveData): Closeable { + return liveData.bindBoolTwoWayToTextViewFocus(textView = this) } diff --git a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UIViewBinding.kt b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UIViewBinding.kt index f0f239ce..2f9506a0 100644 --- a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UIViewBinding.kt +++ b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/binding/UIViewBinding.kt @@ -4,6 +4,7 @@ package dev.icerock.moko.mvvm.binding +import dev.icerock.moko.mvvm.livedata.Closeable import dev.icerock.moko.mvvm.livedata.LiveData import dev.icerock.moko.mvvm.livedata.bindBoolToViewHidden import dev.icerock.moko.mvvm.livedata.revert @@ -13,9 +14,9 @@ import platform.UIKit.UIView fun UIView.bindVisibility( liveData: LiveData, inverted: Boolean = false -) { +): Closeable { val source = if (inverted) liveData else liveData.revert() - source.bindBoolToViewHidden(view = this) + return source.bindBoolToViewHidden(view = this) } diff --git a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UIButtonBindings.kt b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UIButtonBindings.kt index b83923b6..9998cbeb 100644 --- a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UIButtonBindings.kt +++ b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UIButtonBindings.kt @@ -10,22 +10,22 @@ import platform.UIKit.UIButton import platform.UIKit.UIControlStateNormal import platform.UIKit.UIImage -fun LiveData.bindStringToButtonTitle(button: UIButton) { - bind(button) { value -> +fun LiveData.bindStringToButtonTitle(button: UIButton): Closeable { + return bind(button) { value -> setTitle(value, forState = UIControlStateNormal) } } -fun LiveData.bindStringDescToButtonTitle(button: UIButton) { - map { it?.localized() }.bindStringToButtonTitle(button) +fun LiveData.bindStringDescToButtonTitle(button: UIButton): Closeable { + return map { it?.localized() }.bindStringToButtonTitle(button) } fun LiveData.bindBoolToButtonImage( button: UIButton, trueImage: UIImage, falseImage: UIImage -) { - bind(button) { value -> +): Closeable { + return bind(button) { value -> val image = when (value) { true -> trueImage false -> falseImage diff --git a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UIControlBindings.kt b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UIControlBindings.kt index 98461322..d2f0be17 100644 --- a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UIControlBindings.kt +++ b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UIControlBindings.kt @@ -13,19 +13,21 @@ import platform.UIKit.UIControlEventEditingDidEndOnExit fun LiveData.bindBoolToControlEnabled( control: UIControl -) { - bind(control) { this.enabled = it } +): Closeable { + return bind(control) { this.enabled = it } } -fun MutableLiveData.bindBoolTwoWayToControlFocus(control: UIControl) { - bindBoolToResponderFocus(control) +fun MutableLiveData.bindBoolTwoWayToControlFocus(control: UIControl): Closeable { + val readCloseable = bindBoolToViewFocus(view = control) val handler: UIControl.() -> Unit = { val focused = isFocused() if (value != focused) value = focused } - control.setEventHandler(UIControlEventEditingDidBegin, handler) - control.setEventHandler(UIControlEventEditingDidEnd, handler) - control.setEventHandler(UIControlEventEditingDidEndOnExit, handler) + val beginCloseable = control.setEventHandler(UIControlEventEditingDidBegin, handler) + val endCloseable = control.setEventHandler(UIControlEventEditingDidEnd, handler) + val endOnExitCloseable = control.setEventHandler(UIControlEventEditingDidEndOnExit, handler) + + return readCloseable + beginCloseable + endCloseable + endOnExitCloseable } diff --git a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UILabelBindings.kt b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UILabelBindings.kt index 17cc3503..6bafe2c5 100644 --- a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UILabelBindings.kt +++ b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UILabelBindings.kt @@ -10,12 +10,12 @@ import platform.UIKit.UILabel fun LiveData.bindStringToLabelText( label: UILabel -) { - bind(label) { this.text = it } +): Closeable { + return bind(label) { this.text = it } } fun LiveData.bindStringDescToLabelText( label: UILabel -) { - map { it?.localized() }.bindStringToLabelText(label) +): Closeable { + return map { it?.localized() }.bindStringToLabelText(label) } diff --git a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UIResponderBindings.kt b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UIResponderBindings.kt index 99dcf0e5..a067f0cf 100644 --- a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UIResponderBindings.kt +++ b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UIResponderBindings.kt @@ -4,19 +4,14 @@ package dev.icerock.moko.mvvm.livedata -import dev.icerock.moko.mvvm.utils.bind -import platform.UIKit.UIResponder +import platform.UIKit.UIView -fun LiveData.bindBoolToResponderFocus(responder: UIResponder) { - bind(responder) { value -> - if (value) { - becomeFirstResponder() - } else { - if (nextResponder?.canBecomeFirstResponder == true) { - nextResponder?.becomeFirstResponder() - } else { - resignFirstResponder() - } - } - } -} +@Deprecated( + message = "we can't use only UIResponder because need UIView lifecycle", + replaceWith = ReplaceWith( + expression = "bindBoolToViewFocus", + "dev.icerock.moko.mvvm.livedata.bindBoolToViewFocus" + ), + level = DeprecationLevel.WARNING +) +fun LiveData.bindBoolToResponderFocus(responder: UIView): Closeable = bindBoolToViewFocus(responder) diff --git a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UISwitchBindings.kt b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UISwitchBindings.kt index baf4aee9..772ae55f 100644 --- a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UISwitchBindings.kt +++ b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UISwitchBindings.kt @@ -11,18 +11,20 @@ import platform.UIKit.UISwitch fun LiveData.bindBoolToSwitchOn( switch: UISwitch -) { - bind(switch) { this.on = it } +): Closeable { + return bind(switch) { this.on = it } } fun MutableLiveData.bindBoolTwoWayToSwitchOn( switch: UISwitch -) { - bindBoolToSwitchOn(switch) +): Closeable { + val readCloseable = bindBoolToSwitchOn(switch) - switch.setEventHandler(UIControlEventValueChanged) { + val writeCloseable = switch.setEventHandler(UIControlEventValueChanged) { if (value == on) return@setEventHandler value = on } + + return readCloseable + writeCloseable } diff --git a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UITextFieldBindings.kt b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UITextFieldBindings.kt index 73df2b7a..fec7e140 100644 --- a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UITextFieldBindings.kt +++ b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UITextFieldBindings.kt @@ -12,8 +12,8 @@ import platform.UIKit.UITextField fun LiveData.bindStringToTextFieldText( textField: UITextField -) { - bind(textField) { value -> +): Closeable { + return bind(textField) { value -> if (this.text == value) return@bind this.text = value @@ -22,20 +22,22 @@ fun LiveData.bindStringToTextFieldText( fun LiveData.bindStringDescToTextFieldText( textField: UITextField -) { - map { it?.localized() }.bindStringToTextFieldText(textField) +): Closeable { + return map { it?.localized() }.bindStringToTextFieldText(textField) } fun MutableLiveData.bindStringTwoWayToTextFieldText( textField: UITextField -) { - bindStringToTextFieldText(textField) +): Closeable { + val readCloseable = bindStringToTextFieldText(textField) - textField.setEventHandler(UIControlEventEditingChanged) { + val writeCloseable = textField.setEventHandler(UIControlEventEditingChanged) { val newText = this.text.orEmpty() if (value == newText) return@setEventHandler value = newText } + + return readCloseable + writeCloseable } diff --git a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UITextViewBindings.kt b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UITextViewBindings.kt index f602cb0d..9cea76f3 100644 --- a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UITextViewBindings.kt +++ b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UITextViewBindings.kt @@ -15,8 +15,8 @@ import platform.UIKit.UITextViewTextDidEndEditingNotification fun LiveData.bindStringToTextViewText( textView: UITextView -) { - bind(textView) { value -> +): Closeable { + return bind(textView) { value -> if (this.text == value) return@bind this.text = value.orEmpty() @@ -25,16 +25,16 @@ fun LiveData.bindStringToTextViewText( fun LiveData.bindStringDescToTextViewText( textView: UITextView -) { - map { it?.localized() }.bindStringToTextViewText(textView) +): Closeable { + return map { it?.localized() }.bindStringToTextViewText(textView) } fun MutableLiveData.bindStringTwoWayToTextViewText( textView: UITextView -) { - bindStringToTextViewText(textView) +): Closeable { + val readCloseable = bindStringToTextViewText(textView) - NSNotificationCenter.defaultCenter.setEventHandler( + val writeCloseable = NSNotificationCenter.defaultCenter.setEventHandler( notification = UITextViewTextDidChangeNotification, ref = textView ) { @@ -44,10 +44,14 @@ fun MutableLiveData.bindStringTwoWayToTextViewText( value = newText } + + return readCloseable + writeCloseable } -fun MutableLiveData.bindBoolTwoWayToTextViewFocus(textView: UITextView) { - bindBoolToResponderFocus(textView) +fun MutableLiveData.bindBoolTwoWayToTextViewFocus( + textView: UITextView +): Closeable { + val readCloseable = bindBoolToViewFocus(view = textView) val handler: UITextView.() -> Unit = { val focused = isFocused() @@ -55,14 +59,16 @@ fun MutableLiveData.bindBoolTwoWayToTextViewFocus(textView: UITextView) if (value != focused) value = focused } - NSNotificationCenter.defaultCenter.setEventHandler( + val beginCloseable = NSNotificationCenter.defaultCenter.setEventHandler( notification = UITextViewTextDidBeginEditingNotification, ref = textView, lambda = handler ) - NSNotificationCenter.defaultCenter.setEventHandler( + val endCloseable = NSNotificationCenter.defaultCenter.setEventHandler( notification = UITextViewTextDidEndEditingNotification, ref = textView, lambda = handler ) + + return readCloseable + beginCloseable + endCloseable } diff --git a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UIViewBindings.kt b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UIViewBindings.kt index f5ad50e2..fb6f658b 100644 --- a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UIViewBindings.kt +++ b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/livedata/UIViewBindings.kt @@ -14,8 +14,8 @@ fun LiveData.bindBoolToViewBackgroundColor( view: UIView, trueColor: UIColor, falseColor: UIColor -) { - bind(view) { value -> +): Closeable { + return bind(view) { value -> val color = when (value) { true -> trueColor false -> falseColor @@ -27,8 +27,22 @@ fun LiveData.bindBoolToViewBackgroundColor( fun LiveData.bindBoolToViewHidden( view: UIView -) { - bind(view) { value -> +): Closeable { + return bind(view) { value -> hidden = value } } + +fun LiveData.bindBoolToViewFocus(view: UIView): Closeable { + return bind(view) { value -> + if (value) { + becomeFirstResponder() + } else { + if (nextResponder?.canBecomeFirstResponder == true) { + nextResponder?.becomeFirstResponder() + } else { + resignFirstResponder() + } + } + } +} diff --git a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/utils/LiveDataExt.kt b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/utils/LiveDataExt.kt index d0f41dff..ce3d0cdc 100644 --- a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/utils/LiveDataExt.kt +++ b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/utils/LiveDataExt.kt @@ -4,10 +4,11 @@ package dev.icerock.moko.mvvm.utils +import dev.icerock.moko.mvvm.livedata.Closeable import dev.icerock.moko.mvvm.livedata.LiveData import kotlin.native.ref.WeakReference -fun LiveData.bind(view: V, setter: V.(T) -> Unit) { +fun LiveData.bind(view: V, setter: V.(T) -> Unit): Closeable { setter(view, value) val weakView = WeakReference(view) lateinit var observer: (T) -> Unit @@ -17,4 +18,8 @@ fun LiveData.bind(view: V, setter: V.(T) -> Unit) { else setter(strongView, value) } addObserver(observer) + + return Closeable { + removeObserver(observer) + } } diff --git a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/utils/NSNotificationCenterExt.kt b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/utils/NSNotificationCenterExt.kt index 3d86dce4..a8c1353c 100644 --- a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/utils/NSNotificationCenterExt.kt +++ b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/utils/NSNotificationCenterExt.kt @@ -4,6 +4,7 @@ package dev.icerock.moko.mvvm.utils +import dev.icerock.moko.mvvm.livedata.Closeable import kotlinx.cinterop.ExportObjCClass import kotlinx.cinterop.ObjCAction import kotlinx.cinterop.cstr @@ -19,7 +20,7 @@ fun NSNotificationCenter.setEventHandler( notification: NSNotificationName, ref: T, lambda: T.() -> Unit -) { +): Closeable { val lambdaTarget = NotificationLambdaTarget(lambda) addObserver( @@ -35,6 +36,11 @@ fun NSNotificationCenter.setEventHandler( value = lambdaTarget, policy = OBJC_ASSOCIATION_RETAIN ) + + return Closeable { + removeObserver(lambdaTarget) + // TODO remove associated object too, when it will be available in kotlin + } } @ExportObjCClass @@ -44,6 +50,7 @@ private class NotificationLambdaTarget( @ObjCAction fun action(notification: NSNotification) { + @Suppress("UNCHECKED_CAST") val ref = notification.`object` as T lambda(ref) } diff --git a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/utils/UIControlExt.kt b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/utils/UIControlExt.kt index 74b24b28..c5065005 100644 --- a/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/utils/UIControlExt.kt +++ b/mvvm-livedata/src/iosMain/kotlin/dev/icerock/moko/mvvm/utils/UIControlExt.kt @@ -4,6 +4,7 @@ package dev.icerock.moko.mvvm.utils +import dev.icerock.moko.mvvm.livedata.Closeable import kotlinx.cinterop.ExportObjCClass import kotlinx.cinterop.ObjCAction import kotlinx.cinterop.cstr @@ -13,14 +14,17 @@ import platform.UIKit.UIControlEvents import platform.darwin.NSObject import platform.objc.OBJC_ASSOCIATION_RETAIN import platform.objc.objc_setAssociatedObject -import kotlin.native.ref.WeakReference -fun T.setEventHandler(event: UIControlEvents, lambda: T.() -> Unit) { - val lambdaTarget = ControlLambdaTarget(this, lambda) +fun T.setEventHandler( + event: UIControlEvents, + lambda: T.() -> Unit +): Closeable { + val lambdaTarget = ControlLambdaTarget(lambda) + val action = NSSelectorFromString("action:") addTarget( target = lambdaTarget, - action = NSSelectorFromString("action"), + action = action, forControlEvents = event ) @@ -30,18 +34,21 @@ fun T.setEventHandler(event: UIControlEvents, lambda: T.() -> Un value = lambdaTarget, policy = OBJC_ASSOCIATION_RETAIN ) + + return Closeable { + removeTarget(target = lambdaTarget, action = action, forControlEvents = event) + // TODO remove associated object too, when it will be available in kotlin + } } @ExportObjCClass -private class ControlLambdaTarget( - ref: T, - val lambda: T.() -> Unit +private class ControlLambdaTarget( + private val lambda: T.() -> Unit ) : NSObject() { - private val weakRef = WeakReference(ref) @ObjCAction - fun action() { - val ref = weakRef.get() ?: return - lambda(ref) + fun action(sender: UIControl) { + @Suppress("UNCHECKED_CAST") + lambda(sender as T) } } diff --git a/mvvm-livedata/src/iosMain/swift/Core/UIButton+LiveData.swift b/mvvm-livedata/src/iosMain/swift/Core/UIButton+LiveData.swift index 45f68600..0f3e5822 100644 --- a/mvvm-livedata/src/iosMain/swift/Core/UIButton+LiveData.swift +++ b/mvvm-livedata/src/iosMain/swift/Core/UIButton+LiveData.swift @@ -6,10 +6,13 @@ import UIKit import MultiPlatformLibrary public extension UIButton { - func bindEnabled(liveData: LiveData, - enabledColor: UIColor? = nil, - disabledColor: UIColor? = nil) { - UIButtonBindingKt.bindEnabled( + @available(*, deprecated, message: "use LiveData.bindBoolToControlEnabled and LiveData.bindBoolToViewBackgroundColor") + @discardableResult func bindEnabled( + liveData: LiveData, + enabledColor: UIColor? = nil, + disabledColor: UIColor? = nil + ) -> Closeable { + return UIButtonBindingKt.bindEnabled( self, liveData: liveData, enabledColor: enabledColor, @@ -17,24 +20,27 @@ public extension UIButton { ) } - func bindTitle(liveData: LiveData) { - UIButtonBindingKt.bindTitle( + @available(*, deprecated, message: "use LiveData.bindStringToButtonTitle") + @discardableResult func bindTitle(liveData: LiveData) -> Closeable { + return UIButtonBindingKt.bindTitle( self, liveData_: liveData ) } - func bindTitle(liveData: LiveData) { - UIButtonBindingKt.bindTitle( + @available(*, deprecated, message: "use LiveData.bindStringDescToButtonTitle") + @discardableResult func bindTitle(liveData: LiveData) -> Closeable { + return UIButtonBindingKt.bindTitle( self, liveData: liveData ) } - func bindImages(liveData: LiveData, + @available(*, deprecated, message: "use LiveData.bindBoolToButtonImage") + @discardableResult func bindImages(liveData: LiveData, trueImage: UIImage, - falseImage: UIImage) { - UIButtonBindingKt.bindImages( + falseImage: UIImage) -> Closeable { + return UIButtonBindingKt.bindImages( self, liveData: liveData, trueImage: trueImage, diff --git a/mvvm-livedata/src/iosMain/swift/Core/UILabel+LiveData.swift b/mvvm-livedata/src/iosMain/swift/Core/UILabel+LiveData.swift index 59bc195e..d424deb4 100644 --- a/mvvm-livedata/src/iosMain/swift/Core/UILabel+LiveData.swift +++ b/mvvm-livedata/src/iosMain/swift/Core/UILabel+LiveData.swift @@ -6,22 +6,24 @@ import UIKit import MultiPlatformLibrary public extension UILabel { - func bindText( + @available(*, deprecated, message: "use LiveData.bindStringToLabelText") + @discardableResult func bindText( liveData: LiveData, formatter: @escaping ((String) -> String) = { text in return text } - ) { - UILabelBindingKt.bindText( + ) -> Closeable { + return UILabelBindingKt.bindText( self, liveData: liveData, formatter_: formatter ) } - func bindText( + @available(*, deprecated, message: "use LiveData.bindStringDescToLabelText") + @discardableResult func bindText( liveData: LiveData, formatter: @escaping ((String) -> String) = { text in return text } - ) { - UILabelBindingKt.bindText( + ) -> Closeable { + return UILabelBindingKt.bindText( self, liveData: liveData, formatter: formatter diff --git a/mvvm-livedata/src/iosMain/swift/Core/UISwitch+LiveData.swift b/mvvm-livedata/src/iosMain/swift/Core/UISwitch+LiveData.swift index 3a059136..49c0bde7 100644 --- a/mvvm-livedata/src/iosMain/swift/Core/UISwitch+LiveData.swift +++ b/mvvm-livedata/src/iosMain/swift/Core/UISwitch+LiveData.swift @@ -6,15 +6,17 @@ import UIKit import MultiPlatformLibrary public extension UISwitch { - func bindValue(liveData: LiveData) { - UISwitchBindingKt.bindValue( + @available(*, deprecated, message: "use LiveData.bindBoolToSwitchOn") + @discardableResult func bindValue(liveData: LiveData) -> Closeable { + return UISwitchBindingKt.bindValue( self, liveData: liveData ) } - func bindValueTwoWay(liveData: MutableLiveData) { - UISwitchBindingKt.bindValueTwoWay( + @available(*, deprecated, message: "use MutableLiveData.bindBoolTwoWayToSwitchOn") + @discardableResult func bindValueTwoWay(liveData: MutableLiveData) -> Closeable { + return UISwitchBindingKt.bindValueTwoWay( self, liveData: liveData ) diff --git a/mvvm-livedata/src/iosMain/swift/Core/UITextField+LiveData.swift b/mvvm-livedata/src/iosMain/swift/Core/UITextField+LiveData.swift index f96e0f29..e68bfbec 100644 --- a/mvvm-livedata/src/iosMain/swift/Core/UITextField+LiveData.swift +++ b/mvvm-livedata/src/iosMain/swift/Core/UITextField+LiveData.swift @@ -6,34 +6,37 @@ import UIKit import MultiPlatformLibrary public extension UITextField { - func bindText( + @available(*, deprecated, message: "use LiveData.bindStringToTextFieldText") + @discardableResult func bindText( liveData: LiveData, formatter: ((String) -> String)? = nil - ) { - UITextFieldBindingKt.bindText( + ) -> Closeable { + return UITextFieldBindingKt.bindText( self, liveData: liveData, formatter_: formatter ) } - func bindText( + @available(*, deprecated, message: "use LiveData.bindStringDescToTextFieldText") + @discardableResult func bindText( liveData: LiveData, formatter: ((String) -> String)? = nil - ) { - UITextFieldBindingKt.bindText( + ) -> Closeable { + return UITextFieldBindingKt.bindText( self, liveData: liveData, formatter: formatter ) } - func bindTextTwoWay( + @available(*, deprecated, message: "use LiveData.bindStringTwoWayToTextFieldText") + @discardableResult func bindTextTwoWay( liveData: MutableLiveData, formatter: ((String) -> String)? = nil, reverseFormatter:((String) -> String)? = nil - ) { - UITextFieldBindingKt.bindTextTwoWay( + ) -> Closeable { + return UITextFieldBindingKt.bindTextTwoWay( self, liveData: liveData, formatter: formatter, @@ -41,15 +44,17 @@ public extension UITextField { ) } - func bindFocus(liveData: LiveData) { - UITextFieldBindingKt.bindFocus( + @available(*, deprecated, message: "use LiveData.bindBoolToViewFocus") + @discardableResult func bindFocus(liveData: LiveData) -> Closeable { + return UITextFieldBindingKt.bindFocus( self, liveData: liveData ) } - func bindFocus(liveData: MutableLiveData) { - UITextFieldBindingKt.bindFocusTwoWay( + @available(*, deprecated, message: "use MutableLiveData.bindBoolTwoWayToControlFocus") + @discardableResult func bindFocus(liveData: MutableLiveData) -> Closeable { + return UITextFieldBindingKt.bindFocusTwoWay( self, liveData: liveData ) diff --git a/mvvm-livedata/src/iosMain/swift/Core/UITextView+LiveData.swift b/mvvm-livedata/src/iosMain/swift/Core/UITextView+LiveData.swift index b694abf9..ee03e201 100644 --- a/mvvm-livedata/src/iosMain/swift/Core/UITextView+LiveData.swift +++ b/mvvm-livedata/src/iosMain/swift/Core/UITextView+LiveData.swift @@ -6,16 +6,18 @@ import UIKit import MultiPlatformLibrary public extension UITextView { - func bindText(liveData: LiveData) { - UITextViewBindingKt.bindText( + @available(*, deprecated, message: "use LiveData.bindStringDescToTextViewText") + @discardableResult func bindText(liveData: LiveData) -> Closeable { + return UITextViewBindingKt.bindText( self, liveData: liveData, formatter: nil ) } - func bindText(liveData: LiveData) { - UITextViewBindingKt.bindText( + @available(*, deprecated, message: "use LiveData.bindStringToTextViewText") + @discardableResult func bindText(liveData: LiveData) -> Closeable { + return UITextViewBindingKt.bindText( self, liveData: liveData, formatter_: nil diff --git a/mvvm-livedata/src/iosMain/swift/Core/UIView+LiveData.swift b/mvvm-livedata/src/iosMain/swift/Core/UIView+LiveData.swift index e47d2770..b7d99005 100644 --- a/mvvm-livedata/src/iosMain/swift/Core/UIView+LiveData.swift +++ b/mvvm-livedata/src/iosMain/swift/Core/UIView+LiveData.swift @@ -6,11 +6,12 @@ import UIKit import MultiPlatformLibrary public extension UIView { - func bindVisibility( + @available(*, deprecated, message: "use LiveData.bindBoolToViewHidden") + @discardableResult func bindVisibility( liveData: LiveData, inverted: Bool = false - ) { - UIViewBindingKt.bindVisibility( + ) -> Closeable { + return UIViewBindingKt.bindVisibility( self, liveData: liveData, inverted: inverted From b757290329fd3cdd2bff65cfde888a31c61a4f15 Mon Sep 17 00:00:00 2001 From: Andrey Dorofeev Date: Fri, 30 Apr 2021 14:23:08 +0700 Subject: [PATCH 2/2] run apiDump --- mvvm-livedata/api/mvvm-livedata.api | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/mvvm-livedata/api/mvvm-livedata.api b/mvvm-livedata/api/mvvm-livedata.api index c332eb27..16d5fdf7 100644 --- a/mvvm-livedata/api/mvvm-livedata.api +++ b/mvvm-livedata/api/mvvm-livedata.api @@ -32,20 +32,12 @@ public final class dev/icerock/moko/mvvm/livedata/EditTextBindingsKt { public static final fun bindTwoWayToEditTextText (Ldev/icerock/moko/mvvm/livedata/MutableLiveData;Landroidx/lifecycle/LifecycleOwner;Landroid/widget/EditText;)Ldev/icerock/moko/mvvm/livedata/Closeable; } -public final class dev/icerock/moko/mvvm/livedata/FlatMapLiveDataKt { - public static final fun flatMap (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function1;)Ldev/icerock/moko/mvvm/livedata/LiveData; -} - public class dev/icerock/moko/mvvm/livedata/LiveData { - public fun (Ljava/lang/Object;)V + public fun (Landroidx/lifecycle/LiveData;)V public final fun addObserver (Lkotlin/jvm/functions/Function1;)V - protected final fun changeValue (Ljava/lang/Object;)V - protected final fun getArchLiveData ()Landroidx/lifecycle/MutableLiveData; + protected final fun getArchLiveData ()Landroidx/lifecycle/LiveData; public fun getValue ()Ljava/lang/Object; - public final fun isActive ()Z public fun ld ()Landroidx/lifecycle/LiveData; - protected fun onActive ()V - protected fun onInactive ()V public final fun removeObserver (Lkotlin/jvm/functions/Function1;)V } @@ -76,20 +68,16 @@ public final class dev/icerock/moko/mvvm/livedata/LiveDataKt { } public final class dev/icerock/moko/mvvm/livedata/LiveDataTransformsKt { + public static final fun flatMap (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function1;)Ldev/icerock/moko/mvvm/livedata/LiveData; public static final fun flatMapBuffered (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function2;)Ldev/icerock/moko/mvvm/livedata/LiveData; + public static final fun map (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function1;)Ldev/icerock/moko/mvvm/livedata/LiveData; public static final fun mapBuffered (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function2;)Ldev/icerock/moko/mvvm/livedata/LiveData; public static final fun mergeWith (Ldev/icerock/moko/mvvm/livedata/LiveData;Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function2;)Ldev/icerock/moko/mvvm/livedata/MediatorLiveData; } -public final class dev/icerock/moko/mvvm/livedata/MapLiveDataKt { - public static final fun map (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function1;)Ldev/icerock/moko/mvvm/livedata/LiveData; -} - public class dev/icerock/moko/mvvm/livedata/MediatorLiveData : dev/icerock/moko/mvvm/livedata/MutableLiveData { public fun (Ljava/lang/Object;)V public final fun addSource (Ldev/icerock/moko/mvvm/livedata/LiveData;Lkotlin/jvm/functions/Function1;)V - protected fun onActive ()V - protected fun onInactive ()V } public final class dev/icerock/moko/mvvm/livedata/MediatorLiveDataExtKt { @@ -101,6 +89,7 @@ public final class dev/icerock/moko/mvvm/livedata/MediatorLiveDataExtKt { } public class dev/icerock/moko/mvvm/livedata/MutableLiveData : dev/icerock/moko/mvvm/livedata/LiveData { + public fun (Landroidx/lifecycle/MutableLiveData;)V public fun (Ljava/lang/Object;)V public fun getValue ()Ljava/lang/Object; public synthetic fun ld ()Landroidx/lifecycle/LiveData;