Created
August 5, 2018 21:34
-
-
Save ShaishavGandhi/94945ba907dd66944b803d2793d08ee2 to your computer and use it in GitHub Desktop.
With replaying share
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 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