Merge pull request #73 from KKL369/master

开启目录标签功能
pull/78/head
kunfei 5 years ago committed by GitHub
commit dc331ab596
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt
  2. 4
      app/src/main/java/io/legado/app/data/entities/Bookmark.kt
  3. 41
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  4. 4
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  5. 13
      app/src/main/java/io/legado/app/ui/chapterlist/BookmarkAdapter.kt
  6. 22
      app/src/main/java/io/legado/app/ui/chapterlist/BookmarkFragment.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<Int, Bookmark>
@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)
}

@ -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

@ -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<ReadBookViewModel>(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<ReadBookViewModel>(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<ReadBookViewModel>(R.layout.activity_boo
observeEvent<String>(Bus.TIME_CHANGED) { page_view.upTime() }
observeEvent<Int>(Bus.BATTERY_CHANGED) { page_view.upBattery(it) }
observeEvent<BookChapter>(Bus.OPEN_CHAPTER) {
viewModel.openChapter(it.index)
viewModel.openChapter(it.index, ReadBook.durPageIndex)
page_view.upContent()
}
observeEvent<Boolean>(Bus.MEDIA_BUTTON) {

@ -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()

@ -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<Bookmark, BookmarkAdapter.MyViewHolder>(DIFF_CALLBACK) {
class BookmarkAdapter(val callback: Callback) : PagedListAdapter<Bookmark, BookmarkAdapter.MyViewHolder>(DIFF_CALLBACK) {
companion object {
@ -29,8 +31,6 @@ class BookmarkAdapter : PagedListAdapter<Bookmark, BookmarkAdapter.MyViewHolder>
}
}
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<Bookmark, BookmarkAdapter.MyViewHolder>
itemView.onClick {
callback?.open(bookmark)
}
itemView.onLongClick {
execute {
callback?.delBookmark(bookmark)
}
true
}
}
}
interface Callback {
fun open(bookmark: Bookmark)
fun delBookmark(bookmark: Bookmark)
}
}

@ -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<ChapterListViewModel>(R.layout.fragment_bookmark) {
class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_bookmark),
BookmarkAdapter.Callback {
override val viewModel: ChapterListViewModel
get() = getViewModelOfActivity(ChapterListViewModel::class.java)
@ -32,7 +36,7 @@ class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(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<ChapterListViewModel>(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)
}
}
}
Loading…
Cancel
Save