Skip to content

Commit

Permalink
Introduced ability to override AndroidMainThreadFeatureScheduler via …
Browse files Browse the repository at this point in the history
…plugin
  • Loading branch information
LachlanMcKee committed Aug 15, 2023
1 parent cb93baf commit f474107
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
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 {
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) {
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)
}
}

0 comments on commit f474107

Please sign in to comment.