Created
July 13, 2016 04:43
-
-
Save bulwinkel/fc9316a8379260cd6a9ff3fb0138daa3 to your computer and use it in GitHub Desktop.
Generates Bitmap Observables with Picasso instance
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
class PicassoImageObservable( | |
private val picasso: Picasso | |
) : Func1<String, Observable<Bitmap>> { | |
override fun call(url: String): Observable<Bitmap> { | |
return Observable.fromAsync({ emitter -> | |
val target = object : Target { | |
override fun onPrepareLoad(placeHolderDrawable: Drawable?) { | |
//do nothing | |
} | |
override fun onBitmapFailed(errorDrawable: Drawable?) { | |
// our use case does not have an error drawable | |
emitter.onCompleted() | |
} | |
override fun onBitmapLoaded(bitmap: Bitmap, from: LoadedFrom) { | |
emitter.onNext(bitmap) | |
emitter.onCompleted() | |
} | |
} | |
picasso.load(url).into(target) | |
// cancel the request if the end user unsubscribes | |
emitter.setCancellation { picasso.cancelRequest(target) } | |
}, NONE) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment