Stop button spam with RxJava + Kotlin

Image for post
Image for post
A typical user in their natural environment

The problem

The virgin redditor solution

Image for post
Image for post

What you will need

Dependencies

implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.2'
implementation 'com.jakewharton.rxbinding3:rxbinding-material:3.0.0-alpha2'

Writing something that works in a few lines

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
private val compositeDisposableOnPause = CompositeDisposable()
override fun onPause() {
compositeDisposableOnPause.clear()
super.onPause()
}
override fun onResume() {
super.onResume()
compositeDisposableOnPause.add(button.clicks()
.throttleFirst(10, TimeUnit.SECONDS)
.subscribe {
Toast.makeText(this@MainActivity,"BOO",Toast.LENGTH_LONG).show()
}
)
}
Image for post
Image for post

At least explain something!

button.clicks()
compositeDisposableOnPause.add(button.clicks()
compositeDisposableOnPause.clear()
.throttleFirst(10, TimeUnit.SECONDS)
.subscribe {
Toast.makeText(this@MainActivity,"BOO",Toast.LENGTH_LONG).show()
}

Incoherent rambling

Android dude @JustEatUK

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store