From 7c59193bd891fc7feac649112c2dc78cd583e2f0 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Wed, 27 Dec 2023 18:38:19 +0530 Subject: [PATCH] Improved creation of `ZimFileReader`. * If current file is same that is previously opened in the `ZimFileReader` then it will not create the reader again. * Improved the variable naming. --- .../viewmodel/effects/ShowOpenNoteDialog.kt | 2 +- .../kiwixmobile/core/reader/ZimFileReader.kt | 10 +++---- .../core/reader/ZimReaderContainer.kt | 26 ++++++++++++++----- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt index 8b39b99aad..8c27e4a760 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt @@ -60,7 +60,7 @@ data class ShowOpenNoteDialog( )?.let { assetFileDescriptor -> zimReaderContainer.setZimFileOrFileDescriptor( assetFileDescriptor = assetFileDescriptor, - filePath = it + assetDescriptorFilePath = it ) } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt index 26722c1285..306da24124 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt @@ -65,7 +65,7 @@ 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) : @@ -73,13 +73,13 @@ class ZimFileReader constructor( 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 @@ -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, diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimReaderContainer.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimReaderContainer.kt index 6582d2c761..5205cfed0b 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimReaderContainer.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimReaderContainer.kt @@ -36,17 +36,31 @@ class ZimReaderContainer @Inject constructor(private val zimFileReaderFactory: F fun setZimFileOrFileDescriptor( file: File? = null, 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 + } + + assetDescriptorFilePath != null -> { + assetDescriptorFilePath == zimCanonicalPath + } + + else -> false + } + fun getPageUrlFromTitle(title: String) = zimFileReader?.getPageUrlFrom(title) fun getRandomArticleUrl() = zimFileReader?.getRandomArticleUrl() @@ -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