Skip to content

Commit

Permalink
Improved creation of ZimFileReader.
Browse files Browse the repository at this point in the history
* If current file is same that is previously opened in the `ZimFileReader` then it will not create the reader again.
* Improved the variable naming.
  • Loading branch information
MohitMaliFtechiz committed Dec 27, 2023
1 parent d2505b7 commit 7c59193
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ data class ShowOpenNoteDialog(
)?.let { assetFileDescriptor ->
zimReaderContainer.setZimFileOrFileDescriptor(
assetFileDescriptor = assetFileDescriptor,
filePath = it
assetDescriptorFilePath = it

Check warning on line 63 in core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt#L61-L63

Added lines #L61 - L63 were not covered by tests
)
}
}

Check warning on line 66 in core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt#L65-L66

Added lines #L65 - L66 were not covered by tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,21 @@ class ZimFileReader constructor(
fun create(
file: File? = null,
assetFileDescriptor: AssetFileDescriptor? = null,
filePath: String? = null
assetDescriptorFilePath: String? = null
): ZimFileReader?

class Impl @Inject constructor(private val nightModeConfig: NightModeConfig) :
Factory {
override fun create(
file: File?,
assetFileDescriptor: AssetFileDescriptor?,
filePath: String?
assetDescriptorFilePath: String?
) =
try {
when {
file != null -> createArchiveWithFile(file)
assetFileDescriptor != null -> {
createArchiveWithAssetFileDescriptor(assetFileDescriptor, filePath)
createArchiveWithAssetFileDescriptor(assetFileDescriptor, assetDescriptorFilePath)
}

else -> null
Expand Down Expand Up @@ -110,13 +110,13 @@ class ZimFileReader constructor(

private fun createArchiveWithAssetFileDescriptor(
assetFileDescriptor: AssetFileDescriptor,
filePath: String?
assetFileDescriptorPath: String?
): ZimFileReader? =
if (assetFileDescriptor.parcelFileDescriptor.dup().fileDescriptor.valid()) {
ZimFileReader(
null,
assetFileDescriptor,
assetDescriptorFilePath = filePath,
assetDescriptorFilePath = assetFileDescriptorPath,
nightModeConfig = nightModeConfig,
jniKiwixReader = Archive(
assetFileDescriptor.parcelFileDescriptor.dup().fileDescriptor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,31 @@ class ZimReaderContainer @Inject constructor(private val zimFileReaderFactory: F
fun setZimFileOrFileDescriptor(
file: File? = null,

Check warning on line 37 in core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimReaderContainer.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimReaderContainer.kt#L37

Added line #L37 was not covered by tests
assetFileDescriptor: AssetFileDescriptor? = null,
filePath: String? = null
assetDescriptorFilePath: String? = null
) {
if (isValidZimFileReader == true &&
file?.canonicalPath == zimFileReader?.zimFile?.canonicalPath
) {
if (shouldNotCreateZimFileReader(file, assetDescriptorFilePath)) {
return
}
zimFileReader =
zimFileReaderFactory.create(file, assetFileDescriptor, filePath)
zimFileReaderFactory.create(file, assetFileDescriptor, assetDescriptorFilePath)
}

private fun shouldNotCreateZimFileReader(
file: File?,
assetDescriptorFilePath: String?
) =
when {
file != null -> {
file.canonicalPath == zimCanonicalPath

Check warning on line 54 in core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimReaderContainer.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimReaderContainer.kt#L54

Added line #L54 was not covered by tests
}

assetDescriptorFilePath != null -> {
assetDescriptorFilePath == zimCanonicalPath
}

else -> false
}

fun getPageUrlFromTitle(title: String) = zimFileReader?.getPageUrlFrom(title)

fun getRandomArticleUrl() = zimFileReader?.getRandomArticleUrl()
Expand Down Expand Up @@ -81,7 +95,7 @@ class ZimReaderContainer @Inject constructor(private val zimFileReaderFactory: F
zimFileReaderFactory.create(
file = zimFile,
assetFileDescriptor = assetFileDescriptor,
filePath = assetDescriptorPath
assetDescriptorFilePath = assetDescriptorPath
)

val zimFile get() = zimFileReader?.zimFile
Expand Down

0 comments on commit 7c59193

Please sign in to comment.