Skip to content

Commit ef4ab79

Browse files
committed
Handle all observable errors gracefully
1 parent b801e42 commit ef4ab79

File tree

5 files changed

+36
-21
lines changed

5 files changed

+36
-21
lines changed

app/src/main/kotlin/com/njlabs/showjava/activities/decompiler/DecompilerActivity.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import android.annotation.SuppressLint
2222
import android.app.ActivityOptions
2323
import android.content.Intent
2424
import android.graphics.Typeface
25-
import android.graphics.drawable.Drawable
2625
import android.os.Build
2726
import android.os.Bundle
2827
import android.text.Spannable
@@ -46,7 +45,6 @@ import com.njlabs.showjava.decompilers.BaseDecompiler.Companion.isAvailable
4645
import com.njlabs.showjava.utils.ktx.sourceDir
4746
import com.njlabs.showjava.utils.ktx.toBundle
4847
import io.reactivex.Observable
49-
import io.reactivex.ObservableEmitter
5048
import io.reactivex.android.schedulers.AndroidSchedulers
5149
import io.reactivex.schedulers.Schedulers
5250
import kotlinx.android.synthetic.main.activity_decompiler.*
@@ -121,8 +119,8 @@ class DecompilerActivity : BaseActivity() {
121119
}
122120

123121
disposables.add(
124-
Observable.create { emitter: ObservableEmitter<Drawable> ->
125-
emitter.onNext(packageInfo.loadIcon(context))
122+
Observable.fromCallable {
123+
packageInfo.loadIcon(context)
126124
}
127125
.subscribeOn(Schedulers.io())
128126
.observeOn(AndroidSchedulers.mainThread())
@@ -196,9 +194,11 @@ class DecompilerActivity : BaseActivity() {
196194

197195
BaseDecompiler.start(inputMap)
198196

199-
firebaseAnalytics.logEvent(Constants.EVENTS.SELECT_DECOMPILER, hashMapOf(
200-
FirebaseAnalytics.Param.VALUE to decompiler
201-
).toBundle())
197+
firebaseAnalytics.logEvent(
198+
Constants.EVENTS.SELECT_DECOMPILER, hashMapOf(
199+
FirebaseAnalytics.Param.VALUE to decompiler
200+
).toBundle()
201+
)
202202

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

app/src/main/kotlin/com/njlabs/showjava/activities/explorer/navigator/NavigatorActivity.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ class NavigatorActivity : BaseActivity() {
104104
disposables.add(navigationHandler.loadFiles(startDirectory)
105105
.subscribeOn(Schedulers.io())
106106
.observeOn(AndroidSchedulers.mainThread())
107-
.doOnError { Timber.e(it) }
107+
.onErrorReturn {
108+
Timber.e(it)
109+
Toast.makeText(context, R.string.errorLoadingFiles, Toast.LENGTH_SHORT).show()
110+
ArrayList()
111+
}
108112
.subscribe {
109113
updateList(it)
110114
swipeRefresh.isRefreshing = false
@@ -226,7 +230,12 @@ class NavigatorActivity : BaseActivity() {
226230
return currentDirectory?.canonicalPath == selectedApp?.sourceDirectory?.canonicalPath
227231
}
228232

229-
private fun shareArchive(file: File) {
233+
private fun shareArchive(file: File?) {
234+
if (file == null) {
235+
Toast.makeText(context, R.string.genericError, Toast.LENGTH_SHORT).show()
236+
return
237+
}
238+
230239
dismissProgressDialog()
231240
val shareIntent = Intent()
232241
shareIntent.action = Intent.ACTION_SEND
@@ -277,7 +286,10 @@ class NavigatorActivity : BaseActivity() {
277286
)
278287
.subscribeOn(Schedulers.io())
279288
.observeOn(AndroidSchedulers.mainThread())
280-
.doOnError { Timber.e(it) }
289+
.onErrorReturn {
290+
Timber.e(it)
291+
null
292+
}
281293
.subscribe {
282294
sourceArchive = it
283295
shareArchive(it)

app/src/main/kotlin/com/njlabs/showjava/activities/explorer/viewer/CodeViewerActivity.kt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,11 @@ import android.view.MenuItem
2828
import android.view.View
2929
import com.njlabs.showjava.R
3030
import com.njlabs.showjava.activities.BaseActivity
31-
import com.njlabs.showjava.utils.rx.ProcessStatus
3231
import com.njlabs.showjava.utils.views.CodeView
3332
import io.reactivex.Observable
34-
import io.reactivex.ObservableEmitter
3533
import io.reactivex.android.schedulers.AndroidSchedulers
3634
import io.reactivex.schedulers.Schedulers
3735
import kotlinx.android.synthetic.main.activity_code_viewer.*
38-
import timber.log.Timber
3936
import java.io.File
4037

4138
class CodeViewerActivity : BaseActivity(), CodeView.OnHighlightListener {
@@ -105,9 +102,11 @@ class CodeViewerActivity : BaseActivity(), CodeView.OnHighlightListener {
105102
loadFile(file)
106103
.subscribeOn(Schedulers.io())
107104
.observeOn(AndroidSchedulers.mainThread())
108-
.doOnError { error -> Timber.e(error) }
109-
.subscribe { status ->
110-
codeView.setCode(status.result)
105+
.onErrorReturn {
106+
it.localizedMessage
107+
}
108+
.subscribe { fileContent ->
109+
codeView.setCode(fileContent)
111110
.setLanguage(language)
112111
.setWrapLine(wrapLine)
113112
.setDarkMode(invertColors)
@@ -120,10 +119,9 @@ class CodeViewerActivity : BaseActivity(), CodeView.OnHighlightListener {
120119
)
121120
}
122121

123-
private fun loadFile(fileToLoad: File): Observable<ProcessStatus<String>> {
124-
return Observable.create { emitter: ObservableEmitter<ProcessStatus<String>> ->
125-
emitter.onNext(ProcessStatus(fileToLoad.readText()))
126-
emitter.onComplete()
122+
private fun loadFile(fileToLoad: File): Observable<String> {
123+
return Observable.fromCallable {
124+
fileToLoad.readText()
127125
}
128126
}
129127

app/src/main/kotlin/com/njlabs/showjava/activities/landing/LandingActivity.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ class LandingActivity : BaseActivity() {
172172
disposables.add(landingHandler.loadHistory()
173173
.subscribeOn(Schedulers.io())
174174
.observeOn(AndroidSchedulers.mainThread())
175-
.doOnError { Timber.e(it) }
175+
.onErrorReturn {
176+
Timber.e(it)
177+
ArrayList()
178+
}
176179
.subscribe {
177180
historyItems = it
178181
swipeRefresh.isRefreshing = false

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,6 @@
174174
<string name="instanceId">(ID: <xliff:g example="abc134_GTF" id="instanceIdString">%1$s</xliff:g>)</string>
175175
<string name="onlyPositiveIntegersAllowed">Only positive integers are allowed</string>
176176
<string name="errorLoadingInputFile">Error loading input file</string>
177+
<string name="genericError">An unexpected error occurred</string>
178+
<string name="errorLoadingFiles">An error occurred while loading files</string>
177179
</resources>

0 commit comments

Comments
 (0)