From 895e564262b300ebcf14b9f2314ceefeff5b314a Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Tue, 23 Jul 2024 15:11:50 +0530 Subject: [PATCH] Updating the UI when a undeliverable exception is thrown when getting the online library. --- .../RxJavaUncaughtExceptionHandling.kt | 46 ------------------- .../zimManager/ZimManageViewModel.kt | 38 +++++++++++++-- 2 files changed, 35 insertions(+), 49 deletions(-) delete mode 100644 app/src/main/java/org/kiwix/kiwixmobile/RxJavaUncaughtExceptionHandling.kt diff --git a/app/src/main/java/org/kiwix/kiwixmobile/RxJavaUncaughtExceptionHandling.kt b/app/src/main/java/org/kiwix/kiwixmobile/RxJavaUncaughtExceptionHandling.kt deleted file mode 100644 index 708c56b57..000000000 --- a/app/src/main/java/org/kiwix/kiwixmobile/RxJavaUncaughtExceptionHandling.kt +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2024 Kiwix - * 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 . - * - */ - -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) - } - } - } - } - } -} diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zimManager/ZimManageViewModel.kt b/app/src/main/java/org/kiwix/kiwixmobile/zimManager/ZimManageViewModel.kt index b2f5d082c..22a005b4f 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zimManager/ZimManageViewModel.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/zimManager/ZimManageViewModel.kt @@ -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 + ) { + 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) + } + } + } + } + } }