书签绑定书名和作者

pull/1013/head
gedoor 4 years ago
parent ea61daddb9
commit c96961f143
  1. 4
      app/src/main/assets/updateLog.md
  2. 31
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  3. 19
      app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt
  4. 7
      app/src/main/java/io/legado/app/data/entities/Book.kt
  5. 10
      app/src/main/java/io/legado/app/data/entities/Bookmark.kt
  6. 13
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  7. 15
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  8. 16
      app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt

@ -3,6 +3,10 @@
* 关注合作公众号 **[小说拾遗]** 获取好看的小说。 * 关注合作公众号 **[小说拾遗]** 获取好看的小说。
* 旧版数据导入教程:先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】。 * 旧版数据导入教程:先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】。
**2021/05/26**
* 书签绑定书名与作者
* 修复详情页目录问题
**2021/05/24** **2021/05/24**
* 反转目录后刷新内容 * 反转目录后刷新内容
* 修复上下滑动会导致左右切换问题 * 修复上下滑动会导致左右切换问题

@ -24,7 +24,7 @@ val appDb by lazy {
RssSource::class, Bookmark::class, RssArticle::class, RssReadRecord::class, RssSource::class, Bookmark::class, RssArticle::class, RssReadRecord::class,
RssStar::class, TxtTocRule::class, ReadRecord::class, HttpTTS::class, Cache::class, RssStar::class, TxtTocRule::class, ReadRecord::class, HttpTTS::class, Cache::class,
RuleSub::class], RuleSub::class],
version = 32, version = 33,
exportSchema = true exportSchema = true
) )
abstract class AppDatabase : RoomDatabase() { abstract class AppDatabase : RoomDatabase() {
@ -60,7 +60,7 @@ abstract class AppDatabase : RoomDatabase() {
migration_19_20, migration_20_21, migration_21_22, migration_22_23, migration_19_20, migration_20_21, migration_21_22, migration_22_23,
migration_23_24, migration_24_25, migration_25_26, migration_26_27, migration_23_24, migration_24_25, migration_25_26, migration_26_27,
migration_27_28, migration_28_29, migration_29_30, migration_30_31, migration_27_28, migration_28_29, migration_29_30, migration_30_31,
migration_31_32 migration_31_32, migration_32_33
) )
.allowMainThreadQueries() .allowMainThreadQueries()
.addCallback(dbCallback) .addCallback(dbCallback)
@ -304,6 +304,33 @@ abstract class AppDatabase : RoomDatabase() {
database.execSQL("DROP TABLE `epubChapters`") database.execSQL("DROP TABLE `epubChapters`")
} }
} }
private val migration_32_33 = object : Migration(32, 33) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE bookmarks RENAME TO bookmarks_old")
database.execSQL(
"""
CREATE TABLE IF NOT EXISTS `bookmarks` (`time` INTEGER NOT NULL,
`bookName` TEXT NOT NULL, `bookAuthor` TEXT NOT NULL, `chapterIndex` INTEGER NOT NULL,
`chapterPos` INTEGER NOT NULL, `chapterName` TEXT NOT NULL, `bookText` TEXT NOT NULL,
`content` TEXT NOT NULL, PRIMARY KEY(`time`))
"""
)
database.execSQL(
"""
CREATE INDEX IF NOT EXISTS `index_bookmarks_bookName_bookAuthor` ON `bookmarks` (`bookName`, `bookAuthor`)
"""
)
database.execSQL(
"""
insert into bookmarks (time, bookName, bookAuthor, chapterIndex, chapterPos, chapterName, bookText, content)
select time, ifNull(b.name, bookName) bookName, ifNull(b.author, bookAuthor) bookAuthor,
chapterIndex, chapterPos, chapterName, bookText, content from bookmarks_old o
left join books b on o.bookUrl = b.bookUrl
"""
)
}
}
} }
} }

@ -11,15 +11,23 @@ interface BookmarkDao {
@get:Query("select * from bookmarks") @get:Query("select * from bookmarks")
val all: List<Bookmark> val all: List<Bookmark>
@Query("select * from bookmarks where bookUrl = :bookUrl or (bookName = :bookName and bookAuthor = :bookAuthor)") @Query("select * from bookmarks where bookName = :bookName and bookAuthor = :bookAuthor")
fun observeByBook( fun observeByBook(
bookUrl: String,
bookName: String, bookName: String,
bookAuthor: String bookAuthor: String
): LiveData<List<Bookmark>> ): LiveData<List<Bookmark>>
@Query("SELECT * FROM bookmarks where bookUrl = :bookUrl and chapterName like '%'||:key||'%' or content like '%'||:key||'%'") @Query(
fun liveDataSearch(bookUrl: String, key: String): LiveData<List<Bookmark>> """
SELECT * FROM bookmarks where bookName = :bookName and bookAuthor = :bookAuthor
and chapterName like '%'||:key||'%' or content like '%'||:key||'%'
"""
)
fun liveDataSearch(
bookName: String,
bookAuthor: String,
key: String
): LiveData<List<Bookmark>>
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg bookmark: Bookmark) fun insert(vararg bookmark: Bookmark)
@ -30,7 +38,4 @@ interface BookmarkDao {
@Delete @Delete
fun delete(vararg bookmark: Bookmark) fun delete(vararg bookmark: Bookmark)
@Query("delete from bookmarks where bookUrl = :bookUrl and chapterName like '%'||:chapterName||'%'")
fun delByBookmark(bookUrl: String, chapterName: String)
} }

