From 751b0d234cdccbeda5bdd88f608e31720a6340bf Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 25 Jul 2021 14:07:15 +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 --- .../app/ui/book/read/ReadBookBaseActivity.kt | 24 +--- .../legado/app/ui/book/toc/BookmarkDialog.kt | 86 +++++++++++++ .../app/ui/book/toc/BookmarkFragment.kt | 30 +---- app/src/main/res/layout/dialog_bookmark.xml | 117 +++++++++++++----- 4 files changed, 177 insertions(+), 80 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/book/toc/BookmarkDialog.kt diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt index 23976aa2a..0e325f294 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt @@ -12,16 +12,13 @@ import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.AppConst.charsets import io.legado.app.constant.PreferKey -import io.legado.app.data.appDb import io.legado.app.data.entities.Bookmark import io.legado.app.databinding.ActivityBookReadBinding -import io.legado.app.databinding.DialogBookmarkBinding import io.legado.app.databinding.DialogDownloadChoiceBinding import io.legado.app.databinding.DialogEditTextBinding import io.legado.app.help.AppConfig import io.legado.app.help.LocalConfig import io.legado.app.help.ReadBookConfig -import io.legado.app.help.coroutine.Coroutine import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.selector import io.legado.app.lib.theme.ATH @@ -32,8 +29,8 @@ import io.legado.app.service.help.ReadBook import io.legado.app.ui.book.read.config.BgTextConfigDialog import io.legado.app.ui.book.read.config.ClickActionConfigDialog import io.legado.app.ui.book.read.config.PaddingConfigDialog +import io.legado.app.ui.book.toc.BookmarkDialog import io.legado.app.utils.getPrefString -import io.legado.app.utils.requestInputMethod import io.legado.app.utils.viewbindingdelegate.viewBinding /** @@ -199,24 +196,7 @@ abstract class ReadBookBaseActivity : @SuppressLint("InflateParams") fun showBookMark(bookmark: Bookmark) { - alert(title = getString(R.string.bookmark_add)) { - setMessage(bookmark.chapterName) - val alertBinding = DialogBookmarkBinding.inflate(layoutInflater).apply { - editBookText.setText(bookmark.bookText) - editView.setText(bookmark.content) - } - customView { alertBinding.root } - yesButton { - alertBinding.apply { - Coroutine.async { - bookmark.bookText = editBookText.text.toString() - bookmark.content = editView.text.toString() - appDb.bookmarkDao.insert(bookmark) - } - } - } - noButton() - }.show().requestInputMethod() + BookmarkDialog.start(supportFragmentManager, bookmark) } fun showCharsetConfig() { diff --git a/app/src/main/java/io/legado/app/ui/book/toc/BookmarkDialog.kt b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkDialog.kt new file mode 100644 index 000000000..fb61638c1 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkDialog.kt @@ -0,0 +1,86 @@ +package io.legado.app.ui.book.toc + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.FragmentManager +import androidx.lifecycle.lifecycleScope +import io.legado.app.R +import io.legado.app.base.BaseDialogFragment +import io.legado.app.data.appDb +import io.legado.app.data.entities.Bookmark +import io.legado.app.databinding.DialogBookmarkBinding +import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext + +class BookmarkDialog : BaseDialogFragment() { + + companion object { + + fun start(fragmentManager: FragmentManager, bookmark: Bookmark) { + BookmarkDialog().apply { + arguments = Bundle().apply { + putParcelable("bookmark", bookmark) + } + }.show(fragmentManager, "bookMarkDialog") + } + + } + + private val binding by viewBinding(DialogBookmarkBinding::bind) + + override fun onStart() { + super.onStart() + dialog?.window?.setLayout( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT + ) + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + return inflater.inflate(R.layout.dialog_bookmark, container) + } + + override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { + val bookmark = arguments?.getParcelable("bookmark") + bookmark ?: let { + dismiss() + return + } + binding.run { + tvChapterName.text = bookmark.chapterName + editBookText.setText(bookmark.bookText) + editContent.setText(bookmark.content) + tvCancel.setOnClickListener { + dismiss() + } + tvOk.setOnClickListener { + bookmark.bookText = editBookText.text?.toString() ?: "" + bookmark.content = editContent.text?.toString() ?: "" + lifecycleScope.launch { + withContext(IO) { + appDb.bookmarkDao.insert(bookmark) + } + dismiss() + } + } + tvFooterLeft.setOnClickListener { + lifecycleScope.launch { + withContext(IO) { + appDb.bookmarkDao.delete(bookmark) + } + dismiss() + } + } + } + } + + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt index 27fee596f..96bd12fc6 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt @@ -11,13 +11,9 @@ import io.legado.app.R import io.legado.app.base.VMBaseFragment import io.legado.app.data.appDb import io.legado.app.data.entities.Bookmark -import io.legado.app.databinding.DialogBookmarkBinding import io.legado.app.databinding.FragmentBookmarkBinding -import io.legado.app.help.coroutine.Coroutine -import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.ATH import io.legado.app.ui.widget.recycler.VerticalDivider -import io.legado.app.utils.requestInputMethod import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.Job import kotlinx.coroutines.flow.collect @@ -73,30 +69,6 @@ class BookmarkFragment : VMBaseFragment(R.layout.fragment_bookmark } override fun onLongClick(bookmark: Bookmark) { - requireContext().alert(R.string.bookmark) { - setMessage(bookmark.chapterName) - val alertBinding = DialogBookmarkBinding.inflate(layoutInflater).apply { - editBookText.setText(bookmark.bookText) - editView.setText(bookmark.content) - editBookText.textSize = 15f - editView.textSize = 15f - editBookText.maxLines = 6 - editView.maxLines = 6 - } - customView { alertBinding.root } - yesButton { - alertBinding.apply { - Coroutine.async { - bookmark.bookText = editBookText.text.toString() - bookmark.content = editView.text.toString() - appDb.bookmarkDao.insert(bookmark) - } - } - } - noButton() - neutralButton(R.string.delete) { - appDb.bookmarkDao.delete(bookmark) - } - }.show().requestInputMethod() + BookmarkDialog.start(childFragmentManager, bookmark) } } \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_bookmark.xml b/app/src/main/res/layout/dialog_bookmark.xml index 168324bfd..231b945ca 100644 --- a/app/src/main/res/layout/dialog_bookmark.xml +++ b/app/src/main/res/layout/dialog_bookmark.xml @@ -1,48 +1,107 @@ - + android:layout_height="wrap_content" + xmlns:tools="http://schemas.android.com/tools" + android:orientation="vertical"> - + android:theme="?attr/actionBarStyle" + app:title="@string/bookmark" + app:popupTheme="@style/AppTheme.PopupOverlay" + app:titleTextAppearance="@style/ToolbarTitle" /> - + + + + + android:orientation="vertical" + android:padding="16dp"> - + android:paddingTop="3dp"> - + - + - + android:paddingTop="3dp"> + + + + + + + + - + + + + + + + + + - + - \ No newline at end of file + +