Merge pull request #126 from TouchInstinct/feature/rx-extensions

Added extension 'unwrapOrError' to main RX classes typed with Optional
This commit is contained in:
Даниил Борисовский 2020-04-28 21:53:48 +03:00 committed by GitHub
commit aa65ba93af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 70 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,22 @@ 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))
}
fun <T> Flowable<Optional<T>>.unwrapOrFilter(): Flowable<T> = this
.filter { it.get() != null }
.map { it.get() }

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,22 @@ 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))
}
fun <T> Observable<Optional<T>>.unwrapOrFilter(): Observable<T> = this
.filter { it.get() != null }
.map { it.get() }

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"
}