1+ package com.enginebai.base.extensions
2+
3+ import android.text.Editable
4+ import android.text.InputFilter
5+ import android.text.TextWatcher
6+ import android.text.method.HideReturnsTransformationMethod
7+ import android.text.method.PasswordTransformationMethod
8+ import android.widget.EditText
9+ import io.reactivex.Observable
10+ import io.reactivex.Single
11+ import io.reactivex.subjects.PublishSubject
12+
13+ fun EditText.textChanged (listener : (String ) -> Unit ): TextWatcher {
14+ val textWatcher = object : TextWatcher {
15+ override fun afterTextChanged (s : Editable ? ) {
16+ listener.invoke(s?.toString() ? : " " )
17+ }
18+
19+ override fun beforeTextChanged (s : CharSequence? , start : Int , count : Int , after : Int ) {}
20+ override fun onTextChanged (s : CharSequence? , start : Int , before : Int , count : Int ) {}
21+ }
22+ this .addTextChangedListener(textWatcher)
23+ return textWatcher
24+ }
25+
26+ fun EditText.textChanged (): Observable <out CharSequence > {
27+ val source: PublishSubject <String > = PublishSubject .create()
28+ val textWatcher = TextChangeWatcher (source)
29+ addTextChangedListener(textWatcher)
30+ return source.doOnDispose {
31+ textWatcher.unsubscribe()
32+ removeTextChangedListener(textWatcher)
33+ }
34+ }
35+
36+ private class TextChangeWatcher (private var publisher : PublishSubject <String >? = null ) :
37+ TextWatcher {
38+
39+ override fun afterTextChanged (s : Editable ? ) {
40+ publisher?.onNext(s?.toString() ? : " " )
41+ }
42+
43+ override fun beforeTextChanged (s : CharSequence? , start : Int , count : Int , after : Int ) {}
44+ override fun onTextChanged (s : CharSequence? , start : Int , before : Int , count : Int ) {}
45+
46+ fun unsubscribe () {
47+ publisher = null
48+ }
49+ }
50+
51+
52+ fun EditText.validate (errorMessage : String , validator : (String ) -> Boolean ): TextWatcher {
53+ fun showErrorIfInvalid (s : String ) {
54+ this .error = if (validator(s)) null else errorMessage
55+ }
56+ // validate original text
57+ showErrorIfInvalid(this .text.toString())
58+ // validate changed text
59+ return this .textChanged { showErrorIfInvalid(it) }
60+ }
61+
62+ fun EditText.validateEmail (errorMessage : String ): TextWatcher {
63+ return validate(errorMessage) { it.isValidEmail() }
64+ }
65+
66+ fun EditText.validateEmail (): Single <Boolean > {
67+ return Single .fromObservable(this .textChanged()).flatMap {
68+ Single .just(it.toString().isValidEmail())
69+ }
70+ }
71+
72+ fun EditText.showPassword () {
73+ transformationMethod = HideReturnsTransformationMethod .getInstance()
74+ selectAll()
75+ }
76+
77+ fun EditText.hidePassword () {
78+ transformationMethod = PasswordTransformationMethod .getInstance()
79+ selectAll()
80+ }
0 commit comments