Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MVICore RxJava3 update #198

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion binder/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tasks.withType<Test> {
}

dependencies {
api(libs.rxjava2)
api(libs.rxjava3)
implementation(libs.rxkotlin)
implementation(libs.kotlin.stdlib)

Expand Down
2 changes: 1 addition & 1 deletion binder/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<CurrentIssues>
<ID>MaxLineLength:Connection.kt$infix</ID>
<ID>MaxLineLength:ManualLifecycle.kt$ManualLifecycle$class</ID>
<ID>ReturnCount:Consumer.kt$fun &lt;In> Consumer&lt;In>.wrapWithMiddleware( standalone: Boolean = true, name: String? = null, postfix: String? = null, wrapperOf: Any? = null ): Consumer&lt;In></ID>
<ID>ReturnCount:Consumer.kt$fun &lt;In : Any> Consumer&lt;In>.wrapWithMiddleware( standalone: Boolean = true, name: String? = null, postfix: String? = null, wrapperOf: Any? = null ): Consumer&lt;In></ID>
<ID>TooManyFunctions:Binder.kt$Binder : Disposable</ID>
<ID>UseCheckOrError:StandaloneMiddleware.kt$StandaloneMiddleware$throw IllegalStateException("Middleware was initialised in standalone mode, can't accept other connections")</ID>
</CurrentIssues>
Expand Down
39 changes: 22 additions & 17 deletions binder/src/main/java/com/badoo/binder/Binder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import com.badoo.binder.lifecycle.Lifecycle.Event.BEGIN
import com.badoo.binder.lifecycle.Lifecycle.Event.END
import com.badoo.binder.middleware.base.Middleware
import com.badoo.binder.middleware.wrapWithMiddleware
import io.reactivex.Observable
import io.reactivex.Observable.wrap
import io.reactivex.ObservableSource
import io.reactivex.Scheduler
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Consumer
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Observable.wrap
import io.reactivex.rxjava3.core.ObservableSource
import io.reactivex.rxjava3.core.Scheduler
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.functions.Consumer
import io.reactivex.rxjava3.kotlin.plusAssign

class Binder(
private val lifecycle: Lifecycle? = null,
Expand Down Expand Up @@ -56,27 +56,32 @@ class Binder(
subscribeWithLifecycle(connection, middleware)
}
}

else -> subscribe(connection, middleware)
}
}

