简体   繁体   中英

Downloading photo with MVP pattern

I'm trying to download bitmap by using Picasso and rxjava2 in mvp pattern. I'm not getting bitmap back, user photo is not updating.

Model:

override fun downloadPhoto(url: String): Single<Bitmap> = Single.create {
        try {
            if (!it.isDisposed) {
                val bitmap: Bitmap = Picasso.get().load(url).get()
                it.onSuccess(bitmap)
            }
        } catch (e: Throwable) {
            it.onError(e)
        }
    }

presenter

override fun getPhoto() {
    auth.rxGetCurrentUser().subscribe { url = it.photoUrl.toString() }
    dashboardFragmentModel.downloadPhoto(url).subscribe ({ it -> v.setUserPhoto(it)},
            {error -> Log.d("Photo download error", error.message)})
}

View(fragment) }

override fun setUserPhoto(bitmap: Bitmap) {
    rootView.userPhoto.setImageBitmap(bitmap)
}

Maybe url variable isn't initialized when you trying to use downloadPhoto method.

Try this:

override fun downloadPhoto(url: String): Observable<Bitmap> = Observable.fromCallable {
    return@fromCallable Picasso.get().load(url).get()
}

override fun getPhoto() {
    auth.rxGetCurrentUser()
            .flatMap {
                val url = it.photoUrl.toString()
                return@flatMap dashboardFragmentModel.downloadPhoto(url)
                              .subscribeOn(Schedulers.io())
            }
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                    { it -> v.setUserPhoto(it) },
                    { error -> Log.d("Photo download error", error.message) })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM