优化书签功能

pull/293/head
gedoor 5 years ago
parent ac17bedb5c
commit 550295632c
  1. 52
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  2. 8
      app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt
  3. 1
      app/src/main/java/io/legado/app/data/entities/Bookmark.kt
  4. 6
      app/src/main/java/io/legado/app/ui/book/chapterlist/BookmarkFragment.kt
  5. 5
      app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListActivity.kt
  6. 8
      app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListFragment.kt
  7. 12
      app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListViewModel.kt

@ -19,7 +19,7 @@ import kotlinx.coroutines.launch
ReplaceRule::class, SearchBook::class, SearchKeyword::class, Cookie::class, ReplaceRule::class, SearchBook::class, SearchKeyword::class, Cookie::class,
RssSource::class, Bookmark::class, RssArticle::class, RssReadRecord::class, RssSource::class, Bookmark::class, RssArticle::class, RssReadRecord::class,
RssStar::class, TxtTocRule::class], RssStar::class, TxtTocRule::class],
version = 14, version = 15,
exportSchema = true exportSchema = true
) )
abstract class AppDatabase : RoomDatabase() { abstract class AppDatabase : RoomDatabase() {
@ -31,7 +31,13 @@ abstract class AppDatabase : RoomDatabase() {
fun createDatabase(context: Context): AppDatabase { fun createDatabase(context: Context): AppDatabase {
return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME) return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME)
.fallbackToDestructiveMigration() .fallbackToDestructiveMigration()
.addMigrations(migration_10_11, migration_11_12, migration_12_13, migration_13_14) .addMigrations(
migration_10_11,
migration_11_12,
migration_12_13,
migration_13_14,
migration_14_15
)
.addCallback(object : Callback() { .addCallback(object : Callback() {
override fun onDestructiveMigration(db: SupportSQLiteDatabase) { override fun onDestructiveMigration(db: SupportSQLiteDatabase) {
GlobalScope.launch { Restore.restoreDatabase(Backup.backupPath) } GlobalScope.launch { Restore.restoreDatabase(Backup.backupPath) }
@ -56,21 +62,13 @@ abstract class AppDatabase : RoomDatabase() {
private val migration_11_12 = object : Migration(11, 12) { private val migration_11_12 = object : Migration(11, 12) {
override fun migrate(database: SupportSQLiteDatabase) { override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL( database.execSQL("ALTER TABLE rssSources ADD style TEXT ")
"""
ALTER TABLE rssSources ADD style TEXT
"""
)
} }
} }
private val migration_12_13 = object : Migration(12, 13) { private val migration_12_13 = object : Migration(12, 13) {
override fun migrate(database: SupportSQLiteDatabase) { override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL( database.execSQL("ALTER TABLE rssSources ADD articleStyle INTEGER NOT NULL DEFAULT 0 ")
"""
ALTER TABLE rssSources ADD articleStyle INTEGER NOT NULL DEFAULT 0
"""
)
} }
} }
@ -86,26 +84,16 @@ abstract class AppDatabase : RoomDatabase() {
`order` INTEGER NOT NULL, `originOrder` INTEGER NOT NULL, `useReplaceRule` INTEGER NOT NULL, `variable` TEXT, PRIMARY KEY(`bookUrl`)) `order` INTEGER NOT NULL, `originOrder` INTEGER NOT NULL, `useReplaceRule` INTEGER NOT NULL, `variable` TEXT, PRIMARY KEY(`bookUrl`))
""" """
) )
database.execSQL( database.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS `index_books_name_author` ON `books_new` (`name`, `author`) ")
""" database.execSQL("INSERT INTO books_new select * from books ")
CREATE UNIQUE INDEX IF NOT EXISTS `index_books_name_author` ON `books_new` (`name`, `author`) database.execSQL("DROP TABLE books")
""" database.execSQL("ALTER TABLE books_new RENAME TO books")
) }
database.execSQL( }
"""
INSERT INTO books_new select * from books private val migration_14_15 = object : Migration(14, 15) {
""" override fun migrate(database: SupportSQLiteDatabase) {
) database.execSQL("ALTER TABLE bookmarks ADD bookAuthor TEXT NOT NULL DEFAULT ''")
database.execSQL(
"""
DROP TABLE books
"""
)
database.execSQL(
"""
ALTER TABLE books_new RENAME TO books
"""
)
} }
} }
} }

