From f5045bdc0875da9cdba1b3128618fc748067121c Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 6 Jun 2019 22:08:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/constant/DbTable.kt | 7 +++++++ .../main/java/io/legado/app/data/AppDatabase.kt | 4 ++-- .../main/java/io/legado/app/data/dao/BookDao.kt | 12 ++++++------ .../java/io/legado/app/data/dao/BookSourceDao.kt | 14 ++++++++++++++ .../java/io/legado/app/data/dao/ReplaceRuleDao.kt | 3 ++- .../main/java/io/legado/app/data/entities/Book.kt | 3 ++- .../app/data/entities/{Source.kt => BookSource.kt} | 5 +++-- .../legado/app/data/entities/ExploreSearchUrl.kt | 2 +- .../io/legado/app/data/entities/ReplaceRule.kt | 3 ++- .../app/ui/main/booksource/BookSourceAdapter.kt | 10 +++++----- .../app/ui/main/booksource/BookSourceFragment.kt | 14 ++++++++++---- 11 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/io/legado/app/constant/DbTable.kt create mode 100644 app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt rename app/src/main/java/io/legado/app/data/entities/{Source.kt => BookSource.kt} (96%) diff --git a/app/src/main/java/io/legado/app/constant/DbTable.kt b/app/src/main/java/io/legado/app/constant/DbTable.kt new file mode 100644 index 000000000..11e8b2fb5 --- /dev/null +++ b/app/src/main/java/io/legado/app/constant/DbTable.kt @@ -0,0 +1,7 @@ +package io.legado.app.constant + +object DbTable { + const val books = "books" + const val book_sources = "book_sources" + const val replace_rules = "replace_rules" +} \ No newline at end of file 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 4e3be84f6..bd1ff6664 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -8,10 +8,10 @@ import androidx.room.migration.Migration import androidx.sqlite.db.SupportSQLiteDatabase import io.legado.app.data.dao.BookDao import io.legado.app.data.dao.ReplaceRuleDao +import io.legado.app.data.dao.BookSourceDao import io.legado.app.data.entities.Book import io.legado.app.data.entities.Chapter import io.legado.app.data.entities.ReplaceRule -import javax.xml.transform.Source @Database(entities = [Book::class, Chapter::class, ReplaceRule::class], version = 1, exportSchema = true) @@ -51,5 +51,5 @@ abstract class AppDatabase : RoomDatabase() { abstract fun bookDao(): BookDao abstract fun replaceRuleDao(): ReplaceRuleDao - + abstract fun sourceDao() : BookSourceDao } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/dao/BookDao.kt b/app/src/main/java/io/legado/app/data/dao/BookDao.kt index 0ea2baac1..a2cd9d7e7 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookDao.kt @@ -6,25 +6,25 @@ import androidx.room.Dao import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query +import io.legado.app.constant.DbTable import io.legado.app.data.entities.Book - @Dao interface BookDao { - @Query("SELECT * FROM books WHERE `group` = :group") + @Query("SELECT * FROM ${DbTable.books} WHERE `group` = :group") fun observeByGroup(group: Int): DataSource.Factory - @Query("SELECT descUrl FROM books WHERE `group` = :group") + @Query("SELECT descUrl FROM ${DbTable.books} WHERE `group` = :group") fun observeUrlsByGroup(group: Int): LiveData> - @Query("SELECT * FROM books WHERE `name` in (:names)") + @Query("SELECT * FROM ${DbTable.books} WHERE `name` in (:names)") fun findByName(vararg names: String): List - @get:Query("SELECT descUrl FROM books") + @get:Query("SELECT descUrl FROM ${DbTable.books}") val allBookUrls: List - @get:Query("SELECT COUNT(*) FROM books") + @get:Query("SELECT COUNT(*) FROM ${DbTable.books}") val allBookCount: Int @Insert(onConflict = OnConflictStrategy.REPLACE) diff --git a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt new file mode 100644 index 000000000..36e2a53de --- /dev/null +++ b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt @@ -0,0 +1,14 @@ +package io.legado.app.data.dao + +import androidx.paging.DataSource +import androidx.room.Dao +import androidx.room.Query +import io.legado.app.constant.DbTable +import io.legado.app.data.entities.BookSource + +@Dao +interface BookSourceDao { + + @Query("select * from ${DbTable.book_sources} where ") + fun observeAll(): DataSource.Factory +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt b/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt index d4a2073c0..c09c9037e 100644 --- a/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt @@ -3,13 +3,14 @@ package io.legado.app.data.dao import androidx.lifecycle.LiveData import androidx.paging.DataSource import androidx.room.* +import io.legado.app.constant.DbTable import io.legado.app.data.entities.ReplaceRule @Dao interface ReplaceRuleDao { - @Query("SELECT * FROM replace_rules ORDER BY sortOrder ASC") + @Query("SELECT * FROM ${DbTable.replace_rules} ORDER BY sortOrder ASC") fun observeAll(): DataSource.Factory @Query("SELECT id FROM replace_rules ORDER BY sortOrder ASC") diff --git a/app/src/main/java/io/legado/app/data/entities/Book.kt b/app/src/main/java/io/legado/app/data/entities/Book.kt index 78b3694e8..4f49cc7ef 100644 --- a/app/src/main/java/io/legado/app/data/entities/Book.kt +++ b/app/src/main/java/io/legado/app/data/entities/Book.kt @@ -3,10 +3,11 @@ package io.legado.app.data.entities import android.os.Parcelable import androidx.room.* import io.legado.app.constant.AppConst.NOT_AVAILABLE +import io.legado.app.constant.DbTable import kotlinx.android.parcel.Parcelize @Parcelize -@Entity(tableName = "books", indices = [(Index(value = ["descUrl"], unique = true))]) +@Entity(tableName = DbTable.books, indices = [(Index(value = ["descUrl"], unique = true))]) data class Book(@PrimaryKey var descUrl: String = "", // 详情页Url(本地书源存储完整文件路径) var tocUrl: String = "", // 目录页Url (toc=table of Contents) diff --git a/app/src/main/java/io/legado/app/data/entities/Source.kt b/app/src/main/java/io/legado/app/data/entities/BookSource.kt similarity index 96% rename from app/src/main/java/io/legado/app/data/entities/Source.kt rename to app/src/main/java/io/legado/app/data/entities/BookSource.kt index 5399384d7..4867d5dd9 100644 --- a/app/src/main/java/io/legado/app/data/entities/Source.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookSource.kt @@ -3,16 +3,17 @@ package io.legado.app.data.entities import android.os.Parcelable import androidx.room.* import io.legado.app.constant.AppUtils.GSON_CONVERTER +import io.legado.app.constant.DbTable import io.legado.app.data.entities.rule.* import kotlinx.android.parcel.IgnoredOnParcel import kotlinx.android.parcel.Parcelize @Parcelize @Entity( - tableName = "sources", + tableName = DbTable.book_sources, indices = [(Index(value = ["sourceId"])), (Index(value = ["origin"], unique = false))] ) -data class Source ( +data class BookSource ( @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "sourceId") var id: Int = 0, // 编号 diff --git a/app/src/main/java/io/legado/app/data/entities/ExploreSearchUrl.kt b/app/src/main/java/io/legado/app/data/entities/ExploreSearchUrl.kt index 2df63b4bb..97a4d2bfb 100644 --- a/app/src/main/java/io/legado/app/data/entities/ExploreSearchUrl.kt +++ b/app/src/main/java/io/legado/app/data/entities/ExploreSearchUrl.kt @@ -10,7 +10,7 @@ import kotlinx.android.parcel.Parcelize @Parcelize @Entity(tableName = "explore_search_urls", indices = [(Index(value = ["sourceId", "url"], unique = true))], - foreignKeys = [(ForeignKey(entity = Source::class, + foreignKeys = [(ForeignKey(entity = BookSource::class, parentColumns = ["sourceId"], childColumns = ["sourceId"], onDelete = ForeignKey.CASCADE))]) // 删除书源时自动删除章节 diff --git a/app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt b/app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt index 8b5a0fda6..6152c3bfd 100644 --- a/app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt +++ b/app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt @@ -5,10 +5,11 @@ import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.Index import androidx.room.PrimaryKey +import io.legado.app.constant.DbTable import kotlinx.android.parcel.Parcelize @Parcelize -@Entity(tableName = "replace_rules", +@Entity(tableName = DbTable.replace_rules, indices = [(Index(value = ["id"]))]) data class ReplaceRule( @PrimaryKey(autoGenerate = true) diff --git a/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceAdapter.kt b/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceAdapter.kt index ae5a9d506..f21f087e9 100644 --- a/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceAdapter.kt @@ -6,18 +6,18 @@ import android.view.ViewGroup import androidx.paging.PagedListAdapter import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView -import io.legado.app.data.entities.Source +import io.legado.app.data.entities.BookSource -class BookSourceAdapter(context : Context) : PagedListAdapter(DIFF_CALLBACK) { +class BookSourceAdapter(context : Context) : PagedListAdapter(DIFF_CALLBACK) { companion object { @JvmField - val DIFF_CALLBACK = object : DiffUtil.ItemCallback() { - override fun areItemsTheSame(oldItem: Source, newItem: Source): Boolean = + val DIFF_CALLBACK = object : DiffUtil.ItemCallback() { + override fun areItemsTheSame(oldItem: BookSource, newItem: BookSource): Boolean = oldItem.id == newItem.id - override fun areContentsTheSame(oldItem: Source, newItem: Source): Boolean = + override fun areContentsTheSame(oldItem: BookSource, newItem: BookSource): Boolean = oldItem.id == newItem.id && oldItem.name == newItem.name && oldItem.group == newItem.group diff --git a/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt b/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt index 3729409af..da499604f 100644 --- a/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt @@ -5,22 +5,26 @@ import android.view.Menu import android.view.MenuItem import android.view.View import androidx.lifecycle.LiveData +import androidx.lifecycle.Observer +import androidx.paging.LivePagedListBuilder import androidx.paging.PagedList import androidx.recyclerview.widget.LinearLayoutManager +import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseFragment -import io.legado.app.data.entities.Source +import io.legado.app.data.entities.BookSource import kotlinx.android.synthetic.main.fragment_book_source.* import kotlinx.android.synthetic.main.view_titlebar.* class BookSourceFragment : BaseFragment(R.layout.fragment_book_source) { - private lateinit var adapter : BookSourceAdapter - private var sourceLiveDate : LiveData>? = null + private lateinit var adapter: BookSourceAdapter + private var bookSourceLiveDate: LiveData>? = null override fun onViewCreated(view: View, savedInstanceState: Bundle?) { setSupportToolbar(toolbar) initRecyclerView() + initDataObservers() } override fun onCompatCreateOptionsMenu(menu: Menu) { @@ -41,6 +45,8 @@ class BookSourceFragment : BaseFragment(R.layout.fragment_book_source) { } private fun initDataObservers() { - + bookSourceLiveDate?.removeObservers(viewLifecycleOwner) + bookSourceLiveDate = LivePagedListBuilder(App.db.sourceDao().observeAll(), 30).build() + bookSourceLiveDate?.observe(viewLifecycleOwner, Observer { adapter.submitList(it) }) } } \ No newline at end of file