Skip to content

Instantly share code, notes, and snippets.

@Pooh3Mobi
Last active December 23, 2017 11:00
Show Gist options
  • Save Pooh3Mobi/237a9c3a6db01b44bcdc251161cd1a32 to your computer and use it in GitHub Desktop.
Save Pooh3Mobi/237a9c3a6db01b44bcdc251161cd1a32 to your computer and use it in GitHub Desktop.
rx-error-handling-sample
package mobi.pooh3.rxmediaviewer
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import io.reactivex.Observable
import mobi.pooh3.rxmediaviewer.ThumbParam.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
import android.os.Handler
import android.os.Looper
typealias Spec = HashMap<Any, Any>
class MainActivity : AppCompatActivity() {
private val TAG: String = "testtest"
// TODO all ThumbData
// TODO Spec("name", "md5", "uniq-id", "file-type", "mod-time", "thumb-type") -> {(md5)-> ItemData("local-path")} -> LocalThumbData("spec", "path")
// Glide error TODO Spec("name", "md5", "uniq-id", "file-type", "mod-time", "thumb-type") -> {(md5)-> (cache-path)} -> CacheThumbData("spec", "path", "downloaded:false")
// Glide error TODO Spec("name", "md5", "uniq-id", "file-type", "mod-time", "thumb-type") -> {("uniq-id", "md5", "thumb-type") -> Download } -> CacheThumbData("spec", "path", "downloaded:true")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val cacheAbsPath = cacheDir.absolutePath
val spec: Spec = hashMapOf(
NAME to "mockName",
UNIQUE_ID to "mockUID",
MD5 to "mockMd5",
FILE_TYPE to FileType.IMAGE,
THUMB_TYPE to ThumbType.SMALL)
val sLocalCachePath = Observable.just(createCacheLocalPath(cacheAbsPath, spec[FILE_TYPE] as FileType, spec[MD5] as String))
val sDownloadCacheFile = downloadCacheFile(spec[MD5] as String, spec[UNIQUE_ID] as String, spec[THUMB_TYPE] as ThumbType)
val showImage:(String) -> Observable<String> = { path ->showWithGlide(path, spec[FILE_TYPE] as FileType, true) }
val sLocalThumbPath =
Observable.just(spec[MD5] as String)
.map { md5 -> lookupLocalPath(md5) }
.flatMap { path -> showImage(path) }
.flatMapOnError { sLocalCachePath }
.flatMap { path -> showImage(path) }
.flatMapOnError { sDownloadCacheFile }
.doOnError { showErrorImage2(spec[FILE_TYPE] as FileType) }
.onErrorResumeNext { t: Throwable -> Observable.empty<String>() }
.flatMap { path: String -> showWithGlide(path, spec[FILE_TYPE] as FileType, true) }
.doOnError { showErrorImage3(spec[FILE_TYPE] as FileType) }
fab.setOnClickListener { view ->
sLocalThumbPath.subscribe(
{ Log.d(TAG, "success")},
{ e -> e.printStackTrace() }
)
}
}
private fun lookupLocalPath(md5: String):String {
Log.d(TAG, "lookupLocalPath" + Thread.currentThread().name)
return "mock/local/path/$md5"
}
private fun showWithGlide(path: String, fileType: FileType, needCache: Boolean) : Observable<String> {
Log.d(TAG, "showWithGlide" + Thread.currentThread().name)
return Observable.create<String> { emitter ->
Glide.with(this)
.load(path)
.listener(object : RequestListener<Drawable> {
override fun onLoadFailed(e: GlideException?, p1: Any?, p2: Target<Drawable>?, p3: Boolean): Boolean {
Log.d(TAG, "onLoadFailed" + Thread.currentThread().name)
// TODO
Handler(Looper.getMainLooper()).post{
emitter.onError(e ?: Exception("GlideException is null"))
}
return true
}
override fun onResourceReady(p0: Drawable?, p1: Any?, p2: Target<Drawable>?, p3: DataSource?, p4: Boolean): Boolean {
Log.d(TAG, "onResourceReady" + Thread.currentThread().name)
emitter.onNext(path)
emitter.onComplete()
return true
}
})
.into(imageView)
}
}
private fun showErrorImage(fileType: FileType) {
Log.d(TAG, "showErrorImage" + Thread.currentThread().name)
}
private fun showErrorImage2(fileType: FileType) {
Log.d(TAG, "showErrorImage2" + Thread.currentThread().name)
}
private fun showErrorImage3(fileType: FileType) {
Log.d(TAG, "showErrorImage3" + Thread.currentThread().name)
}
private fun createCacheLocalPath(absPath:String, fileType: FileType, md5: String) : String {
Log.d(TAG, "createCacheLocalPath" + Thread.currentThread().name)
return "$absPath/$fileType/$md5"
}
private fun downloadCacheFile(md5: String, uniqId:String, thumbType: ThumbType): Observable<String> {
Log.d(TAG, "downloadCacheFile" + Thread.currentThread().name)
return Observable.error(Exception("testtest"))
// return Observable.just(md5)
}
}
fun <T, R> Observable<out T>.flatMapOnError(transform: (Throwable) -> Observable<R>) =
this.flatMap( { Observable.empty<R>() }, { t -> transform(t) }, { Observable.empty<R>() })
public enum class ThumbParam {
NAME, MD5, UNIQUE_ID, FILE_TYPE, MOD_TIME,
THUMB_TYPE, ORI_LOC_PATH, CACHE_PATH, SPEC
}
public enum class FileType {
IMAGE, MOVIE
}
public enum class ThumbType {
LARGE, SMALL, ORIGINAL
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment