From 65cf6465209e978be8836a6a57bf8db0fb256b8c Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 8 Sep 2021 21:44:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/data/entities/Book.kt | 8 ++++-- .../legado/app/data/entities/BookChapter.kt | 8 ++++-- .../io/legado/app/data/entities/RssArticle.kt | 8 ++++-- .../io/legado/app/data/entities/RssStar.kt | 8 ++++-- .../io/legado/app/data/entities/SearchBook.kt | 8 ++++-- .../legado/app/model/analyzeRule/RuleData.kt | 8 ++++-- .../model/analyzeRule/RuleDataInterface.kt | 6 ++++- .../app/ui/book/info/BookInfoActivity.kt | 27 +++++++++++++++++++ app/src/main/res/menu/book_info.xml | 5 ++++ app/src/main/res/values-es-rES/strings.xml | 1 + app/src/main/res/values-ja-rJP/strings.xml | 1 + app/src/main/res/values-pt-rBR/strings.xml | 1 + app/src/main/res/values-zh-rHK/strings.xml | 1 + app/src/main/res/values-zh-rTW/strings.xml | 1 + app/src/main/res/values-zh/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 16 files changed, 80 insertions(+), 13 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 318fb8a88..5b61a6cc7 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 @@ -94,8 +94,12 @@ data class Book( GSON.fromJsonObject>(variable) ?: HashMap() } - override fun putVariable(key: String, value: String) { - variableMap[key] = value + override fun putVariable(key: String, value: String?) { + if (value != null) { + variableMap[key] = value + } else { + variableMap.remove(key) + } 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 dc0aa7951..bd02b125f 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 @@ -49,8 +49,12 @@ data class BookChapter( GSON.fromJsonObject>(variable) ?: HashMap() } - override fun putVariable(key: String, value: String) { - variableMap[key] = value + override fun putVariable(key: String, value: String?) { + if (value != null) { + variableMap[key] = value + } else { + variableMap.remove(key) + } variable = GSON.toJson(variableMap) } 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 9e0f91b2d..911fb16b8 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 @@ -40,8 +40,12 @@ data class RssArticle( GSON.fromJsonObject>(variable) ?: HashMap() } - override fun putVariable(key: String, value: String) { - variableMap[key] = value + override fun putVariable(key: String, value: String?) { + if (value != null) { + variableMap[key] = value + } else { + variableMap.remove(key) + } variable = GSON.toJson(variableMap) } 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 dcd8ea737..df4bd546f 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 @@ -32,8 +32,12 @@ data class RssStar( GSON.fromJsonObject>(variable) ?: HashMap() } - override fun putVariable(key: String, value: String) { - variableMap[key] = value + override fun putVariable(key: String, value: String?) { + if (value != null) { + variableMap[key] = value + } else { + variableMap.remove(key) + } 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 70896b9da..96c525b3a 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 @@ -63,8 +63,12 @@ data class SearchBook( GSON.fromJsonObject>(variable) ?: HashMap() } - override fun putVariable(key: String, value: String) { - variableMap[key] = value + override fun putVariable(key: String, value: String?) { + if (value != null) { + variableMap[key] = value + } else { + variableMap.remove(key) + } variable = GSON.toJson(variableMap) } 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 index b26e43715..3752ac113 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/RuleData.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/RuleData.kt @@ -6,8 +6,12 @@ class RuleData : RuleDataInterface { hashMapOf() } - override fun putVariable(key: String, value: String) { - variableMap[key] = value + override fun putVariable(key: String, value: String?) { + if (value != null) { + variableMap[key] = value + } else { + variableMap.remove(key) + } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/RuleDataInterface.kt b/app/src/main/java/io/legado/app/model/analyzeRule/RuleDataInterface.kt index 80bf658f6..8b87f949b 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/RuleDataInterface.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/RuleDataInterface.kt @@ -4,6 +4,10 @@ interface RuleDataInterface { val variableMap: HashMap - fun putVariable(key: String, value: String) + fun putVariable(key: String, value: String?) + + fun getVariable(key: String): String? { + return variableMap[key] + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index d6f4a8cc4..ce9a16923 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -122,6 +122,8 @@ class BookInfoActivity : !viewModel.bookSource?.loginUrl.isNullOrBlank() menu.findItem(R.id.menu_set_source_variable)?.isVisible = viewModel.bookSource != null + menu.findItem(R.id.menu_set_book_variable)?.isVisible = + viewModel.bookSource != null return super.onMenuOpened(featureId, menu) } @@ -161,6 +163,7 @@ class BookInfoActivity : } R.id.menu_top -> viewModel.topBook() R.id.menu_set_source_variable -> setSourceVariable() + R.id.menu_set_book_variable -> setBookVariable() R.id.menu_copy_book_url -> viewModel.bookData.value?.bookUrl?.let { sendToClip(it) } ?: toastOnUi(R.string.no_book) @@ -336,7 +339,31 @@ class BookInfoActivity : } }.show() } + } + private fun setBookVariable() { + launch { + val variable = withContext(IO) { viewModel.bookData.value?.getVariable("custom") } + alert(R.string.set_source_variable) { + setMessage("""书籍变量可在js中通过book.getVariable("custom")获取""") + val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply { + editView.hint = "book variable" + editView.setText(variable) + } + customView { alertBinding.root } + okButton { + viewModel.bookData.value + ?.putVariable("custom", alertBinding.editView.text?.toString()) + viewModel.saveBook() + } + cancelButton() + neutralButton(R.string.delete) { + viewModel.bookData.value + ?.putVariable("custom", null) + viewModel.saveBook() + } + }.show() + } } @SuppressLint("InflateParams") diff --git a/app/src/main/res/menu/book_info.xml b/app/src/main/res/menu/book_info.xml index 37acb0fa3..b2d1657ce 100644 --- a/app/src/main/res/menu/book_info.xml +++ b/app/src/main/res/menu/book_info.xml @@ -34,6 +34,11 @@ android:title="@string/set_source_variable" app:showAsAction="never" /> + + 用于导出书源书单时生成直链url 拷贝播放Url 设置源变量 + 设置书籍变量 diff --git a/app/src/main/res/values-ja-rJP/strings.xml b/app/src/main/res/values-ja-rJP/strings.xml index 572840413..75bf1aab9 100644 --- a/app/src/main/res/values-ja-rJP/strings.xml +++ b/app/src/main/res/values-ja-rJP/strings.xml @@ -873,4 +873,5 @@ 用于导出书源书单时生成直链url 拷贝播放Url 设置源变量 + 设置书籍变量 diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index 93ad6d6b0..192b5742b 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -873,5 +873,6 @@ 用于导出书源书单时生成直链url 拷贝播放Url 设置源变量 + 设置书籍变量 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index f6f79fe01..f60da25d3 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -870,5 +870,6 @@ 用于导出书源书单时生成直链url 拷贝播放Url 设置源变量 + 设置书籍变量 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 49f5841bc..3102efc92 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -871,5 +871,6 @@ 用于导出书源书单时生成直链url 拷贝播放Url 设置源变量 + 设置书籍变量 diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index f32c7c762..aa291b690 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -872,5 +872,6 @@ 用于导出书源书单时生成直链url 拷贝播放Url 设置源变量 + 设置书籍变量 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e437d54b9..f1a46592a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -873,5 +873,6 @@ 用于导出书源书单时生成直链url 拷贝播放Url 设置源变量 + 设置书籍变量