From 2bfbdef1ebdbd4529588befa65ff6f3b4025bcfd Mon Sep 17 00:00:00 2001 From: 1552980358 <1552980358@qq.com> Date: Wed, 9 Sep 2020 12:25:13 +0800 Subject: [PATCH 1/9] Book.kt: Modify functions into assigning style Signed-off-by: 1552980358 <1552980358@qq.com> --- .../java/io/legado/app/data/entities/Book.kt | 78 +++++++++---------- 1 file changed, 38 insertions(+), 40 deletions(-) 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 c6759c237..c6c62aa4b 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 @@ -55,93 +55,91 @@ data class Book( var originOrder: Int = 0, //书源排序 var useReplaceRule: Boolean = AppConfig.replaceEnableDefault, // 正文使用净化替换规则 var variable: String? = null // 自定义书籍变量信息(用于书源规则检索书籍信息) -) : Parcelable, BaseBook { - +): Parcelable, BaseBook { + fun isLocalBook(): Boolean { return origin == BookType.local } - + fun isLocalTxt(): Boolean { return isLocalBook() && originName.endsWith(".txt", true) } - + fun isEpub(): Boolean { return originName.endsWith(".epub", true) } - + fun isOnLineTxt(): Boolean { return !isLocalBook() && type == 0 } - + override fun equals(other: Any?): Boolean { if (other is Book) { return other.bookUrl == bookUrl } return false } - + override fun hashCode(): Int { return bookUrl.hashCode() } - + @delegate:Transient @delegate:Ignore @IgnoredOnParcel override val variableMap by lazy { GSON.fromJsonObject>(variable) ?: HashMap() } - + override fun putVariable(key: String, value: String) { variableMap[key] = value variable = GSON.toJson(variableMap) } - + @Ignore @IgnoredOnParcel override var infoHtml: String? = null - + @Ignore @IgnoredOnParcel override var tocHtml: String? = null - + fun getRealAuthor() = author.replace(AppPattern.authorRegex, "") - + fun getUnreadChapterNum() = max(totalChapterNum - durChapterIndex - 1, 0) - + fun getDisplayCover() = if (customCoverUrl.isNullOrEmpty()) coverUrl else customCoverUrl - + fun getDisplayIntro() = if (customIntro.isNullOrEmpty()) intro else customIntro - + fun fileCharset(): Charset { return charset(charset ?: "UTF-8") } - + fun getFolderName(): String { return name.replace(AppPattern.fileNameRegex, "") + MD5Utils.md5Encode16(bookUrl) } - - fun toSearchBook(): SearchBook { - return SearchBook( - name = name, - author = author, - kind = kind, - bookUrl = bookUrl, - origin = origin, - originName = originName, - type = type, - wordCount = wordCount, - latestChapterTitle = latestChapterTitle, - coverUrl = coverUrl, - intro = intro, - tocUrl = tocUrl, - originOrder = originOrder, - variable = variable - ).apply { - this.infoHtml = this@Book.infoHtml - this.tocHtml = this@Book.tocHtml - } + + fun toSearchBook() = SearchBook( + name = name, + author = author, + kind = kind, + bookUrl = bookUrl, + origin = origin, + originName = originName, + type = type, + wordCount = wordCount, + latestChapterTitle = latestChapterTitle, + coverUrl = coverUrl, + intro = intro, + tocUrl = tocUrl, + originOrder = originOrder, + variable = variable + ).apply { + this.infoHtml = this@Book.infoHtml + this.tocHtml = this@Book.tocHtml } - + fun changeTo(newBook: Book) { newBook.group = group newBook.order = order @@ -153,7 +151,7 @@ data class Book( delete() App.db.bookDao().insert(newBook) } - + fun delete() { if (ReadBook.book?.bookUrl == bookUrl) { ReadBook.book = null From 8947c7f8f8a76129c376cd2578764646efcca04d Mon Sep 17 00:00:00 2001 From: 1552980358 <1552980358@qq.com> Date: Wed, 9 Sep 2020 12:25:35 +0800 Subject: [PATCH 2/9] BookChapter.kt: Modify functions into assigning style Signed-off-by: 1552980358 <1552980358@qq.com> --- .../java/io/legado/app/data/entities/BookChapter.kt | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/BookChapter.kt b/app/src/main/java/io/legado/app/data/entities/BookChapter.kt index f7a008870..607704e6a 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookChapter.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookChapter.kt @@ -52,16 +52,9 @@ data class BookChapter( variable = GSON.toJson(variableMap) } - override fun hashCode(): Int { - return url.hashCode() - } + override fun hashCode() = url.hashCode() - override fun equals(other: Any?): Boolean { - if (other is BookChapter) { - return other.url == url - } - return false - } + override fun equals(other: Any?) = if (other is BookChapter) other.url == url else false } From 66e24bc1c4f829562120fd551158c1857fbf8633 Mon Sep 17 00:00:00 2001 From: 1552980358 <1552980358@qq.com> Date: Wed, 9 Sep 2020 12:47:11 +0800 Subject: [PATCH 3/9] BookSource.kt: Modify functions into assigning style Signed-off-by: 1552980358 <1552980358@qq.com> --- .../io/legado/app/data/entities/BookSource.kt | 182 +++++++----------- 1 file changed, 70 insertions(+), 112 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/BookSource.kt b/app/src/main/java/io/legado/app/data/entities/BookSource.kt index 68a23b636..a5d7ebf56 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookSource.kt +++ b/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.utils.* import kotlinx.android.parcel.Parcelize -import java.util.* import javax.script.SimpleBindings +import kotlin.collections.HashMap @Parcelize @TypeConverters(BookSource.Converters::class) @@ -42,23 +42,17 @@ data class BookSource( var ruleBookInfo: BookInfoRule? = null, // 书籍信息页规则 var ruleToc: TocRule? = null, // 目录页规则 var ruleContent: ContentRule? = null // 正文页规则 -) : Parcelable, JsExtensions { - +): Parcelable, JsExtensions { + override fun hashCode(): Int { return bookSourceUrl.hashCode() } - - override fun equals(other: Any?): Boolean { - if (other is BookSource) { - return other.bookSourceUrl == bookSourceUrl - } - return false - } - + + override fun equals(other: Any?) = if (other is BookSource) other.bookSourceUrl == bookSourceUrl else false + @Throws(Exception::class) - fun getHeaderMap(): Map { - val headerMap = HashMap() - headerMap[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: userAgent + fun getHeaderMap() = (HashMap().apply { + this[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: userAgent header?.let { val header1 = when { it.startsWith("@js:", true) -> @@ -68,32 +62,21 @@ data class BookSource( else -> it } GSON.fromJsonObject>(header1)?.let { map -> - headerMap.putAll(map) + putAll(map) } } - return headerMap - } - - fun getSearchRule(): SearchRule { - return ruleSearch ?: SearchRule() - } - - fun getExploreRule(): ExploreRule { - return ruleExplore ?: ExploreRule() - } - - fun getBookInfoRule(): BookInfoRule { - return ruleBookInfo ?: BookInfoRule() - } - - fun getTocRule(): TocRule { - return ruleToc ?: TocRule() - } - - fun getContentRule(): ContentRule { - return ruleContent ?: ContentRule() - } - + }) as Map + + fun getSearchRule() = ruleSearch ?: SearchRule() + + fun getExploreRule() = ruleExplore ?: ExploreRule() + + fun getBookInfoRule() = ruleBookInfo ?: BookInfoRule() + + fun getTocRule() = ruleToc ?: TocRule() + + fun getContentRule() = ruleContent ?: ContentRule() + fun addGroup(group: String) { bookSourceGroup?.let { if (!it.contains(group)) { @@ -103,16 +86,15 @@ data class BookSource( bookSourceGroup = group } } - + fun removeGroup(group: String) { bookSourceGroup?.splitNotBlank("[,;,;]".toRegex())?.toHashSet()?.let { it.remove(group) bookSourceGroup = TextUtils.join(",", it) } } - - fun getExploreKinds(): ArrayList? { - val exploreKinds = arrayListOf() + + fun getExploreKinds() = arrayListOf().apply { exploreUrl?.let { var a = it if (a.isNotBlank()) { @@ -135,16 +117,15 @@ data class BookSource( b.forEach { c -> val d = c.split("::") if (d.size > 1) - exploreKinds.add(ExploreKind(d[0], d[1])) + add(ExploreKind(d[0], d[1])) } } catch (e: Exception) { - exploreKinds.add(ExploreKind(e.localizedMessage ?: "")) + add(ExploreKind(e.localizedMessage ?: "")) } } } - return exploreKinds } - + /** * 执行JS */ @@ -154,86 +135,63 @@ data class BookSource( bindings["java"] = this return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings) } - - fun equal(source: BookSource): Boolean { - return equal(bookSourceName, source.bookSourceName) - && equal(bookSourceUrl, source.bookSourceUrl) - && equal(bookSourceGroup, source.bookSourceGroup) - && bookSourceType == source.bookSourceType - && equal(bookUrlPattern, source.bookUrlPattern) - && equal(bookSourceComment, source.bookSourceComment) - && enabled == source.enabled - && enabledExplore == source.enabledExplore - && equal(header, source.header) - && equal(loginUrl, source.loginUrl) - && equal(exploreUrl, source.exploreUrl) - && equal(searchUrl, source.searchUrl) - && getSearchRule() == source.getSearchRule() - && getExploreRule() == source.getExploreRule() - && getBookInfoRule() == source.getBookInfoRule() - && getTocRule() == source.getTocRule() - && getContentRule() == source.getContentRule() - } - - private fun equal(a: String?, b: String?): Boolean { - return a == b || (a.isNullOrEmpty() && b.isNullOrEmpty()) - } - + + fun equal(source: BookSource) = + equal(bookSourceName, source.bookSourceName) + && equal(bookSourceUrl, source.bookSourceUrl) + && equal(bookSourceGroup, source.bookSourceGroup) + && bookSourceType == source.bookSourceType + && equal(bookUrlPattern, source.bookUrlPattern) + && equal(bookSourceComment, source.bookSourceComment) + && enabled == source.enabled + && enabledExplore == source.enabledExplore + && equal(header, source.header) + && equal(loginUrl, source.loginUrl) + && equal(exploreUrl, source.exploreUrl) + && equal(searchUrl, source.searchUrl) + && getSearchRule() == source.getSearchRule() + && getExploreRule() == source.getExploreRule() + && getBookInfoRule() == source.getBookInfoRule() + && getTocRule() == source.getTocRule() + && getContentRule() == source.getContentRule() + + private fun equal(a: String?, b: String?) = a == b || (a.isNullOrEmpty() && b.isNullOrEmpty()) + data class ExploreKind( var title: String, var url: String? = null ) - + class Converters { @TypeConverter - fun exploreRuleToString(exploreRule: ExploreRule?): String? { - return GSON.toJson(exploreRule) - } - + fun exploreRuleToString(exploreRule: ExploreRule?) = GSON.toJson(exploreRule) + @TypeConverter - fun stringToExploreRule(json: String?): ExploreRule? { - return GSON.fromJsonObject(json) - } - + fun stringToExploreRule(json: String?) = GSON.fromJsonObject(json) + @TypeConverter - fun searchRuleToString(searchRule: SearchRule?): String? { - return GSON.toJson(searchRule) - } - + fun searchRuleToString(searchRule: SearchRule?) = GSON.toJson(searchRule) + @TypeConverter - fun stringToSearchRule(json: String?): SearchRule? { - return GSON.fromJsonObject(json) - } - + fun stringToSearchRule(json: String?) = GSON.fromJsonObject(json) + @TypeConverter - fun bookInfoRuleToString(bookInfoRule: BookInfoRule?): String? { - return GSON.toJson(bookInfoRule) - } - + fun bookInfoRuleToString(bookInfoRule: BookInfoRule?) = GSON.toJson(bookInfoRule) + @TypeConverter - fun stringToBookInfoRule(json: String?): BookInfoRule? { - return GSON.fromJsonObject(json) - } - + fun stringToBookInfoRule(json: String?) = GSON.fromJsonObject(json) + @TypeConverter - fun tocRuleToString(tocRule: TocRule?): String? { - return GSON.toJson(tocRule) - } - + fun tocRuleToString(tocRule: TocRule?) = GSON.toJson(tocRule) + @TypeConverter - fun stringToTocRule(json: String?): TocRule? { - return GSON.fromJsonObject(json) - } - + fun stringToTocRule(json: String?) = GSON.fromJsonObject(json) + @TypeConverter - fun contentRuleToString(contentRule: ContentRule?): String? { - return GSON.toJson(contentRule) - } - + fun contentRuleToString(contentRule: ContentRule?) = GSON.toJson(contentRule) + @TypeConverter - fun stringToContentRule(json: String?): ContentRule? { - return GSON.fromJsonObject(json) - } - + fun stringToContentRule(json: String?) = GSON.fromJsonObject(json) + } } \ No newline at end of file From 011b7c8255b47c016c1eed3af55e9c2863da7b15 Mon Sep 17 00:00:00 2001 From: 1552980358 <1552980358@qq.com> Date: Wed, 9 Sep 2020 12:49:20 +0800 Subject: [PATCH 4/9] RssArticle.kt: Modify functions into assigning style Signed-off-by: 1552980358 <1552980358@qq.com> --- .../io/legado/app/data/entities/RssArticle.kt | 43 ++++++++----------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/RssArticle.kt b/app/src/main/java/io/legado/app/data/entities/RssArticle.kt index d9ae5531b..fda4769d2 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssArticle.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssArticle.kt @@ -19,32 +19,23 @@ data class RssArticle( var image: String? = null, var read: Boolean = false ) { - - override fun hashCode(): Int { - return link.hashCode() - } - + + override fun hashCode() = link.hashCode() + override fun equals(other: Any?): Boolean { - if (other == null) { - return false - } - if (other is RssArticle) { - return origin == other.origin && link == other.link - } - return false - } - - fun toStar(): RssStar { - return RssStar( - origin = origin, - sort = sort, - title = title, - starTime = System.currentTimeMillis(), - link = link, - pubDate = pubDate, - description = description, - content = content, - image = image - ) + other ?: return false + return if (other is RssArticle) origin == other.origin && link == other.link else false } + + fun toStar() = RssStar( + origin = origin, + sort = sort, + title = title, + starTime = System.currentTimeMillis(), + link = link, + pubDate = pubDate, + description = description, + content = content, + image = image + ) } \ No newline at end of file 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 5/9] 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 From 56fac707054f7586cdfd08d15469b7c3c283fe28 Mon Sep 17 00:00:00 2001 From: 1552980358 <1552980358@qq.com> Date: Wed, 9 Sep 2020 12:55:43 +0800 Subject: [PATCH 6/9] BookSource.kt: Modify redundant procedure Signed-off-by: 1552980358 <1552980358@qq.com> --- .../io/legado/app/data/entities/BookSource.kt | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/BookSource.kt b/app/src/main/java/io/legado/app/data/entities/BookSource.kt index a5d7ebf56..bddfe1ab5 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookSource.kt @@ -54,14 +54,15 @@ data class BookSource( fun getHeaderMap() = (HashMap().apply { this[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: 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 -> + 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) } } From e6bb108818dc97c4d27fb11f07ab0fdff1f2295d Mon Sep 17 00:00:00 2001 From: 1552980358 <1552980358@qq.com> Date: Wed, 9 Sep 2020 13:12:21 +0800 Subject: [PATCH 7/9] RssStar.kt: Modify function with assigning method Signed-off-by: 1552980358 <1552980358@qq.com> --- app/src/main/java/io/legado/app/data/entities/RssStar.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/RssStar.kt b/app/src/main/java/io/legado/app/data/entities/RssStar.kt index 94196416b..7e78550bb 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssStar.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssStar.kt @@ -18,8 +18,7 @@ data class RssStar( var content: String? = null, var image: String? = null ) { - fun toRssArticle(): RssArticle { - return RssArticle( + fun toRssArticle() = RssArticle( origin = origin, sort = sort, title = title, @@ -29,5 +28,4 @@ data class RssStar( content = content, image = image ) - } } \ No newline at end of file From 763bc1585e353d99e812d7b008eb0433cb18c127 Mon Sep 17 00:00:00 2001 From: 1552980358 <1552980358@qq.com> Date: Wed, 9 Sep 2020 13:14:33 +0800 Subject: [PATCH 8/9] SearchBook.kt: Modify function with assigning method Signed-off-by: 1552980358 <1552980358@qq.com> --- .../io/legado/app/data/entities/SearchBook.kt | 75 ++++++++----------- 1 file changed, 32 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/SearchBook.kt b/app/src/main/java/io/legado/app/data/entities/SearchBook.kt index eb7ea0c10..71bd9016f 100644 --- a/app/src/main/java/io/legado/app/data/entities/SearchBook.kt +++ b/app/src/main/java/io/legado/app/data/entities/SearchBook.kt @@ -36,54 +36,45 @@ data class SearchBook( var time: Long = System.currentTimeMillis(), var variable: String? = null, var originOrder: Int = 0 -) : Parcelable, BaseBook, Comparable { - +): Parcelable, BaseBook, Comparable { + @Ignore @IgnoredOnParcel override var infoHtml: String? = null - + @Ignore @IgnoredOnParcel override var tocHtml: String? = null - - override fun equals(other: Any?): Boolean { - if (other is SearchBook) { - if (other.bookUrl == bookUrl) { - return true - } - } - return false - } - - override fun hashCode(): Int { - return bookUrl.hashCode() - } - + + override fun equals(other: Any?) = other is SearchBook && other.bookUrl == bookUrl + + override fun hashCode() = bookUrl.hashCode() + override fun compareTo(other: SearchBook): Int { return other.originOrder - this.originOrder } - + @delegate:Transient @delegate:Ignore @IgnoredOnParcel override val variableMap by lazy { GSON.fromJsonObject>(variable) ?: HashMap() } - + override fun putVariable(key: String, value: String) { variableMap[key] = value variable = GSON.toJson(variableMap) } - + @delegate:Transient @delegate:Ignore @IgnoredOnParcel val origins: LinkedHashSet by lazy { linkedSetOf(origin) } - + fun addOrigin(origin: String) { origins.add(origin) } - + fun getDisplayLastChapterTitle(): String { latestChapterTitle?.let { if (it.isNotEmpty()) { @@ -92,26 +83,24 @@ data class SearchBook( } return "无最新章节" } - - fun toBook(): Book { - return Book( - name = name, - author = author, - kind = kind, - bookUrl = bookUrl, - origin = origin, - originName = originName, - type = type, - wordCount = wordCount, - latestChapterTitle = latestChapterTitle, - coverUrl = coverUrl, - intro = intro, - tocUrl = tocUrl, - originOrder = originOrder, - variable = variable - ).apply { - this.infoHtml = this@SearchBook.infoHtml - this.tocUrl = this@SearchBook.tocUrl - } + + fun toBook() = Book( + name = name, + author = author, + kind = kind, + bookUrl = bookUrl, + origin = origin, + originName = originName, + type = type, + wordCount = wordCount, + latestChapterTitle = latestChapterTitle, + coverUrl = coverUrl, + intro = intro, + tocUrl = tocUrl, + originOrder = originOrder, + variable = variable + ).apply { + this.infoHtml = this@SearchBook.infoHtml + this.tocUrl = this@SearchBook.tocUrl } } \ No newline at end of file From ab7babd2538c9240cd24ca0f4f58ae98b5cd8760 Mon Sep 17 00:00:00 2001 From: 1552980358 <1552980358@qq.com> Date: Wed, 9 Sep 2020 13:16:29 +0800 Subject: [PATCH 9/9] AppDatabase.kt: Modify function with assigning method Signed-off-by: 1552980358 <1552980358@qq.com> --- .../java/io/legado/app/data/AppDatabase.kt | 49 +++++++++---------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt index fc3401beb..a865f8d86 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -19,14 +19,14 @@ import io.legado.app.data.entities.* version = 20, exportSchema = true ) -abstract class AppDatabase : RoomDatabase() { - +abstract class AppDatabase: RoomDatabase() { + companion object { - + private const val DATABASE_NAME = "legado.db" - - fun createDatabase(context: Context): AppDatabase { - return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME) + + fun createDatabase(context: Context) = + Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME) .fallbackToDestructiveMigration() .addMigrations( migration_10_11, @@ -41,9 +41,8 @@ abstract class AppDatabase : RoomDatabase() { ) .allowMainThreadQueries() .build() - } - - private val migration_10_11 = object : Migration(10, 11) { + + private val migration_10_11 = object: Migration(10, 11) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("DROP TABLE txtTocRules") database.execSQL( @@ -55,20 +54,20 @@ abstract class AppDatabase : RoomDatabase() { ) } } - - private val migration_11_12 = object : Migration(11, 12) { + + private val migration_11_12 = object: Migration(11, 12) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE rssSources ADD style TEXT ") } } - - private val migration_12_13 = object : Migration(12, 13) { + + private val migration_12_13 = object: Migration(12, 13) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE rssSources ADD articleStyle INTEGER NOT NULL DEFAULT 0 ") } } - - private val migration_13_14 = object : Migration(13, 14) { + + private val migration_13_14 = object: Migration(13, 14) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL( """ @@ -86,26 +85,26 @@ abstract class AppDatabase : RoomDatabase() { database.execSQL("ALTER TABLE books_new RENAME TO books") } } - - private val migration_14_15 = object : Migration(14, 15) { + + private val migration_14_15 = object: Migration(14, 15) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE bookmarks ADD bookAuthor TEXT NOT NULL DEFAULT ''") } } - - private val migration_15_17 = object : Migration(15, 17) { + + private val migration_15_17 = object: Migration(15, 17) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("CREATE TABLE IF NOT EXISTS `readRecord` (`bookName` TEXT NOT NULL, `readTime` INTEGER NOT NULL, PRIMARY KEY(`bookName`))") } } - - private val migration_17_18 = object : Migration(17, 18) { + + private val migration_17_18 = object: Migration(17, 18) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("CREATE TABLE IF NOT EXISTS `httpTTS` (`id` INTEGER NOT NULL, `name` TEXT NOT NULL, `url` TEXT NOT NULL, PRIMARY KEY(`id`))") } } - - private val migration_18_19 = object : Migration(18, 19) { + + private val migration_18_19 = object: Migration(18, 19) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("CREATE TABLE IF NOT EXISTS `readRecordNew` (`androidId` TEXT NOT NULL, `bookName` TEXT NOT NULL, `readTime` INTEGER NOT NULL, PRIMARY KEY(`androidId`, `bookName`))") database.execSQL("INSERT INTO readRecordNew(androidId, bookName, readTime) select '${App.androidId}' as androidId, bookName, readTime from readRecord") @@ -113,13 +112,13 @@ abstract class AppDatabase : RoomDatabase() { database.execSQL("ALTER TABLE readRecordNew RENAME TO readRecord") } } - private val migration_19_20 = object : Migration(19,20) { + private val migration_19_20 = object: Migration(19, 20) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE book_sources ADD bookSourceComment TEXT") } } } - + abstract fun bookDao(): BookDao abstract fun bookGroupDao(): BookGroupDao abstract fun bookSourceDao(): BookSourceDao