0

I need to handle multiple button click within short period of time in a way such that I need to get number of click that the user clicked within 500ms to reduce the number of API calls I make to the backend.

val buttonStream = view.plusButton.clicks()
buttonStream
        .buffer(buttonStream.debounce(500, TimeUnit.MILLISECONDS))
        .map { it.size }
        .subscribe({ clicks ->
            Log.i(TAG, "Number of clicks: $clicks")
        })

I have implemented the above code but it doesn't display anything when I click the button. When I remove .buffer(buttonStream.debounce(500, TimeUnit.MILLISECONDS)) and just add .buffer(500, TimeUnit.MILLISECONDS) Log starts printing every 500ms. Is there any way to get my work done?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
kokilayaa
  • 579
  • 4
  • 7
  • 19

1 Answers1

0

Managed to get the expected out with the following code.

val buttonStream = view.plusButton.clicks()
        buttonStream
                .buffer(1000, TimeUnit.MILLISECONDS)
                .map { it.size }
                .filter { size -> size > 0 }
                .subscribe({ clicks ->
                    Log.i(TAG, "Number of clicks from the QUICK_ADD: $clicks")
                    uploadWaterIntake(clicks)
                })
kokilayaa
  • 579
  • 4
  • 7
  • 19