From 7005ad9291a210ffbb5919fc0ecaf7002b7ab9b4 Mon Sep 17 00:00:00 2001 From: 1552980358 <1552980358@qq.com> Date: Wed, 9 Sep 2020 12:55:02 +0800 Subject: [PATCH] RssSource.kt: Modify functions into assigning style Signed-off-by: 1552980358 <1552980358@qq.com> --- .../io/legado/app/data/entities/RssSource.kt | 105 ++++++++---------- 1 file changed, 46 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/RssSource.kt b/app/src/main/java/io/legado/app/data/entities/RssSource.kt index e30707446..300049f82 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssSource.kt @@ -39,80 +39,67 @@ data class RssSource( var header: String? = null, var enableJs: Boolean = false, var loadWithBaseUrl: Boolean = false, - + var customOrder: Int = 0 -) : Parcelable, JsExtensions { - - override fun equals(other: Any?): Boolean { - if (other is RssSource) { - return other.sourceUrl == sourceUrl - } - return false - } - - override fun hashCode(): Int { - return sourceUrl.hashCode() - } - +): Parcelable, JsExtensions { + + override fun equals(other: Any?) = if (other is RssSource) other.sourceUrl == sourceUrl else false + + override fun hashCode() = sourceUrl.hashCode() + @Throws(Exception::class) - fun getHeaderMap(): Map { - val headerMap = HashMap() - headerMap[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: AppConst.userAgent + fun getHeaderMap() = HashMap().apply { + this[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: AppConst.userAgent header?.let { - val header1 = when { - it.startsWith("@js:", true) -> - evalJS(it.substring(4)).toString() - it.startsWith("", true) -> - evalJS(it.substring(4, it.lastIndexOf("<"))).toString() - else -> it - } - GSON.fromJsonObject>(header1)?.let { map -> - headerMap.putAll(map) + GSON.fromJsonObject>( + when { + it.startsWith("@js:", true) -> + evalJS(it.substring(4)).toString() + it.startsWith("", true) -> + evalJS(it.substring(4, it.lastIndexOf("<"))).toString() + else -> it + } + )?.let { map -> + putAll(map) } } - return headerMap } - + /** * 执行JS */ @Throws(Exception::class) - private fun evalJS(jsStr: String): Any { - val bindings = SimpleBindings() - bindings["java"] = this - return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings) - } - + private fun evalJS(jsStr: String): Any = AppConst.SCRIPT_ENGINE.eval(jsStr, SimpleBindings().apply { this["java"] = this@RssSource }) + fun equal(source: RssSource): Boolean { return equal(sourceUrl, source.sourceUrl) - && equal(sourceIcon, source.sourceIcon) - && enabled == source.enabled - && equal(sourceGroup, source.sourceGroup) - && equal(ruleArticles, source.ruleArticles) - && equal(ruleNextPage, source.ruleNextPage) - && equal(ruleTitle, source.ruleTitle) - && equal(rulePubDate, source.rulePubDate) - && equal(ruleDescription, source.ruleDescription) - && equal(ruleLink, source.ruleLink) - && equal(ruleContent, source.ruleContent) - && enableJs == source.enableJs - && loadWithBaseUrl == source.loadWithBaseUrl + && equal(sourceIcon, source.sourceIcon) + && enabled == source.enabled + && equal(sourceGroup, source.sourceGroup) + && equal(ruleArticles, source.ruleArticles) + && equal(ruleNextPage, source.ruleNextPage) + && equal(ruleTitle, source.ruleTitle) + && equal(rulePubDate, source.rulePubDate) + && equal(ruleDescription, source.ruleDescription) + && equal(ruleLink, source.ruleLink) + && equal(ruleContent, source.ruleContent) + && enableJs == source.enableJs + && loadWithBaseUrl == source.loadWithBaseUrl } - + private fun equal(a: String?, b: String?): Boolean { return a == b || (a.isNullOrEmpty() && b.isNullOrEmpty()) } - - fun sortUrls(): LinkedHashMap { - val sortMap = linkedMapOf() - sortUrl?.split("(&&|\n)+".toRegex())?.forEach { c -> - val d = c.split("::") - if (d.size > 1) - sortMap[d[0]] = d[1] - } - if (sortMap.isEmpty()) { - sortMap[""] = sourceUrl + + fun sortUrls(): LinkedHashMap = + linkedMapOf().apply { + sortUrl?.split("(&&|\n)+".toRegex())?.forEach { c -> + val d = c.split("::") + if (d.size > 1) + this[d[0]] = d[1] + } + if (isEmpty()) { + this[""] = sourceUrl + } } - return sortMap - } } \ No newline at end of file