Skip to content

Instantly share code, notes, and snippets.

@ShaishavGandhi
Created August 5, 2018 21:34
Show Gist options
  • Save ShaishavGandhi/94945ba907dd66944b803d2793d08ee2 to your computer and use it in GitHub Desktop.
Save ShaishavGandhi/94945ba907dd66944b803d2793d08ee2 to your computer and use it in GitHub Desktop.
With replaying share
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 com.jakewharton.rx.replayingShare
import io.reactivex.Observable
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.PublishSubject
class ImageRepository(private val resources: Resources) {
companion object {
private const val TAG = "ImageRepository"
}
private val subject = PublishSubject.create<Int>()
private val imageObservable = subject
.map {
Log.d(TAG, "Performing expensive operation")
return@map BitmapFactory.decodeStream(resources.openRawResource(it))
}.replayingShare()
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