1
1
package com.enginebai.base.extensions
2
2
3
3
import android.text.Editable
4
- import android.text.InputFilter
5
4
import android.text.TextWatcher
6
5
import android.text.method.HideReturnsTransformationMethod
7
6
import android.text.method.PasswordTransformationMethod
@@ -16,41 +15,41 @@ import io.reactivex.subjects.PublishSubject
16
15
* @return TextWatcher remember to remove when no longer use it anymore.
17
16
*/
18
17
fun 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
+ }
23
22
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
29
28
}
30
29
31
30
fun 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
+ }
39
38
}
40
39
41
40
private class TextChangeWatcher (private var publisher : PublishSubject <String >? = null ) :
42
- TextWatcher {
41
+ TextWatcher {
43
42
44
- override fun afterTextChanged (s : Editable ? ) {
45
- publisher?.onNext(s?.toString() ? : " " )
46
- }
43
+ override fun afterTextChanged (s : Editable ? ) {
44
+ publisher?.onNext(s?.toString() ? : " " )
45
+ }
47
46
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 ) {}
50
49
51
- fun unsubscribe () {
52
- publisher = null
53
- }
50
+ fun unsubscribe () {
51
+ publisher = null
52
+ }
54
53
}
55
54
56
55
/* *
@@ -60,49 +59,52 @@ private class TextChangeWatcher(private var publisher: PublishSubject<String>? =
60
59
* @return TextWatcher remember to remove when no longer use it anymore.
61
60
*/
62
61
fun 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) }
70
69
}
71
70
72
71
/* *
73
72
* Validate if input is valid email, and invoke either valid or invalid listener.
74
73
* @return TextWatcher remember to remove when no longer use it anymore.
75
74
*/
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
+ }
81
83
}
82
84
83
85
/* *
84
86
* Validate if input is valid email, and display error if invalid. * @return TextWatcher remember to remove when no longer use it anymore.
85
87
* @return TextWatcher remember to remove when no longer use it anymore.
86
88
*/
87
89
fun EditText.validateEmail (errorMessage : String ): TextWatcher {
88
- return validate(errorMessage) { it.isValidEmail() }
90
+ return validate(errorMessage) { it.isValidEmail() }
89
91
}
90
92
91
93
/* *
92
94
* Validate if input is valid email and return result as stream.
93
95
*/
94
96
fun 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
+ }
98
100
}
99
101
100
102
fun EditText.showPassword () {
101
- transformationMethod = HideReturnsTransformationMethod .getInstance()
102
- selectAll()
103
+ transformationMethod = HideReturnsTransformationMethod .getInstance()
104
+ selectAll()
103
105
}
104
106
105
107
fun EditText.hidePassword () {
106
- transformationMethod = PasswordTransformationMethod .getInstance()
107
- selectAll()
108
+ transformationMethod = PasswordTransformationMethod .getInstance()
109
+ selectAll()
108
110
}
0 commit comments