Fixed: UseOrEmpty issue of detekt to simplify the conditions and use the already available extension functions

This commit is contained in:
MohitMaliFtechiz 2025-02-24 16:23:35 +05:30
parent 226289fbad
commit d521acd3a6
17 changed files with 42 additions and 44 deletions

View File

@ -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()) {

View File

@ -125,7 +125,7 @@ class LibkiwixBookmarks @Inject constructor(
getBookmarksList()
.filter { it.zimId == reader.id }
.map(LibkiwixBookmarkItem::bookmarkUrl)
} ?: emptyList()
}.orEmpty()
}
fun bookmarkUrlsForCurrentBook(zimId: String): Flowable<List<String>> =

View File

@ -56,13 +56,13 @@ class NewBookmarksDao @Inject constructor(val box: Box<BookmarkEntity>) : 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<BookmarkEntity>) : PageDa
box.query {
equal(
BookmarkEntity_.zimId,
zimFileReader?.id ?: "",
zimFileReader?.id.orEmpty(),
QueryBuilder.StringOrder.CASE_INSENSITIVE
)
.or()

View File

@ -35,7 +35,7 @@ class NewRecentSearchDao @Inject constructor(
box.query {
equal(
RecentSearchEntity_.zimId,
zimId ?: "",
zimId.orEmpty(),
QueryBuilder.StringOrder.CASE_INSENSITIVE
)
orderDesc(RecentSearchEntity_.id)

View File

@ -90,13 +90,13 @@ data class BookOnDiskEntity(
class ZimSourceConverter : PropertyConverter<ZimReaderSource, String> {
override fun convertToDatabaseValue(entityProperty: ZimReaderSource?): String =
entityProperty?.toDatabase() ?: ""
entityProperty?.toDatabase().orEmpty()
override fun convertToEntityProperty(databaseValue: String?): ZimReaderSource =
fromDatabaseValue(databaseValue) ?: ZimReaderSource(File(""))
}
class StringToFileConverter : PropertyConverter<File, String> {
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())
}

View File

@ -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 =

View File

@ -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)

View File

@ -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,

View File

@ -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"

View File

@ -145,7 +145,7 @@ class KiwixTextToSpeech internal constructor(
languageAvailabilityResult == LANG_MISSING_DATA ||
languageAvailabilityResult == LANG_NOT_SUPPORTED
private fun getFeatures(tts: TextToSpeech?): Set<String> = tts?.voice?.features ?: setOf()
private fun getFeatures(tts: TextToSpeech?): Set<String> = tts?.voice?.features.orEmpty()
private fun loadURL(webView: WebView) {
// We use JavaScript to get the content of the page conveniently, earlier making some

View File

@ -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<String, String>): WebResourceResponse = runBlocking {
return@runBlocking WebResourceResponse(
zimFileReader?.getMimeTypeFromUrl(url),

View File

@ -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 {

View File

@ -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)

View File

@ -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

View File

@ -25,13 +25,12 @@ import kotlin.math.pow
@JvmInline
value class KiloByte(private val kilobyteString: String?) {
val humanReadable
get() =
kilobyteString?.toLongOrNull()?.let {
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()
}

View File

@ -27,8 +27,7 @@ import org.kiwix.kiwixmobile.core.zim_manager.KiwixTag.TagValue.YES
sealed class KiwixTag {
companion object {
fun from(tagString: String?) =
tagString?.split(";")
fun from(tagString: String?) = tagString?.split(";")
?.map { tags ->
val split = tags.split(":")
val value = split.getOrNull(1)
@ -40,7 +39,7 @@ sealed class KiwixTag {
"_category" -> CategoryTag(value)
else -> value?.let { ArbitraryTag(tag, it) } ?: TagOnly(tag)
}
} ?: emptyList()
}.orEmpty()
data class CategoryTag(val categoryValue: String?) : KiwixTag()
data class ArbitraryTag(val tag: String, val value: String) : KiwixTag()

View File

@ -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) {