11package com.enginebai.base.extensions
22
33import android.text.Editable
4- import android.text.InputFilter
54import android.text.TextWatcher
65import android.text.method.HideReturnsTransformationMethod
76import android.text.method.PasswordTransformationMethod
@@ -16,41 +15,41 @@ import io.reactivex.subjects.PublishSubject
1615 * @return TextWatcher remember to remove when no longer use it anymore.
1716 */
1817fun EditText.textChanged (listener : (String ) -> Unit ): TextWatcher {
19- val textWatcher = object : TextWatcher {
20- override fun afterTextChanged (s : Editable ? ) {
21- listener.invoke(s?.toString() ? : " " )
22- }
18+ val textWatcher = object : TextWatcher {
19+ override fun afterTextChanged (s : Editable ? ) {
20+ listener.invoke(s?.toString() ? : " " )
21+ }
2322
24- override fun beforeTextChanged (s : CharSequence? , start : Int , count : Int , after : Int ) {}
25- override fun onTextChanged (s : CharSequence? , start : Int , before : Int , count : Int ) {}
26- }
27- this .addTextChangedListener(textWatcher)
28- return textWatcher
23+ override fun beforeTextChanged (s : CharSequence? , start : Int , count : Int , after : Int ) {}
24+ override fun onTextChanged (s : CharSequence? , start : Int , before : Int , count : Int ) {}
25+ }
26+ this .addTextChangedListener(textWatcher)
27+ return textWatcher
2928}
3029
3130fun EditText.textChanged (): Observable <out CharSequence > {
32- val source: PublishSubject <String > = PublishSubject .create()
33- val textWatcher = TextChangeWatcher (source)
34- addTextChangedListener(textWatcher)
35- return source.doOnDispose {
36- textWatcher.unsubscribe()
37- removeTextChangedListener(textWatcher)
38- }
31+ val source: PublishSubject <String > = PublishSubject .create()
32+ val textWatcher = TextChangeWatcher (source)
33+ addTextChangedListener(textWatcher)
34+ return source.doOnDispose {
35+ textWatcher.unsubscribe()
36+ removeTextChangedListener(textWatcher)
37+ }
3938}
4039
4140private class TextChangeWatcher (private var publisher : PublishSubject <String >? = null ) :
42- TextWatcher {
41+ TextWatcher {
4342
44- override fun afterTextChanged (s : Editable ? ) {
45- publisher?.onNext(s?.toString() ? : " " )
46- }
43+ override fun afterTextChanged (s : Editable ? ) {
44+ publisher?.onNext(s?.toString() ? : " " )
45+ }
4746
48- override fun beforeTextChanged (s : CharSequence? , start : Int , count : Int , after : Int ) {}
49- override fun onTextChanged (s : CharSequence? , start : Int , before : Int , count : Int ) {}
47+ override fun beforeTextChanged (s : CharSequence? , start : Int , count : Int , after : Int ) {}
48+ override fun onTextChanged (s : CharSequence? , start : Int , before : Int , count : Int ) {}
5049
51- fun unsubscribe () {
52- publisher = null
53- }
50+ fun unsubscribe () {
51+ publisher = null
52+ }
5453}
5554
5655/* *
@@ -60,49 +59,52 @@ private class TextChangeWatcher(private var publisher: PublishSubject<String>? =
6059 * @return TextWatcher remember to remove when no longer use it anymore.
6160 */
6261fun EditText.validate (errorMessage : String , validator : (String ) -> Boolean ): TextWatcher {
63- fun showErrorIfInvalid (s : String ) {
64- this .error = if (validator(s)) null else errorMessage
65- }
66- // validate original text
67- showErrorIfInvalid(this .text.toString())
68- // validate changed text
69- return this .textChanged { showErrorIfInvalid(it) }
62+ fun showErrorIfInvalid (s : String ) {
63+ this .error = if (validator(s)) null else errorMessage
64+ }
65+ // validate original text
66+ showErrorIfInvalid(this .text.toString())
67+ // validate changed text
68+ return this .textChanged { showErrorIfInvalid(it) }
7069}
7170
7271/* *
7372 * Validate if input is valid email, and invoke either valid or invalid listener.
7473 * @return TextWatcher remember to remove when no longer use it anymore.
7574 */
76- fun EditText.validateEmail (validListener : (String ) -> Unit , invalidListener : (String ) -> Unit = {}): TextWatcher {
77- return this .textChanged {
78- if (it.isValidEmail()) validListener(it)
79- else invalidListener(it)
80- }
75+ fun EditText.validateEmail (
76+ validListener : (String ) -> Unit ,
77+ invalidListener : (String ) -> Unit = {}
78+ ): TextWatcher {
79+ return this .textChanged {
80+ if (it.isValidEmail()) validListener(it)
81+ else invalidListener(it)
82+ }
8183}
8284
8385/* *
8486 * Validate if input is valid email, and display error if invalid. * @return TextWatcher remember to remove when no longer use it anymore.
8587 * @return TextWatcher remember to remove when no longer use it anymore.
8688 */
8789fun EditText.validateEmail (errorMessage : String ): TextWatcher {
88- return validate(errorMessage) { it.isValidEmail() }
90+ return validate(errorMessage) { it.isValidEmail() }
8991}
9092
9193/* *
9294 * Validate if input is valid email and return result as stream.
9395 */
9496fun EditText.validateEmail (): Single <Boolean > {
95- return Single .fromObservable(this .textChanged()).flatMap {
96- Single .just(it.toString().isValidEmail())
97- }
97+ return Single .fromObservable(this .textChanged()).flatMap {
98+ Single .just(it.toString().isValidEmail())
99+ }
98100}
99101
100102fun EditText.showPassword () {
101- transformationMethod = HideReturnsTransformationMethod .getInstance()
102- selectAll()
103+ transformationMethod = HideReturnsTransformationMethod .getInstance()
104+ selectAll()
103105}
104106
105107fun EditText.hidePassword () {
106- transformationMethod = PasswordTransformationMethod .getInstance()
107- selectAll()
108+ transformationMethod = PasswordTransformationMethod .getInstance()
109+ selectAll()
108110}
0 commit comments