开启目录标签功能

pull/73/head
Administrator 5 years ago
parent af400cfd7d
commit cf41c2d162
  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.paging.DataSource
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query import androidx.room.Query
import io.legado.app.data.entities.Bookmark import io.legado.app.data.entities.Bookmark
@ -15,4 +17,10 @@ interface BookmarkDao {
@Query("select * from bookmarks where bookUrl = :bookUrl") @Query("select * from bookmarks where bookUrl = :bookUrl")
fun observeByBook(bookUrl: String): DataSource.Factory<Int, Bookmark> 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 import kotlinx.android.parcel.Parcelize
@Parcelize @Parcelize
@Entity(tableName = "bookmarks", indices = [(Index(value = ["bookUrl"], unique = true))]) @Entity(tableName = "bookmarks", indices = [(Index(value = ["time"], unique = true))])
data class Bookmark( data class Bookmark(
@PrimaryKey @PrimaryKey
var time: Long = System.currentTimeMillis(), var time: Long = System.currentTimeMillis(),
var bookUrl: String = "", var bookUrl: String = "",
var bookName: String = "", var bookName: String = "",
var chapterName: String = "",
var chapterIndex: Int = 0, var chapterIndex: Int = 0,
var pageIndex: Int = 0, var pageIndex: Int = 0,
var chapterName: String = "",
var content: String = "" var content: String = ""
) : Parcelable ) : Parcelable

@ -4,15 +4,18 @@ import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.AsyncTask.execute
import android.os.Bundle import android.os.Bundle
import android.text.SpannableStringBuilder import android.text.SpannableStringBuilder
import android.view.KeyEvent import android.view.KeyEvent
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.widget.EditText
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import com.jaredrummler.android.colorpicker.ColorPickerDialogListener import com.jaredrummler.android.colorpicker.ColorPickerDialogListener
import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.Bus 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.constant.Status
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.Bookmark
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.lib.dialogs.* import io.legado.app.lib.dialogs.*
import io.legado.app.receiver.TimeElectricityReceiver import io.legado.app.receiver.TimeElectricityReceiver
@ -41,6 +45,7 @@ import io.legado.app.ui.replacerule.edit.ReplaceEditDialog
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.android.synthetic.main.activity_book_read.* import kotlinx.android.synthetic.main.activity_book_read.*
import kotlinx.android.synthetic.main.dialog_download_choice.view.* 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_book_page.*
import kotlinx.android.synthetic.main.view_read_menu.* import kotlinx.android.synthetic.main.view_read_menu.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
@ -174,7 +179,35 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
}.show().applyTint() }.show().applyTint()
} }
R.id.menu_add_bookmark -> { 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 -> { R.id.menu_copy_text -> {
@ -438,8 +471,8 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
when (requestCode) { when (requestCode) {
requestCodeEditSource -> viewModel.upBookSource() requestCodeEditSource -> viewModel.upBookSource()
requestCodeChapterList -> requestCodeChapterList ->
data?.getIntExtra("index", ReadBook.durChapterIndex)?.let { data?.getIntExtra("index", ReadBook.durChapterIndex)?.let { index ->
viewModel.openChapter(it) viewModel.openChapter(index, data?.getIntExtra("pageIndex", ReadBook.durPageIndex))
} }
requestCodeReplace -> ReadBook.loadContent() requestCodeReplace -> ReadBook.loadContent()
} }
@ -476,7 +509,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
observeEvent<String>(Bus.TIME_CHANGED) { page_view.upTime() } observeEvent<String>(Bus.TIME_CHANGED) { page_view.upTime() }
observeEvent<Int>(Bus.BATTERY_CHANGED) { page_view.upBattery(it) } observeEvent<Int>(Bus.BATTERY_CHANGED) { page_view.upBattery(it) }
observeEvent<BookChapter>(Bus.OPEN_CHAPTER) { observeEvent<BookChapter>(Bus.OPEN_CHAPTER) {
viewModel.openChapter(it.index) viewModel.openChapter(it.index, ReadBook.durPageIndex)
page_view.upContent() page_view.upContent()
} }
observeEvent<Boolean>(Bus.MEDIA_BUTTON) { 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.prevTextChapter = null
ReadBook.curTextChapter = null ReadBook.curTextChapter = null
ReadBook.nextTextChapter = null ReadBook.nextTextChapter = null
ReadBook.callBack?.upContent() ReadBook.callBack?.upContent()
if (index != ReadBook.durChapterIndex) { if (index != ReadBook.durChapterIndex) {
ReadBook.durChapterIndex = index ReadBook.durChapterIndex = index
ReadBook.durPageIndex = 0 ReadBook.durPageIndex = pageIndex
} }
ReadBook.saveRead() ReadBook.saveRead()
ReadBook.loadContent() ReadBook.loadContent()

@ -1,5 +1,6 @@
package io.legado.app.ui.chapterlist package io.legado.app.ui.chapterlist
import android.os.AsyncTask.execute
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -10,9 +11,10 @@ import io.legado.app.R
import io.legado.app.data.entities.Bookmark import io.legado.app.data.entities.Bookmark
import kotlinx.android.synthetic.main.item_bookmark.view.* import kotlinx.android.synthetic.main.item_bookmark.view.*
import org.jetbrains.anko.sdk27.listeners.onClick 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 { companion object {
@ -29,8 +31,6 @@ class BookmarkAdapter : PagedListAdapter<Bookmark, BookmarkAdapter.MyViewHolder>
} }
} }
var callback: Callback? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_bookmark, parent, false)) 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 { itemView.onClick {
callback?.open(bookmark) callback?.open(bookmark)
} }
itemView.onLongClick {
execute {
callback?.delBookmark(bookmark)
}
true
}
} }
} }
interface Callback { interface Callback {
fun open(bookmark: Bookmark) fun open(bookmark: Bookmark)
fun delBookmark(bookmark: Bookmark)
} }
} }

@ -1,5 +1,7 @@
package io.legado.app.ui.chapterlist package io.legado.app.ui.chapterlist
import android.app.Activity
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import android.widget.LinearLayout import android.widget.LinearLayout
@ -17,7 +19,9 @@ import io.legado.app.lib.theme.ATH
import io.legado.app.utils.getViewModelOfActivity import io.legado.app.utils.getViewModelOfActivity
import kotlinx.android.synthetic.main.fragment_bookmark.* 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 override val viewModel: ChapterListViewModel
get() = getViewModelOfActivity(ChapterListViewModel::class.java) get() = getViewModelOfActivity(ChapterListViewModel::class.java)
@ -32,7 +36,7 @@ class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_
private fun initRecyclerView() { private fun initRecyclerView() {
ATH.applyEdgeEffectColor(recycler_view) ATH.applyEdgeEffectColor(recycler_view)
adapter = BookmarkAdapter() adapter = BookmarkAdapter(this)
recycler_view.layoutManager = LinearLayoutManager(requireContext()) recycler_view.layoutManager = LinearLayoutManager(requireContext())
recycler_view.addItemDecoration( recycler_view.addItemDecoration(
DividerItemDecoration( DividerItemDecoration(
@ -48,4 +52,18 @@ class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_
bookmarkLiveData = LivePagedListBuilder(App.db.bookmarkDao().observeByBook(viewModel.bookUrl ?: ""), 20).build() bookmarkLiveData = LivePagedListBuilder(App.db.bookmarkDao().observeByBook(viewModel.bookUrl ?: ""), 20).build()
bookmarkLiveData?.observe(viewLifecycleOwner, Observer { adapter.submitList(it) }) 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