书签绑定书名和作者

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. 10
      app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt

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

@ -24,7 +24,7 @@ val appDb by lazy {
RssSource::class, Bookmark::class, RssArticle::class, RssReadRecord::class,
RssStar::class, TxtTocRule::class, ReadRecord::class, HttpTTS::class, Cache::class,
RuleSub::class],
version = 32,
version = 33,
exportSchema = true
)
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_23_24, migration_24_25, migration_25_26, migration_26_27,
migration_27_28, migration_28_29, migration_29_30, migration_30_31,
migration_31_32
migration_31_32, migration_32_33
)
.allowMainThreadQueries()
.addCallback(dbCallback)
@ -304,6 +304,33 @@ abstract class AppDatabase : RoomDatabase() {
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")
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(
bookUrl: String,
bookName: String,
bookAuthor: String
): LiveData<List<Bookmark>>
@Query("SELECT * FROM bookmarks where bookUrl = :bookUrl and chapterName like '%'||:key||'%' or content like '%'||:key||'%'")
fun liveDataSearch(bookUrl: String, key: String): LiveData<List<Bookmark>>
@Query(
"""
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)
fun insert(vararg bookmark: Bookmark)
@ -30,7 +38,4 @@ interface BookmarkDao {
@Delete
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 {
const val hTag = 2L
const val rubyTag = 4L

@ -7,12 +7,14 @@ import androidx.room.PrimaryKey
import kotlinx.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(
@PrimaryKey
var time: Long = System.currentTimeMillis(),
var bookUrl: String = "",
var bookName: String = "",
val time: Long = System.currentTimeMillis(),
val bookName: String = "",
val bookAuthor: String = "",
var chapterIndex: 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.BookChapter
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.ReadTipConfig
import io.legado.app.help.storage.Backup
@ -241,14 +240,12 @@ class ReadBookActivity : ReadBookBaseActivity(),
val book = ReadBook.book
val page = ReadBook.curTextChapter?.page(ReadBook.durPageIndex())
if (book != null && page != null) {
val bookmark = Bookmark(
bookUrl = book.bookUrl,
bookName = book.name,
chapterIndex = ReadBook.durChapterIndex,
chapterPos = ReadBook.durChapterPos,
chapterName = page.title,
val bookmark = book.createBookMark().apply {
chapterIndex = ReadBook.durChapterIndex
chapterPos = ReadBook.durChapterPos
chapterName = page.title
bookText = page.text.trim()
)
}
showBookMark(bookmark)
}
}

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

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

Loading…
Cancel
Save