Skip to content

Commit

Permalink
update api
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex committed Aug 21, 2020
1 parent a496783 commit 1650635
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 50 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
min_sdk_version = 17
sdk_version = 29

kotlin_version = '1.3.72'
kotlin_version = '1.4.0'
rxjava_version = '3.0.5'
rxandroid_version = '3.0.0'
archx_version = '2.2.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.disposables.Disposable

typealias OnVisibleChangeAction = (isVisible: Boolean) -> Unit

abstract class BaseVisualControl<T>(
initialValue: T
initialValue: T,
initialEnabled: Boolean,
initialVisibility: Visibility
) : BaseControl() {

enum class Visibility(val value: Int) {
VISIBLE(View.VISIBLE),
INVISIBLE(View.INVISIBLE),
GONE(View.GONE)
}

val value = state(initialValue)
val isEnabled = state(true)
val isVisible = state(true)
val enabled = state(initialEnabled)
val visibility = state(initialVisibility)

val actionChangeValue = action<T>()

Expand All @@ -38,25 +44,24 @@ abstract class BaseVisualControl<T>(

fun defaultBindTo(
view: View,
invisibleState: Int = View.GONE,
onVisibleChange: OnVisibleChangeAction? = null
bindEnable: Boolean,
bindVisible: Boolean
): Disposable = CompositeDisposable().apply {
add(
isEnabled
.toViewFlowable()
.subscribe { view.isEnabled = it }
)
if (bindEnable) {
add(
enabled
.toViewFlowable()
.subscribe { view.isEnabled = it }
)
}

add(
isVisible
.toViewFlowable()
.subscribe {
when {
onVisibleChange != null -> onVisibleChange.invoke(it)
else -> view.visibility = if (it) View.VISIBLE else invisibleState
}
}
)
if (bindVisible) {
add(
visibility
.toViewFlowable()
.subscribe { view.visibility = it.value }
)
}
}

protected open fun transformObservable(observable: Observable<T>): Observable<T> = observable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,29 @@ import io.reactivex.rxjava3.disposables.Disposable

@SuppressLint("CheckResult")
class CheckControl internal constructor(
initialChecked: Boolean
) : BaseVisualControl<Boolean>(initialChecked)
initialChecked: Boolean,
initialEnabled: Boolean,
initialVisibility: Visibility
) : BaseVisualControl<Boolean>(initialChecked, initialEnabled, initialVisibility)

fun checkControl(initialChecked: Boolean = false): CheckControl = CheckControl(initialChecked)
fun checkControl(
initialChecked: Boolean = false,
initialEnabled: Boolean = true,
initialVisibility: BaseVisualControl.Visibility = BaseVisualControl.Visibility.VISIBLE
): CheckControl = CheckControl(
initialChecked = initialChecked,
initialEnabled = initialEnabled,
initialVisibility = initialVisibility
)

fun CheckControl.bindTo(
compoundButton: CompoundButton,
invisibleState: Int = View.GONE,
onVisibleChange: OnVisibleChangeAction? = null
bindEnable: Boolean = true,
bindVisible: Boolean = true
): Disposable {
var editing = false
return CompositeDisposable().apply {
add(defaultBindTo(compoundButton, invisibleState, onVisibleChange))
add(defaultBindTo(compoundButton, bindEnable, bindVisible))
add(
value
.toViewFlowable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ typealias FormatterAction = (text: String) -> String
class InputControl internal constructor(
initialText: String,
private val hideErrorOnUserInput: Boolean,
private val formatter: FormatterAction?
) : BaseVisualControl<String>(initialText) {
private val formatter: FormatterAction?,
initialEnabled: Boolean,
initialVisibility: Visibility
) : BaseVisualControl<String>(initialText, initialEnabled, initialVisibility) {

val error = state<String>()

Expand All @@ -37,18 +39,26 @@ class InputControl internal constructor(
fun inputControl(
initialText: String = "",
hideErrorOnUserInput: Boolean = true,
formatter: FormatterAction? = null
): InputControl = InputControl(initialText, hideErrorOnUserInput, formatter)
formatter: FormatterAction? = null,
initialEnabled: Boolean = true,
initialVisibility: BaseVisualControl.Visibility = BaseVisualControl.Visibility.VISIBLE
): InputControl = InputControl(
initialText = initialText,
hideErrorOnUserInput = hideErrorOnUserInput,
formatter = formatter,
initialEnabled = initialEnabled,
initialVisibility = initialVisibility
)

fun InputControl.bindTo(
editText: EditText,
useError: Boolean = false,
invisibleState: Int = View.GONE,
onVisibleChange: OnVisibleChangeAction? = null
bindError: Boolean = false,
bindEnable: Boolean = true,
bindVisible: Boolean = true
): Disposable {
var editing = false
return CompositeDisposable().apply {
add(defaultBindTo(editText, invisibleState, onVisibleChange))
add(defaultBindTo(editText, bindEnable, bindVisible))
add(
value
.toViewFlowable()
Expand All @@ -68,7 +78,7 @@ fun InputControl.bindTo(
}
)

if (useError) {
if (bindError) {
add(
error
.toViewFlowable()
Expand All @@ -88,12 +98,12 @@ fun InputControl.bindTo(

fun InputControl.bindTo(
textInputLayout: TextInputLayout,
useError: Boolean = false,
invisibleState: Int = View.GONE,
onVisibleChange: OnVisibleChangeAction? = null
bindError: Boolean = false,
bindEnable: Boolean = true,
bindVisible: Boolean = true
): Disposable = CompositeDisposable().apply {
add(bindTo(textInputLayout.editText!!, false, invisibleState, onVisibleChange))
if (useError) {
add(bindTo(textInputLayout.editText!!, false, bindEnable, bindVisible))
if (bindError) {
add(
error
.observable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
package com.alexdeww.reactiveviewmodel.widget

import android.annotation.SuppressLint
import android.view.View
import android.widget.RatingBar
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.disposables.Disposable

@SuppressLint("CheckResult")
class RatingControl internal constructor(
initialValue: Float
) : BaseVisualControl<Float>(initialValue)
initialValue: Float,
initialEnabled: Boolean,
initialVisibility: Visibility
) : BaseVisualControl<Float>(initialValue, initialEnabled, initialVisibility)

fun ratingControl(initialValue: Float = 0f): RatingControl = RatingControl(initialValue)
fun ratingControl(
initialValue: Float = 0f,
initialEnabled: Boolean = true,
initialVisibility: BaseVisualControl.Visibility = BaseVisualControl.Visibility.VISIBLE
): RatingControl = RatingControl(
initialValue = initialValue,
initialEnabled = initialEnabled,
initialVisibility = initialVisibility
)

fun RatingControl.bindTo(
ratingBar: RatingBar,
invisibleState: Int = View.GONE,
onVisibleChange: OnVisibleChangeAction? = null
bindEnable: Boolean = true,
bindVisible: Boolean = true
): Disposable {
var editing = false
return CompositeDisposable().apply {
add(defaultBindTo(ratingBar, invisibleState, onVisibleChange))
add(defaultBindTo(ratingBar, bindEnable, bindVisible))
add(
value
.toViewFlowable()
Expand Down

0 comments on commit 1650635

Please sign in to comment.