Improved the scanning of ZIM files from storage.

* Improved the `TestObserver` to properly to receive the test values.
This commit is contained in:
MohitMaliFtechiz 2025-05-09 16:12:30 +05:30
parent bbae4dbd64
commit bfb209acd5
5 changed files with 18 additions and 23 deletions

View File

@ -303,9 +303,7 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal
) )
} }
if (savedInstanceState != null && savedInstanceState.getBoolean(WAS_IN_ACTION_MODE)) { if (savedInstanceState != null && savedInstanceState.getBoolean(WAS_IN_ACTION_MODE)) {
lifecycleScope.launch { offerAction(FileSelectActions.RestartActionMode)
zimManageViewModel.fileSelectActions.emit(FileSelectActions.RestartActionMode)
}
} }
showCopyMoveDialogForOpenedZimFileFromStorage() showCopyMoveDialogForOpenedZimFileFromStorage()
} }

View File

@ -41,9 +41,10 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -667,11 +668,10 @@ class ZimManageViewModel @Inject constructor(
private fun booksFromStorageNotIn( private fun booksFromStorageNotIn(
booksFromDao: Flow<List<BookOnDisk>>, booksFromDao: Flow<List<BookOnDisk>>,
scanningProgressListener: ScanningProgressListener scanningProgressListener: ScanningProgressListener
): Flow<List<BookOnDisk>> { ): Flow<List<BookOnDisk>> = flow {
return storageObserver.getBooksOnFileSystem(scanningProgressListener) val scannedBooks = storageObserver.getBooksOnFileSystem(scanningProgressListener).first()
.combine(booksFromDao.map { it.map { bookOnDisk -> bookOnDisk.book.id } }) { files, daoBooks -> val daoBookIds = booksFromDao.first().map { it.book.id }
removeBooksAlreadyInDao(files, daoBooks) emit(removeBooksAlreadyInDao(scannedBooks, daoBookIds))
}
} }
private fun removeBooksAlreadyInDao( private fun removeBooksAlreadyInDao(

View File

@ -45,7 +45,7 @@ class TestObserver<T>(
job = scope.launch { job = scope.launch {
flow.collect { flow.collect {
values.add(it) values.add(it)
completionChannel.trySend(Unit) completionChannel.send(Unit)
} }
} }
} }

View File

@ -21,9 +21,9 @@ package org.kiwix.kiwixmobile.core
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow 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.flowOn
import kotlinx.coroutines.flow.map
import org.kiwix.kiwixmobile.core.dao.DownloadRoomDao import org.kiwix.kiwixmobile.core.dao.DownloadRoomDao
import org.kiwix.kiwixmobile.core.dao.LibkiwixBookmarks import org.kiwix.kiwixmobile.core.dao.LibkiwixBookmarks
import org.kiwix.kiwixmobile.core.downloader.model.DownloadModel import org.kiwix.kiwixmobile.core.downloader.model.DownloadModel
@ -44,16 +44,13 @@ class StorageObserver @Inject constructor(
fun getBooksOnFileSystem( fun getBooksOnFileSystem(
scanningProgressListener: ScanningProgressListener, scanningProgressListener: ScanningProgressListener,
dispatcher: CoroutineDispatcher = Dispatchers.IO dispatcher: CoroutineDispatcher = Dispatchers.IO
): Flow<List<BookOnDisk>> { ): Flow<List<BookOnDisk>> = flow {
return scanFiles(scanningProgressListener) val files = scanFiles(scanningProgressListener).first()
.combine(downloadRoomDao.downloads()) { files, downloads -> val downloads = downloadRoomDao.downloads().first()
toFilesThatAreNotDownloading(files, downloads) val result = toFilesThatAreNotDownloading(files, downloads)
} .mapNotNull { convertToBookOnDisk(it) }
.map { files -> emit(result)
files.mapNotNull { convertToBookOnDisk(it) } }.flowOn(dispatcher)
}
.flowOn(dispatcher)
}
private fun scanFiles(scanningProgressListener: ScanningProgressListener): Flow<List<File>> = private fun scanFiles(scanningProgressListener: ScanningProgressListener): Flow<List<File>> =
fileSearch.scan(scanningProgressListener) fileSearch.scan(scanningProgressListener)

View File

@ -229,7 +229,7 @@ class TestObserver<T>(
job = scope.launch { job = scope.launch {
flow.collect { flow.collect {
values.add(it) values.add(it)
completionChannel.trySend(Unit) completionChannel.send(Unit)
} }
} }
} }