From 550295632ce92c124a939af8053d75389fbaf40f Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 2 Aug 2020 19:57:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B9=A6=E7=AD=BE=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/data/AppDatabase.kt | 52 +++++++------------ .../io/legado/app/data/dao/BookmarkDao.kt | 8 ++- .../io/legado/app/data/entities/Bookmark.kt | 1 + .../ui/book/chapterlist/BookmarkFragment.kt | 12 +++-- .../book/chapterlist/ChapterListActivity.kt | 9 ++-- .../book/chapterlist/ChapterListFragment.kt | 8 +-- .../book/chapterlist/ChapterListViewModel.kt | 12 +++++ 7 files changed, 55 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt index df83c5e7c..3e50aab3a 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -19,7 +19,7 @@ import kotlinx.coroutines.launch ReplaceRule::class, SearchBook::class, SearchKeyword::class, Cookie::class, RssSource::class, Bookmark::class, RssArticle::class, RssReadRecord::class, RssStar::class, TxtTocRule::class], - version = 14, + version = 15, exportSchema = true ) abstract class AppDatabase : RoomDatabase() { @@ -31,7 +31,13 @@ abstract class AppDatabase : RoomDatabase() { fun createDatabase(context: Context): AppDatabase { return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME) .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() { override fun onDestructiveMigration(db: SupportSQLiteDatabase) { GlobalScope.launch { Restore.restoreDatabase(Backup.backupPath) } @@ -56,21 +62,13 @@ abstract class AppDatabase : RoomDatabase() { private val migration_11_12 = object : Migration(11, 12) { override fun migrate(database: SupportSQLiteDatabase) { - database.execSQL( - """ - ALTER TABLE rssSources ADD style TEXT - """ - ) + database.execSQL("ALTER TABLE rssSources ADD style TEXT ") } } private val migration_12_13 = object : Migration(12, 13) { override fun migrate(database: SupportSQLiteDatabase) { - database.execSQL( - """ - ALTER TABLE rssSources ADD articleStyle INTEGER NOT NULL DEFAULT 0 - """ - ) + database.execSQL("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`)) """ ) - 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 - """ - ) - database.execSQL( - """ - DROP TABLE books - """ - ) - database.execSQL( - """ - ALTER TABLE books_new RENAME TO books - """ - ) + 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 ") + database.execSQL("DROP TABLE books") + database.execSQL("ALTER TABLE books_new RENAME TO 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 ''") } } } diff --git a/app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt b/app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt index c539b34d6..cec2761b0 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt @@ -11,8 +11,12 @@ interface BookmarkDao { @Query("select * from bookmarks") fun all(): List - @Query("select * from bookmarks where bookUrl = :bookUrl") - fun observeByBook(bookUrl: String): DataSource.Factory + @Query("select * from bookmarks where bookUrl = :bookUrl or (bookName = :bookName and bookAuthor = :bookAuthor)") + fun observeByBook( + bookUrl: String, + bookName: String, + bookAuthor: String + ): DataSource.Factory @Query("SELECT * FROM bookmarks where bookUrl = :bookUrl and chapterName like '%'||:key||'%' or content like '%'||:key||'%'") fun liveDataSearch(bookUrl: String, key: String): DataSource.Factory diff --git a/app/src/main/java/io/legado/app/data/entities/Bookmark.kt b/app/src/main/java/io/legado/app/data/entities/Bookmark.kt index acd6005a3..e7222bf33 100644 --- a/app/src/main/java/io/legado/app/data/entities/Bookmark.kt +++ b/app/src/main/java/io/legado/app/data/entities/Bookmark.kt @@ -13,6 +13,7 @@ data class Bookmark( var time: Long = System.currentTimeMillis(), var bookUrl: String = "", var bookName: String = "", + val bookAuthor: String = "", var chapterIndex: Int = 0, var pageIndex: Int = 0, var chapterName: String = "", diff --git a/app/src/main/java/io/legado/app/ui/book/chapterlist/BookmarkFragment.kt b/app/src/main/java/io/legado/app/ui/book/chapterlist/BookmarkFragment.kt index 32987475e..47c607e8f 100644 --- a/app/src/main/java/io/legado/app/ui/book/chapterlist/BookmarkFragment.kt +++ b/app/src/main/java/io/legado/app/ui/book/chapterlist/BookmarkFragment.kt @@ -43,10 +43,14 @@ class BookmarkFragment : VMBaseFragment(R.layout.fragment_ } private fun initData() { - bookmarkLiveData?.removeObservers(viewLifecycleOwner) - bookmarkLiveData = - LivePagedListBuilder(App.db.bookmarkDao().observeByBook(viewModel.bookUrl), 20).build() - bookmarkLiveData?.observe(viewLifecycleOwner, Observer { adapter.submitList(it) }) + viewModel.book?.let { book -> + bookmarkLiveData?.removeObservers(viewLifecycleOwner) + bookmarkLiveData = + LivePagedListBuilder( + App.db.bookmarkDao().observeByBook(book.bookUrl, book.name, book.author), 20 + ).build() + bookmarkLiveData?.observe(viewLifecycleOwner, Observer { adapter.submitList(it) }) + } } override fun startBookmarkSearch(newText: String?) { diff --git a/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListActivity.kt b/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListActivity.kt index 7420fc047..58f19a6b4 100644 --- a/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListActivity.kt @@ -28,9 +28,12 @@ class ChapterListActivity : VMBaseActivity(R.layout.activi override fun onActivityCreated(savedInstanceState: Bundle?) { tab_layout.isTabIndicatorFullWidth = false tab_layout.setSelectedTabIndicatorColor(accentColor) - viewModel.bookUrl = intent.getStringExtra("bookUrl") ?: "" - view_pager.adapter = TabFragmentPageAdapter(supportFragmentManager) - tab_layout.setupWithViewPager(view_pager) + intent.getStringExtra("bookUrl")?.let { + viewModel.initBook(it) { + view_pager.adapter = TabFragmentPageAdapter(supportFragmentManager) + tab_layout.setupWithViewPager(view_pager) + } + } } override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { diff --git a/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListFragment.kt b/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListFragment.kt index 104f78e23..f22b54516 100644 --- a/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListFragment.kt @@ -33,7 +33,6 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme get() = getViewModelOfActivity(ChapterListViewModel::class.java) lateinit var adapter: ChapterListAdapter - private var book: Book? = null private var durChapterIndex = 0 private lateinit var mLayoutManager: UpLinearLayoutManager private var tocLiveData: LiveData>? = null @@ -70,11 +69,8 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme @SuppressLint("SetTextI18n") private fun initBook() { launch { - withContext(IO) { - book = App.db.bookDao().getBook(viewModel.bookUrl) - } initDoc() - book?.let { + viewModel.book?.let { durChapterIndex = it.durChapterIndex tv_current_chapter_info.text = "${it.durChapterTitle}(${it.durChapterIndex + 1}/${it.totalChapterNum})" @@ -106,7 +102,7 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme override fun observeLiveBus() { observeEvent(EventBus.SAVE_CONTENT) { chapter -> - book?.bookUrl?.let { bookUrl -> + viewModel.book?.bookUrl?.let { bookUrl -> if (chapter.bookUrl == bookUrl) { adapter.cacheFileNames.add(BookHelp.formatChapterName(chapter)) adapter.notifyItemChanged(chapter.index, true) diff --git a/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListViewModel.kt b/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListViewModel.kt index ad3361fd1..7671facc9 100644 --- a/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListViewModel.kt @@ -2,13 +2,25 @@ package io.legado.app.ui.book.chapterlist import android.app.Application +import io.legado.app.App import io.legado.app.base.BaseViewModel +import io.legado.app.data.entities.Book class ChapterListViewModel(application: Application) : BaseViewModel(application) { var bookUrl: String = "" + var book: Book? = null var chapterCallBack: ChapterListCallBack? = 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?) { chapterCallBack?.startChapterListSearch(newText) }