Skip to content

Instantly share code, notes, and snippets.

@bulwinkel
Created July 13, 2016 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bulwinkel/fc9316a8379260cd6a9ff3fb0138daa3 to your computer and use it in GitHub Desktop.
Save bulwinkel/fc9316a8379260cd6a9ff3fb0138daa3 to your computer and use it in GitHub Desktop.
Generates Bitmap Observables with Picasso instance
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