From 765222eb750d0009f3cb177c7a13e8a12e2af69b Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 19 Dec 2020 20:35:55 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E9=98=85=E6=9B=B4=E4=B9=A6=E7=B1=8D?= =?UTF-8?q?=E4=B8=80=E6=A0=B7=E5=8F=AF=E4=BB=A5=E4=BF=9D=E5=AD=98=E6=95=B0?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/data/AppDatabase.kt | 12 +++++- .../io/legado/app/data/dao/RssArticleDao.kt | 2 +- .../io/legado/app/data/entities/BaseBook.kt | 6 +-- .../io/legado/app/data/entities/RssArticle.kt | 28 ++++++++++--- .../io/legado/app/data/entities/RssStar.kt | 41 ++++++++++++++----- .../legado/app/model/analyzeRule/RuleData.kt | 9 ++++ 6 files changed, 75 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/io/legado/app/model/analyzeRule/RuleData.kt 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 f332fec37..d9af543b2 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -19,7 +19,7 @@ import java.util.* RssSource::class, Bookmark::class, RssArticle::class, RssReadRecord::class, RssStar::class, TxtTocRule::class, ReadRecord::class, HttpTTS::class, Cache::class, RuleSub::class], - version = 27, + version = 28, exportSchema = true ) abstract class AppDatabase : RoomDatabase() { @@ -53,7 +53,8 @@ abstract class AppDatabase : RoomDatabase() { migration_10_11, migration_11_12, migration_12_13, migration_13_14, migration_14_15, migration_15_17, migration_17_18, migration_18_19, migration_19_20, migration_20_21, migration_21_22, migration_22_23, - migration_23_24, migration_24_25, migration_25_26, migration_26_27 + migration_23_24, migration_24_25, migration_25_26, migration_26_27, + migration_27_28 ) .allowMainThreadQueries() .addCallback(dbCallback) @@ -257,6 +258,13 @@ abstract class AppDatabase : RoomDatabase() { database.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS `index_bookmarks_time` ON `bookmarks` (`time`)") } } + + private val migration_27_28 = object : Migration(27, 28) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("ALTER TABLE rssArticles ADD variable TEXT") + database.execSQL("ALTER TABLE rssStars ADD variable TEXT") + } + } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/dao/RssArticleDao.kt b/app/src/main/java/io/legado/app/data/dao/RssArticleDao.kt index 10bf63933..c05dcb880 100644 --- a/app/src/main/java/io/legado/app/data/dao/RssArticleDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/RssArticleDao.kt @@ -12,7 +12,7 @@ interface RssArticleDao { fun get(origin: String, link: String): RssArticle? @Query( - """select t1.link, t1.sort, t1.origin, t1.`order`, t1.title, t1.content, t1.description, t1.image, t1.pubDate, ifNull(t2.read, 0) as read + """select t1.link, t1.sort, t1.origin, t1.`order`, t1.title, t1.content, t1.description, t1.image, t1.pubDate, t1.variable, ifNull(t2.read, 0) as read from rssArticles as t1 left join rssReadRecords as t2 on t1.link = t2.record where origin = :origin and sort = :sort order by `order` desc""" diff --git a/app/src/main/java/io/legado/app/data/entities/BaseBook.kt b/app/src/main/java/io/legado/app/data/entities/BaseBook.kt index e52f9e185..ac97fb252 100644 --- a/app/src/main/java/io/legado/app/data/entities/BaseBook.kt +++ b/app/src/main/java/io/legado/app/data/entities/BaseBook.kt @@ -1,20 +1,18 @@ package io.legado.app.data.entities +import io.legado.app.model.analyzeRule.RuleData import io.legado.app.utils.splitNotBlank -interface BaseBook { +interface BaseBook : RuleData { var name: String var author: String var bookUrl: String - val variableMap: HashMap var kind: String? var wordCount: String? var infoHtml: String? var tocHtml: String? - fun putVariable(key: String, value: String) {} - fun getKindList(): List { val kindList = arrayListOf() wordCount?.let { 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 fda4769d2..7847c0631 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 @@ -1,6 +1,11 @@ package io.legado.app.data.entities import androidx.room.Entity +import androidx.room.Ignore +import io.legado.app.model.analyzeRule.RuleData +import io.legado.app.utils.GSON +import io.legado.app.utils.fromJsonObject +import kotlinx.parcelize.IgnoredOnParcel @Entity( @@ -17,16 +22,29 @@ data class RssArticle( var description: String? = null, var content: String? = null, var image: String? = null, - var read: Boolean = false -) { - + var read: Boolean = false, + var variable: String? = null +) : RuleData { + override fun hashCode() = link.hashCode() - + override fun equals(other: Any?): Boolean { other ?: return false return if (other is RssArticle) origin == other.origin && link == other.link else false } - + + @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) + } + fun toStar() = RssStar( origin = origin, sort = sort, 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 7e78550bb..f583e1936 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 @@ -1,6 +1,11 @@ package io.legado.app.data.entities import androidx.room.Entity +import androidx.room.Ignore +import io.legado.app.model.analyzeRule.RuleData +import io.legado.app.utils.GSON +import io.legado.app.utils.fromJsonObject +import kotlinx.parcelize.IgnoredOnParcel @Entity( @@ -16,16 +21,30 @@ data class RssStar( var pubDate: String? = null, var description: String? = null, var content: String? = null, - var image: String? = null -) { + var image: String? = null, + var variable: String? = null +) : RuleData { + + @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) + } + fun toRssArticle() = RssArticle( - origin = origin, - sort = sort, - title = title, - link = link, - pubDate = pubDate, - description = description, - content = content, - image = image - ) + origin = origin, + sort = sort, + title = title, + link = link, + pubDate = pubDate, + description = description, + content = content, + image = image + ) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/RuleData.kt b/app/src/main/java/io/legado/app/model/analyzeRule/RuleData.kt new file mode 100644 index 000000000..a7bc8c72f --- /dev/null +++ b/app/src/main/java/io/legado/app/model/analyzeRule/RuleData.kt @@ -0,0 +1,9 @@ +package io.legado.app.model.analyzeRule + +interface RuleData { + + val variableMap: HashMap + + fun putVariable(key: String, value: String) + +} \ No newline at end of file