From d521acd3a65f57fe5420ea3428addb85e9c0e60c Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Mon, 24 Feb 2025 16:23:35 +0530 Subject: [PATCH] Fixed: UseOrEmpty issue of detekt to simplify the conditions and use the already available extension functions --- .../kiwix/kiwixmobile/core/JNIInitialiser.kt | 2 +- .../kiwixmobile/core/dao/LibkiwixBookmarks.kt | 2 +- .../kiwixmobile/core/dao/NewBookmarksDao.kt | 6 ++--- .../core/dao/NewRecentSearchDao.kt | 2 +- .../core/dao/entities/BookOnDiskEntity.kt | 6 ++--- .../core/dao/entities/HistoryRoomEntity.kt | 2 +- .../core/data/remote/BasicAuthInterceptor.kt | 2 +- .../core/downloader/model/DownloadItem.kt | 2 +- .../kiwixmobile/core/main/AddNoteDialog.kt | 6 ++--- .../core/main/KiwixTextToSpeech.kt | 2 +- .../core/reader/ZimReaderContainer.kt | 2 +- .../kiwixmobile/core/utils/ServerUtils.kt | 2 +- .../core/utils/SharedPreferenceUtil.kt | 2 +- .../kiwixmobile/core/utils/StyleUtils.kt | 2 +- .../kiwixmobile/core/zim_manager/KiloByte.kt | 17 ++++++------ .../kiwixmobile/core/zim_manager/KiwixTag.kt | 27 +++++++++---------- .../core/zim_manager/MountPointProducer.kt | 2 +- 17 files changed, 42 insertions(+), 44 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/JNIInitialiser.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/JNIInitialiser.kt index 25b2fae00..35dc4c0b2 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/JNIInitialiser.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/JNIInitialiser.kt @@ -36,7 +36,7 @@ internal class JNIInitialiser @Inject constructor(context: Context, jniKiwix: JN if (!icuDir.exists()) { icuDir.mkdirs() } - val icuFileNames = context.assets.list("icu") ?: emptyArray() + val icuFileNames = context.assets.list("icu").orEmpty() for (icuFileName in icuFileNames) { val icuDataFile = File(icuDir, icuFileName) if (!icuDataFile.exists()) { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/LibkiwixBookmarks.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/LibkiwixBookmarks.kt index 8694cabf7..c4613f28c 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/LibkiwixBookmarks.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/LibkiwixBookmarks.kt @@ -125,7 +125,7 @@ class LibkiwixBookmarks @Inject constructor( getBookmarksList() .filter { it.zimId == reader.id } .map(LibkiwixBookmarkItem::bookmarkUrl) - } ?: emptyList() + }.orEmpty() } fun bookmarkUrlsForCurrentBook(zimId: String): Flowable> = diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewBookmarksDao.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewBookmarksDao.kt index f7875c2b0..239a32b8b 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewBookmarksDao.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewBookmarksDao.kt @@ -56,13 +56,13 @@ class NewBookmarksDao @Inject constructor(val box: Box) : PageDa box.query { equal( BookmarkEntity_.zimId, - zimFileReader?.id ?: "", + zimFileReader?.id.orEmpty(), QueryBuilder.StringOrder.CASE_INSENSITIVE ) .or() .equal( BookmarkEntity_.zimName, - zimFileReader?.name ?: "", + zimFileReader?.name.orEmpty(), QueryBuilder.StringOrder.CASE_INSENSITIVE ) order(BookmarkEntity_.bookmarkTitle) @@ -76,7 +76,7 @@ class NewBookmarksDao @Inject constructor(val box: Box) : PageDa box.query { equal( BookmarkEntity_.zimId, - zimFileReader?.id ?: "", + zimFileReader?.id.orEmpty(), QueryBuilder.StringOrder.CASE_INSENSITIVE ) .or() diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchDao.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchDao.kt index 14f4edb39..3fd99a7d7 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchDao.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchDao.kt @@ -35,7 +35,7 @@ class NewRecentSearchDao @Inject constructor( box.query { equal( RecentSearchEntity_.zimId, - zimId ?: "", + zimId.orEmpty(), QueryBuilder.StringOrder.CASE_INSENSITIVE ) orderDesc(RecentSearchEntity_.id) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/BookOnDiskEntity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/BookOnDiskEntity.kt index 49b134cb3..7d759c469 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/BookOnDiskEntity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/BookOnDiskEntity.kt @@ -90,13 +90,13 @@ data class BookOnDiskEntity( class ZimSourceConverter : PropertyConverter { override fun convertToDatabaseValue(entityProperty: ZimReaderSource?): String = - entityProperty?.toDatabase() ?: "" + entityProperty?.toDatabase().orEmpty() override fun convertToEntityProperty(databaseValue: String?): ZimReaderSource = fromDatabaseValue(databaseValue) ?: ZimReaderSource(File("")) } class StringToFileConverter : PropertyConverter { - override fun convertToDatabaseValue(entityProperty: File?) = entityProperty?.path ?: "" - override fun convertToEntityProperty(databaseValue: String?) = File(databaseValue ?: "") + override fun convertToDatabaseValue(entityProperty: File?) = entityProperty?.path.orEmpty() + override fun convertToEntityProperty(databaseValue: String?) = File(databaseValue.orEmpty()) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/HistoryRoomEntity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/HistoryRoomEntity.kt index 8335ba2a8..51b3e4cae 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/HistoryRoomEntity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/HistoryRoomEntity.kt @@ -57,7 +57,7 @@ data class HistoryRoomEntity( class ZimSourceRoomConverter { @TypeConverter fun convertToDatabaseValue(entityProperty: ZimReaderSource?): String = - entityProperty?.toDatabase() ?: "" + entityProperty?.toDatabase().orEmpty() @TypeConverter fun convertToEntityProperty(databaseValue: String?): ZimReaderSource = diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/remote/BasicAuthInterceptor.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/data/remote/BasicAuthInterceptor.kt index 6d60f4246..40695ed17 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/remote/BasicAuthInterceptor.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/remote/BasicAuthInterceptor.kt @@ -31,7 +31,7 @@ class BasicAuthInterceptor : Interceptor { val request: Request = chain.request() val url = request.url.toString() if (url.isAuthenticationUrl) { - val userNameAndPassword = System.getenv(url.secretKey) ?: "" + val userNameAndPassword = System.getenv(url.secretKey).orEmpty() val userName = userNameAndPassword.substringBefore(":", "") val password = userNameAndPassword.substringAfter(":", "") val credentials = okhttp3.Credentials.basic(userName, password) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/model/DownloadItem.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/model/DownloadItem.kt index 0bb856edb..b0cf08251 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/model/DownloadItem.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/model/DownloadItem.kt @@ -43,7 +43,7 @@ data class DownloadItem( val eta: Seconds, val downloadState: DownloadState ) { - val readableEta: CharSequence = eta.takeIf { it.seconds > 0L }?.toHumanReadableTime() ?: "" + val readableEta: CharSequence = eta.takeIf { it.seconds > 0L }?.toHumanReadableTime().orEmpty() constructor(downloadModel: DownloadModel) : this( downloadModel.downloadId, diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt index 7c1ed6f8c..2f707c16f 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt @@ -182,8 +182,8 @@ class AddNoteDialog : DialogFragment() { private val zimNoteDirectoryName: String get() { - val noteDirectoryName = getTextAfterLastSlashWithoutExtension(zimFileName ?: "") - return (if (noteDirectoryName.isNotEmpty()) noteDirectoryName else zimFileTitle) ?: "" + val noteDirectoryName = getTextAfterLastSlashWithoutExtension(zimFileName.orEmpty()) + return (if (noteDirectoryName.isNotEmpty()) noteDirectoryName else zimFileTitle).orEmpty() } private fun getArticleNoteFileName(): String { @@ -199,7 +199,7 @@ class AddNoteDialog : DialogFragment() { } else { noteFileName = getTextAfterLastSlashWithoutExtension(articleUrl) } - return noteFileName.ifEmpty { articleTitle } ?: "" + return noteFileName.ifEmpty { articleTitle }.orEmpty() } /* From ".../Kiwix/granbluefantasy_en_all_all_nopic_2018-10.zim", returns "granbluefantasy_en_all_all_nopic_2018-10" diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.kt index 00e068590..9d609cd0b 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.kt @@ -145,7 +145,7 @@ class KiwixTextToSpeech internal constructor( languageAvailabilityResult == LANG_MISSING_DATA || languageAvailabilityResult == LANG_NOT_SUPPORTED - private fun getFeatures(tts: TextToSpeech?): Set = tts?.voice?.features ?: setOf() + private fun getFeatures(tts: TextToSpeech?): Set = tts?.voice?.features.orEmpty() private fun loadURL(webView: WebView) { // We use JavaScript to get the content of the page conveniently, earlier making some 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 4955c5271..348100579 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 @@ -51,7 +51,7 @@ class ZimReaderContainer @Inject constructor(private val zimFileReaderFactory: F fun getRandomArticleUrl() = zimFileReader?.getRandomArticleUrl() fun isRedirect(url: String): Boolean = zimFileReader?.isRedirect(url) == true - fun getRedirect(url: String): String = zimFileReader?.getRedirect(url) ?: "" + fun getRedirect(url: String): String = zimFileReader?.getRedirect(url).orEmpty() fun load(url: String, requestHeaders: Map): WebResourceResponse = runBlocking { return@runBlocking WebResourceResponse( zimFileReader?.getMimeTypeFromUrl(url), diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt index c5ef660a8..e105994d0 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt @@ -51,7 +51,7 @@ object ServerUtils { } private fun formatLocalAddress(inetAddress: InetAddress): String = - (inetAddress.hostAddress + "\n").takeIf { inetAddress.isSiteLocalAddress } ?: "" + (inetAddress.hostAddress + "\n").takeIf { inetAddress.isSiteLocalAddress }.orEmpty() @Suppress("MagicNumber") fun formatIpForAndroidPie(ip: String): String { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt index d1776d780..830ac5574 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt @@ -89,7 +89,7 @@ class SharedPreferenceUtil @Inject constructor(val context: Context) { get() = sharedPreferences.getString(PREF_LANG, "") ?: Locale.ROOT.toString() val prefDeviceDefaultLanguage: String - get() = sharedPreferences.getString(PREF_DEVICE_DEFAULT_LANG, "") ?: "" + get() = sharedPreferences.getString(PREF_DEVICE_DEFAULT_LANG, "").orEmpty() val prefIsBookmarksMigrated: Boolean get() = sharedPreferences.getBoolean(PREF_BOOKMARKS_MIGRATED, false) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/StyleUtils.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/StyleUtils.kt index d6234db48..ce8b7826b 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/StyleUtils.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/StyleUtils.kt @@ -44,7 +44,7 @@ object StyleUtils { } @JvmStatic fun String?.fromHtml(): Spanned { - return (this ?: "").let { + return (this.orEmpty()).let { Html.fromHtml( this, Html.FROM_HTML_MODE_LEGACY diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/zim_manager/KiloByte.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/zim_manager/KiloByte.kt index 35a41c312..906a317ff 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/zim_manager/KiloByte.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/zim_manager/KiloByte.kt @@ -25,13 +25,12 @@ import kotlin.math.pow @JvmInline value class KiloByte(private val kilobyteString: String?) { val humanReadable - get() = - kilobyteString?.toLongOrNull()?.let { - val units = arrayOf("KB", "MB", "GB", "TB") - val conversion = (log10(it.toDouble()) / log10(1024.0)).toInt() - DecimalFormat("#,##0.#") - .format(it / 1024.0.pow(conversion.toDouble())) + - " " + - units[conversion] - } ?: "" + get() = kilobyteString?.toLongOrNull()?.let { + val units = arrayOf("KB", "MB", "GB", "TB") + val conversion = (log10(it.toDouble()) / log10(1024.0)).toInt() + DecimalFormat("#,##0.#") + .format(it / 1024.0.pow(conversion.toDouble())) + + " " + + units[conversion] + }.orEmpty() } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/zim_manager/KiwixTag.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/zim_manager/KiwixTag.kt index e81538d3c..ed22faa13 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/zim_manager/KiwixTag.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/zim_manager/KiwixTag.kt @@ -27,20 +27,19 @@ import org.kiwix.kiwixmobile.core.zim_manager.KiwixTag.TagValue.YES sealed class KiwixTag { companion object { - fun from(tagString: String?) = - tagString?.split(";") - ?.map { tags -> - val split = tags.split(":") - val value = split.getOrNull(1) - when (val tag = split[0]) { - "_ftindex" -> FtIndexTag(value) - "_pictures" -> PicturesTag(value) - "_videos" -> VideoTag(value) - "_details" -> DetailsTag(value) - "_category" -> CategoryTag(value) - else -> value?.let { ArbitraryTag(tag, it) } ?: TagOnly(tag) - } - } ?: emptyList() + fun from(tagString: String?) = tagString?.split(";") + ?.map { tags -> + val split = tags.split(":") + val value = split.getOrNull(1) + when (val tag = split[0]) { + "_ftindex" -> FtIndexTag(value) + "_pictures" -> PicturesTag(value) + "_videos" -> VideoTag(value) + "_details" -> DetailsTag(value) + "_category" -> CategoryTag(value) + else -> value?.let { ArbitraryTag(tag, it) } ?: TagOnly(tag) + } + }.orEmpty() data class CategoryTag(val categoryValue: String?) : KiwixTag() data class ArbitraryTag(val tag: String, val value: String) : KiwixTag() diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/zim_manager/MountPointProducer.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/zim_manager/MountPointProducer.kt index a3075f63a..db44e5e8b 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/zim_manager/MountPointProducer.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/zim_manager/MountPointProducer.kt @@ -27,7 +27,7 @@ class MountPointProducer @Inject constructor() { .takeIf(File::exists) ?.readLines() ?.map { MountInfo(it.split(" ")) } - ?: emptyList() + .orEmpty() } data class MountInfo(val device: String, val mountPoint: String, val fileSystem: String) {