Last active
August 5, 2018 21:23
-
-
Save ShaishavGandhi/f1329ae9f43d87fcb43ba56b2bcf1910 to your computer and use it in GitHub Desktop.
With a hot observable
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
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