Fixed: Online library was not retrieving after clicking “Allow downloading via mobile network” in the confirmation dialog.

* Cause: The request object emitted after user confirmation was identical to the previous one, so the flow collector skipped it and the fetch was never triggered.
* Solution: Added a `System.nanoTime()` field to the request to ensure a unique emission while still preserving existing values.
* Refactored the `updateOnlineLibraryFilters updates onlineLibraryRequest` according to this new change.
This commit is contained in:
MohitMaliFtechiz 2025-08-13 17:53:24 +05:30 committed by Kelson
parent 26cc6faec8
commit 3b4a5d29fa
2 changed files with 33 additions and 14 deletions

View File

@ -155,7 +155,9 @@ class ZimManageViewModel @Inject constructor(
val category: String? = null,
val lang: String? = null,
val isLoadMoreItem: Boolean,
val page: Int
val page: Int,
// Bug Fix #4381
val version: Long = System.nanoTime()
)
data class OnlineLibraryResult(
@ -371,7 +373,20 @@ class ZimManageViewModel @Inject constructor(
category = newRequest.category ?: current.category,
lang = newRequest.lang ?: current.lang,
page = newRequest.page,
isLoadMoreItem = newRequest.isLoadMoreItem
isLoadMoreItem = newRequest.isLoadMoreItem,
version = if (isUnitTestCase) {
// In unit tests, we want predictable and testable values,
// so use the provided version instead of a dynamic timestamp.
newRequest.version
} else {
// Bug Fix #4381:
// Force StateFlow to emit even if all other fields are unchanged.
// Without this, identical requests may not trigger observers,
// causing the UI not to refresh.
// Using System.nanoTime() ensures a unique value each time,
// guaranteeing that collectors receive an update.
System.nanoTime()
}
)
}
}

View File

@ -302,13 +302,16 @@ class ZimManageViewModelTest {
}
@Test
fun `updateOnlineLibraryFilters updates onlineLibraryRequest`() = runTest {
fun `updateOnlineLibraryFilters updates onlineLibraryRequest`() = flakyTest {
runTest {
viewModel.setIsUnitTestCase()
val newRequest = ZimManageViewModel.OnlineLibraryRequest(
query = "test",
category = "cat",
lang = "en",
page = 2,
isLoadMoreItem = true
isLoadMoreItem = true,
version = 100L
)
viewModel.onlineLibraryRequest.test {
skipItems(1)
@ -316,6 +319,7 @@ class ZimManageViewModelTest {
assertThat(awaitItem()).isEqualTo(newRequest)
}
}
}
@Test
fun `library update removes from sources and maps to list items`() = flakyTest {