-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced ability to override AndroidMainThreadFeatureScheduler via …
…plugin
- Loading branch information
1 parent
cb93baf
commit f474107
Showing
5 changed files
with
87 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
mvicore-android/src/main/java/com/badoo/mvicore/android/MviCoreAndroidPlugins.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...rc/test/java/com/badoo/mvicore/android/lifecycle/AndroidMainThreadFeatureSchedulerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |