From 59f74e05d8cc9e4e3933abbb19603ccf94c96a08 Mon Sep 17 00:00:00 2001 From: GKF Date: Tue, 11 Jun 2019 14:24:53 +0800 Subject: [PATCH] up --- .../io/legado/app/data/entities/BaseBook.kt | 6 +++++ .../java/io/legado/app/data/entities/Book.kt | 23 ++++++++++++++++++- .../ui/main/myconfig/PreferenceFragment.kt | 8 ++++--- .../io/legado/app/utils/GsonExtensions.kt | 5 ++++ app/src/main/res/xml/pref_main.xml | 4 +--- 5 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/io/legado/app/data/entities/BaseBook.kt create mode 100644 app/src/main/java/io/legado/app/utils/GsonExtensions.kt 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 new file mode 100644 index 000000000..e3605d6f6 --- /dev/null +++ b/app/src/main/java/io/legado/app/data/entities/BaseBook.kt @@ -0,0 +1,6 @@ +package io.legado.app.data.entities + +interface BaseBook { + var variableMap: HashMap? + fun putVariable(key: String, value: String) +} \ No newline at end of file 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 b1a18961f..6c7a2e4b3 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,10 +1,15 @@ 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 import androidx.room.PrimaryKey +import com.google.gson.Gson import io.legado.app.constant.AppConst.NOT_AVAILABLE +import io.legado.app.utils.fromJson +import kotlinx.android.parcel.IgnoredOnParcel import kotlinx.android.parcel.Parcelize @Parcelize @@ -40,7 +45,10 @@ data class Book( var order: Int = 0, // 手动排序 var useReplaceRule: Boolean = true, // 正文使用净化替换规则 var variable: String? = null // 自定义书籍变量信息(用于书源规则检索书籍信息) -) : Parcelable { +) : Parcelable, BaseBook { + @IgnoredOnParcel + @Ignore + override var variableMap: HashMap? = null fun getUnreadChapterNum() = Math.max(totalChapterNum - durChapterIndex - 1, 0) @@ -52,4 +60,17 @@ data class Book( fun getDisplayDescription() = customDescription ?: description + private fun initVariableMap() { + if (variableMap == null) { + variableMap = if (isEmpty(variable)) { + HashMap() + } else { + Gson().fromJson>(variable!!) + } + } + } + + override fun putVariable(key: String, value: String) { + initVariableMap() + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/myconfig/PreferenceFragment.kt b/app/src/main/java/io/legado/app/ui/main/myconfig/PreferenceFragment.kt index a440d76f2..d3f862d20 100644 --- a/app/src/main/java/io/legado/app/ui/main/myconfig/PreferenceFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/myconfig/PreferenceFragment.kt @@ -6,6 +6,7 @@ import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import io.legado.app.App import io.legado.app.R +import io.legado.app.ui.about.AboutActivity import io.legado.app.ui.config.ConfigActivity import io.legado.app.ui.config.ConfigViewModel import org.jetbrains.anko.startActivity @@ -39,19 +40,20 @@ class PreferenceFragment : PreferenceFragmentCompat(), SharedPreferences.OnShare when (preference.key) { "setting" -> { requireContext().startActivity( - Pair("configType", ConfigViewModel.TYPE_CONFIG) + Pair("configType", ConfigViewModel.TYPE_CONFIG) ) } "web_dav_setting" -> { requireContext().startActivity( - Pair("configType", ConfigViewModel.TYPE_WEB_DAV_CONFIG) + Pair("configType", ConfigViewModel.TYPE_WEB_DAV_CONFIG) ) } "theme_setting" -> { requireContext().startActivity( - Pair("configType", ConfigViewModel.TYPE_THEME_CONFIG) + Pair("configType", ConfigViewModel.TYPE_THEME_CONFIG) ) } + "about" -> requireContext().startActivity() } } return super.onPreferenceTreeClick(preference) diff --git a/app/src/main/java/io/legado/app/utils/GsonExtensions.kt b/app/src/main/java/io/legado/app/utils/GsonExtensions.kt new file mode 100644 index 000000000..7a5b315c9 --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/GsonExtensions.kt @@ -0,0 +1,5 @@ +package io.legado.app.utils + +import com.google.gson.Gson + +inline fun Gson.fromJson(json: String) = fromJson(json, T::class.java) \ No newline at end of file diff --git a/app/src/main/res/xml/pref_main.xml b/app/src/main/res/xml/pref_main.xml index 4cc79904a..689bc4282 100644 --- a/app/src/main/res/xml/pref_main.xml +++ b/app/src/main/res/xml/pref_main.xml @@ -5,14 +5,12 @@ @@ -43,7 +41,7 @@