androidrx-java2throttlingrx-binding

synchronise two views with RxBinding


I use RxJava's throttleFirst for one second to avoid rapid button clicks, however I also want that on a button click a second button be also disabled for a second and vice versa, So, in short I do not allow rapid clicks on those two buttons one after another... What would you recommend? Can I achieve this using RxBinding?

RxView.clicks(button)
                .throttleFirst(1000, TimeUnit.MILLISECONDS)
                .subscribe {
                    doSomething()
                }

Solution

  • here's one possible approach...

    1. create a stream for each button
    2. you'll likely need to know which button was clicked, so map the output of each stream to some known value that will be handled in the subscriber
    3. merge the two streams together and throttleFirst that merged stream

    that would all look something like this:

    RxView.clicks(button1).map { "button1" }
            .mergeWith(RxView.clicks(button2).map { "button2" })
            .throttleFirst(1, TimeUnit.SECONDS)
            .subscribeBy(
                    onNext = { value ->
                        if(value == "button1") {
                            // handle button1 click
                        } else {
                            // handle button2 click
                        }
                    },
                    onError = {
                        ...
                    }
            )
    

    maybe not the most elegant, but hopefully it at least inspires some thought!