Improved article loading.

* We are previously checking `hasEntryByPath`, `hasEntryByTitle`, `mainEntry.isRedirect` which are internally calling the same function as we are calling after checking this condition so it would be better to directly use those function to avoid calling same function twice see more details https://github.com/kiwix/java-libkiwix/pull/60.
This commit is contained in:
MohitMali 2023-08-02 19:05:11 +05:30
parent bfdc00a009
commit 5fec01c695

View File

@ -36,7 +36,6 @@ import org.kiwix.kiwixmobile.core.utils.files.FileUtils
import org.kiwix.libkiwix.JNIKiwixException import org.kiwix.libkiwix.JNIKiwixException
import org.kiwix.libzim.Archive import org.kiwix.libzim.Archive
import org.kiwix.libzim.DirectAccessInfo import org.kiwix.libzim.DirectAccessInfo
import org.kiwix.libzim.EntryNotFoundException
import org.kiwix.libzim.Item import org.kiwix.libzim.Item
import org.kiwix.libzim.Query import org.kiwix.libzim.Query
import org.kiwix.libzim.Search import org.kiwix.libzim.Search
@ -90,11 +89,8 @@ class ZimFileReader constructor(
val mainPage: String? val mainPage: String?
get() = get() =
try { try {
if (jniKiwixReader.mainEntry.isRedirect)
jniKiwixReader.mainEntry.getItem(true).path jniKiwixReader.mainEntry.getItem(true).path
else } catch (ignore: Exception) {
jniKiwixReader.mainEntry.path
} catch (entryNotFound: EntryNotFoundException) {
null null
} }
val id: String get() = jniKiwixReader.uuid val id: String get() = jniKiwixReader.uuid
@ -143,10 +139,11 @@ class ZimFileReader constructor(
} }
fun getPageUrlFrom(title: String): String? = fun getPageUrlFrom(title: String): String? =
if (jniKiwixReader.hasEntryByTitle(title)) try {
jniKiwixReader.getEntryByTitle(title).path jniKiwixReader.getEntryByTitle(title).path
else } catch (ignore: Exception) {
null null
}
fun getRandomArticleUrl(): String? = jniKiwixReader.randomEntry.path fun getRandomArticleUrl(): String? = jniKiwixReader.randomEntry.path
@ -180,12 +177,12 @@ class ZimFileReader constructor(
private fun getActualUrl(url: String, actualUrl: Boolean = false): String { private fun getActualUrl(url: String, actualUrl: Boolean = false): String {
val actualPath = url.toUri().filePath.decodeUrl val actualPath = url.toUri().filePath.decodeUrl
var redirectPath = if (jniKiwixReader.hasEntryByPath(actualPath)) { var redirectPath = try {
jniKiwixReader.getEntryByPath(actualPath) jniKiwixReader.getEntryByPath(actualPath)
.getItem(true) .getItem(true)
.path .path
.replaceWithEncodedString .replaceWithEncodedString
} else { } catch (ignore: Exception) {
actualPath.replaceWithEncodedString actualPath.replaceWithEncodedString
} }
if (actualUrl && url.decodeUrl.contains("?")) { if (actualUrl && url.decodeUrl.contains("?")) {
@ -208,9 +205,9 @@ class ZimFileReader constructor(
} }
private fun loadAsset(uri: String): InputStream? { private fun loadAsset(uri: String): InputStream? {
val article = if (jniKiwixReader.hasEntryByPath(uri.filePath)) { val article = try {
jniKiwixReader.getEntryByPath(uri.filePath).getItem(true) jniKiwixReader.getEntryByPath(uri.filePath).getItem(true)
} else { } catch (ignore: Exception) {
null null
} }
val infoPair = article?.directAccessInformation val infoPair = article?.directAccessInformation
@ -260,9 +257,9 @@ class ZimFileReader constructor(
} }
private fun getItem(url: String): Item? = private fun getItem(url: String): Item? =
if (jniKiwixReader.hasEntryByPath(getActualUrl(url))) { try {
jniKiwixReader.getEntryByPath(getActualUrl(url)).getItem(true) jniKiwixReader.getEntryByPath(getActualUrl(url)).getItem(true)
} else { } catch (ignore: Exception) {
null null
} }