pull/1630/head
parent
55e8af89af
commit
400db881ad
@ -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 ?: "") |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
@ -0,0 +1,25 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
android:background="@color/background" |
||||
android:orientation="vertical"> |
||||
|
||||
<androidx.appcompat.widget.Toolbar |
||||
android:id="@+id/tool_bar" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:theme="?attr/actionBarStyle" |
||||
app:title="code edit" |
||||
app:popupTheme="@style/AppTheme.PopupOverlay" |
||||
app:titleTextAppearance="@style/ToolbarTitle" /> |
||||
|
||||
<io.legado.app.lib.theme.view.ThemeEditText |
||||
android:id="@+id/content_view" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:padding="12dp" |
||||
android:textIsSelectable="true" /> |
||||
|
||||
</LinearLayout> |
@ -0,0 +1,18 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
tools:ignore="AlwaysShowAction"> |
||||
|
||||
<item |
||||
android:id="@+id/menu_save" |
||||
android:icon="@drawable/ic_save" |
||||
android:title="@string/action_save" |
||||
app:showAsAction="always" /> |
||||
|
||||
<item |
||||
android:id="@+id/menu_reset" |
||||
android:title="@string/reset" |
||||
app:showAsAction="never" /> |
||||
|
||||
</menu> |
Loading…
Reference in new issue