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 6a7d561ee..144f59ec4 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 118acf35b..0ab5a9879 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 @@ -1,7 +1,6 @@ package io.legado.app.data.entities import android.os.Parcelable -import android.text.TextUtils.isEmpty import androidx.room.Entity import androidx.room.Ignore import androidx.room.Index @@ -50,11 +49,7 @@ data class Book( ) : Parcelable, BaseBook { @IgnoredOnParcel @Ignore - override var variableMap: HashMap? = null - get() = run { - initVariableMap() - return field - } + override var variableMap: HashMap = GSON.fromJsonObject(variable) ?: HashMap() fun getUnreadChapterNum() = max(totalChapterNum - durChapterIndex - 1, 0) @@ -62,19 +57,8 @@ data class Book( fun getDisplayIntro() = if (customIntro.isNullOrEmpty()) intro else customIntro - private fun initVariableMap() { - if (variableMap == null) { - variableMap = if (isEmpty(variable)) { - HashMap() - } else { - GSON.fromJsonObject(variable) - } - } - } - override fun putVariable(key: String, value: String) { - initVariableMap() - variableMap?.put(key, value) + variableMap[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 00efc0e47..69caf7c6c 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 @@ -1,7 +1,6 @@ package io.legado.app.data.entities import android.os.Parcelable -import android.text.TextUtils import androidx.room.Entity import androidx.room.Ignore import androidx.room.Index @@ -51,25 +50,10 @@ data class SearchBook( @IgnoredOnParcel @Ignore - override var variableMap: HashMap? = null - get() = run { - initVariableMap() - return field - } - - private fun initVariableMap() { - variableMap?.let { - variableMap = if (TextUtils.isEmpty(variable)) { - HashMap() - } else { - GSON.fromJsonObject(variable) - } - } - } + override var variableMap: HashMap = GSON.fromJsonObject(variable) ?: HashMap() override fun putVariable(key: String, value: String) { - initVariableMap() - variableMap?.put(key, value) + variableMap[key] = value variable = GSON.toJson(variableMap) }