private fun <Out, In> subscribeWithLifecycle(
private fun <Out : Any, In : Any> subscribeWithLifecycle(
connection: Connection<Out, In>,
middleware: Middleware<Out, In>?
) {
connectionDisposables += wrap(connection.from)
.subscribeWithMiddleware(connection, middleware)
connection.from?.let {
connectionDisposables += wrap(it)
.subscribeWithMiddleware(connection, middleware)
}
}

private fun <Out, In> subscribe(
private fun <Out : Any, In : Any> subscribe(
connection: Connection<Out, In>,
middleware: Middleware<Out, In>?
) {
disposables += wrap(connection.from)
.subscribeWithMiddleware(connection, middleware)
connection.from?.let {
disposables += wrap(it)
.subscribeWithMiddleware(connection, middleware)
}
}

private fun <Out, In> Observable<out Out>.subscribeWithMiddleware(
private fun <Out : Any, In : Any> Observable<out Out>.subscribeWithMiddleware(
connection: Connection<Out, In>,
middleware: Middleware<Out, In>?
): Disposable =
Expand All @@ -97,7 +102,7 @@ class Binder(
}
}

private fun <Out, In> Observable<out Out>.applyTransformer(
private fun <Out : Any, In : Any> Observable<out Out>.applyTransformer(
connection: Connection<Out, In>
): Observable<In> =
connection.connector?.let {
Expand Down Expand Up @@ -153,7 +158,7 @@ class Binder(
disposables.clear()
}

private fun <T> Observable<T>.optionalObserveOn(scheduler: Scheduler?) =
private fun <T : Any> Observable<T>.optionalObserveOn(scheduler: Scheduler?) =
if (scheduler != null) {
observeOn(scheduler)
} else {
Expand Down
20 changes: 10 additions & 10 deletions binder/src/main/java/com/badoo/binder/Connection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package com.badoo.binder

import com.badoo.binder.connector.Connector
import com.badoo.binder.connector.NotNullConnector
import io.reactivex.ObservableSource
import io.reactivex.Scheduler
import io.reactivex.functions.Consumer
import io.reactivex.rxjava3.core.ObservableSource
import io.reactivex.rxjava3.core.Scheduler
import io.reactivex.rxjava3.functions.Consumer

data class Connection<Out, In>(
data class Connection<Out : Any, In : Any>(
val from: ObservableSource<out Out>? = null,
val to: Consumer<in In>,
val connector: Connector<Out, In>? = null,
Expand All @@ -24,36 +24,36 @@ data class Connection<Out, In>(
"<${name ?: ANONYMOUS}> (${from ?: "?"} --> $to${connector?.let { " using $it" } ?: ""})"
}

infix fun <Out, In> Pair<ObservableSource<out Out>, Consumer<in In>>.using(transformer: (Out) -> In?): Connection<Out, In> =
infix fun <Out : Any, In : Any> Pair<ObservableSource<out Out>, Consumer<in In>>.using(transformer: (Out) -> In?): Connection<Out, In> =
using(NotNullConnector(transformer))

infix fun <Out, In> Pair<ObservableSource<out Out>, Consumer<in In>>.using(connector: Connector<Out, In>): Connection<Out, In> =
infix fun <Out : Any, In : Any> Pair<ObservableSource<out Out>, Consumer<in In>>.using(connector: Connector<Out, In>): Connection<Out, In> =
Connection(
from = first,
to = second,
connector = connector
)

infix fun <T> Pair<ObservableSource<out T>, Consumer<in T>>.named(name: String): Connection<T, T> =
infix fun <T : Any> Pair<ObservableSource<out T>, Consumer<in T>>.named(name: String): Connection<T, T> =
Connection(
from = first,
to = second,
name = name
)

infix fun <T> Pair<ObservableSource<out T>, Consumer<in T>>.observeOn(scheduler: Scheduler): Connection<T, T> =
infix fun <T : Any> Pair<ObservableSource<out T>, Consumer<in T>>.observeOn(scheduler: Scheduler): Connection<T, T> =
Connection(
from = first,
to = second,
observeScheduler = scheduler
)

infix fun <Out, In> Connection<Out, In>.named(name: String) =
infix fun <Out : Any, In : Any> Connection<Out, In>.named(name: String) =
copy(
name = name
)

infix fun <Out, In> Connection<Out, In>.observeOn(scheduler: Scheduler) =
infix fun <Out : Any, In : Any> Connection<Out, In>.observeOn(scheduler: Scheduler) =
copy(
observeScheduler = scheduler
)
4 changes: 2 additions & 2 deletions binder/src/main/java/com/badoo/binder/connector/Connector.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.badoo.binder.connector

import io.reactivex.ObservableSource
import io.reactivex.rxjava3.core.ObservableSource

interface Connector<Out, In>: (ObservableSource<out Out>) -> ObservableSource<In>
interface Connector<Out : Any, In : Any>: (ObservableSource<out Out>) -> ObservableSource<In>
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.badoo.binder.connector

import io.reactivex.Observable
import io.reactivex.Observable.wrap
import io.reactivex.ObservableSource
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Observable.wrap
import io.reactivex.rxjava3.core.ObservableSource

internal class NotNullConnector<Out, In>(private val mapper: (Out) -> In?): Connector<Out, In> {
internal class NotNullConnector<Out : Any, In : Any>(private val mapper: (Out) -> In?): Connector<Out, In> {
override fun invoke(element: ObservableSource<out Out>): ObservableSource<In> =
wrap(element)
.flatMap {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.badoo.binder.lifecycle

import com.badoo.binder.lifecycle.internal.FromObservableSource
import io.reactivex.ObservableSource
import io.reactivex.rxjava3.core.ObservableSource

interface Lifecycle : ObservableSource<Lifecycle.Event> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.badoo.binder.lifecycle
import com.badoo.binder.lifecycle.Lifecycle.Event
import com.badoo.binder.lifecycle.Lifecycle.Event.BEGIN
import com.badoo.binder.lifecycle.Lifecycle.Event.END
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.rxjava3.subjects.BehaviorSubject

class ManualLifecycle(private val subject : BehaviorSubject<Event> = BehaviorSubject.create()) : Lifecycle by Lifecycle.wrap(subject) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.badoo.binder.lifecycle.internal

import com.badoo.binder.lifecycle.Lifecycle
import io.reactivex.ObservableSource
import io.reactivex.rxjava3.core.ObservableSource

internal class FromObservableSource(
source: ObservableSource<Lifecycle.Event>
Expand Down
4 changes: 2 additions & 2 deletions binder/src/main/java/com/badoo/binder/middleware/Consumer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.badoo.binder.middleware.base.Middleware
import com.badoo.binder.middleware.base.StandaloneMiddleware
import com.badoo.binder.middleware.config.Middlewares
import com.badoo.binder.middleware.config.NonWrappable
import io.reactivex.functions.Consumer
import io.reactivex.rxjava3.functions.Consumer

/**
* Wraps a Consumer<T> with Middlewares. The list of Middlewares that will be applied is resolved
Expand All @@ -19,7 +19,7 @@ import io.reactivex.functions.Consumer
* @param postfix Passed on to [ConsumerMiddleware], in most cases you shouldn't need to override this.
* @param wrapperOf Passed on to [ConsumerMiddleware], in most cases you shouldn't need to override this.
*/
fun <In> Consumer<In>.wrapWithMiddleware(
fun <In : Any> Consumer<In>.wrapWithMiddleware(
Fixed Show fixed Hide fixed
standalone: Boolean = true,
name: String? = null,
postfix: String? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.badoo.binder.middleware.base

import com.badoo.binder.Connection
import io.reactivex.functions.Consumer
import io.reactivex.rxjava3.functions.Consumer

abstract class Middleware<Out, In>(
abstract class Middleware<Out : Any, In : Any>(
protected val wrapped: Consumer<In>
): Consumer<In> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.badoo.binder.middleware.base

import com.badoo.binder.Connection
import io.reactivex.disposables.Disposable
import io.reactivex.rxjava3.disposables.Disposable

internal class StandaloneMiddleware<In>(
internal class StandaloneMiddleware<In : Any>(
private val wrappedMiddleware: Middleware<In, In>,
name: String? = null,
postfix: String? = null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.badoo.binder.middleware.config

import com.badoo.binder.middleware.base.Middleware
import io.reactivex.functions.Consumer
import io.reactivex.rxjava3.functions.Consumer

typealias ConsumerMiddlewareFactory<T> = (Consumer<T>) -> Middleware<Any, T>
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.badoo.binder.middleware.config

import com.badoo.binder.middleware.base.Middleware
import io.reactivex.functions.Consumer
import io.reactivex.rxjava3.functions.Consumer

data class MiddlewareConfiguration(
private val condition: WrappingCondition,
private val factories: List<ConsumerMiddlewareFactory<*>>
) {

fun <T> applyOn(
fun <T : Any> applyOn(
consumerToWrap: Consumer<T>,
targetToCheck: Any,
name: String?,
Expand Down
8 changes: 4 additions & 4 deletions binder/src/test/java/com/badoo/binder/BinderTest.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.badoo.binder

import com.badoo.binder.connector.Connector
import io.reactivex.Observable
import io.reactivex.ObservableSource
import io.reactivex.subjects.PublishSubject
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.ObservableSource
import io.reactivex.rxjava3.subjects.PublishSubject
import org.junit.jupiter.api.Test

class BinderTest {
Expand Down Expand Up @@ -36,7 +36,7 @@ class BinderTest {

@Test
fun `covariant endpoints compile for connection dsl`() {
val anyConsumer = TestConsumer<Any?>()
val anyConsumer = TestConsumer<Any>()
binder.bind(source to anyConsumer using IntToString)
}

Expand Down
6 changes: 3 additions & 3 deletions binder/src/test/java/com/badoo/binder/LifecycleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.badoo.binder

import com.badoo.binder.lifecycle.Lifecycle
import com.badoo.binder.lifecycle.ManualLifecycle
import io.reactivex.ObservableSource
import io.reactivex.functions.Consumer
import io.reactivex.subjects.PublishSubject
import io.reactivex.rxjava3.core.ObservableSource
import io.reactivex.rxjava3.functions.Consumer
import io.reactivex.rxjava3.subjects.PublishSubject
import org.junit.jupiter.api.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
Expand Down
6 changes: 3 additions & 3 deletions binder/src/test/java/com/badoo/binder/TestConsumer.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.badoo.binder

import io.reactivex.functions.Consumer
import io.reactivex.rxjava3.functions.Consumer
import org.junit.jupiter.api.Assertions.assertEquals

class TestConsumer<T> : Consumer<T> {
class TestConsumer<T : Any> : Consumer<T> {
val values = mutableListOf<T>()

override fun accept(item: T) {
values.add(item)
}
}

fun <T> TestConsumer<T>.assertValues(vararg values: T) =
fun <T : Any> TestConsumer<T>.assertValues(vararg values: T) =
assertEquals(values.toList(), this.values)
16 changes: 8 additions & 8 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ androidMaterialVersion = "1.4.0"
constraintLayoutVersion = "2.1.0"

# Rx
rxJavaVersion = "2.2.21"
rxKotlinVersion = "2.4.0"
rxAndroidVersion = "2.1.1"
rxJavaVersion = "3.1.9"
rxKotlinVersion = "3.0.1"
rxAndroidVersion = "3.0.2"

# DI
daggerVersion = "2.46.1"
Expand All @@ -25,7 +25,7 @@ scalpelVersion = "1.1.2"
glideVersion = "4.13.2"

# Testing
retrofitVersion = "2.4.0"
retrofitVersion = "2.11.0"
gsonVersion = "2.10.1"

# Testing
Expand All @@ -46,9 +46,9 @@ androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayo
google-material = { module = "com.google.android.material:material", version.ref = "androidMaterialVersion" }

# Rx
rxjava2 = { module = "io.reactivex.rxjava2:rxjava", version.ref = "rxJavaVersion" }
rxkotlin = { module = "io.reactivex.rxjava2:rxkotlin", version.ref = "rxKotlinVersion" }
rxandroid = { module = "io.reactivex.rxjava2:rxandroid", version.ref = "rxAndroidVersion" }
rxjava3 = { module = "io.reactivex.rxjava3:rxjava", version.ref = "rxJavaVersion" }
rxkotlin = { module = "io.reactivex.rxjava3:rxkotlin", version.ref = "rxKotlinVersion" }
rxandroid = { module = "io.reactivex.rxjava3:rxandroid", version.ref = "rxAndroidVersion" }

# DI
dagger-runtime = { module = "com.google.dagger:dagger", version.ref = "daggerVersion" }
Expand All @@ -75,7 +75,7 @@ glide-compiler = { module = "com.github.bumptech.glide:compiler", version.ref =

# Network
retrofit-runtime = { module = "com.squareup.retrofit2:retrofit", version.ref = "retrofitVersion" }
retrofit-adapter-rxjava2 = { module = "com.squareup.retrofit2:adapter-rxjava2", version.ref = "retrofitVersion" }
retrofit-adapter-rxjava3 = { module = "com.squareup.retrofit2:adapter-rxjava3", version.ref = "retrofitVersion" }
retrofit-converter-simplexml = { module = "com.squareup.retrofit2:converter-simplexml", version.ref = "retrofitVersion" }
gson = { module = "com.google.code.gson:gson", version.ref = "gsonVersion" }

Expand Down
2 changes: 1 addition & 1 deletion mvicore-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ dependencies {
api(project(":mvicore"))
api(project(":binder"))
api(libs.androidx.lifecycle.common)
api(libs.rxjava2)
api(libs.rxjava3)

implementation(libs.kotlin.stdlib)
implementation(libs.rxandroid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.badoo.mvicore.android

import android.os.Looper
import com.badoo.mvicore.feature.FeatureScheduler
import io.reactivex.Scheduler
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Scheduler
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers

/**
* A feature scheduler that ensures that MVICore feature only manipulates state on the Android
Expand Down
Loading