Made the convertToBookOnDisk method suspend, as it creates the ZimFileReader object to convert the files into Books.

* Refactored the `StorageObserverTest` to align with this new change.
This commit is contained in:
MohitMaliFtechiz 2025-01-21 17:08:54 +05:30
parent 2f5da0ea3e
commit b1c065882d
2 changed files with 18 additions and 4 deletions

View File

@ -19,9 +19,12 @@
package org.kiwix.kiwixmobile.core package org.kiwix.kiwixmobile.core
import io.reactivex.Flowable import io.reactivex.Flowable
import io.reactivex.Single
import io.reactivex.functions.BiFunction import io.reactivex.functions.BiFunction
import io.reactivex.schedulers.Schedulers 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.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
@ -45,7 +48,17 @@ class StorageObserver @Inject constructor(
): Flowable<List<BookOnDisk>> { ): Flowable<List<BookOnDisk>> {
return scanFiles(scanningProgressListener) return scanFiles(scanningProgressListener)
.withLatestFrom(downloadRoomDao.downloads(), BiFunction(::toFilesThatAreNotDownloading)) .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) = private fun scanFiles(scanningProgressListener: ScanningProgressListener) =
@ -57,7 +70,7 @@ class StorageObserver @Inject constructor(
private fun fileHasNoMatchingDownload(downloads: List<DownloadModel>, file: File) = private fun fileHasNoMatchingDownload(downloads: List<DownloadModel>, file: File) =
downloads.firstOrNull { file.absolutePath.endsWith(it.fileNameFromUrl) } == null downloads.firstOrNull { file.absolutePath.endsWith(it.fileNameFromUrl) } == null
private fun convertToBookOnDisk(file: File) = runBlocking { private suspend fun convertToBookOnDisk(file: File) =
zimReaderFactory.create(ZimReaderSource(file)) zimReaderFactory.create(ZimReaderSource(file))
?.let { zimFileReader -> ?.let { zimFileReader ->
BookOnDisk(zimFileReader).also { BookOnDisk(zimFileReader).also {
@ -66,5 +79,4 @@ class StorageObserver @Inject constructor(
zimFileReader.dispose() zimFileReader.dispose()
} }
} }
}
} }

View File

@ -42,6 +42,7 @@ import org.kiwix.sharedFunctions.bookOnDisk
import org.kiwix.sharedFunctions.resetSchedulers import org.kiwix.sharedFunctions.resetSchedulers
import org.kiwix.sharedFunctions.setScheduler import org.kiwix.sharedFunctions.setScheduler
import java.io.File import java.io.File
import java.util.concurrent.TimeUnit
class StorageObserverTest { class StorageObserverTest {
@ -106,6 +107,7 @@ class StorageObserverTest {
.also { .also {
downloads.offer(listOf(downloadModel)) downloads.offer(listOf(downloadModel))
files.offer(listOf(file)) files.offer(listOf(file))
it.awaitDone(2, TimeUnit.SECONDS)
} }
private fun withFiltering() { private fun withFiltering() {