Skip to content

Commit

Permalink
update input control bind
Browse files Browse the repository at this point in the history
add check control saved sate delegate
  • Loading branch information
Alex committed Dec 8, 2022
1 parent 06c6819 commit c8ac0e9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion reactiveviewmodel/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ afterEvaluate {
release(MavenPublication) {
from components.release
groupId = 'com.alexdeww.reactiveviewmodel'
version = '2.5.0'
version = '2.5.1'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,33 @@ fun <T : Any> SavedStateHandle.displayableControl(
control
}

fun SavedStateHandle.checkControl(
initialChecked: Boolean = false,
initialEnabled: Boolean = true,
initialVisibility: BaseVisualControl.Visibility = BaseVisualControl.Visibility.VISIBLE
): ReadOnlyProperty<ReactiveViewModel, CheckControl> = 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<T>(
private val savedStateHandle: SavedStateHandle,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -50,6 +51,7 @@ fun InputControl.bindTo(
bindVisible: Boolean = true
) = bindTo(
rvmViewComponent = rvmViewComponent,
view = editText,
editText = editText,
actionOnError = { editText.error = it },
bindError = bindError,
Expand All @@ -65,6 +67,7 @@ fun InputControl.bindTo(
bindVisible: Boolean = true
) = bindTo(
rvmViewComponent = rvmViewComponent,
view = textInputLayout,
editText = textInputLayout.editText!!,
actionOnError = { textInputLayout.error = it },
bindError = bindError,
Expand All @@ -74,6 +77,7 @@ fun InputControl.bindTo(

internal fun InputControl.bindTo(
rvmViewComponent: RvmViewComponent,
view: View,
editText: EditText,
actionOnError: (String) -> Unit,
bindError: Boolean = false,
Expand All @@ -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 ->
Expand Down

0 comments on commit c8ac0e9

Please sign in to comment.