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 acf795430..0c96c2123 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 @@ -3,7 +3,7 @@ package io.legado.app.data.entities import io.legado.app.utils.splitNotBlank interface BaseBook { - var variableMap: HashMap + var variableMap: HashMap? var kind: String? var wordCount: String? 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 f04686bd8..d98f79603 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 @@ -47,9 +47,16 @@ data class Book( var useReplaceRule: Boolean = true, // 正文使用净化替换规则 var variable: String? = null // 自定义书籍变量信息(用于书源规则检索书籍信息) ) : Parcelable, BaseBook { + @Ignore @IgnoredOnParcel - override var variableMap: HashMap = GSON.fromJsonObject(variable) ?: HashMap() + override var variableMap: HashMap? = null + get() { + if (field == null) { + field = GSON.fromJsonObject>(variable) ?: HashMap() + } + return field + } @Ignore @IgnoredOnParcel @@ -66,7 +73,7 @@ data class Book( fun getDisplayIntro() = if (customIntro.isNullOrEmpty()) intro else customIntro override fun putVariable(key: String, value: String) { - variableMap[key] = value + variableMap?.put(key, value) variable = GSON.toJson(variableMap) } 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 61006a6ea..b88151e5d 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 @@ -3,7 +3,11 @@ package io.legado.app.data.entities import android.os.Parcelable import androidx.room.Entity import androidx.room.ForeignKey +import androidx.room.Ignore import androidx.room.Index +import io.legado.app.utils.GSON +import io.legado.app.utils.fromJsonObject +import kotlinx.android.parcel.IgnoredOnParcel import kotlinx.android.parcel.Parcelize @@ -11,7 +15,10 @@ import kotlinx.android.parcel.Parcelize @Entity( tableName = "chapters", primaryKeys = ["url", "bookUrl"], - indices = [(Index(value = ["bookUrl"], unique = false)), (Index(value = ["bookUrl", "index"], unique = true))], + indices = [(Index(value = ["bookUrl"], unique = false)), (Index( + value = ["bookUrl", "index"], + unique = true + ))], foreignKeys = [(ForeignKey( entity = Book::class, parentColumns = ["bookUrl"], @@ -27,6 +34,25 @@ data class BookChapter( var resourceUrl: String? = null, // 音频真实URL var tag: String? = null, // var start: Long? = null, // 章节起始位置 - var end: Long? = null // 章节终止位置 -) : Parcelable + var end: Long? = null, // 章节终止位置 + var variable: String? = null +) : Parcelable { + + @Ignore + @IgnoredOnParcel + var variableMap: HashMap? = null + private set + get() { + if (field == null) { + field = GSON.fromJsonObject>(variable) ?: HashMap() + } + return field + } + + fun putVariable(key: String, value: String) { + variableMap?.put(key, value) + variable = GSON.toJson(variableMap) + } + +} 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 5d3fd9e1f..acca6db8f 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 @@ -56,12 +56,18 @@ data class SearchBook( return other.originOrder - this.originOrder } - @IgnoredOnParcel @Ignore - override var variableMap: HashMap = GSON.fromJsonObject(variable) ?: HashMap() + @IgnoredOnParcel + override var variableMap: HashMap? = null + get() { + if (field == null) { + field = GSON.fromJsonObject>(variable) ?: HashMap() + } + return field + } override fun putVariable(key: String, value: String) { - variableMap[key] = value + variableMap?.put(key, value) variable = GSON.toJson(variableMap) }