更新时间:2023-12-04 09:30:28
You can use publish()
and connect()
.
val email = RxTextView.textChanges(emailEditText)
val emailIsValid = email.map { charSequence ->
Log.d("asdf", "emailIsValid call: " + charSequence)
Pattern.matches(Patterns.EMAIL_ADDRESS.pattern(), charSequence)
}.publish()
RxView.focusChanges(emailEditText)
.withLatestFrom(emailIsValid) { hasFocus, emailIsValid ->
(!hasFocus && !emailIsValid)
}
.subscribe { showError ->
if (showError) {
Log.d("asdf", "error")
}
}
val password = RxTextView.textChanges(passwordEditText)
Observable.combineLatest(emailIsValid, password) { emailIsValid, password ->
Log.d("asdf", "valid: $emailIsValid, password: $password")
(emailIsValid && password.length > 0)
}.subscribe(RxView.enabled(button))
emailIsValid.connect()
Or just switch the order of your subscribe
because it causes the issue.
val email = RxTextView.textChanges(emailEditText)
val emailIsValid = email.map { charSequence ->
Log.d("asdf", "emailIsValid call: " + charSequence)
Pattern.matches(Patterns.EMAIL_ADDRESS.pattern(), charSequence)
}.share()
val password = RxTextView.textChanges(passwordEditText)
Observable.combineLatest(emailIsValid, password) { emailIsValid, password ->
Log.d("asdf", "valid: $emailIsValid, password: $password")
(emailIsValid && password.length > 0)
}.subscribe(RxView.enabled(button))
RxView.focusChanges(emailEditText)
.withLatestFrom(emailIsValid) { hasFocus, emailIsValid ->
(!hasFocus && !emailIsValid)
}
.subscribe { showError ->
if (showError) {
Log.d("asdf", "error")
}
}
Note: code is in Kotlin and more info about publish/connect is at http://www.introtorx.com/content/v1.0.10621.0/14_HotAndColdObservables.html#PublishAndConnect
Your problem is similar to what mention there in PublishAndConnect section.:
The second subscription subscribes late and misses the first publication. We could move the invocation of the Connect() method until after all subscriptions have been made. That way, even with the call to Thread.Sleep we will not really subscribe to the underlying until after both subscriptions are made.