Skip to content

Instantly share code, notes, and snippets.

@arkivanov
Created July 28, 2020 11:38
Show Gist options
  • Select an option

  • Save arkivanov/b33b33dec0d91f134b1bf6344dc2ec47 to your computer and use it in GitHub Desktop.

Select an option

Save arkivanov/b33b33dec0d91f134b1bf6344dc2ec47 to your computer and use it in GitHub Desktop.
TestKittenDataSource
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