Skip to content

Commit

Permalink
Handle all observable errors gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
niranjan94 committed Dec 25, 2018
1 parent b801e42 commit ef4ab79
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import android.annotation.SuppressLint
import android.app.ActivityOptions
import android.content.Intent
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.text.Spannable
Expand All @@ -46,7 +45,6 @@ import com.njlabs.showjava.decompilers.BaseDecompiler.Companion.isAvailable
import com.njlabs.showjava.utils.ktx.sourceDir
import com.njlabs.showjava.utils.ktx.toBundle
import io.reactivex.Observable
import io.reactivex.ObservableEmitter
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_decompiler.*
Expand Down Expand Up @@ -121,8 +119,8 @@ class DecompilerActivity : BaseActivity() {
}

disposables.add(
Observable.create { emitter: ObservableEmitter<Drawable> ->
emitter.onNext(packageInfo.loadIcon(context))
Observable.fromCallable {
packageInfo.loadIcon(context)
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
Expand Down Expand Up @@ -196,9 +194,11 @@ class DecompilerActivity : BaseActivity() {

BaseDecompiler.start(inputMap)

firebaseAnalytics.logEvent(Constants.EVENTS.SELECT_DECOMPILER, hashMapOf(
FirebaseAnalytics.Param.VALUE to decompiler
).toBundle())
firebaseAnalytics.logEvent(
Constants.EVENTS.SELECT_DECOMPILER, hashMapOf(
FirebaseAnalytics.Param.VALUE to decompiler
).toBundle()
)

firebaseAnalytics.logEvent(Constants.EVENTS.DECOMPILE_APP, inputMap.toBundle())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ class NavigatorActivity : BaseActivity() {
disposables.add(navigationHandler.loadFiles(startDirectory)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnError { Timber.e(it) }
.onErrorReturn {
Timber.e(it)
Toast.makeText(context, R.string.errorLoadingFiles, Toast.LENGTH_SHORT).show()
ArrayList()
}
.subscribe {
updateList(it)
swipeRefresh.isRefreshing = false
Expand Down Expand Up @@ -226,7 +230,12 @@ class NavigatorActivity : BaseActivity() {
return currentDirectory?.canonicalPath == selectedApp?.sourceDirectory?.canonicalPath
}

private fun shareArchive(file: File) {
private fun shareArchive(file: File?) {
if (file == null) {
Toast.makeText(context, R.string.genericError, Toast.LENGTH_SHORT).show()
return
}

dismissProgressDialog()
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
Expand Down Expand Up @@ -277,7 +286,10 @@ class NavigatorActivity : BaseActivity() {
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnError { Timber.e(it) }
.onErrorReturn {
Timber.e(it)
null
}
.subscribe {
sourceArchive = it
shareArchive(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ import android.view.MenuItem
import android.view.View
import com.njlabs.showjava.R
import com.njlabs.showjava.activities.BaseActivity
import com.njlabs.showjava.utils.rx.ProcessStatus
import com.njlabs.showjava.utils.views.CodeView
import io.reactivex.Observable
import io.reactivex.ObservableEmitter
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_code_viewer.*
import timber.log.Timber
import java.io.File

class CodeViewerActivity : BaseActivity(), CodeView.OnHighlightListener {
Expand Down Expand Up @@ -105,9 +102,11 @@ class CodeViewerActivity : BaseActivity(), CodeView.OnHighlightListener {
loadFile(file)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnError { error -> Timber.e(error) }
.subscribe { status ->
codeView.setCode(status.result)
.onErrorReturn {
it.localizedMessage
}
.subscribe { fileContent ->
codeView.setCode(fileContent)
.setLanguage(language)
.setWrapLine(wrapLine)
.setDarkMode(invertColors)
Expand All @@ -120,10 +119,9 @@ class CodeViewerActivity : BaseActivity(), CodeView.OnHighlightListener {
)
}

private fun loadFile(fileToLoad: File): Observable<ProcessStatus<String>> {
return Observable.create { emitter: ObservableEmitter<ProcessStatus<String>> ->
emitter.onNext(ProcessStatus(fileToLoad.readText()))
emitter.onComplete()
private fun loadFile(fileToLoad: File): Observable<String> {
return Observable.fromCallable {
fileToLoad.readText()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ class LandingActivity : BaseActivity() {
disposables.add(landingHandler.loadHistory()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnError { Timber.e(it) }
.onErrorReturn {
Timber.e(it)
ArrayList()
}
.subscribe {
historyItems = it
swipeRefresh.isRefreshing = false
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,6 @@
<string name="instanceId">(ID: <xliff:g example="abc134_GTF" id="instanceIdString">%1$s</xliff:g>)</string>
<string name="onlyPositiveIntegersAllowed">Only positive integers are allowed</string>
<string name="errorLoadingInputFile">Error loading input file</string>
<string name="genericError">An unexpected error occurred</string>
<string name="errorLoadingFiles">An error occurred while loading files</string>
</resources>

0 comments on commit ef4ab79

Please sign in to comment.