BookSource.kt: Modify functions into assigning style

Signed-off-by: 1552980358 <1552980358@qq.com>
pull/369/head
1552980358 5 years ago
parent 8947c7f8f8
commit 66e24bc1c4
  1. 182
      app/src/main/java/io/legado/app/data/entities/BookSource.kt

@ -11,8 +11,8 @@ import io.legado.app.data.entities.rule.*
import io.legado.app.help.JsExtensions import io.legado.app.help.JsExtensions
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.android.parcel.Parcelize import kotlinx.android.parcel.Parcelize
import java.util.*
import javax.script.SimpleBindings import javax.script.SimpleBindings
import kotlin.collections.HashMap
@Parcelize @Parcelize
@TypeConverters(BookSource.Converters::class) @TypeConverters(BookSource.Converters::class)
@ -42,23 +42,17 @@ data class BookSource(
var ruleBookInfo: BookInfoRule? = null, // 书籍信息页规则 var ruleBookInfo: BookInfoRule? = null, // 书籍信息页规则
var ruleToc: TocRule? = null, // 目录页规则 var ruleToc: TocRule? = null, // 目录页规则
var ruleContent: ContentRule? = null // 正文页规则 var ruleContent: ContentRule? = null // 正文页规则
) : Parcelable, JsExtensions { ): Parcelable, JsExtensions {
override fun hashCode(): Int { override fun hashCode(): Int {
return bookSourceUrl.hashCode() return bookSourceUrl.hashCode()
} }
override fun equals(other: Any?): Boolean { override fun equals(other: Any?) = if (other is BookSource) other.bookSourceUrl == bookSourceUrl else false
if (other is BookSource) {
return other.bookSourceUrl == bookSourceUrl
}
return false
}
@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") ?: userAgent
headerMap[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: userAgent
header?.let { header?.let {
val header1 = when { val header1 = when {
it.startsWith("@js:", true) -> it.startsWith("@js:", true) ->
@ -68,32 +62,21 @@ data class BookSource(
else -> it else -> it
} }
GSON.fromJsonObject<Map<String, String>>(header1)?.let { map -> GSON.fromJsonObject<Map<String, String>>(header1)?.let { map ->
headerMap.putAll(map) putAll(map)
} }
} }
return headerMap }) as Map<String, String>
}
fun getSearchRule() = ruleSearch ?: SearchRule()
fun getSearchRule(): SearchRule {
return ruleSearch ?: SearchRule() fun getExploreRule() = ruleExplore ?: ExploreRule()
}
fun getBookInfoRule() = ruleBookInfo ?: BookInfoRule()
fun getExploreRule(): ExploreRule {
return ruleExplore ?: ExploreRule() fun getTocRule() = ruleToc ?: TocRule()
}
fun getContentRule() = ruleContent ?: ContentRule()
fun getBookInfoRule(): BookInfoRule {
return ruleBookInfo ?: BookInfoRule()
}
fun getTocRule(): TocRule {
return ruleToc ?: TocRule()
}
fun getContentRule(): ContentRule {
return ruleContent ?: ContentRule()
}
fun addGroup(group: String) { fun addGroup(group: String) {
bookSourceGroup?.let { bookSourceGroup?.let {
if (!it.contains(group)) { if (!it.contains(group)) {
@ -103,16 +86,15 @@ data class BookSource(
bookSourceGroup = group bookSourceGroup = group
} }
} }
fun removeGroup(group: String) { fun removeGroup(group: String) {
bookSourceGroup?.splitNotBlank("[,;,;]".toRegex())?.toHashSet()?.let { bookSourceGroup?.splitNotBlank("[,;,;]".toRegex())?.toHashSet()?.let {
it.remove(group) it.remove(group)
bookSourceGroup = TextUtils.join(",", it) bookSourceGroup = TextUtils.join(",", it)
} }
} }
fun getExploreKinds(): ArrayList<ExploreKind>? { fun getExploreKinds() = arrayListOf<ExploreKind>().apply {
val exploreKinds = arrayListOf<ExploreKind>()
exploreUrl?.let { exploreUrl?.let {
var a = it var a = it
if (a.isNotBlank()) { if (a.isNotBlank()) {
@ -135,16 +117,15 @@ data class BookSource(
b.forEach { c -> b.forEach { c ->
val d = c.split("::") val d = c.split("::")
if (d.size > 1) if (d.size > 1)
exploreKinds.add(ExploreKind(d[0], d[1])) add(ExploreKind(d[0], d[1]))
} }
} catch (e: Exception) { } catch (e: Exception) {
exploreKinds.add(ExploreKind(e.localizedMessage ?: "")) add(ExploreKind(e.localizedMessage ?: ""))
} }
} }
} }
return exploreKinds
} }
/** /**
* 执行JS * 执行JS
*/ */
@ -154,86 +135,63 @@ data class BookSource(
bindings["java"] = this bindings["java"] = this
return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings) return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings)
} }
fun equal(source: BookSource): Boolean { fun equal(source: BookSource) =
return equal(bookSourceName, source.bookSourceName) equal(bookSourceName, source.bookSourceName)
&& equal(bookSourceUrl, source.bookSourceUrl) && equal(bookSourceUrl, source.bookSourceUrl)
&& equal(bookSourceGroup, source.bookSourceGroup) && equal(bookSourceGroup, source.bookSourceGroup)
&& bookSourceType == source.bookSourceType && bookSourceType == source.bookSourceType
&& equal(bookUrlPattern, source.bookUrlPattern) && equal(bookUrlPattern, source.bookUrlPattern)
&& equal(bookSourceComment, source.bookSourceComment) && equal(bookSourceComment, source.bookSourceComment)
&& enabled == source.enabled && enabled == source.enabled
&& enabledExplore == source.enabledExplore && enabledExplore == source.enabledExplore
&& equal(header, source.header) && equal(header, source.header)
&& equal(loginUrl, source.loginUrl) && equal(loginUrl, source.loginUrl)
&& equal(exploreUrl, source.exploreUrl) && equal(exploreUrl, source.exploreUrl)
&& equal(searchUrl, source.searchUrl) && equal(searchUrl, source.searchUrl)
&& getSearchRule() == source.getSearchRule() && getSearchRule() == source.getSearchRule()
&& getExploreRule() == source.getExploreRule() && getExploreRule() == source.getExploreRule()
&& getBookInfoRule() == source.getBookInfoRule() && getBookInfoRule() == source.getBookInfoRule()
&& getTocRule() == source.getTocRule() && getTocRule() == source.getTocRule()
&& getContentRule() == source.getContentRule() && getContentRule() == source.getContentRule()
}
private fun equal(a: String?, b: String?) = a == b || (a.isNullOrEmpty() && b.isNullOrEmpty())
private fun equal(a: String?, b: String?): Boolean {
return a == b || (a.isNullOrEmpty() && b.isNullOrEmpty())
}
data class ExploreKind( data class ExploreKind(
var title: String, var title: String,
var url: String? = null var url: String? = null
) )
class Converters { class Converters {
@TypeConverter @TypeConverter
fun exploreRuleToString(exploreRule: ExploreRule?): String? { fun exploreRuleToString(exploreRule: ExploreRule?) = GSON.toJson(exploreRule)
return GSON.toJson(exploreRule)
}
@TypeConverter @TypeConverter
fun stringToExploreRule(json: String?): ExploreRule? { fun stringToExploreRule(json: String?) = GSON.fromJsonObject<ExploreRule>(json)
return GSON.fromJsonObject<ExploreRule>(json)
}
@TypeConverter @TypeConverter
fun searchRuleToString(searchRule: SearchRule?): String? { fun searchRuleToString(searchRule: SearchRule?) = GSON.toJson(searchRule)
return GSON.toJson(searchRule)
}
@TypeConverter @TypeConverter
fun stringToSearchRule(json: String?): SearchRule? { fun stringToSearchRule(json: String?) = GSON.fromJsonObject<SearchRule>(json)
return GSON.fromJsonObject<SearchRule>(json)
}
@TypeConverter @TypeConverter
fun bookInfoRuleToString(bookInfoRule: BookInfoRule?): String? { fun bookInfoRuleToString(bookInfoRule: BookInfoRule?) = GSON.toJson(bookInfoRule)
return GSON.toJson(bookInfoRule)
}
@TypeConverter @TypeConverter
fun stringToBookInfoRule(json: String?): BookInfoRule? { fun stringToBookInfoRule(json: String?) = GSON.fromJsonObject<BookInfoRule>(json)
return GSON.fromJsonObject<BookInfoRule>(json)
}
@TypeConverter @TypeConverter
fun tocRuleToString(tocRule: TocRule?): String? { fun tocRuleToString(tocRule: TocRule?) = GSON.toJson(tocRule)
return GSON.toJson(tocRule)
}
@TypeConverter @TypeConverter
fun stringToTocRule(json: String?): TocRule? { fun stringToTocRule(json: String?) = GSON.fromJsonObject<TocRule>(json)
return GSON.fromJsonObject<TocRule>(json)
}
@TypeConverter @TypeConverter
fun contentRuleToString(contentRule: ContentRule?): String? { fun contentRuleToString(contentRule: ContentRule?) = GSON.toJson(contentRule)
return GSON.toJson(contentRule)
}
@TypeConverter @TypeConverter
fun stringToContentRule(json: String?): ContentRule? { fun stringToContentRule(json: String?) = GSON.fromJsonObject<ContentRule>(json)
return GSON.fromJsonObject<ContentRule>(json)
}
} }
} }
Loading…
Cancel
Save