Created
January 22, 2018 12:16
-
-
Save watabee/a7f8efef2d070206456d7e0375f5e091 to your computer and use it in GitHub Desktop.
This file contains 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.Context | |
import android.support.annotation.MainThread | |
import android.support.v7.app.AlertDialog | |
import io.reactivex.Observable | |
enum class RxDialogActionStyle { | |
POSITIVE, NEGATIVE, NEUTRAL | |
} | |
data class RxDialogAction(val title: CharSequence, val style: RxDialogActionStyle) | |
enum class RxDialogResult { | |
POSITIVE, NEGATIVE, NEUTRAL, CANCEL | |
} | |
@MainThread | |
fun showAlertDialog(context: Context, title: CharSequence?, message: CharSequence, actions: Iterable<RxDialogAction>, cancellable: Boolean = true): Observable<RxDialogResult> { | |
return Observable.create<RxDialogResult> { emitter -> | |
val builder = AlertDialog.Builder(context) | |
builder.setTitle(title) | |
builder.setMessage(message) | |
builder.setCancelable(cancellable) | |
if (cancellable) { | |
builder.setOnCancelListener { | |
emitter.onNext(RxDialogResult.CANCEL) | |
emitter.onComplete() | |
} | |
} | |
actions.forEach { action -> | |
when (action.style) { | |
RxDialogActionStyle.POSITIVE -> { | |
builder.setPositiveButton(action.title, { _, _ -> | |
emitter.onNext(RxDialogResult.POSITIVE) | |
emitter.onComplete() | |
}) | |
} | |
RxDialogActionStyle.NEGATIVE -> { | |
builder.setNegativeButton(action.title, { _, _ -> | |
emitter.onNext(RxDialogResult.NEGATIVE) | |
emitter.onComplete() | |
}) | |
} | |
RxDialogActionStyle.NEUTRAL -> { | |
builder.setNeutralButton(action.title, { _, _ -> | |
emitter.onNext(RxDialogResult.NEUTRAL) | |
emitter.onComplete() | |
}) | |
} | |
} | |
} | |
val dialog = builder.create() | |
dialog.show() | |
emitter.setCancellable { | |
dialog.dismiss() | |
} | |
} | |
} |
This file contains 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.os.Bundle | |
import android.support.v7.app.AppCompatActivity | |
import android.widget.Toast | |
import com.jakewharton.rxbinding2.view.RxView | |
import io.reactivex.disposables.CompositeDisposable | |
import io.reactivex.disposables.Disposable | |
class SampleActivity : AppCompatActivity() { | |
private val compositeDisposable = CompositeDisposable() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
RxView.clicks(findViewById(R.id.button)) | |
.flatMap { | |
val positive = RxDialogAction("OK", RxDialogActionStyle.POSITIVE) | |
val negative = RxDialogAction("Cancel", RxDialogActionStyle.NEGATIVE) | |
showAlertDialog(this@SampleActivity, "title", "message", listOf(positive, negative)) | |
}.subscribe { result -> | |
when (result) { | |
RxDialogResult.POSITIVE -> Toast.makeText(this@SampleActivity, "Positive", Toast.LENGTH_SHORT).show() | |
RxDialogResult.NEGATIVE -> Toast.makeText(this@SampleActivity, "Negative", Toast.LENGTH_SHORT).show() | |
RxDialogResult.CANCEL -> Toast.makeText(this@SampleActivity, "Cancel", Toast.LENGTH_SHORT).show() | |
else -> throw IllegalStateException() | |
} | |
}.addTo(compositeDisposable) | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
compositeDisposable.clear() | |
} | |
private fun Disposable.addTo(compositeDisposable: CompositeDisposable) = | |
compositeDisposable.add(this) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment