1

I need to show error view if status is error and call.isEmpty() returns true.

Is there a better way to achieve this result?

val errorVisible = states.asFlowable()
    .map { it.status == Status.ERROR }
    .flatMap { isError ->
        if (isError) call.isEmpty()
        else Flowable.just(false)
    }

call.isEmpty() returns Flowable<Boolean> but can return something else like Single<Boolean>.

EDIT: Another question, if call.isEmpty() returns Flowable<Boolean> how can I merge two streams (two Flowable<Boolean>s - states.isError and call.isEmpty()) and map it to one Flowable<Boolean> so it's one condition?

alashow
  • 2,735
  • 3
  • 21
  • 47
  • well, you don't need to use `map` and put both conditions into `flatMap` if this is what you're asking – martin Sep 02 '18 at 19:53
  • @martin Yes, I can skip `map` in this case. But I was looking for a different, possibly better way of doing the same thing by changing `flatMap`. My end goal is to return `Flowable` when it's `isError` and `call.isEmpty()`. – alashow Sep 02 '18 at 19:58

3 Answers3

3

You can break the condition

.flatMap {
  if (it.isEmpty()) {
    Observable.empty()
  } else {
    Observable.just(true)
  }
}
tharinduPro
  • 99
  • 1
  • 6
1

Maybe you can use something like this

val errorVisible = states.asFlowable()
        .onErrorReturn { true }
        .flatMap { Flowable.just(it.status == Status.ERROR) }
0

You could use a when or if block:

val errorVisible = states.asFlowable()
    .flatMap {
        when(it.status) {
            Status.ERROR -> call.isEmpty()
            else -> Flowable.just(false)
        }
    }
ESala
  • 6,878
  • 4
  • 34
  • 55
  • I was looking more of an RX way of doing this if there is any. BTW, I also added a new question. – alashow Sep 02 '18 at 20:52