Created
February 6, 2019 09:09
-
-
Save hleinone/0a9720013b13ab20b5a901ccc8bee01a to your computer and use it in GitHub Desktop.
Android AlertDialog Rx extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.DialogInterface | |
import androidx.appcompat.app.AlertDialog | |
import com.jakewharton.rxbinding2.view.clicks | |
import io.reactivex.Maybe | |
import io.reactivex.Single | |
/** | |
* Sets the dialog non-cancelable. Emits either {@link DialogInterface#BUTTON_POSITIVE}, | |
* {@link DialogInterface#BUTTON_NEGATIVE} or {@link DialogInterface#BUTTON_NEUTRAL} and dismisses | |
* the dialog. | |
*/ | |
@androidx.annotation.CheckResult | |
fun AlertDialog.clicks(): Single<Int> { | |
setCancelable(false) | |
return Maybe.merge( | |
getButton(DialogInterface.BUTTON_POSITIVE).clicks().firstElement().map { DialogInterface.BUTTON_POSITIVE }, | |
getButton(DialogInterface.BUTTON_NEGATIVE).clicks().firstElement().map { DialogInterface.BUTTON_NEGATIVE }, | |
getButton(DialogInterface.BUTTON_NEUTRAL).clicks().firstElement().map { DialogInterface.BUTTON_NEUTRAL }) | |
.firstOrError() | |
.doOnSuccess { | |
dismiss() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment