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

Introduced ability to override AndroidMainThreadFeatureScheduler via plugin #194

Open
wants to merge 1 commit 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
([#193](https://github.com/badoo/MVICore/pull/193)):
Updated Kotlin to 1.8.10

([#194](https://github.com/badoo/MVICore/pull/194)):
Introduced the ability to override `AndroidMainThreadFeatureScheduler` via `MviCoreAndroidPlugins` (similar to `RxAndroidPlugins` within RxAndroid).

### 1.4.0

#### Additions
Expand Down
1 change: 1 addition & 0 deletions mvicore-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ android {
}

testOptions {
unitTests.isReturnDefaultValues = true
unitTests.all {
it.useJUnitPlatform()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,29 @@ import io.reactivex.android.schedulers.AndroidSchedulers
*
* It also uses the 'isOnFeatureThread' field to avoid observing on the main thread if it is already
* the current thread.
*
* To help facilitate testing, you can override the scheduler using
* [MviCoreAndroidPlugins.setMainThreadFeatureScheduler]
*/
object AndroidMainThreadFeatureScheduler: FeatureScheduler {
object AndroidMainThreadFeatureScheduler : FeatureScheduler {
private val featureSchedulerDelegate: FeatureScheduler
get() = MviCoreAndroidPlugins.mainThreadFeatureScheduler

override val scheduler: Scheduler
get() = AndroidSchedulers.mainThread()
get() = featureSchedulerDelegate.scheduler

override val isOnFeatureThread: Boolean
get() = Looper.myLooper() == Looper.getMainLooper()
get() = featureSchedulerDelegate.isOnFeatureThread

/**
* The default implementation of the [AndroidMainThreadFeatureScheduler] which delegates to the
* RxAndroid main thread scheduler
*/
object Default : FeatureScheduler {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hide? We have reset() for reset. Otherwise not clear what to use: AndroidMainThreadFeatureScheduler or AndroidMainThreadFeatureScheduler.Default.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems reasonable

override val scheduler: Scheduler
get() = AndroidSchedulers.mainThread()

override val isOnFeatureThread: Boolean
get() = Looper.myLooper() == Looper.getMainLooper()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.badoo.mvicore.android

import com.badoo.mvicore.feature.FeatureScheduler

/**
* Allows customisation of the MVICore Android integration.
*/
object MviCoreAndroidPlugins {
@Volatile
var mainThreadFeatureScheduler: FeatureScheduler = AndroidMainThreadFeatureScheduler.Default

/**
* Overrides the [AndroidMainThreadFeatureScheduler].
*/
fun setMainThreadFeatureScheduler(schedulerProvider: () -> FeatureScheduler) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the point of using provider if you invoke it instantly?
Wanted to make it lazy?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RxJava uses these as functions. I assume this is because some users may want to do some lazy initialisation.
It thought it would be wise to leave a similar option for users.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because in RxJava these providers are actually lazy and invoked only when we invoke Schedulers.io() (or other getter).
In our case we just create a lamda and instantly invoke it.

mainThreadFeatureScheduler = schedulerProvider()
}

/**
* Resets the plugins back to the original state.
*/
fun reset() {
mainThreadFeatureScheduler = AndroidMainThreadFeatureScheduler.Default
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.badoo.mvicore.android.lifecycle

import com.badoo.mvicore.android.AndroidMainThreadFeatureScheduler
import com.badoo.mvicore.android.MviCoreAndroidPlugins
import com.badoo.mvicore.feature.FeatureSchedulers
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertSame
import org.junit.jupiter.api.Test

class AndroidMainThreadFeatureSchedulerTest {
@AfterEach
fun after() {
MviCoreAndroidPlugins.reset()
}

@Test
fun `GIVEN android main scheduler not overridden WHEN scheduler accessed THEN scheduler is android main thread scheduler`() {
assertSame(AndroidSchedulers.mainThread(), AndroidMainThreadFeatureScheduler.scheduler)
}

@Test
fun `GIVEN android main scheduler overridden with trampoline feature scheduler WHEN scheduler accessed THEN scheduler is trampoline scheduler`() {
MviCoreAndroidPlugins.setMainThreadFeatureScheduler { FeatureSchedulers.TrampolineFeatureScheduler }

assertSame(Schedulers.trampoline(), AndroidMainThreadFeatureScheduler.scheduler)
}

@Test
fun `GIVEN android main scheduler overridden with trampoline feature scheduler AND reset WHEN scheduler accessed THEN scheduler is android main thread scheduler`() {
MviCoreAndroidPlugins.setMainThreadFeatureScheduler { FeatureSchedulers.TrampolineFeatureScheduler }
MviCoreAndroidPlugins.reset()

assertSame(AndroidSchedulers.mainThread(), AndroidMainThreadFeatureScheduler.scheduler)
}
}