Created
July 28, 2020 11:38
-
-
Save arkivanov/b33b33dec0d91f134b1bf6344dc2ec47 to your computer and use it in GitHub Desktop.
TestKittenDataSource
This file contains hidden or 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
| internal class TestKittenDataSource( | |
| private val scheduler: TestScheduler | |
| ) : KittenDataSource { | |
| private var images by AtomicReference<List<String>?>(null) | |
| private var seed by AtomicInt() | |
| override fun load(limit: Int, page: Int): Maybe<String> = | |
| singleFromFunction { images } | |
| .notNull() | |
| .map { | |
| val offset = page * limit | |
| it.subList(fromIndex = offset, toIndex = offset + limit) | |
| } | |
| .mapIterable { it.toJsonObject() } | |
| .map { JsonArray(it).toString() } | |
| .onErrorComplete() | |
| .observeOn(scheduler) | |
| private fun String.toJsonObject(): JsonObject = | |
| JsonObject(mapOf("url" to JsonPrimitive(this))) | |
| fun generateImages(): List<String> { | |
| val images = List(MAX_IMAGES) { "Img${seed + it}" } | |
| this.images = images | |
| seed += MAX_IMAGES | |
| return images | |
| } | |
| private companion object { | |
| private const val MAX_IMAGES = 50 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment