From c8ac0e9c6117e0ac973293bd1b413d3ea7c21999 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 8 Dec 2022 22:01:05 +0700 Subject: [PATCH] update input control bind add check control saved sate delegate --- reactiveviewmodel/build.gradle | 2 +- .../core/RvmSavedStateDelegates.kt | 27 +++++++++++++++++++ .../reactiveviewmodel/widget/InputControl.kt | 6 ++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/reactiveviewmodel/build.gradle b/reactiveviewmodel/build.gradle index 3bd95c5..0b195ff 100644 --- a/reactiveviewmodel/build.gradle +++ b/reactiveviewmodel/build.gradle @@ -45,7 +45,7 @@ afterEvaluate { release(MavenPublication) { from components.release groupId = 'com.alexdeww.reactiveviewmodel' - version = '2.5.0' + version = '2.5.1' } } } diff --git a/reactiveviewmodel/src/main/java/com/alexdeww/reactiveviewmodel/core/RvmSavedStateDelegates.kt b/reactiveviewmodel/src/main/java/com/alexdeww/reactiveviewmodel/core/RvmSavedStateDelegates.kt index b2f7c6a..a1e9389 100644 --- a/reactiveviewmodel/src/main/java/com/alexdeww/reactiveviewmodel/core/RvmSavedStateDelegates.kt +++ b/reactiveviewmodel/src/main/java/com/alexdeww/reactiveviewmodel/core/RvmSavedStateDelegates.kt @@ -110,6 +110,33 @@ fun SavedStateHandle.displayableControl( control } +fun SavedStateHandle.checkControl( + initialChecked: Boolean = false, + initialEnabled: Boolean = true, + initialVisibility: BaseVisualControl.Visibility = BaseVisualControl.Visibility.VISIBLE +): ReadOnlyProperty = delegate { thisRef, stateHandle, key -> + val checkedKey = "$key.checked" + val enabledKey = "$key.enabled" + val visibilityKey = "$key.visibility" + val control = CheckControl( + initialChecked = stateHandle[checkedKey] ?: initialChecked, + initialEnabled = stateHandle[enabledKey] ?: initialEnabled, + initialVisibility = stateHandle[visibilityKey] ?: initialVisibility + ) + thisRef.run { + control.value.viewFlowable + .subscribe { stateHandle[checkedKey] = it } + .disposeOnCleared() + control.enabled.viewFlowable + .subscribe { stateHandle[enabledKey] = it } + .disposeOnCleared() + control.visibility.viewFlowable + .subscribe { stateHandle[visibilityKey] = it } + .disposeOnCleared() + } + control +} + @PublishedApi internal class SavedStateProperty( private val savedStateHandle: SavedStateHandle, diff --git a/reactiveviewmodel/src/main/java/com/alexdeww/reactiveviewmodel/widget/InputControl.kt b/reactiveviewmodel/src/main/java/com/alexdeww/reactiveviewmodel/widget/InputControl.kt index 983271f..8b2b636 100644 --- a/reactiveviewmodel/src/main/java/com/alexdeww/reactiveviewmodel/widget/InputControl.kt +++ b/reactiveviewmodel/src/main/java/com/alexdeww/reactiveviewmodel/widget/InputControl.kt @@ -1,6 +1,7 @@ package com.alexdeww.reactiveviewmodel.widget import android.text.* +import android.view.View import android.widget.EditText import com.alexdeww.reactiveviewmodel.core.RvmViewComponent import com.google.android.material.textfield.TextInputLayout @@ -50,6 +51,7 @@ fun InputControl.bindTo( bindVisible: Boolean = true ) = bindTo( rvmViewComponent = rvmViewComponent, + view = editText, editText = editText, actionOnError = { editText.error = it }, bindError = bindError, @@ -65,6 +67,7 @@ fun InputControl.bindTo( bindVisible: Boolean = true ) = bindTo( rvmViewComponent = rvmViewComponent, + view = textInputLayout, editText = textInputLayout.editText!!, actionOnError = { textInputLayout.error = it }, bindError = bindError, @@ -74,6 +77,7 @@ fun InputControl.bindTo( internal fun InputControl.bindTo( rvmViewComponent: RvmViewComponent, + view: View, editText: EditText, actionOnError: (String) -> Unit, bindError: Boolean = false, @@ -83,7 +87,7 @@ internal fun InputControl.bindTo( var textWatcher: TextWatcher? = null baseBindTo( rvmViewComponent = rvmViewComponent, - view = editText, + view = view, bindEnable = bindEnable, bindVisible = bindVisible, onValueChanged = { newValue ->