Fixed: Hotspot list of books appearing empty(when reopening the ZimHostScreen).

* The issue occurred when reopening the `ZimHostScreen`. The presenter was detaching the view of the previous `ZimHostScreen`, but since the presenter is a singleton, it also cleared the reference to the view attached to the new `ZimHostScreen`. As a result, the `ZimHostPresenter` could not post books because the view reference was null.
* Updated `BasePresenter` so that it only detaches the view if it matches the one currently attached, preventing accidental clearing of the new screen’s view.
This commit is contained in:
MohitMaliFtechiz 2025-09-03 15:46:37 +05:30
parent 796ff4ccc0
commit 76a70bed09
4 changed files with 14 additions and 10 deletions

View File

@ -404,7 +404,7 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
override fun onDestroyView() { override fun onDestroyView() {
super.onDestroyView() super.onDestroyView()
unRegisterHotspotService() unRegisterHotspotService()
presenter.detachView() presenter.detachView(this)
} }
private fun unRegisterHotspotService() { private fun unRegisterHotspotService() {

View File

@ -30,11 +30,9 @@ import javax.inject.Inject
@ActivityScope @ActivityScope
class ZimHostPresenter @Inject internal constructor( class ZimHostPresenter @Inject internal constructor(
private val dataSource: DataSource private val dataSource: DataSource
) : BasePresenter<View>(), ) : BasePresenter<View>(), Presenter {
Presenter {
@Suppress("TooGenericExceptionCaught")
override suspend fun loadBooks(previouslyHostedBooks: Set<String>) { override suspend fun loadBooks(previouslyHostedBooks: Set<String>) {
try { runCatching {
val books = dataSource.getLanguageCategorizedBooks().first() val books = dataSource.getLanguageCategorizedBooks().first()
books.forEach { item -> books.forEach { item ->
if (item is BooksOnDiskListItem.BookOnDisk) { if (item is BooksOnDiskListItem.BookOnDisk) {
@ -43,8 +41,8 @@ class ZimHostPresenter @Inject internal constructor(
} }
} }
view?.addBooks(books) view?.addBooks(books)
} catch (e: Exception) { }.onFailure {
Log.e(TAG, "Unable to load books", e) Log.e(TAG, "Unable to load books", it)
} }
} }

View File

@ -34,7 +34,9 @@ class BaseContract {
/** /**
* Drops the reference to the view when destroyed * Drops the reference to the view when destroyed
*
* @param view the view instance to be detached
*/ */
fun detachView() fun detachView(view: T)
} }
} }

View File

@ -32,7 +32,11 @@ abstract class BasePresenter<T : View<*>?> : Presenter<T> {
this.view = view this.view = view
} }
override fun detachView() { override fun detachView(view: T) {
view = null // Detach the view only if it matches the one currently attached.
// Bug Fix #4409
if (this.view == view) {
this.view = null
}
} }
} }