2

I am using RxJava for form validation for validating username and password. My use-case is pretty simple if both the fields satisfy respective condition then enable the login button else to disable it. following is my code.


    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()
    }

I am subscription is a CompositeDisposable object reference which i am creating in onCreate and onResume(if not created or already cleared/disposed) and disposing it in onStop()

Edit

following is part of my base fragment

 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()
        }
    }

my login fragment

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

For the first time Observable.combineLatest is working fine, but the problem is once the app goes into the background and resumes it's not firing again. when it went into background onStop() calls clear the compositeDisposable disposing of all the disposables, when it resumes I am re-adding new instance of disposables to compositeDisposable still, it's not firing. I am not able to figure out a way to fix this, any leads will be appreciated.

r4jiv007
  • 2,974
  • 3
  • 29
  • 36

1 Answers1

0

Initialize CompositeDisposable in your login fragment and add all disposable to the CompositeDisposable and clear it onPause. Looks like:

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()
}
Abu Noman
  • 435
  • 3
  • 13