Added extension 'unwrapOrError' to main RX classes typed with Optional

This commit is contained in:
Daniil Borisovskii 2020-04-27 13:11:04 +03:00
parent 4597623cbf
commit 175affafb3
6 changed files with 62 additions and 1 deletions

View File

@ -1,6 +1,18 @@
apply plugin: 'kotlin'
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
android {
compileSdkVersion versions.compileSdk
defaultConfig {
minSdkVersion 16
}
}
dependencies {
api project(":utils")
api project(":logging")
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "io.reactivex.rxjava2:rxjava:$versions.rxJava"
}

View File

@ -2,7 +2,18 @@ package ru.touchin.extensions.rx
import io.reactivex.Completable
import io.reactivex.Flowable
import ru.touchin.extensions.rx.utils.StringConstants
import ru.touchin.roboswag.core.utils.Optional
import ru.touchin.roboswag.core.utils.ShouldNotHappenException
fun <T> Flowable<T>.emitAfter(other: Completable): Flowable<T> = this.flatMap { value ->
other.andThen(Flowable.just(value))
}
fun <T> Flowable<Optional<T>>.unwrapOrError(
errorMessage: String = StringConstants.OPTIONAL_UNWRAPPING_ERROR_MESSAGE
): Flowable<T> = this.flatMap { wrapper ->
wrapper.get()
?.let { Flowable.just(it) }
?: Flowable.error(ShouldNotHappenException(errorMessage))
}

View File

@ -2,7 +2,18 @@ package ru.touchin.extensions.rx
import io.reactivex.Completable
import io.reactivex.Maybe
import ru.touchin.extensions.rx.utils.StringConstants
import ru.touchin.roboswag.core.utils.Optional
import ru.touchin.roboswag.core.utils.ShouldNotHappenException
fun <T> Maybe<T>.emitAfter(other: Completable): Maybe<T> = this.flatMap { value ->
other.andThen(Maybe.just(value))
}
fun <T> Maybe<Optional<T>>.unwrapOrError(
errorMessage: String = StringConstants.OPTIONAL_UNWRAPPING_ERROR_MESSAGE
): Maybe<T> = this.flatMap { wrapper ->
wrapper.get()
?.let { Maybe.just(it) }
?: Maybe.error(ShouldNotHappenException(errorMessage))
}

View File

@ -2,7 +2,18 @@ package ru.touchin.extensions.rx
import io.reactivex.Completable
import io.reactivex.Observable
import ru.touchin.extensions.rx.utils.StringConstants
import ru.touchin.roboswag.core.utils.Optional
import ru.touchin.roboswag.core.utils.ShouldNotHappenException
fun <T> Observable<T>.emitAfter(other: Completable): Observable<T> = this.flatMap { value ->
other.andThen(Observable.just(value))
}
fun <T> Observable<Optional<T>>.unwrapOrError(
errorMessage: String = StringConstants.OPTIONAL_UNWRAPPING_ERROR_MESSAGE
): Observable<T> = this.flatMap { wrapper ->
wrapper.get()
?.let { Observable.just(it) }
?: Observable.error(ShouldNotHappenException(errorMessage))
}

View File

@ -2,7 +2,18 @@ package ru.touchin.extensions.rx
import io.reactivex.Completable
import io.reactivex.Single
import ru.touchin.extensions.rx.utils.StringConstants
import ru.touchin.roboswag.core.utils.Optional
import ru.touchin.roboswag.core.utils.ShouldNotHappenException
fun <T> Single<T>.emitAfter(other: Completable): Single<T> = this.flatMap { value ->
other.andThen(Single.just(value))
}
fun <T> Single<Optional<T>>.unwrapOrError(
errorMessage: String = StringConstants.OPTIONAL_UNWRAPPING_ERROR_MESSAGE
): Single<T> = this.flatMap { wrapper ->
wrapper.get()
?.let { Single.just(it) }
?: Single.error(ShouldNotHappenException(errorMessage))
}

View File

@ -0,0 +1,5 @@
package ru.touchin.extensions.rx.utils
object StringConstants {
const val OPTIONAL_UNWRAPPING_ERROR_MESSAGE = "Wrapped object must not be null"
}