Updating the UI when a undeliverable exception is thrown when getting the online library.

This commit is contained in:
MohitMaliFtechiz 2024-07-23 15:11:50 +05:30
parent baa21cb85b
commit 895e564262
2 changed files with 35 additions and 49 deletions

View File

@ -1,46 +0,0 @@
/*
* Kiwix Android
* Copyright (c) 2024 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile
import io.reactivex.exceptions.UndeliverableException
import io.reactivex.plugins.RxJavaPlugins
import org.kiwix.kiwixmobile.core.utils.files.Log
object RxJavaUncaughtExceptionHandling {
private const val TAG_RX_JAVA_DEFAULT_ERROR_HANDLER = "RxJavaDefaultErrorHandler"
fun setUp() {
RxJavaPlugins.setErrorHandler { exception ->
when (exception) {
is UndeliverableException -> {
// Merely log undeliverable exceptions
Log.i(
TAG_RX_JAVA_DEFAULT_ERROR_HANDLER,
"Caught undeliverable exception: ${exception.cause}"
)
}
else -> {
Thread.currentThread().also { thread ->
thread.uncaughtExceptionHandler?.uncaughtException(thread, exception)
}
}
}
}
}
}

View File

@ -26,12 +26,13 @@ import androidx.lifecycle.ViewModel
import io.reactivex.Flowable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.exceptions.UndeliverableException
import io.reactivex.functions.BiFunction
import io.reactivex.functions.Function6
import io.reactivex.plugins.RxJavaPlugins
import io.reactivex.processors.BehaviorProcessor
import io.reactivex.processors.PublishProcessor
import io.reactivex.schedulers.Schedulers
import org.kiwix.kiwixmobile.RxJavaUncaughtExceptionHandling
import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.StorageObserver
import org.kiwix.kiwixmobile.core.base.SideEffect
@ -48,6 +49,7 @@ import org.kiwix.kiwixmobile.core.extensions.calculateSearchMatches
import org.kiwix.kiwixmobile.core.extensions.registerReceiver
import org.kiwix.kiwixmobile.core.utils.BookUtils
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.core.utils.files.Log
import org.kiwix.kiwixmobile.core.utils.files.ScanningProgressListener
import org.kiwix.kiwixmobile.core.zim_manager.Language
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.SelectionMode.MULTI
@ -82,6 +84,8 @@ import javax.inject.Inject
const val DEFAULT_PROGRESS = 0
const val MAX_PROGRESS = 100
private const val TAG_RX_JAVA_DEFAULT_ERROR_HANDLER = "RxJavaDefaultErrorHandler"
class ZimManageViewModel @Inject constructor(
private val downloadDao: FetchDownloadDao,
private val bookDao: NewBookDao,
@ -124,7 +128,6 @@ class ZimManageViewModel @Inject constructor(
private var compositeDisposable: CompositeDisposable? = CompositeDisposable()
init {
RxJavaUncaughtExceptionHandling.setUp()
compositeDisposable?.addAll(*disposables())
context.registerReceiver(connectivityBroadcastReceiver)
}
@ -158,7 +161,9 @@ class ZimManageViewModel @Inject constructor(
updateNetworkStates(),
requestsAndConnectivtyChangesToLibraryRequests(networkLibrary),
fileSelectActions()
)
).also {
setUpUncaughtErrorHandlerForOnlineLibrary(networkLibrary)
}
}
private fun fileSelectActions() = fileSelectActions.subscribe({
@ -323,10 +328,12 @@ class ZimManageViewModel @Inject constructor(
fromLocalesWithNetworkMatchesSetActiveBy(
networkLanguageCounts(booksFromNetwork), defaultLanguage()
)
booksFromNetwork.isNotEmpty() && allLanguages.isNotEmpty() ->
fromLocalesWithNetworkMatchesSetActiveBy(
networkLanguageCounts(booksFromNetwork), allLanguages
)
else -> throw RuntimeException("Impossible state")
}
@ -514,4 +521,29 @@ class ZimManageViewModel @Inject constructor(
}
)
}
private fun setUpUncaughtErrorHandlerForOnlineLibrary(
library: PublishProcessor<LibraryNetworkEntity>
) {
RxJavaPlugins.setErrorHandler { exception ->
when (exception) {
is UndeliverableException -> {
library.onNext(
LibraryNetworkEntity().apply { book = LinkedList() }
).also {
Log.i(
TAG_RX_JAVA_DEFAULT_ERROR_HANDLER,
"Caught undeliverable exception: ${exception.cause}"
)
}
}
else -> {
Thread.currentThread().also { thread ->
thread.uncaughtExceptionHandler?.uncaughtException(thread, exception)
}
}
}
}
}
}