@ -11,8 +11,12 @@ interface BookmarkDao {
@Query("select * from bookmarks") @Query("select * from bookmarks")
fun all(): List<Bookmark> fun all(): List<Bookmark>
@Query("select * from bookmarks where bookUrl = :bookUrl") @Query("select * from bookmarks where bookUrl = :bookUrl or (bookName = :bookName and bookAuthor = :bookAuthor)")
fun observeByBook(bookUrl: String): DataSource.Factory<Int, Bookmark> fun observeByBook(
bookUrl: String,
bookName: String,
bookAuthor: String
): DataSource.Factory<Int, Bookmark>
@Query("SELECT * FROM bookmarks where bookUrl = :bookUrl and chapterName like '%'||:key||'%' or content like '%'||:key||'%'") @Query("SELECT * FROM bookmarks where bookUrl = :bookUrl and chapterName like '%'||:key||'%' or content like '%'||:key||'%'")
fun liveDataSearch(bookUrl: String, key: String): DataSource.Factory<Int, Bookmark> fun liveDataSearch(bookUrl: String, key: String): DataSource.Factory<Int, Bookmark>

@ -13,6 +13,7 @@ data class Bookmark(
var time: Long = System.currentTimeMillis(), var time: Long = System.currentTimeMillis(),
var bookUrl: String = "", var bookUrl: String = "",
var bookName: String = "", var bookName: String = "",
val bookAuthor: String = "",
var chapterIndex: Int = 0, var chapterIndex: Int = 0,
var pageIndex: Int = 0, var pageIndex: Int = 0,
var chapterName: String = "", var chapterName: String = "",

@ -43,11 +43,15 @@ class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_
} }
private fun initData() { private fun initData() {
viewModel.book?.let { book ->
bookmarkLiveData?.removeObservers(viewLifecycleOwner) bookmarkLiveData?.removeObservers(viewLifecycleOwner)
bookmarkLiveData = bookmarkLiveData =
LivePagedListBuilder(App.db.bookmarkDao().observeByBook(viewModel.bookUrl), 20).build() LivePagedListBuilder(
App.db.bookmarkDao().observeByBook(book.bookUrl, book.name, book.author), 20
).build()
bookmarkLiveData?.observe(viewLifecycleOwner, Observer { adapter.submitList(it) }) bookmarkLiveData?.observe(viewLifecycleOwner, Observer { adapter.submitList(it) })
} }
}
override fun startBookmarkSearch(newText: String?) { override fun startBookmarkSearch(newText: String?) {
if (newText.isNullOrBlank()) { if (newText.isNullOrBlank()) {

@ -28,10 +28,13 @@ class ChapterListActivity : VMBaseActivity<ChapterListViewModel>(R.layout.activi
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
tab_layout.isTabIndicatorFullWidth = false tab_layout.isTabIndicatorFullWidth = false
tab_layout.setSelectedTabIndicatorColor(accentColor) tab_layout.setSelectedTabIndicatorColor(accentColor)
viewModel.bookUrl = intent.getStringExtra("bookUrl") ?: "" intent.getStringExtra("bookUrl")?.let {
viewModel.initBook(it) {
view_pager.adapter = TabFragmentPageAdapter(supportFragmentManager) view_pager.adapter = TabFragmentPageAdapter(supportFragmentManager)
tab_layout.setupWithViewPager(view_pager) tab_layout.setupWithViewPager(view_pager)
} }
}
}
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.search_view, menu) menuInflater.inflate(R.menu.search_view, menu)

@ -33,7 +33,6 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
get() = getViewModelOfActivity(ChapterListViewModel::class.java) get() = getViewModelOfActivity(ChapterListViewModel::class.java)
lateinit var adapter: ChapterListAdapter lateinit var adapter: ChapterListAdapter
private var book: Book? = null
private var durChapterIndex = 0 private var durChapterIndex = 0
private lateinit var mLayoutManager: UpLinearLayoutManager private lateinit var mLayoutManager: UpLinearLayoutManager
private var tocLiveData: LiveData<List<BookChapter>>? = null private var tocLiveData: LiveData<List<BookChapter>>? = null
@ -70,11 +69,8 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
private fun initBook() { private fun initBook() {
launch { launch {
withContext(IO) {
book = App.db.bookDao().getBook(viewModel.bookUrl)
}
initDoc() initDoc()
book?.let { viewModel.book?.let {
durChapterIndex = it.durChapterIndex durChapterIndex = it.durChapterIndex
tv_current_chapter_info.text = tv_current_chapter_info.text =
"${it.durChapterTitle}(${it.durChapterIndex + 1}/${it.totalChapterNum})" "${it.durChapterTitle}(${it.durChapterIndex + 1}/${it.totalChapterNum})"
@ -106,7 +102,7 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
override fun observeLiveBus() { override fun observeLiveBus() {
observeEvent<BookChapter>(EventBus.SAVE_CONTENT) { chapter -> observeEvent<BookChapter>(EventBus.SAVE_CONTENT) { chapter ->
book?.bookUrl?.let { bookUrl -> viewModel.book?.bookUrl?.let { bookUrl ->
if (chapter.bookUrl == bookUrl) { if (chapter.bookUrl == bookUrl) {
adapter.cacheFileNames.add(BookHelp.formatChapterName(chapter)) adapter.cacheFileNames.add(BookHelp.formatChapterName(chapter))
adapter.notifyItemChanged(chapter.index, true) adapter.notifyItemChanged(chapter.index, true)

@ -2,13 +2,25 @@ package io.legado.app.ui.book.chapterlist
import android.app.Application import android.app.Application
import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.Book
class ChapterListViewModel(application: Application) : BaseViewModel(application) { class ChapterListViewModel(application: Application) : BaseViewModel(application) {
var bookUrl: String = "" var bookUrl: String = ""
var book: Book? = null
var chapterCallBack: ChapterListCallBack? = null var chapterCallBack: ChapterListCallBack? = null
var bookMarkCallBack: BookmarkCallBack? = null var bookMarkCallBack: BookmarkCallBack? = null
fun initBook(bookUrl: String, success: () -> Unit) {
this.bookUrl = bookUrl
execute {
book = App.db.bookDao().getBook(bookUrl)
}.onSuccess {
success.invoke()
}
}
fun startChapterListSearch(newText: String?) { fun startChapterListSearch(newText: String?) {
chapterCallBack?.startChapterListSearch(newText) chapterCallBack?.startChapterListSearch(newText)
} }

Loading…
Cancel
Save