Skip to content

Commit e52981b

Browse files
author
Alex
committed
update InputControl
1 parent 2616a1a commit e52981b

File tree

6 files changed

+18
-18
lines changed

6 files changed

+18
-18
lines changed

reactiveviewmodel/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ afterEvaluate {
4444
release(MavenPublication) {
4545
from components.release
4646
groupId = 'com.alexdeww.reactiveviewmodel'
47-
version = '2.3.7'
47+
version = '2.3.8'
4848
}
4949
}
5050
}

reactiveviewmodel/src/main/java/com/alexdeww/reactiveviewmodel/component/ReactiveActivity.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.alexdeww.reactiveviewmodel.component
22

3+
import androidx.annotation.CallSuper
34
import androidx.appcompat.app.AppCompatActivity
45
import androidx.lifecycle.LifecycleOwner
56
import com.alexdeww.reactiveviewmodel.core.RvmViewComponent
@@ -13,12 +14,14 @@ abstract class ReactiveActivity : AppCompatActivity(), RvmViewComponent {
1314
override val componentLifecycleOwner: LifecycleOwner
1415
get() = this
1516

17+
@CallSuper
1618
override fun onStop() {
1719
disposableOnStopList.values.forEach { it.dispose() }
1820
disposableOnStopList.clear()
1921
super.onStop()
2022
}
2123

24+
@CallSuper
2225
override fun onDestroy() {
2326
disposableOnDestroyList.values.forEach { it.dispose() }
2427
disposableOnDestroyList.clear()

reactiveviewmodel/src/main/java/com/alexdeww/reactiveviewmodel/component/ReactiveFragment.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.alexdeww.reactiveviewmodel.component
22

3+
import androidx.annotation.CallSuper
34
import androidx.fragment.app.Fragment
45
import androidx.lifecycle.LifecycleOwner
56
import com.alexdeww.reactiveviewmodel.core.RvmViewComponent
@@ -14,18 +15,21 @@ abstract class ReactiveFragment : Fragment(), RvmViewComponent {
1415
override val componentLifecycleOwner: LifecycleOwner
1516
get() = viewLifecycleOwner
1617

18+
@CallSuper
1719
override fun onStop() {
1820
disposableOnStopList.values.forEach { it.dispose() }
1921
disposableOnStopList.clear()
2022
super.onStop()
2123
}
2224

25+
@CallSuper
2326
override fun onDestroyView() {
2427
disposableOnDestroyViewList.values.forEach { it.dispose() }
2528
disposableOnDestroyViewList.clear()
2629
super.onDestroyView()
2730
}
2831

32+
@CallSuper
2933
override fun onDestroy() {
3034
disposableOnDestroyList.values.forEach { it.dispose() }
3135
disposableOnDestroyList.clear()

reactiveviewmodel/src/main/java/com/alexdeww/reactiveviewmodel/core/property/State.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import io.reactivex.rxjava3.core.Flowable
66
import io.reactivex.rxjava3.core.Observable
77
import io.reactivex.rxjava3.functions.Consumer
88
import io.reactivex.rxjava3.subjects.BehaviorSubject
9-
import java.util.concurrent.TimeUnit
109

1110
class State<T : Any> internal constructor(
1211
initValue: T? = null,
@@ -19,7 +18,10 @@ class State<T : Any> internal constructor(
1918
}
2019
private val serializedSubject = subject.toSerialized()
2120

22-
internal val consumer: Consumer<T> = Consumer { serializedSubject.onNext(it) }
21+
internal var valueChangesHook: ((value: T) -> T)? = null
22+
internal val consumer: Consumer<T> = Consumer { newValue ->
23+
serializedSubject.onNext(valueChangesHook?.invoke(newValue) ?: newValue)
24+
}
2325
internal val observable: Observable<T> = serializedSubject.letDebounce(debounceInterval)
2426

2527
val value: T? get() = subject.value

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import android.view.View
44
import androidx.annotation.CallSuper
55
import androidx.lifecycle.MediatorLiveData
66
import com.alexdeww.reactiveviewmodel.core.RvmViewComponent
7-
import io.reactivex.rxjava3.core.Observable
87
import io.reactivex.rxjava3.functions.Consumer
98
import java.lang.ref.WeakReference
109

@@ -29,12 +28,9 @@ abstract class BaseVisualControl<T : Any>(
2928
init {
3029
actionChangeValue.observable
3130
.filter { it != value.value }
32-
.let { transformObservable(it) }
3331
.subscribe(::onChangedValue)
3432
}
3533

36-
protected open fun transformObservable(observable: Observable<T>): Observable<T> = observable
37-
3834
@CallSuper
3935
protected open fun onChangedValue(newValue: T) {
4036
value.consumer.accept(newValue)

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

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

3-
import android.annotation.SuppressLint
43
import android.text.*
54
import android.widget.EditText
65
import com.alexdeww.reactiveviewmodel.core.RvmViewComponent
76
import com.google.android.material.textfield.TextInputLayout
8-
import io.reactivex.rxjava3.core.Observable
97

108
typealias FormatterAction = (text: String) -> String
119

12-
@SuppressLint("CheckResult")
1310
class InputControl internal constructor(
1411
initialText: String,
1512
private val hideErrorOnUserInput: Boolean,
16-
private val formatter: FormatterAction?,
13+
formatter: FormatterAction?,
1714
initialEnabled: Boolean,
1815
initialVisibility: Visibility
1916
) : BaseVisualControl<String>(initialText, initialEnabled, initialVisibility) {
2017

21-
val error = state<String>()
22-
23-
override fun transformObservable(
24-
observable: Observable<String>
25-
): Observable<String> = observable.map { s ->
26-
formatter?.let { it(s) } ?: s
18+
init {
19+
value.valueChangesHook = formatter
2720
}
2821

22+
val error = state<String>()
23+
2924
override fun onChangedValue(newValue: String) {
30-
super.onChangedValue(newValue)
3125
if (hideErrorOnUserInput) error.consumer.accept("")
26+
super.onChangedValue(newValue)
3227
}
3328

3429
}

0 commit comments

Comments
 (0)