@ -227,6 +227,13 @@ data class Book(
} }
} }
fun createBookMark(): Bookmark {
return Bookmark(
bookName = name,
bookAuthor = author,
)
}
companion object { companion object {
const val hTag = 2L const val hTag = 2L
const val rubyTag = 4L const val rubyTag = 4L

@ -7,12 +7,14 @@ import androidx.room.PrimaryKey
import kotlinx.parcelize.Parcelize import kotlinx.parcelize.Parcelize
@Parcelize @Parcelize
@Entity(tableName = "bookmarks", indices = [(Index(value = ["time"], unique = true))]) @Entity(
tableName = "bookmarks",
indices = [(Index(value = ["bookName", "bookAuthor"], unique = false))]
)
data class Bookmark( data class Bookmark(
@PrimaryKey @PrimaryKey
var time: Long = System.currentTimeMillis(), val time: Long = System.currentTimeMillis(),
var bookUrl: String = "", val bookName: String = "",
var bookName: String = "",
val bookAuthor: String = "", val bookAuthor: String = "",
var chapterIndex: Int = 0, var chapterIndex: Int = 0,
var chapterPos: Int = 0, var chapterPos: Int = 0,

@ -23,7 +23,6 @@ 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.BookProgress import io.legado.app.data.entities.BookProgress
import io.legado.app.data.entities.Bookmark
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.help.ReadTipConfig import io.legado.app.help.ReadTipConfig
import io.legado.app.help.storage.Backup import io.legado.app.help.storage.Backup
@ -241,14 +240,12 @@ class ReadBookActivity : ReadBookBaseActivity(),
val book = ReadBook.book val book = ReadBook.book
val page = ReadBook.curTextChapter?.page(ReadBook.durPageIndex()) val page = ReadBook.curTextChapter?.page(ReadBook.durPageIndex())
if (book != null && page != null) { if (book != null && page != null) {
val bookmark = Bookmark( val bookmark = book.createBookMark().apply {
bookUrl = book.bookUrl, chapterIndex = ReadBook.durChapterIndex
bookName = book.name, chapterPos = ReadBook.durChapterPos
chapterIndex = ReadBook.durChapterIndex, chapterName = page.title
chapterPos = ReadBook.durChapterPos,
chapterName = page.title,
bookText = page.text.trim() bookText = page.text.trim()
) }
showBookMark(bookmark) showBookMark(bookmark)
} }
} }

@ -497,17 +497,14 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
fun createBookmark(): Bookmark? { fun createBookmark(): Bookmark? {
val page = relativePage(selectStart[0]) val page = relativePage(selectStart[0])
page.getTextChapter()?.let { chapter -> page.getTextChapter()?.let { chapter ->
val chapterPos = chapter.getReadLength(page.index) +
page.getSelectStartLength(selectStart[1], selectStart[2])
ReadBook.book?.let { book -> ReadBook.book?.let { book ->
return Bookmark( return book.createBookMark().apply {
bookUrl = book.bookUrl, chapterIndex = page.chapterIndex
bookName = book.name, chapterPos = chapter.getReadLength(page.index) +
chapterIndex = page.chapterIndex, page.getSelectStartLength(selectStart[1], selectStart[2])
chapterPos = chapterPos, chapterName = chapter.title
chapterName = chapter.title,
bookText = selectedText bookText = selectedText
) }
} }
} }
return null return null

@ -48,19 +48,19 @@ class BookmarkFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_bookmark
private fun initData(book: Book) { private fun initData(book: Book) {
bookmarkLiveData?.removeObservers(viewLifecycleOwner) bookmarkLiveData?.removeObservers(viewLifecycleOwner)
bookmarkLiveData = appDb.bookmarkDao.observeByBook(book.bookUrl, book.name, book.author) bookmarkLiveData = appDb.bookmarkDao.observeByBook(book.name, book.author)
bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.setItems(it) }) bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.setItems(it) })
} }
override fun startBookmarkSearch(newText: String?) { override fun startBookmarkSearch(newText: String?) {
if (newText.isNullOrBlank()) { viewModel.bookData.value?.let { book ->
viewModel.bookData.value?.let { if (newText.isNullOrBlank()) {
initData(it) initData(book)
} else {
bookmarkLiveData?.removeObservers(viewLifecycleOwner)
bookmarkLiveData = appDb.bookmarkDao.liveDataSearch(book.name, book.author, newText)
bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.setItems(it) })
} }
} else {
bookmarkLiveData?.removeObservers(viewLifecycleOwner)
bookmarkLiveData = appDb.bookmarkDao.liveDataSearch(viewModel.bookUrl, newText)
bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.setItems(it) })
} }
} }

Loading…
Cancel
Save