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 68cfe4b7c..dab416bef 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/StorageObserver.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/StorageObserver.kt @@ -19,9 +19,12 @@ package org.kiwix.kiwixmobile.core import io.reactivex.Flowable +import io.reactivex.Single import io.reactivex.functions.BiFunction import io.reactivex.schedulers.Schedulers -import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import org.kiwix.kiwixmobile.core.dao.DownloadRoomDao import org.kiwix.kiwixmobile.core.dao.LibkiwixBookmarks import org.kiwix.kiwixmobile.core.downloader.model.DownloadModel @@ -45,7 +48,17 @@ class StorageObserver @Inject constructor( ): Flowable> { return scanFiles(scanningProgressListener) .withLatestFrom(downloadRoomDao.downloads(), BiFunction(::toFilesThatAreNotDownloading)) - .map { it.mapNotNull(::convertToBookOnDisk) } + .flatMapSingle { files -> + Single.create { emitter -> + CoroutineScope(Dispatchers.IO).launch { + try { + emitter.onSuccess(files.mapNotNull { convertToBookOnDisk(it) }) + } catch (ignore: Exception) { + emitter.onError(ignore) + } + } + } + } } private fun scanFiles(scanningProgressListener: ScanningProgressListener) = @@ -57,7 +70,7 @@ class StorageObserver @Inject constructor( private fun fileHasNoMatchingDownload(downloads: List, file: File) = downloads.firstOrNull { file.absolutePath.endsWith(it.fileNameFromUrl) } == null - private fun convertToBookOnDisk(file: File) = runBlocking { + private suspend fun convertToBookOnDisk(file: File) = zimReaderFactory.create(ZimReaderSource(file)) ?.let { zimFileReader -> BookOnDisk(zimFileReader).also { @@ -66,5 +79,4 @@ class StorageObserver @Inject constructor( zimFileReader.dispose() } } - } } diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/StorageObserverTest.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/StorageObserverTest.kt index 80cd08221..f8d25d044 100644 --- a/core/src/test/java/org/kiwix/kiwixmobile/core/StorageObserverTest.kt +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/StorageObserverTest.kt @@ -42,6 +42,7 @@ import org.kiwix.sharedFunctions.bookOnDisk import org.kiwix.sharedFunctions.resetSchedulers import org.kiwix.sharedFunctions.setScheduler import java.io.File +import java.util.concurrent.TimeUnit class StorageObserverTest { @@ -106,6 +107,7 @@ class StorageObserverTest { .also { downloads.offer(listOf(downloadModel)) files.offer(listOf(file)) + it.awaitDone(2, TimeUnit.SECONDS) } private fun withFiltering() {