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 category: String? = null,
val lang: String? = null, val lang: String? = null,
val isLoadMoreItem: Boolean, val isLoadMoreItem: Boolean,
val page: Int val page: Int,
// Bug Fix #4381
val version: Long = System.nanoTime()
) )
data class OnlineLibraryResult( data class OnlineLibraryResult(
@ -371,7 +373,20 @@ class ZimManageViewModel @Inject constructor(
category = newRequest.category ?: current.category, category = newRequest.category ?: current.category,
lang = newRequest.lang ?: current.lang, lang = newRequest.lang ?: current.lang,
page = newRequest.page, 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,18 +302,22 @@ class ZimManageViewModelTest {
} }
@Test @Test
fun `updateOnlineLibraryFilters updates onlineLibraryRequest`() = runTest { fun `updateOnlineLibraryFilters updates onlineLibraryRequest`() = flakyTest {
val newRequest = ZimManageViewModel.OnlineLibraryRequest( runTest {
query = "test", viewModel.setIsUnitTestCase()
category = "cat", val newRequest = ZimManageViewModel.OnlineLibraryRequest(
lang = "en", query = "test",
page = 2, category = "cat",
isLoadMoreItem = true lang = "en",
) page = 2,
viewModel.onlineLibraryRequest.test { isLoadMoreItem = true,
skipItems(1) version = 100L
viewModel.updateOnlineLibraryFilters(newRequest) )
assertThat(awaitItem()).isEqualTo(newRequest) viewModel.onlineLibraryRequest.test {
skipItems(1)
viewModel.updateOnlineLibraryFilters(newRequest)
assertThat(awaitItem()).isEqualTo(newRequest)
}
} }
} }