From fd72b858155ad8b87087e333dec60997072c42be Mon Sep 17 00:00:00 2001 From: Administrator <1760316362@qq.com> Date: Thu, 4 Jul 2019 11:39:13 +0800 Subject: [PATCH 1/2] update --- .../io/legado/app/data/dao/BookChapterDao.kt | 2 ++ .../java/io/legado/app/data/entities/Book.kt | 2 +- .../io/legado/app/utils/GsonExtensions.kt | 21 +++++++++++-------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt b/app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt index ad45e6290..9cfe22e8d 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt @@ -1,9 +1,11 @@ package io.legado.app.data.dao +import androidx.room.Dao import androidx.room.Insert import androidx.room.OnConflictStrategy import io.legado.app.data.entities.BookChapter +@Dao interface BookChapterDao { diff --git a/app/src/main/java/io/legado/app/data/entities/Book.kt b/app/src/main/java/io/legado/app/data/entities/Book.kt index cf7ded306..315ddf6a5 100644 --- a/app/src/main/java/io/legado/app/data/entities/Book.kt +++ b/app/src/main/java/io/legado/app/data/entities/Book.kt @@ -69,7 +69,7 @@ data class Book( variableMap = if (isEmpty(variable)) { HashMap() } else { - GSON.fromJsonObject>(variable!!) + GSON.fromJsonObject>(variable) } } } diff --git a/app/src/main/java/io/legado/app/utils/GsonExtensions.kt b/app/src/main/java/io/legado/app/utils/GsonExtensions.kt index 593979856..bccf6e6ba 100644 --- a/app/src/main/java/io/legado/app/utils/GsonExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/GsonExtensions.kt @@ -3,20 +3,23 @@ package io.legado.app.utils import com.google.gson.Gson import com.google.gson.GsonBuilder import com.google.gson.JsonParser +import com.google.gson.reflect.TypeToken import org.jetbrains.anko.attempt val GSON: Gson by lazy { GsonBuilder().create() } -inline fun Gson.fromJsonObject(json: String?): T? = fromJson(json, T::class.java) +inline fun genericType() = object : TypeToken() {}.type -inline fun Gson.fromJsonArray(json: String): ArrayList? { +inline fun Gson.fromJsonObject(json: String?): T? {//可转成任意类型 return attempt { - with(JsonParser().parse(json).asJsonArray) { - val result = ArrayList() - for (obj in this) { - attempt { fromJson(obj, T::class.java) }.value?.run { result.add(this) } - } - result - } + val result: T? = fromJson(json, genericType()) + result + }.value +} + +inline fun Gson.fromJsonArray(json: String?): List? { + return attempt { + val result: List? = fromJson(json, genericType>()) + result }.value } \ No newline at end of file From 78db8194dc411c4151790868b1ed3855dc367bd5 Mon Sep 17 00:00:00 2001 From: Administrator <1760316362@qq.com> Date: Thu, 4 Jul 2019 11:53:22 +0800 Subject: [PATCH 2/2] StringExtensions --- .../app/model/analyzeRule/AnalyzeByJSoup.kt | 2 +- .../io/legado/app/utils/StringExtensions.kt | 35 ++++++++----------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt index 119ae12ce..8b4bcc811 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt @@ -414,7 +414,7 @@ class AnalyzeByJSoup { var replacement = "" init { - if (startWithIgnoreCase(ruleStr, "@CSS:")) { + if (ruleStr.startWithIgnoreCase("@CSS:")) { isCss = true elementsRule = ruleStr.substring(5).trim { it <= ' ' } } else { diff --git a/app/src/main/java/io/legado/app/utils/StringExtensions.kt b/app/src/main/java/io/legado/app/utils/StringExtensions.kt index 2abbf078d..0ec34e16e 100644 --- a/app/src/main/java/io/legado/app/utils/StringExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/StringExtensions.kt @@ -1,38 +1,33 @@ package io.legado.app.utils -import android.text.TextUtils - // import org.apache.commons.text.StringEscapeUtils fun String?.safeTrim() = if (this.isNullOrBlank()) null else this.trim() -fun String.isAbsUrl() = this.startsWith("http://", true) +fun String?.isAbsUrl() = if (this.isNullOrBlank()) false else this.startsWith("http://", true) || this.startsWith("https://", true) -fun String.isJson(): Boolean = kotlin.run { - var result = false - if (!TextUtils.isEmpty(this)) { - val str = this.trim() - if (str.startsWith("{") && str.endsWith("}")) { - result = true - } else if (str.startsWith("[") && str.endsWith("]")) { - result = true - } +fun String?.isJson(): Boolean = this?.run { + val str = this.trim() + when { + str.startsWith("{") && str.endsWith("}") -> true + str.startsWith("[") && str.endsWith("]") -> true + else -> false } - return result -} +} ?: false -fun String.htmlFormat(): String = if (TextUtils.isEmpty(this)) "" else +fun String?.htmlFormat(): String = if (this.isNullOrBlank()) "" else this.replace("(?i)<(br[\\s/]*|/*p.*?|/*div.*?)>".toRegex(), "\n")// 替换特定标签为换行符 .replace("<[script>]*.*?>| ".toRegex(), "")// 删除script标签对和空格转义符 .replace("\\s*\\n+\\s*".toRegex(), "\n  ")// 移除空行,并增加段前缩进2个汉字 .replace("^[\\n\\s]+".toRegex(), "  ")//移除开头空行,并增加段前缩进2个汉字 .replace("[\\n\\s]+$".toRegex(), "") //移除尾部空行 -fun String.splitNotBlank(delim: String) = if (!this.contains(delim)) sequenceOf(this) else - this.split(delim).asSequence().map { it.trim() }.filterNot { it.isBlank() } +fun String?.splitNotBlank(delim: String) = this?.run { + if (!this.contains(delim)) sequenceOf(this) else + this.split(delim).asSequence().map { it.trim() }.filterNot { it.isBlank() } +} -fun startWithIgnoreCase(src: String?, obj: String?): Boolean { - if (src == null || obj == null) return false - return if (obj.length > src.length) false else src.substring(0, obj.length).equals(obj, ignoreCase = true) +fun String?.startWithIgnoreCase(start: String): Boolean { + return if (this.isNullOrBlank()) false else startsWith(start, true) } \ No newline at end of file