Reputation: 29
I have a code snippet using Rxjava and I have written it using subscribing inside a subscription. How can I use the only one subscribe using maps or flatmaps?
amount.textChanges.observable.map {
when {
(it.isNotEmpty() && it.toBigDecimal() > BigDecimal(1500)) -> showDialog(R.string.max_amount_error)
(it.isNotEmpty() && it.toBigDecimal() > BigDecimal.ZERO) -> {
getCommissionUseCase.execute(it.toInt() * 100).subscribe { payment ->
commission.accept((payment.data.toDouble() / 100).toString())
}.untilDestroy()
}
(it.isEmpty()) -> nextBtnEnabled.consumer.accept(false)
}
}.subscribe()
Upvotes: 2
Views: 437
Reputation: 1692
amount.textChanges.observable
.flatMapCompletable {
when {
(it.isNotEmpty() && it.toBigDecimal() > BigDecimal(1500)) -> Completable.fromAction { showDialog(R.string.max_amount_error) }
(it.isNotEmpty() && it.toBigDecimal() > BigDecimal.ZERO) -> getCommissionUseCase.execute(it.toInt() * 100)
.doOnSuccess { payment ->
commission.accept((payment.data.toDouble() / 100).toString())
}
.ignoreElement()
(it.isEmpty()) -> Completable.fromAction { nextBtnEnabled.consumer.accept(false) }
}
}.subscribe()
You can use doOnNext
, ignoreElements
if you're using Observable
instead of the doOnSuccess
, ignoreElement
, which is only available on Single
Upvotes: 1