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() {
super.onDestroyView()
unRegisterHotspotService()
presenter.detachView()
presenter.detachView(this)
}
private fun unRegisterHotspotService() {

View File

@ -30,11 +30,9 @@ import javax.inject.Inject
@ActivityScope
class ZimHostPresenter @Inject internal constructor(
private val dataSource: DataSource
) : BasePresenter<View>(),
Presenter {
@Suppress("TooGenericExceptionCaught")
) : BasePresenter<View>(), Presenter {
override suspend fun loadBooks(previouslyHostedBooks: Set<String>) {
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)
}
}

View File

@ -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)
}
}

View File

@ -32,7 +32,11 @@ abstract class BasePresenter<T : View<*>?> : Presenter<T> {
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
}
}
}