kunfei il y a 3 ans
Parent
commit
1175e1e92d

+ 8 - 0
app/src/main/java/io/legado/app/data/entities/BaseBook.kt

@@ -23,6 +23,14 @@ interface BaseBook : RuleDataInterface {
         return true
     }
 
+    fun putCustomVariable(value: String?) {
+        putVariable("custom", value)
+    }
+
+    fun getCustomVariable(): String? {
+        return getVariable("custom")
+    }
+
     override fun putBigVariable(key: String, value: String?) {
         RuleBigDataHelp.putBookVariable(bookUrl, key, value)
     }

+ 33 - 24
app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt

@@ -18,7 +18,6 @@ import io.legado.app.data.entities.Book
 import io.legado.app.data.entities.BookChapter
 import io.legado.app.data.entities.BookSource
 import io.legado.app.databinding.ActivityBookInfoBinding
-import io.legado.app.databinding.DialogEditTextBinding
 import io.legado.app.exception.NoStackTraceException
 import io.legado.app.help.AppWebDav
 import io.legado.app.help.book.*
@@ -56,7 +55,8 @@ class BookInfoActivity :
     VMBaseActivity<ActivityBookInfoBinding, BookInfoViewModel>(toolBarTheme = Theme.Dark),
     GroupSelectDialog.CallBack,
     ChangeBookSourceDialog.CallBack,
