2

我正在使用 RxJava 进行表单验证以验证用户名和密码。如果两个字段都满足各自的条件,那么我的用例非常简单,然后启用登录按钮以禁用它。以下是我的代码。


    lateinit var subscriptions: CompositeDisposable
    private fun validateForm() {
        val emailObservable = viewBinding.detUserName.editText.textChangeEvents()
            .skipInitialValue()
            .map { isValidEmail(it.text) || isValidPhoneNumber(it.text) }
            .doOnDispose {
                Log.i("disposed", "emailObservable")
            }


        val passwordObservable = viewBinding.detPassword.editText.textChangeEvents()
            .skipInitialValue()
            .map { !TextUtils.isEmpty(it.text) }
            .doOnDispose {
                Log.i("disposed", "passwordObservable")
            }

        val disposable = Observable.combineLatest(emailObservable, passwordObservable,
            BiFunction<Boolean, Boolean, Boolean> { t1, t2 -> t1 && t2 }).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                viewBinding.bLogin.isEnabled = it
            }
        subscriptions.add(disposable)
    }

fun isValidEmail(target: CharSequence): Boolean {
        return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches()
    }

    fun isValidPhoneNumber(target: CharSequence): Boolean {
        return !TextUtils.isEmpty(target) && Pattern.compile("(05|9665)[0-9]{8}").matcher(target).matches()
    }

订阅是一个 CompositeDisposable 对象引用,我在 onCreate 和 onResume 中创建(如果未创建或已清除/处置)并将其处置在 onStop()

编辑

以下是我的基本片段的一部分

 override fun onResume() {
        super.onResume()
        createDisposable()
    }

    protected fun createDisposable() {
        if (!this::subscriptions.isInitialized) {
            subscriptions = CompositeDisposable()
        }
    }
   override fun onStop() {
        super.onStop()
        disposeAll()
    }

    protected fun disposeAll() {
        if (this::subscriptions.isInitialized) {
            subscriptions.clear()
        }
    }

我的登录片段

override fun onResume() {
        super.onResume()
        validateForm()
    }

Observable.combineLatest第一次工作正常,但问题是一旦应用程序进入后台并恢复它就不会再次触发。当它进入后台时 onStop() 调用清除所有一次性用品的compositeDisposable处置,当它恢复时,我仍在将一次性用品的新实例重新添加到compositeDisposable,它没有触发。我无法找到解决此问题的方法,任何线索将不胜感激。

4

1 回答 1

0

在您的登录片段中初始化CompositeDisposable并将所有内容添加disposableCompositeDisposable并清除它onPause。好像:

private var subscriptions: CompositeDisposable = CompositeDisposable()

private fun validateForm() {
    val emailObservable = viewBinding.detUserName.editText.textChangeEvents()
        .skipInitialValue()
        .map { isValidEmail(it.text) || isValidPhoneNumber(it.text) }
        .doOnDispose {
            Log.i("disposed", "emailObservable")
        }


    val passwordObservable = viewBinding.detPassword.editText.textChangeEvents()
        .skipInitialValue()
        .map { !TextUtils.isEmpty(it.text) }
        .doOnDispose {
            Log.i("disposed", "passwordObservable")
        }

    val disposable = Observable.combineLatest(emailObservable, passwordObservable,
        BiFunction<Boolean, Boolean, Boolean> { t1, t2 -> t1 && t2 }).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe {
            viewBinding.bLogin.isEnabled = it
        }
    subscriptions.add(emailObservable, passwordObservable, disposable)
}

override fun onResume() {
    super.onResume()
    validateForm()
}

override fun onPause() {
    subscriptions.clear()
    super.onPause()
}
于 2019-08-28T11:44:20.203 回答