Skip to content

Commit 2d09f52

Browse files
committed
Support triggerAfterValidation attr
1 parent b812bf1 commit 2d09f52

File tree

4 files changed

+48
-36
lines changed

4 files changed

+48
-36
lines changed

example/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@
8080
<co.kyash.vtl.ValidatableTextInputLayout
8181
android:id="@+id/email"
8282
style="@style/InputRow"
83-
app:trigger="text_changed">
83+
app:trigger="text_changed"
84+
app:triggerAfterValidation="true">
8485

8586
<EditText
8687
style="@style/BaseEditText"
@@ -200,7 +201,6 @@
200201
android:paddingStart="@dimen/space_16dp"
201202
android:paddingTop="@dimen/space_8dp">
202203

203-
<!-- Email -->
204204
<co.kyash.vtl.ValidatableTextInputLayout
205205
android:id="@+id/colors"
206206
style="@style/InputRow"

library/src/main/java/co/kyash/vtl/ValidatableTextInputLayout.kt

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import io.reactivex.Flowable
1818
import io.reactivex.disposables.CompositeDisposable
1919
import io.reactivex.functions.Consumer
2020
import io.reactivex.internal.functions.Functions
21-
import io.reactivex.processors.FlowableProcessor
2221
import io.reactivex.processors.PublishProcessor
2322
import io.reactivex.schedulers.Schedulers
2423
import java.util.concurrent.TimeUnit
@@ -29,37 +28,42 @@ class ValidatableTextInputLayout @JvmOverloads constructor(
2928
defStyleAttr: Int = 0
3029
) : TextInputLayout(context, attrs, defStyleAttr), ValidatableView {
3130

32-
override val validationFlowables: ArrayList<Flowable<Any>> = ArrayList()
31+
override val validationFlowables = ArrayList<Flowable<Any>>()
3332

3433
companion object {
34+
private val NONE = -1
3535
private val FOCUS_CHANGED = 1
3636
private val TEXT_CHANGED = 1 shl 1
3737
}
3838

39-
private var shouldValidateOnFocusChanged: Boolean = false
40-
private var shouldValidateOnTextChanged: Boolean = false
41-
private var shouldValidateOnTextChangedOnce: Boolean = false
42-
private var validationInterval: Long = 300L
39+
private var shouldValidateOnFocusChanged = false
40+
private var shouldValidateOnTextChanged = false
41+
private var shouldValidateOnTextChangedOnce = false
42+
private var triggerAfterValidation = false
43+
private var validationInterval = 300L
4344

4445
init {
4546
val a = context.theme.obtainStyledAttributes(attrs, R.styleable.ValidatableTextInputLayout, 0, 0)
4647

47-
val trigger = a.getInt(R.styleable.ValidatableTextInputLayout_trigger, FOCUS_CHANGED)
48-
shouldValidateOnFocusChanged = trigger and FOCUS_CHANGED != 0
49-
shouldValidateOnTextChanged = trigger and TEXT_CHANGED != 0
48+
val trigger = a.getInt(R.styleable.ValidatableTextInputLayout_trigger, NONE)
49+
if (trigger > 0) {
50+
shouldValidateOnFocusChanged = trigger and FOCUS_CHANGED != 0
51+
shouldValidateOnTextChanged = trigger and TEXT_CHANGED != 0
52+
}
5053

54+
triggerAfterValidation = a.getBoolean(R.styleable.ValidatableTextInputLayout_triggerAfterValidation, false)
5155
validationInterval = a.getInteger(R.styleable.ValidatableTextInputLayout_interval, 300).toLong()
5256

5357
a.recycle()
5458
}
5559

56-
private val textProcessor: FlowableProcessor<String> = PublishProcessor.create()
60+
private val textProcessor = PublishProcessor.create<String>()
5761

58-
private val compositeDisposable: CompositeDisposable = CompositeDisposable()
62+
private val compositeDisposable = CompositeDisposable()
5963

60-
private val validators: ArrayList<VtlValidator> = ArrayList()
64+
private val validators = ArrayList<VtlValidator>()
6165

62-
private val mainHandler: Handler = HandlerProvider.createMainHandler()
66+
private val mainHandler = HandlerProvider.createMainHandler()
6367

6468
private val textWatcher = object : TextWatcher {
6569
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
@@ -71,30 +75,36 @@ class ValidatableTextInputLayout @JvmOverloads constructor(
7175
}
7276

7377
override fun afterTextChanged(s: Editable) {
74-
textProcessor.onNext(s.toString())
78+
if (!triggerAfterValidation) {
79+
textProcessor.onNext(s.toString())
80+
}
7581
}
7682
}
7783

7884
private val onCustomFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
79-
if (hasFocus) {
80-
if (shouldValidateOnTextChanged || shouldValidateOnTextChangedOnce) {
81-
shouldValidateOnTextChangedOnce = false
82-
compositeDisposable.clear()
83-
compositeDisposable.add(
84-
Flowable.zip(validationFlowables) { Any() }
85-
.doOnError({ this.showErrorMessage(it) })
86-
.retry() // non-terminated stream
87-
.subscribeOn(Schedulers.computation())
88-
.subscribe({ clearErrorMessage() }, {})
89-
)
90-
}
91-
} else {
92-
if (shouldValidateOnFocusChanged) {
93-
compositeDisposable.clear()
94-
compositeDisposable.add(
95-
validateAsCompletable().subscribe(Functions.EMPTY_ACTION, Consumer<Throwable> {})
96-
)
85+
if (!triggerAfterValidation) {
86+
87+
if (hasFocus) {
88+
if (shouldValidateOnTextChanged || shouldValidateOnTextChangedOnce) {
89+
shouldValidateOnTextChangedOnce = false
90+
compositeDisposable.clear()
91+
compositeDisposable.add(
92+
Flowable.zip(validationFlowables) { Any() }
93+
.doOnError({ this.showErrorMessage(it) })
94+
.retry() // non-terminated stream
95+
.subscribeOn(Schedulers.computation())
96+
.subscribe({ clearErrorMessage() }, {})
97+
)
98+
}
99+
} else {
100+
if (shouldValidateOnFocusChanged) {
101+
compositeDisposable.clear()
102+
compositeDisposable.add(
103+
validateAsCompletable().subscribe(Functions.EMPTY_ACTION, Consumer<Throwable> {})
104+
)
105+
}
97106
}
107+
98108
}
99109
}
100110

@@ -221,6 +231,7 @@ class ValidatableTextInputLayout @JvmOverloads constructor(
221231
mainHandler.post {
222232
error = errorMessage
223233
isErrorEnabled = true
234+
triggerAfterValidation = false
224235
}
225236
}
226237
}

library/src/main/res/values/attrs.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<flag name="focus_changed" value="1" />
77
<flag name="text_changed" value="2" />
88
</attr>
9-
<attr name="enable_floating_label" format="boolean" />
109
<attr name="interval" format="integer" />
10+
<attr name="triggerAfterValidation" format="boolean" />
1111
</declare-styleable>
1212

1313
</resources>

library/src/test/java/co/kyash/vtl/validators/AsciiOnlyValidatorTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class AsciiOnlyValidatorTest(
3535
arrayOf("a", true, null),
3636
arrayOf("-", true, null),
3737
arrayOf("@", true, null),
38-
arrayOf("*", true, null)
38+
arrayOf("*", true, null),
39+
arrayOf("1", true, null)
3940
)
4041
}
4142
}

0 commit comments

Comments
 (0)