diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostFragment.kt b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostFragment.kt index 69661ee63..a96f27df3 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostFragment.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostFragment.kt @@ -404,7 +404,7 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View { override fun onDestroyView() { super.onDestroyView() unRegisterHotspotService() - presenter.detachView() + presenter.detachView(this) } private fun unRegisterHotspotService() { diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostPresenter.kt b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostPresenter.kt index 96b8138af..0a40e66f3 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostPresenter.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostPresenter.kt @@ -30,11 +30,9 @@ import javax.inject.Inject @ActivityScope class ZimHostPresenter @Inject internal constructor( private val dataSource: DataSource -) : BasePresenter(), - Presenter { - @Suppress("TooGenericExceptionCaught") +) : BasePresenter(), Presenter { override suspend fun loadBooks(previouslyHostedBooks: Set) { - try { + runCatching { val books = dataSource.getLanguageCategorizedBooks().first() books.forEach { item -> if (item is BooksOnDiskListItem.BookOnDisk) { @@ -43,8 +41,8 @@ class ZimHostPresenter @Inject internal constructor( } } view?.addBooks(books) - } catch (e: Exception) { - Log.e(TAG, "Unable to load books", e) + }.onFailure { + Log.e(TAG, "Unable to load books", it) } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/base/BaseContract.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/base/BaseContract.kt index 806b294d3..e3d17b5c2 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/base/BaseContract.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/base/BaseContract.kt @@ -34,7 +34,9 @@ class BaseContract { /** * Drops the reference to the view when destroyed + * + * @param view the view instance to be detached */ - fun detachView() + fun detachView(view: T) } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/base/BasePresenter.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/base/BasePresenter.kt index 0db426ea6..10e065cd6 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/base/BasePresenter.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/base/BasePresenter.kt @@ -32,7 +32,11 @@ abstract class BasePresenter?> : Presenter { this.view = view } - override fun detachView() { - view = null + override fun detachView(view: T) { + // Detach the view only if it matches the one currently attached. + // Bug Fix #4409 + if (this.view == view) { + this.view = null + } } }