使用 MVP 模式下载照片

Downloading photo with MVP pattern

我正在尝试通过在 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)
}

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

试试这个:

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