Skip to content

Instantly share code, notes, and snippets.

@ShaishavGandhi
Last active August 5, 2018 21:23
Show Gist options
  • Save ShaishavGandhi/f1329ae9f43d87fcb43ba56b2bcf1910 to your computer and use it in GitHub Desktop.
Save ShaishavGandhi/f1329ae9f43d87fcb43ba56b2bcf1910 to your computer and use it in GitHub Desktop.
With a hot observable
package com.shaishavgandhi.rxreplayingsharesample
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Log
import androidx.annotation.RawRes
import io.reactivex.Observable
import io.reactivex.subjects.BehaviorSubject
class ImageRepository(private val resources: Resources) {
companion object {
private const val TAG = "ImageRepository"
}
private val subject = BehaviorSubject.create<Int>()
private val imageObservable = subject
.map {
// We log to make sure we know we're not repeating expensive operations.
Log.d(TAG, "Performing expensive operation")
return@map BitmapFactory.decodeStream(resources.openRawResource(it))
}
fun image(): Observable<Bitmap> {
return imageObservable
}
fun loadImage(@RawRes imageId: Int) {
subject.onNext(imageId)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment