Skip to content

Commit c8ac0e9

Browse files
author
Alex
committed
update input control bind
add check control saved sate delegate
1 parent 06c6819 commit c8ac0e9

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

reactiveviewmodel/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ afterEvaluate {
4545
release(MavenPublication) {
4646
from components.release
4747
groupId = 'com.alexdeww.reactiveviewmodel'
48-
version = '2.5.0'
48+
version = '2.5.1'
4949
}
5050
}
5151
}

reactiveviewmodel/src/main/java/com/alexdeww/reactiveviewmodel/core/RvmSavedStateDelegates.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,33 @@ fun <T : Any> SavedStateHandle.displayableControl(
110110
control
111111
}
112112

113+
fun SavedStateHandle.checkControl(
114+
initialChecked: Boolean = false,
115+
initialEnabled: Boolean = true,
116+
initialVisibility: BaseVisualControl.Visibility = BaseVisualControl.Visibility.VISIBLE
117+
): ReadOnlyProperty<ReactiveViewModel, CheckControl> = delegate { thisRef, stateHandle, key ->
118+
val checkedKey = "$key.checked"
119+
val enabledKey = "$key.enabled"
120+
val visibilityKey = "$key.visibility"
121+
val control = CheckControl(
122+
initialChecked = stateHandle[checkedKey] ?: initialChecked,
123+
initialEnabled = stateHandle[enabledKey] ?: initialEnabled,
124+
initialVisibility = stateHandle[visibilityKey] ?: initialVisibility
125+
)
126+
thisRef.run {
127+
control.value.viewFlowable
128+
.subscribe { stateHandle[checkedKey] = it }
129+
.disposeOnCleared()
130+
control.enabled.viewFlowable
131+
.subscribe { stateHandle[enabledKey] = it }
132+
.disposeOnCleared()
133+
control.visibility.viewFlowable
134+
.subscribe { stateHandle[visibilityKey] = it }
135+
.disposeOnCleared()
136+
}
137+
control
138+
}
139+
113140
@PublishedApi
114141
internal class SavedStateProperty<T>(
115142
private val savedStateHandle: SavedStateHandle,

reactiveviewmodel/src/main/java/com/alexdeww/reactiveviewmodel/widget/InputControl.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.alexdeww.reactiveviewmodel.widget
22

33
import android.text.*
4+
import android.view.View
45
import android.widget.EditText
56
import com.alexdeww.reactiveviewmodel.core.RvmViewComponent
67
import com.google.android.material.textfield.TextInputLayout
@@ -50,6 +51,7 @@ fun InputControl.bindTo(
5051
bindVisible: Boolean = true
5152
) = bindTo(
5253
rvmViewComponent = rvmViewComponent,
54+
view = editText,
5355
editText = editText,
5456
actionOnError = { editText.error = it },
5557
bindError = bindError,
@@ -65,6 +67,7 @@ fun InputControl.bindTo(
6567
bindVisible: Boolean = true
6668
) = bindTo(
6769
rvmViewComponent = rvmViewComponent,
70+
view = textInputLayout,
6871
editText = textInputLayout.editText!!,
6972
actionOnError = { textInputLayout.error = it },
7073
bindError = bindError,
@@ -74,6 +77,7 @@ fun InputControl.bindTo(
7477

7578
internal fun InputControl.bindTo(
7679
rvmViewComponent: RvmViewComponent,
80+
view: View,
7781
editText: EditText,
7882
actionOnError: (String) -> Unit,
7983
bindError: Boolean = false,
@@ -83,7 +87,7 @@ internal fun InputControl.bindTo(
8387
var textWatcher: TextWatcher? = null
8488
baseBindTo(
8589
rvmViewComponent = rvmViewComponent,
86-
view = editText,
90+
view = view,
8791
bindEnable = bindEnable,
8892
bindVisible = bindVisible,
8993
onValueChanged = { newValue ->

0 commit comments

Comments
 (0)