|
|
@@ -0,0 +1,103 @@
|
|
|
+package io.legado.app.ui.book.read
|
|
|
+
|
|
|
+import android.app.Application
|
|
|
+import android.os.Bundle
|
|
|
+import android.view.View
|
|
|
+import android.view.ViewGroup
|
|
|
+import androidx.fragment.app.viewModels
|
|
|
+import io.legado.app.R
|
|
|
+import io.legado.app.base.BaseDialogFragment
|
|
|
+import io.legado.app.base.BaseViewModel
|
|
|
+import io.legado.app.data.appDb
|
|
|
+import io.legado.app.databinding.DialogContentEditBinding
|
|
|
+import io.legado.app.help.BookHelp
|
|
|
+import io.legado.app.help.ContentProcessor
|
|
|
+import io.legado.app.lib.theme.primaryColor
|
|
|
+import io.legado.app.model.ReadBook
|
|
|
+import io.legado.app.model.webBook.WebBook
|
|
|
+import io.legado.app.utils.applyTint
|
|
|
+import io.legado.app.utils.setLayout
|
|
|
+import io.legado.app.utils.viewbindingdelegate.viewBinding
|
|
|
+import kotlinx.coroutines.Dispatchers.IO
|
|
|
+import kotlinx.coroutines.launch
|
|
|
+import kotlinx.coroutines.withContext
|
|
|
+
|
|
|
+/**
|
|
|
+ * 内容编辑
|
|
|
+ */
|
|
|
+class ContentEditDialog : BaseDialogFragment(R.layout.dialog_content_edit) {
|
|
|
+
|
|
|
+ val binding by viewBinding(DialogContentEditBinding::bind)
|
|
|
+ val viewModel by viewModels<ContentEditViewModel>()
|
|
|
+
|
|
|
+ override fun onStart() {
|
|
|
+ super.onStart()
|
|
|
+ setLayout(ViewGroup.LayoutParams.MATCH_PARENT, 0.9f)
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
+ binding.toolBar.setBackgroundColor(primaryColor)
|
|
|
+ binding.toolBar.title = ReadBook.curTextChapter?.title
|
|
|
+ initMenu()
|
|
|
+ viewModel.initContent {
|
|
|
+ binding.contentView.setText(it)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun initMenu() {
|
|
|
+ binding.toolBar.inflateMenu(R.menu.content_edit)
|
|
|
+ binding.toolBar.menu.applyTint(requireContext())
|
|
|
+ binding.toolBar.setOnMenuItemClickListener {
|
|
|
+ when (it.itemId) {
|
|
|
+ R.id.menu_save -> launch {
|
|
|
+ binding.contentView.text?.toString()?.let { content ->
|
|
|
+ withContext(IO) {
|
|
|
+ val book = ReadBook.book ?: return@withContext
|
|
|
+ val chapter = appDb.bookChapterDao
|
|
|
+ .getChapter(book.bookUrl, ReadBook.durChapterIndex)
|
|
|
+ ?: return@withContext
|
|
|
+ BookHelp.saveText(book, chapter, content)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ReadBook.loadContent(ReadBook.durChapterIndex, resetPageOffset = false)
|
|
|
+ dismiss()
|
|
|
+ }
|
|
|
+ R.id.menu_reset -> viewModel.initContent(true) { content ->
|
|
|
+ binding.contentView.setText(content)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return@setOnMenuItemClickListener true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ class ContentEditViewModel(application: Application) : BaseViewModel(application) {
|
|
|
+
|
|
|
+ var content: String? = null
|
|
|
+
|
|
|
+ fun initContent(reset: Boolean = false, success: (String) -> Unit) {
|
|
|
+ execute {
|
|
|
+ val book = ReadBook.book ?: return@execute null
|
|
|
+ val chapter = appDb.bookChapterDao
|
|
|
+ .getChapter(book.bookUrl, ReadBook.durChapterIndex)
|
|
|
+ ?: return@execute null
|
|
|
+ if (reset) {
|
|
|
+ content = null
|
|
|
+ BookHelp.delContent(book, chapter)
|
|
|
+ if (!book.isLocalBook()) ReadBook.bookSource?.let { bookSource ->
|
|
|
+ WebBook.getContentAwait(this, bookSource, book, chapter)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return@execute content ?: let {
|
|
|
+ val contentProcessor = ContentProcessor.get(book.name, book.origin)
|
|
|
+ val content = BookHelp.getContent(book, chapter) ?: return@let null
|
|
|
+ contentProcessor.getContent(book, chapter, content, includeTitle = false)
|
|
|
+ .joinToString("\n")
|
|
|
+ }
|
|
|
+ }.onSuccess {
|
|
|
+ success.invoke(it ?: "")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|