RssSource.kt: Modify functions into assigning style

Signed-off-by: 1552980358 <1552980358@qq.com>
pull/369/head
1552980358 5 years ago
parent 011b7c8255
commit 7005ad9291
  1. 105
      app/src/main/java/io/legado/app/data/entities/RssSource.kt

@ -39,80 +39,67 @@ data class RssSource(
var header: String? = null, var header: String? = null,
var enableJs: Boolean = false, var enableJs: Boolean = false,
var loadWithBaseUrl: Boolean = false, var loadWithBaseUrl: Boolean = false,
var customOrder: Int = 0 var customOrder: Int = 0
) : Parcelable, JsExtensions { ): Parcelable, JsExtensions {
override fun equals(other: Any?): Boolean { override fun equals(other: Any?) = if (other is RssSource) other.sourceUrl == sourceUrl else false
if (other is RssSource) {
return other.sourceUrl == sourceUrl override fun hashCode() = sourceUrl.hashCode()
}
return false
}
override fun hashCode(): Int {
return sourceUrl.hashCode()
}
@Throws(Exception::class) @Throws(Exception::class)
fun getHeaderMap(): Map<String, String> { fun getHeaderMap() = HashMap<String, String>().apply {
val headerMap = HashMap<String, String>() this[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: AppConst.userAgent
headerMap[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: AppConst.userAgent
header?.let { header?.let {
val header1 = when { GSON.fromJsonObject<Map<String, String>>(
it.startsWith("@js:", true) -> when {
evalJS(it.substring(4)).toString() it.startsWith("@js:", true) ->
it.startsWith("<js>", true) -> evalJS(it.substring(4)).toString()
evalJS(it.substring(4, it.lastIndexOf("<"))).toString() it.startsWith("<js>", true) ->
else -> it evalJS(it.substring(4, it.lastIndexOf("<"))).toString()
} else -> it
GSON.fromJsonObject<Map<String, String>>(header1)?.let { map -> }
headerMap.putAll(map) )?.let { map ->
putAll(map)
} }
} }
return headerMap
} }
/** /**
* 执行JS * 执行JS
*/ */
@Throws(Exception::class) @Throws(Exception::class)
private fun evalJS(jsStr: String): Any { private fun evalJS(jsStr: String): Any = AppConst.SCRIPT_ENGINE.eval(jsStr, SimpleBindings().apply { this["java"] = this@RssSource })
val bindings = SimpleBindings()
bindings["java"] = this
return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings)
}
fun equal(source: RssSource): Boolean { fun equal(source: RssSource): Boolean {
return equal(sourceUrl, source.sourceUrl) return equal(sourceUrl, source.sourceUrl)
&& equal(sourceIcon, source.sourceIcon) && equal(sourceIcon, source.sourceIcon)
&& enabled == source.enabled && enabled == source.enabled
&& equal(sourceGroup, source.sourceGroup) && equal(sourceGroup, source.sourceGroup)
&& equal(ruleArticles, source.ruleArticles) && equal(ruleArticles, source.ruleArticles)
&& equal(ruleNextPage, source.ruleNextPage) && equal(ruleNextPage, source.ruleNextPage)
&& equal(ruleTitle, source.ruleTitle) && equal(ruleTitle, source.ruleTitle)
&& equal(rulePubDate, source.rulePubDate) && equal(rulePubDate, source.rulePubDate)
&& equal(ruleDescription, source.ruleDescription) && equal(ruleDescription, source.ruleDescription)
&& equal(ruleLink, source.ruleLink) && equal(ruleLink, source.ruleLink)
&& equal(ruleContent, source.ruleContent) && equal(ruleContent, source.ruleContent)
&& enableJs == source.enableJs && enableJs == source.enableJs
&& loadWithBaseUrl == source.loadWithBaseUrl && loadWithBaseUrl == source.loadWithBaseUrl
} }
private fun equal(a: String?, b: String?): Boolean { private fun equal(a: String?, b: String?): Boolean {
return a == b || (a.isNullOrEmpty() && b.isNullOrEmpty()) return a == b || (a.isNullOrEmpty() && b.isNullOrEmpty())
} }
fun sortUrls(): LinkedHashMap<String, String> { fun sortUrls(): LinkedHashMap<String, String> =
val sortMap = linkedMapOf<String, String>() linkedMapOf<String, String>().apply {
sortUrl?.split("(&&|\n)+".toRegex())?.forEach { c -> sortUrl?.split("(&&|\n)+".toRegex())?.forEach { c ->
val d = c.split("::") val d = c.split("::")
if (d.size > 1) if (d.size > 1)
sortMap[d[0]] = d[1] this[d[0]] = d[1]
} }
if (sortMap.isEmpty()) { if (isEmpty()) {
sortMap[""] = sourceUrl this[""] = sourceUrl
}
} }
return sortMap
}
} }
Loading…
Cancel
Save