Improved the books() method to properly return the new books along with the old saved books.

This commit is contained in:
MohitMaliFtechiz 2024-08-25 16:35:27 +05:30
parent 4e3f66243b
commit 1cdd683e79

View File

@ -31,7 +31,19 @@ import javax.inject.Inject
class NewBookDao @Inject constructor(private val box: Box<BookOnDiskEntity>) {
fun books() = box.asFlowable()
.doOnNext(::removeBooksThatDoNotExist)
.map { books ->
books.map { bookOnDiskEntity ->
bookOnDiskEntity.file.let { file ->
// set zimReaderSource for previously saved books
val zimReaderSource = ZimReaderSource(file)
if (zimReaderSource.canOpenInLibkiwix()) {
bookOnDiskEntity.zimReaderSource = zimReaderSource
}
}
bookOnDiskEntity
}
}
.doOnNext { removeBooksThatDoNotExist(it.toMutableList()) }
.map { books -> books.filter { it.zimReaderSource.exists() } }
.map { it.map(::BookOnDisk) }
@ -92,7 +104,7 @@ class NewBookDao @Inject constructor(private val box: Box<BookOnDiskEntity>) {
}
private fun removeBooksThatDoNotExist(books: MutableList<BookOnDiskEntity>) {
delete(books.filterNot { it.zimReaderSource.exists() || it.file.exists() })
delete(books.filterNot { it.zimReaderSource.exists() })
}
private fun delete(books: List<BookOnDiskEntity>) {