-    ChangeCoverDialog.CallBack {
+    ChangeCoverDialog.CallBack,
+    VariableDialog.Callback {
 
     private val tocActivityResult = registerForActivityResult(TocActivityResult()) {
         it?.let {
@@ -421,7 +421,15 @@ class BookInfoActivity :
                 return@launch
             }
             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("书源不存在")
                 return@launch
             }
-            val variable = withContext(IO) { viewModel.bookData.value?.getVariable("custom") }
-            alert(R.string.set_source_variable) {
-                setMessage(source.getDisplayVariableComment("""书籍变量可在js中通过book.getVariable("custom")获取"""))
-                val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply {
-                    editView.hint = "book variable"
-                    editView.setText(variable)
-                }
-                customView { alertBinding.root }
-                okButton {
-                    viewModel.bookData.value?.let { book ->
-                        book.putVariable("custom", alertBinding.editView.text?.toString())
-                        viewModel.saveBook(book)
-                    }
-                }
-                cancelButton()
-                neutralButton(R.string.delete) {
-                    viewModel.bookData.value?.let { book ->
-                        book.putVariable("custom", null)
-                        viewModel.saveBook(book)
-                    }
-                }
+            val book = viewModel.bookData.value
+            if (book == null) {
+                toastOnUi("书籍不存在")
+                return@launch
+            }
+            val variable = withContext(IO) { book.getCustomVariable() }
+            val comment = source.getDisplayVariableComment("""书籍变量可在js中通过book.getVariable("custom")获取""")
+            showDialogFragment(VariableDialog(
+                getString(R.string.set_book_variable),
+                book.bookUrl,
+                variable,
+                comment
+            ))
+        }
+    }
+
+    override fun setVariable(key: String, variable: String?) {
+        when(key) {
+            viewModel.bookSource?.getKey() -> viewModel.bookSource?.setVariable(variable)
+            viewModel.bookData.value?.bookUrl -> viewModel.bookData.value?.let {
+                it.putCustomVariable(variable)
+                viewModel.saveBook(it)
             }
         }
     }

+ 22 - 7
app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt

@@ -41,7 +41,8 @@ import kotlinx.coroutines.withContext
 
 class BookSourceEditActivity :
     VMBaseActivity<ActivityBookSourceEditBinding, BookSourceEditViewModel>(false),
-    KeyboardToolPop.CallBack {
+    KeyboardToolPop.CallBack,
+    VariableDialog.Callback {
 
     override val binding by viewBinding(ActivityBookSourceEditBinding::inflate)
     override val viewModel by viewModels<BookSourceEditViewModel>()
@@ -593,13 +594,27 @@ class BookSourceEditActivity :
     }
 
     private fun setSourceVariable() {
-        val source = viewModel.bookSource
-        if (source == null) {
-            toastOnUi("先保存书源")
-            return
+        launch {
+            val source = viewModel.bookSource
+            if (source == null) {
+                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)
     }
 
 }

+ 24 - 7
app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt

@@ -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.utils.*
 import io.legado.app.utils.viewbindingdelegate.viewBinding
+import kotlinx.coroutines.Dispatchers
 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 viewModel by viewModels<RssSortViewModel>()
@@ -103,13 +106,27 @@ class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewMo
     }
 
     private fun setSourceVariable() {
-        val source = viewModel.rssSource
-        if (source == null) {
-            toastOnUi("源不存在")
-            return
+        launch {
+            val source = viewModel.rssSource
+            if (source == null) {
+                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 :

+ 23 - 18
app/src/main/java/io/legado/app/ui/widget/dialog/VariableDialog.kt

@@ -11,7 +11,6 @@ import io.legado.app.R
 import io.legado.app.base.BaseDialogFragment
 import io.legado.app.base.BaseViewModel
 import io.legado.app.databinding.DialogVariableBinding
-import io.legado.app.help.CacheManager
 import io.legado.app.lib.theme.primaryColor
 import io.legado.app.utils.applyTint
 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 viewModel by viewModels<ViewModel>()
 
-    constructor(key: String, comment: String) : this() {
+    constructor(title: String, key: String, variable: String?, comment: String) : this() {
         arguments = Bundle().apply {
+            putString("title", title)
             putString("key", key)
+            putString("variable", variable)
             putString("comment", comment)
         }
     }
@@ -38,10 +39,14 @@ class VariableDialog() : BaseDialogFragment(R.layout.dialog_variable, true),
     override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
         binding.toolBar.setBackgroundColor(primaryColor)
         arguments?.let {
+            binding.toolBar.title = it.getString("title")
             viewModel.init(it) {
                 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.menu.applyTint(requireContext())
@@ -50,41 +55,41 @@ class VariableDialog() : BaseDialogFragment(R.layout.dialog_variable, true),
 
     override fun onMenuItemClick(item: MenuItem?): Boolean {
         when (item?.itemId) {
-            R.id.menu_save -> viewModel.save(binding.tvVariable.text?.toString()) {
-                dismiss()
+            R.id.menu_save -> {
+                callback?.setVariable(
+                    viewModel.key ?: "",
+                    binding.tvVariable.text?.toString()
+                )
+                dismissAllowingStateLoss()
             }
         }
         return true
     }
 
+    val callback get() = (parentFragment as? Callback) ?: (activity as? Callback)
+
     class ViewModel(application: Application) : BaseViewModel(application) {
 
         var key: String? = null
         var comment: String? = null
-        var mVariable: String? = null
+        var variable: String? = null
 
         fun init(arguments: Bundle, onFinally: () -> Unit) {
             if (key != null) return
             execute {
                 key = arguments.getString("key")
                 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.invoke()
-            }
-        }
+    }
+
+    interface Callback {
+
+        fun setVariable(key: String, variable: String?)
 
     }
 

+ 17 - 18
app/src/main/res/layout/dialog_variable.xml

@@ -23,14 +23,27 @@
             android:theme="?attr/actionBarStyle"
             app:displayHomeAsUp="false"
             app:fitStatusBar="false"
+            app:title="Set variable"
             app:popupTheme="@style/AppTheme.PopupOverlay"
-            app:title="@string/set_source_variable"
             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
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            android:paddingTop="6dp"
+
             android:overScrollMode="ifContentScrolls">
 
             <LinearLayout
@@ -38,20 +51,6 @@
                 android:layout_height="wrap_content"
                 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
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"
@@ -63,8 +62,8 @@
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"
                     android:padding="3dp"
-                    android:textIsSelectable="true"
-                    android:textColor="@color/secondaryText"/>
+                    android:textColor="@color/secondaryText"
+                    android:textIsSelectable="true" />
 
             </LinearLayout>