使用MVP模式下载照片

时间:2019-02-01 18:47:12

标签: java kotlin rx-java2 picasso

我正在尝试通过以mvp模式使用Picasso和rxjava2下载位图。我没有恢复位图,用户照片没有更新。

型号:

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)
        }
    }

演示者

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)})
}

查看(片段)     }

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

1 个答案:

答案 0 :(得分:1)

当您尝试使用url方法时,也许downloadPhoto变量未初始化。

尝试一下:

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) })
}