优化
This commit is contained in:
@@ -23,6 +23,14 @@ interface BaseBook : RuleDataInterface {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun putCustomVariable(value: String?) {
|
||||||
|
putVariable("custom", value)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getCustomVariable(): String? {
|
||||||
|
return getVariable("custom")
|
||||||
|
}
|
||||||
|
|
||||||
override fun putBigVariable(key: String, value: String?) {
|
override fun putBigVariable(key: String, value: String?) {
|
||||||
RuleBigDataHelp.putBookVariable(bookUrl, key, value)
|
RuleBigDataHelp.putBookVariable(bookUrl, key, value)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import io.legado.app.data.entities.Book
|
|||||||
import io.legado.app.data.entities.BookChapter
|
import io.legado.app.data.entities.BookChapter
|
||||||
import io.legado.app.data.entities.BookSource
|
import io.legado.app.data.entities.BookSource
|
||||||
import io.legado.app.databinding.ActivityBookInfoBinding
|
import io.legado.app.databinding.ActivityBookInfoBinding
|
||||||
import io.legado.app.databinding.DialogEditTextBinding
|
|
||||||
import io.legado.app.exception.NoStackTraceException
|
import io.legado.app.exception.NoStackTraceException
|
||||||
import io.legado.app.help.AppWebDav
|
import io.legado.app.help.AppWebDav
|
||||||
import io.legado.app.help.book.*
|
import io.legado.app.help.book.*
|
||||||
@@ -56,7 +55,8 @@ class BookInfoActivity :
|
|||||||
VMBaseActivity<ActivityBookInfoBinding, BookInfoViewModel>(toolBarTheme = Theme.Dark),
|
VMBaseActivity<ActivityBookInfoBinding, BookInfoViewModel>(toolBarTheme = Theme.Dark),
|
||||||
GroupSelectDialog.CallBack,
|
GroupSelectDialog.CallBack,
|
||||||
ChangeBookSourceDialog.CallBack,
|
ChangeBookSourceDialog.CallBack,
|
||||||
ChangeCoverDialog.CallBack {
|
ChangeCoverDialog.CallBack,
|
||||||
|
VariableDialog.Callback {
|
||||||
|
|
||||||
private val tocActivityResult = registerForActivityResult(TocActivityResult()) {
|
private val tocActivityResult = registerForActivityResult(TocActivityResult()) {
|
||||||
it?.let {
|
it?.let {
|
||||||
@@ -421,7 +421,15 @@ class BookInfoActivity :
|
|||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
val comment = source.getDisplayVariableComment("源变量可在js中通过source.getVariable()获取")
|
val comment = source.getDisplayVariableComment("源变量可在js中通过source.getVariable()获取")
|
||||||
showDialogFragment(VariableDialog(source.getKey(), comment))
|
val variable = withContext(IO) { source.getVariable() }
|
||||||
|
showDialogFragment(
|
||||||
|
VariableDialog(
|
||||||
|
getString(R.string.set_source_variable),
|
||||||
|
source.getKey(),
|
||||||
|
variable,
|
||||||
|
comment
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -432,27 +440,28 @@ class BookInfoActivity :
|
|||||||
toastOnUi("书源不存在")
|
toastOnUi("书源不存在")
|
||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
val variable = withContext(IO) { viewModel.bookData.value?.getVariable("custom") }
|
val book = viewModel.bookData.value
|
||||||
alert(R.string.set_source_variable) {
|
if (book == null) {
|
||||||
setMessage(source.getDisplayVariableComment("""书籍变量可在js中通过book.getVariable("custom")获取"""))
|
toastOnUi("书籍不存在")
|
||||||
val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply {
|
return@launch
|
||||||
editView.hint = "book variable"
|
}
|
||||||
editView.setText(variable)
|
val variable = withContext(IO) { book.getCustomVariable() }
|
||||||
}
|
val comment = source.getDisplayVariableComment("""书籍变量可在js中通过book.getVariable("custom")获取""")
|
||||||
customView { alertBinding.root }
|
showDialogFragment(VariableDialog(
|
||||||
okButton {
|
getString(R.string.set_book_variable),
|
||||||
viewModel.bookData.value?.let { book ->
|
book.bookUrl,
|
||||||
book.putVariable("custom", alertBinding.editView.text?.toString())
|
variable,
|
||||||
viewModel.saveBook(book)
|
comment
|
||||||
}
|
))
|
||||||
}
|
}
|
||||||
cancelButton()
|
}
|
||||||
neutralButton(R.string.delete) {
|
|
||||||
viewModel.bookData.value?.let { book ->
|
override fun setVariable(key: String, variable: String?) {
|
||||||
book.putVariable("custom", null)
|
when(key) {
|
||||||
viewModel.saveBook(book)
|
viewModel.bookSource?.getKey() -> viewModel.bookSource?.setVariable(variable)
|
||||||
}
|
viewModel.bookData.value?.bookUrl -> viewModel.bookData.value?.let {
|
||||||
}
|
it.putCustomVariable(variable)
|
||||||
|
viewModel.saveBook(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,8 @@ import kotlinx.coroutines.withContext
|
|||||||
|
|
||||||
class BookSourceEditActivity :
|
class BookSourceEditActivity :
|
||||||
VMBaseActivity<ActivityBookSourceEditBinding, BookSourceEditViewModel>(false),
|
VMBaseActivity<ActivityBookSourceEditBinding, BookSourceEditViewModel>(false),
|
||||||
KeyboardToolPop.CallBack {
|
KeyboardToolPop.CallBack,
|
||||||
|
VariableDialog.Callback {
|
||||||
|
|
||||||
override val binding by viewBinding(ActivityBookSourceEditBinding::inflate)
|
override val binding by viewBinding(ActivityBookSourceEditBinding::inflate)
|
||||||
override val viewModel by viewModels<BookSourceEditViewModel>()
|
override val viewModel by viewModels<BookSourceEditViewModel>()
|
||||||
@@ -593,13 +594,27 @@ class BookSourceEditActivity :
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun setSourceVariable() {
|
private fun setSourceVariable() {
|
||||||
val source = viewModel.bookSource
|
launch {
|
||||||
if (source == null) {
|
val source = viewModel.bookSource
|
||||||
toastOnUi("先保存书源")
|
if (source == null) {
|
||||||
return
|
toastOnUi("先保存书源")
|
||||||
|
return@launch
|
||||||
|
}
|
||||||
|
val comment = source.getDisplayVariableComment("源变量可在js中通过source.getVariable()获取")
|
||||||
|
val variable = withContext(IO) { source.getVariable() }
|
||||||
|
showDialogFragment(
|
||||||
|
VariableDialog(
|
||||||
|
getString(R.string.set_source_variable),
|
||||||
|
source.getKey(),
|
||||||
|
variable,
|
||||||
|
comment
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
val comment = source.getDisplayVariableComment("源变量可在js中通过source.getVariable()获取")
|
}
|
||||||
showDialogFragment(VariableDialog(source.getKey(), comment))
|
|
||||||
|
override fun setVariable(key: String, variable: String?) {
|
||||||
|
viewModel.bookSource?.setVariable(variable)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,9 +19,12 @@ import io.legado.app.ui.rss.source.edit.RssSourceEditActivity
|
|||||||
import io.legado.app.ui.widget.dialog.VariableDialog
|
import io.legado.app.ui.widget.dialog.VariableDialog
|
||||||
import io.legado.app.utils.*
|
import io.legado.app.utils.*
|
||||||
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewModel>() {
|
class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewModel>(),
|
||||||
|
VariableDialog.Callback {
|
||||||
|
|
||||||
override val binding by viewBinding(ActivityRssArtivlesBinding::inflate)
|
override val binding by viewBinding(ActivityRssArtivlesBinding::inflate)
|
||||||
override val viewModel by viewModels<RssSortViewModel>()
|
override val viewModel by viewModels<RssSortViewModel>()
|
||||||
@@ -103,13 +106,27 @@ class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewMo
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun setSourceVariable() {
|
private fun setSourceVariable() {
|
||||||
val source = viewModel.rssSource
|
launch {
|
||||||
if (source == null) {
|
val source = viewModel.rssSource
|
||||||
toastOnUi("源不存在")
|
if (source == null) {
|
||||||
return
|
toastOnUi("源不存在")
|
||||||
|
return@launch
|
||||||
|
}
|
||||||
|
val comment = source.getDisplayVariableComment("源变量可在js中通过source.getVariable()获取")
|
||||||
|
val variable = withContext(Dispatchers.IO) { source.getVariable() }
|
||||||
|
showDialogFragment(
|
||||||
|
VariableDialog(
|
||||||
|
getString(R.string.set_source_variable),
|
||||||
|
source.getKey(),
|
||||||
|
variable,
|
||||||
|
comment
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
val comment = source.getDisplayVariableComment("源变量可在js中通过source.getVariable()获取")
|
}
|
||||||
showDialogFragment(VariableDialog(source.getKey(), comment))
|
|
||||||
|
override fun setVariable(key: String, variable: String?) {
|
||||||
|
viewModel.rssSource?.setVariable(variable)
|
||||||
}
|
}
|
||||||
|
|
||||||
private inner class TabFragmentPageAdapter :
|
private inner class TabFragmentPageAdapter :
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import io.legado.app.R
|
|||||||
import io.legado.app.base.BaseDialogFragment
|
import io.legado.app.base.BaseDialogFragment
|
||||||
import io.legado.app.base.BaseViewModel
|
import io.legado.app.base.BaseViewModel
|
||||||
import io.legado.app.databinding.DialogVariableBinding
|
import io.legado.app.databinding.DialogVariableBinding
|
||||||
import io.legado.app.help.CacheManager
|
|
||||||
import io.legado.app.lib.theme.primaryColor
|
import io.legado.app.lib.theme.primaryColor
|
||||||
import io.legado.app.utils.applyTint
|
import io.legado.app.utils.applyTint
|
||||||
import io.legado.app.utils.setLayout
|
import io.legado.app.utils.setLayout
|
||||||
@@ -23,9 +22,11 @@ class VariableDialog() : BaseDialogFragment(R.layout.dialog_variable, true),
|
|||||||
private val binding by viewBinding(DialogVariableBinding::bind)
|
private val binding by viewBinding(DialogVariableBinding::bind)
|
||||||
private val viewModel by viewModels<ViewModel>()
|
private val viewModel by viewModels<ViewModel>()
|
||||||
|
|
||||||
constructor(key: String, comment: String) : this() {
|
constructor(title: String, key: String, variable: String?, comment: String) : this() {
|
||||||
arguments = Bundle().apply {
|
arguments = Bundle().apply {
|
||||||
|
putString("title", title)
|
||||||
putString("key", key)
|
putString("key", key)
|
||||||
|
putString("variable", variable)
|
||||||
putString("comment", comment)
|
putString("comment", comment)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -38,10 +39,14 @@ class VariableDialog() : BaseDialogFragment(R.layout.dialog_variable, true),
|
|||||||
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
binding.toolBar.setBackgroundColor(primaryColor)
|
binding.toolBar.setBackgroundColor(primaryColor)
|
||||||
arguments?.let {
|
arguments?.let {
|
||||||
|
binding.toolBar.title = it.getString("title")
|
||||||
viewModel.init(it) {
|
viewModel.init(it) {
|
||||||
binding.tvComment.text = viewModel.comment
|
binding.tvComment.text = viewModel.comment
|
||||||
binding.tvVariable.setText(viewModel.mVariable)
|
binding.tvVariable.setText(viewModel.variable)
|
||||||
}
|
}
|
||||||
|
} ?: let {
|
||||||
|
dismiss()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
binding.toolBar.inflateMenu(R.menu.save)
|
binding.toolBar.inflateMenu(R.menu.save)
|
||||||
binding.toolBar.menu.applyTint(requireContext())
|
binding.toolBar.menu.applyTint(requireContext())
|
||||||
@@ -50,37 +55,31 @@ class VariableDialog() : BaseDialogFragment(R.layout.dialog_variable, true),
|
|||||||
|
|
||||||
override fun onMenuItemClick(item: MenuItem?): Boolean {
|
override fun onMenuItemClick(item: MenuItem?): Boolean {
|
||||||
when (item?.itemId) {
|
when (item?.itemId) {
|
||||||
R.id.menu_save -> viewModel.save(binding.tvVariable.text?.toString()) {
|
R.id.menu_save -> {
|
||||||
dismiss()
|
callback?.setVariable(
|
||||||
|
viewModel.key ?: "",
|
||||||
|
binding.tvVariable.text?.toString()
|
||||||
|
)
|
||||||
|
dismissAllowingStateLoss()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val callback get() = (parentFragment as? Callback) ?: (activity as? Callback)
|
||||||
|
|
||||||
class ViewModel(application: Application) : BaseViewModel(application) {
|
class ViewModel(application: Application) : BaseViewModel(application) {
|
||||||
|
|
||||||
var key: String? = null
|
var key: String? = null
|
||||||
var comment: String? = null
|
var comment: String? = null
|
||||||
var mVariable: String? = null
|
var variable: String? = null
|
||||||
|
|
||||||
fun init(arguments: Bundle, onFinally: () -> Unit) {
|
fun init(arguments: Bundle, onFinally: () -> Unit) {
|
||||||
if (key != null) return
|
if (key != null) return
|
||||||
execute {
|
execute {
|
||||||
key = arguments.getString("key")
|
key = arguments.getString("key")
|
||||||
comment = arguments.getString("comment")
|
comment = arguments.getString("comment")
|
||||||
mVariable = CacheManager.get("sourceVariable_${key}")
|
variable = arguments.getString("variable")
|
||||||
}.onFinally {
|
|
||||||
onFinally.invoke()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun save(variable: String?, onFinally: () -> Unit) {
|
|
||||||
execute {
|
|
||||||
if (variable == null) {
|
|
||||||
CacheManager.delete("sourceVariable_${key}")
|
|
||||||
} else {
|
|
||||||
CacheManager.put("sourceVariable_${key}", variable)
|
|
||||||
}
|
|
||||||
}.onFinally {
|
}.onFinally {
|
||||||
onFinally.invoke()
|
onFinally.invoke()
|
||||||
}
|
}
|
||||||
@@ -88,4 +87,10 @@ class VariableDialog() : BaseDialogFragment(R.layout.dialog_variable, true),
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Callback {
|
||||||
|
|
||||||
|
fun setVariable(key: String, variable: String?)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -23,14 +23,27 @@
|
|||||||
android:theme="?attr/actionBarStyle"
|
android:theme="?attr/actionBarStyle"
|
||||||
app:displayHomeAsUp="false"
|
app:displayHomeAsUp="false"
|
||||||
app:fitStatusBar="false"
|
app:fitStatusBar="false"
|
||||||
|
app:title="Set variable"
|
||||||
app:popupTheme="@style/AppTheme.PopupOverlay"
|
app:popupTheme="@style/AppTheme.PopupOverlay"
|
||||||
app:title="@string/set_source_variable"
|
|
||||||
app:titleTextAppearance="@style/ToolbarTitle" />
|
app:titleTextAppearance="@style/ToolbarTitle" />
|
||||||
|
|
||||||
|
<io.legado.app.ui.widget.text.AccentTextView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:padding="3dp"
|
||||||
|
android:text="variable"
|
||||||
|
tools:ignore="HardcodedText" />
|
||||||
|
|
||||||
|
<io.legado.app.lib.theme.view.ThemeEditText
|
||||||
|
android:id="@+id/tv_variable"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" />
|
||||||
|
|
||||||
<androidx.core.widget.NestedScrollView
|
<androidx.core.widget.NestedScrollView
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:paddingTop="6dp"
|
|
||||||
android:overScrollMode="ifContentScrolls">
|
android:overScrollMode="ifContentScrolls">
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
@@ -38,20 +51,6 @@
|
|||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="vertical">
|
android:orientation="vertical">
|
||||||
|
|
||||||
<io.legado.app.ui.widget.text.TextInputLayout
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:hint="source variable"
|
|
||||||
tools:ignore="HardcodedText">
|
|
||||||
|
|
||||||
<io.legado.app.lib.theme.view.ThemeEditText
|
|
||||||
android:id="@+id/tv_variable"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" />
|
|
||||||
|
|
||||||
</io.legado.app.ui.widget.text.TextInputLayout>
|
|
||||||
|
|
||||||
<io.legado.app.ui.widget.text.AccentTextView
|
<io.legado.app.ui.widget.text.AccentTextView
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
@@ -63,8 +62,8 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:padding="3dp"
|
android:padding="3dp"
|
||||||
android:textIsSelectable="true"
|
android:textColor="@color/secondaryText"
|
||||||
android:textColor="@color/secondaryText"/>
|
android:textIsSelectable="true" />
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user