From bfb209acd5fa182144f14fe1511a793729531689 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Fri, 9 May 2025 16:12:30 +0530 Subject: [PATCH] Improved the scanning of ZIM files from storage. * Improved the `TestObserver` to properly to receive the test values. --- .../library/local/LocalLibraryFragment.kt | 4 +--- .../zimManager/ZimManageViewModel.kt | 12 +++++------ .../org/kiwix/kiwixmobile/TestObserver.kt | 2 +- .../kiwix/kiwixmobile/core/StorageObserver.kt | 21 ++++++++----------- .../download/CustomDownloadViewModelTest.kt | 2 +- 5 files changed, 18 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/local/LocalLibraryFragment.kt b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/local/LocalLibraryFragment.kt index 80a8cd6dd..3141090f5 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/local/LocalLibraryFragment.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/local/LocalLibraryFragment.kt @@ -303,9 +303,7 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal ) } if (savedInstanceState != null && savedInstanceState.getBoolean(WAS_IN_ACTION_MODE)) { - lifecycleScope.launch { - zimManageViewModel.fileSelectActions.emit(FileSelectActions.RestartActionMode) - } + offerAction(FileSelectActions.RestartActionMode) } showCopyMoveDialogForOpenedZimFileFromStorage() } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zimManager/ZimManageViewModel.kt b/app/src/main/java/org/kiwix/kiwixmobile/zimManager/ZimManageViewModel.kt index 7e4117eeb..e089db8e7 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zimManager/ZimManageViewModel.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/zimManager/ZimManageViewModel.kt @@ -41,9 +41,10 @@ import kotlinx.coroutines.Job import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.catch -import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.flatMapLatest +import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch @@ -667,11 +668,10 @@ class ZimManageViewModel @Inject constructor( private fun booksFromStorageNotIn( booksFromDao: Flow>, scanningProgressListener: ScanningProgressListener - ): Flow> { - return storageObserver.getBooksOnFileSystem(scanningProgressListener) - .combine(booksFromDao.map { it.map { bookOnDisk -> bookOnDisk.book.id } }) { files, daoBooks -> - removeBooksAlreadyInDao(files, daoBooks) - } + ): Flow> = flow { + val scannedBooks = storageObserver.getBooksOnFileSystem(scanningProgressListener).first() + val daoBookIds = booksFromDao.first().map { it.book.id } + emit(removeBooksAlreadyInDao(scannedBooks, daoBookIds)) } private fun removeBooksAlreadyInDao( diff --git a/app/src/test/java/org/kiwix/kiwixmobile/TestObserver.kt b/app/src/test/java/org/kiwix/kiwixmobile/TestObserver.kt index d2528736c..571fcfc2e 100644 --- a/app/src/test/java/org/kiwix/kiwixmobile/TestObserver.kt +++ b/app/src/test/java/org/kiwix/kiwixmobile/TestObserver.kt @@ -45,7 +45,7 @@ class TestObserver( job = scope.launch { flow.collect { values.add(it) - completionChannel.trySend(Unit) + completionChannel.send(Unit) } } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/StorageObserver.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/StorageObserver.kt index a59ea2405..ae91ed280 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/StorageObserver.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/StorageObserver.kt @@ -21,9 +21,9 @@ package org.kiwix.kiwixmobile.core import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flowOn -import kotlinx.coroutines.flow.map import org.kiwix.kiwixmobile.core.dao.DownloadRoomDao import org.kiwix.kiwixmobile.core.dao.LibkiwixBookmarks import org.kiwix.kiwixmobile.core.downloader.model.DownloadModel @@ -44,16 +44,13 @@ class StorageObserver @Inject constructor( fun getBooksOnFileSystem( scanningProgressListener: ScanningProgressListener, dispatcher: CoroutineDispatcher = Dispatchers.IO - ): Flow> { - return scanFiles(scanningProgressListener) - .combine(downloadRoomDao.downloads()) { files, downloads -> - toFilesThatAreNotDownloading(files, downloads) - } - .map { files -> - files.mapNotNull { convertToBookOnDisk(it) } - } - .flowOn(dispatcher) - } + ): Flow> = flow { + val files = scanFiles(scanningProgressListener).first() + val downloads = downloadRoomDao.downloads().first() + val result = toFilesThatAreNotDownloading(files, downloads) + .mapNotNull { convertToBookOnDisk(it) } + emit(result) + }.flowOn(dispatcher) private fun scanFiles(scanningProgressListener: ScanningProgressListener): Flow> = fileSearch.scan(scanningProgressListener) diff --git a/custom/src/test/java/org/kiwix/kiwixmobile/custom/download/CustomDownloadViewModelTest.kt b/custom/src/test/java/org/kiwix/kiwixmobile/custom/download/CustomDownloadViewModelTest.kt index 462775292..a18c4fdb6 100644 --- a/custom/src/test/java/org/kiwix/kiwixmobile/custom/download/CustomDownloadViewModelTest.kt +++ b/custom/src/test/java/org/kiwix/kiwixmobile/custom/download/CustomDownloadViewModelTest.kt @@ -229,7 +229,7 @@ class TestObserver( job = scope.launch { flow.collect { values.add(it) - completionChannel.trySend(Unit) + completionChannel.send(Unit) } } }