From cf41c2d162f1b00c424d88541311f921c8fe066d Mon Sep 17 00:00:00 2001 From: Administrator Date: Wed, 15 Jan 2020 09:44:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=90=AF=E7=9B=AE=E5=BD=95=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/data/dao/BookmarkDao.kt | 8 ++++ .../io/legado/app/data/entities/Bookmark.kt | 4 +- .../app/ui/book/read/ReadBookActivity.kt | 41 +++++++++++++++++-- .../app/ui/book/read/ReadBookViewModel.kt | 4 +- .../app/ui/chapterlist/BookmarkAdapter.kt | 13 ++++-- .../app/ui/chapterlist/BookmarkFragment.kt | 22 +++++++++- 6 files changed, 79 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt b/app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt index eb757c547..9010aa950 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt @@ -2,6 +2,8 @@ package io.legado.app.data.dao import androidx.paging.DataSource import androidx.room.Dao +import androidx.room.Insert +import androidx.room.OnConflictStrategy import androidx.room.Query import io.legado.app.data.entities.Bookmark @@ -15,4 +17,10 @@ interface BookmarkDao { @Query("select * from bookmarks where bookUrl = :bookUrl") fun observeByBook(bookUrl: String): DataSource.Factory + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insert(vararg bookmark: Bookmark) + + @Query("delete from bookmarks where bookUrl = :bookUrl and chapterName like '%'||:chapterName||'%'") + fun delByBookmark(bookUrl: String, chapterName: String) + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/Bookmark.kt b/app/src/main/java/io/legado/app/data/entities/Bookmark.kt index c1f638137..acd6005a3 100644 --- a/app/src/main/java/io/legado/app/data/entities/Bookmark.kt +++ b/app/src/main/java/io/legado/app/data/entities/Bookmark.kt @@ -7,15 +7,15 @@ import androidx.room.PrimaryKey import kotlinx.android.parcel.Parcelize @Parcelize -@Entity(tableName = "bookmarks", indices = [(Index(value = ["bookUrl"], unique = true))]) +@Entity(tableName = "bookmarks", indices = [(Index(value = ["time"], unique = true))]) data class Bookmark( @PrimaryKey var time: Long = System.currentTimeMillis(), var bookUrl: String = "", var bookName: String = "", - var chapterName: String = "", var chapterIndex: Int = 0, var pageIndex: Int = 0, + var chapterName: String = "", var content: String = "" ) : Parcelable \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 3a2523293..3125f70d0 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -4,15 +4,18 @@ import android.annotation.SuppressLint import android.app.Activity import android.content.Intent import android.net.Uri +import android.os.AsyncTask.execute import android.os.Bundle import android.text.SpannableStringBuilder import android.view.KeyEvent import android.view.Menu import android.view.MenuItem import android.view.View +import android.widget.EditText import androidx.core.view.isVisible import androidx.lifecycle.Observer import com.jaredrummler.android.colorpicker.ColorPickerDialogListener +import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Bus @@ -20,6 +23,7 @@ import io.legado.app.constant.PreferKey import io.legado.app.constant.Status import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter +import io.legado.app.data.entities.Bookmark import io.legado.app.help.ReadBookConfig import io.legado.app.lib.dialogs.* import io.legado.app.receiver.TimeElectricityReceiver @@ -41,6 +45,7 @@ import io.legado.app.ui.replacerule.edit.ReplaceEditDialog import io.legado.app.utils.* import kotlinx.android.synthetic.main.activity_book_read.* import kotlinx.android.synthetic.main.dialog_download_choice.view.* +import kotlinx.android.synthetic.main.dialog_edit_text.view.* import kotlinx.android.synthetic.main.view_book_page.* import kotlinx.android.synthetic.main.view_read_menu.* import kotlinx.coroutines.Dispatchers.IO @@ -174,7 +179,35 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo }.show().applyTint() } R.id.menu_add_bookmark -> { - + val book = ReadBook.book + val textChapter = ReadBook.curTextChapter + alert(title = getString(R.string.bookmark_add)) { + var editText: EditText? = null + message = book?.name + " • " + textChapter?.title + customView { + layoutInflater.inflate(R.layout.dialog_edit_text, null).apply { + editText = edit_view.apply { + hint = "备注内容" + } + } + } + yesButton { + editText?.text?.toString()?.let { editContent -> + execute { + val bookmark = Bookmark( + book!!.durChapterTime, + book!!.bookUrl, + book!!.name, + ReadBook.durChapterIndex, + ReadBook.durPageIndex, + textChapter!!.title, + editContent) + App.db.bookmarkDao().insert(bookmark) + } + } + } + noButton() + }.show().applyTint().requestInputMethod() } R.id.menu_copy_text -> { @@ -438,8 +471,8 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo when (requestCode) { requestCodeEditSource -> viewModel.upBookSource() requestCodeChapterList -> - data?.getIntExtra("index", ReadBook.durChapterIndex)?.let { - viewModel.openChapter(it) + data?.getIntExtra("index", ReadBook.durChapterIndex)?.let { index -> + viewModel.openChapter(index, data?.getIntExtra("pageIndex", ReadBook.durPageIndex)) } requestCodeReplace -> ReadBook.loadContent() } @@ -476,7 +509,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo observeEvent(Bus.TIME_CHANGED) { page_view.upTime() } observeEvent(Bus.BATTERY_CHANGED) { page_view.upBattery(it) } observeEvent(Bus.OPEN_CHAPTER) { - viewModel.openChapter(it.index) + viewModel.openChapter(it.index, ReadBook.durPageIndex) page_view.upContent() } observeEvent(Bus.MEDIA_BUTTON) { diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 8ee7b8e47..959068a8a 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -154,14 +154,14 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } } - fun openChapter(index: Int) { + fun openChapter(index: Int, pageIndex: Int) { ReadBook.prevTextChapter = null ReadBook.curTextChapter = null ReadBook.nextTextChapter = null ReadBook.callBack?.upContent() if (index != ReadBook.durChapterIndex) { ReadBook.durChapterIndex = index - ReadBook.durPageIndex = 0 + ReadBook.durPageIndex = pageIndex } ReadBook.saveRead() ReadBook.loadContent() diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/BookmarkAdapter.kt b/app/src/main/java/io/legado/app/ui/chapterlist/BookmarkAdapter.kt index 6eff6e223..ea3f41801 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/BookmarkAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/BookmarkAdapter.kt @@ -1,5 +1,6 @@ package io.legado.app.ui.chapterlist +import android.os.AsyncTask.execute import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -10,9 +11,10 @@ import io.legado.app.R import io.legado.app.data.entities.Bookmark import kotlinx.android.synthetic.main.item_bookmark.view.* import org.jetbrains.anko.sdk27.listeners.onClick +import org.jetbrains.anko.sdk27.listeners.onLongClick -class BookmarkAdapter : PagedListAdapter(DIFF_CALLBACK) { +class BookmarkAdapter(val callback: Callback) : PagedListAdapter(DIFF_CALLBACK) { companion object { @@ -29,8 +31,6 @@ class BookmarkAdapter : PagedListAdapter } } - var callback: Callback? = null - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_bookmark, parent, false)) } @@ -49,10 +49,17 @@ class BookmarkAdapter : PagedListAdapter itemView.onClick { callback?.open(bookmark) } + itemView.onLongClick { + execute { + callback?.delBookmark(bookmark) + } + true + } } } interface Callback { fun open(bookmark: Bookmark) + fun delBookmark(bookmark: Bookmark) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/BookmarkFragment.kt b/app/src/main/java/io/legado/app/ui/chapterlist/BookmarkFragment.kt index 0d42b5600..31efa44c3 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/BookmarkFragment.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/BookmarkFragment.kt @@ -1,5 +1,7 @@ package io.legado.app.ui.chapterlist +import android.app.Activity +import android.content.Intent import android.os.Bundle import android.view.View import android.widget.LinearLayout @@ -17,7 +19,9 @@ import io.legado.app.lib.theme.ATH import io.legado.app.utils.getViewModelOfActivity import kotlinx.android.synthetic.main.fragment_bookmark.* -class BookmarkFragment : VMBaseFragment(R.layout.fragment_bookmark) { + +class BookmarkFragment : VMBaseFragment(R.layout.fragment_bookmark), + BookmarkAdapter.Callback { override val viewModel: ChapterListViewModel get() = getViewModelOfActivity(ChapterListViewModel::class.java) @@ -32,7 +36,7 @@ class BookmarkFragment : VMBaseFragment(R.layout.fragment_ private fun initRecyclerView() { ATH.applyEdgeEffectColor(recycler_view) - adapter = BookmarkAdapter() + adapter = BookmarkAdapter(this) recycler_view.layoutManager = LinearLayoutManager(requireContext()) recycler_view.addItemDecoration( DividerItemDecoration( @@ -48,4 +52,18 @@ class BookmarkFragment : VMBaseFragment(R.layout.fragment_ bookmarkLiveData = LivePagedListBuilder(App.db.bookmarkDao().observeByBook(viewModel.bookUrl ?: ""), 20).build() bookmarkLiveData?.observe(viewLifecycleOwner, Observer { adapter.submitList(it) }) } + + override fun open(bookmark: Bookmark) { + val bookmarkData = Intent() + bookmarkData.putExtra("index", bookmark.chapterIndex) + bookmarkData.putExtra("pageIndex", bookmark.pageIndex) + activity?.setResult(Activity.RESULT_OK, bookmarkData) + activity?.finish() + } + + override fun delBookmark(bookmark: Bookmark) { + bookmark?.let { + App.db.bookmarkDao().delByBookmark(it.bookUrl, it.chapterName) + } + } } \ No newline at end of file