pull/32/head
gedoor 6 years ago
parent 160793f3ae
commit f5045bdc08
  1. 7
      app/src/main/java/io/legado/app/constant/DbTable.kt
  2. 4
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  3. 12
      app/src/main/java/io/legado/app/data/dao/BookDao.kt
  4. 14
      app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt
  5. 3
      app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt
  6. 3
      app/src/main/java/io/legado/app/data/entities/Book.kt
  7. 5
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  8. 2
      app/src/main/java/io/legado/app/data/entities/ExploreSearchUrl.kt
  9. 3
      app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt
  10. 10
      app/src/main/java/io/legado/app/ui/main/booksource/BookSourceAdapter.kt
  11. 14
      app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.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"
}

@ -8,10 +8,10 @@ import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase import androidx.sqlite.db.SupportSQLiteDatabase
import io.legado.app.data.dao.BookDao import io.legado.app.data.dao.BookDao
import io.legado.app.data.dao.ReplaceRuleDao 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.Book
import io.legado.app.data.entities.Chapter import io.legado.app.data.entities.Chapter
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
import javax.xml.transform.Source
@Database(entities = [Book::class, Chapter::class, ReplaceRule::class], version = 1, exportSchema = true) @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 bookDao(): BookDao
abstract fun replaceRuleDao(): ReplaceRuleDao abstract fun replaceRuleDao(): ReplaceRuleDao
abstract fun sourceDao() : BookSourceDao
} }

@ -6,25 +6,25 @@ import androidx.room.Dao
import androidx.room.Insert import androidx.room.Insert
import androidx.room.OnConflictStrategy import androidx.room.OnConflictStrategy
import androidx.room.Query import androidx.room.Query
import io.legado.app.constant.DbTable
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
@Dao @Dao
interface BookDao { interface BookDao {
@Query("SELECT * FROM books WHERE `group` = :group") @Query("SELECT * FROM ${DbTable.books} WHERE `group` = :group")
fun observeByGroup(group: Int): DataSource.Factory<Int, Book> fun observeByGroup(group: Int): DataSource.Factory<Int, Book>
@Query("SELECT descUrl FROM books WHERE `group` = :group") @Query("SELECT descUrl FROM ${DbTable.books} WHERE `group` = :group")
fun observeUrlsByGroup(group: Int): LiveData<List<String>> fun observeUrlsByGroup(group: Int): LiveData<List<String>>
@Query("SELECT * FROM books WHERE `name` in (:names)") @Query("SELECT * FROM ${DbTable.books} WHERE `name` in (:names)")
fun findByName(vararg names: String): List<Book> fun findByName(vararg names: String): List<Book>
@get:Query("SELECT descUrl FROM books") @get:Query("SELECT descUrl FROM ${DbTable.books}")
val allBookUrls: List<String> val allBookUrls: List<String>
@get:Query("SELECT COUNT(*) FROM books") @get:Query("SELECT COUNT(*) FROM ${DbTable.books}")
val allBookCount: Int val allBookCount: Int
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)

@ -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<Int, BookSource>
}

@ -3,13 +3,14 @@ package io.legado.app.data.dao
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.paging.DataSource import androidx.paging.DataSource
import androidx.room.* import androidx.room.*
import io.legado.app.constant.DbTable
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
@Dao @Dao
interface ReplaceRuleDao { 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<Int, ReplaceRule> fun observeAll(): DataSource.Factory<Int, ReplaceRule>
@Query("SELECT id FROM replace_rules ORDER BY sortOrder ASC") @Query("SELECT id FROM replace_rules ORDER BY sortOrder ASC")

@ -3,10 +3,11 @@ package io.legado.app.data.entities
import android.os.Parcelable import android.os.Parcelable
import androidx.room.* import androidx.room.*
import io.legado.app.constant.AppConst.NOT_AVAILABLE import io.legado.app.constant.AppConst.NOT_AVAILABLE
import io.legado.app.constant.DbTable
import kotlinx.android.parcel.Parcelize import kotlinx.android.parcel.Parcelize
@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 data class Book(@PrimaryKey
var descUrl: String = "", // 详情页Url(本地书源存储完整文件路径) var descUrl: String = "", // 详情页Url(本地书源存储完整文件路径)
var tocUrl: String = "", // 目录页Url (toc=table of Contents) var tocUrl: String = "", // 目录页Url (toc=table of Contents)

@ -3,16 +3,17 @@ package io.legado.app.data.entities
import android.os.Parcelable import android.os.Parcelable
import androidx.room.* import androidx.room.*
import io.legado.app.constant.AppUtils.GSON_CONVERTER import io.legado.app.constant.AppUtils.GSON_CONVERTER
import io.legado.app.constant.DbTable
import io.legado.app.data.entities.rule.* import io.legado.app.data.entities.rule.*
import kotlinx.android.parcel.IgnoredOnParcel import kotlinx.android.parcel.IgnoredOnParcel
import kotlinx.android.parcel.Parcelize import kotlinx.android.parcel.Parcelize
@Parcelize @Parcelize
@Entity( @Entity(
tableName = "sources", tableName = DbTable.book_sources,
indices = [(Index(value = ["sourceId"])), (Index(value = ["origin"], unique = false))] indices = [(Index(value = ["sourceId"])), (Index(value = ["origin"], unique = false))]
) )
data class Source ( data class BookSource (
@PrimaryKey(autoGenerate = true) @PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "sourceId") @ColumnInfo(name = "sourceId")
var id: Int = 0, // 编号 var id: Int = 0, // 编号

@ -10,7 +10,7 @@ import kotlinx.android.parcel.Parcelize
@Parcelize @Parcelize
@Entity(tableName = "explore_search_urls", @Entity(tableName = "explore_search_urls",
indices = [(Index(value = ["sourceId", "url"], unique = true))], indices = [(Index(value = ["sourceId", "url"], unique = true))],
foreignKeys = [(ForeignKey(entity = Source::class, foreignKeys = [(ForeignKey(entity = BookSource::class,
parentColumns = ["sourceId"], parentColumns = ["sourceId"],
childColumns = ["sourceId"], childColumns = ["sourceId"],
onDelete = ForeignKey.CASCADE))]) // 删除书源时自动删除章节 onDelete = ForeignKey.CASCADE))]) // 删除书源时自动删除章节

@ -5,10 +5,11 @@ import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
import androidx.room.Index import androidx.room.Index
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
import io.legado.app.constant.DbTable
import kotlinx.android.parcel.Parcelize import kotlinx.android.parcel.Parcelize
@Parcelize @Parcelize
@Entity(tableName = "replace_rules", @Entity(tableName = DbTable.replace_rules,
indices = [(Index(value = ["id"]))]) indices = [(Index(value = ["id"]))])
data class ReplaceRule( data class ReplaceRule(
@PrimaryKey(autoGenerate = true) @PrimaryKey(autoGenerate = true)

@ -6,18 +6,18 @@ import android.view.ViewGroup
import androidx.paging.PagedListAdapter import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import io.legado.app.data.entities.Source import io.legado.app.data.entities.BookSource
class BookSourceAdapter(context : Context) : PagedListAdapter<Source, BookSourceAdapter.MyViewHolder>(DIFF_CALLBACK) { class BookSourceAdapter(context : Context) : PagedListAdapter<BookSource, BookSourceAdapter.MyViewHolder>(DIFF_CALLBACK) {
companion object { companion object {
@JvmField @JvmField
val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Source>() { val DIFF_CALLBACK = object : DiffUtil.ItemCallback<BookSource>() {
override fun areItemsTheSame(oldItem: Source, newItem: Source): Boolean = override fun areItemsTheSame(oldItem: BookSource, newItem: BookSource): Boolean =
oldItem.id == newItem.id 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.id == newItem.id
&& oldItem.name == newItem.name && oldItem.name == newItem.name
&& oldItem.group == newItem.group && oldItem.group == newItem.group

@ -5,22 +5,26 @@ import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList import androidx.paging.PagedList
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseFragment 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.fragment_book_source.*
import kotlinx.android.synthetic.main.view_titlebar.* import kotlinx.android.synthetic.main.view_titlebar.*
class BookSourceFragment : BaseFragment(R.layout.fragment_book_source) { class BookSourceFragment : BaseFragment(R.layout.fragment_book_source) {
private lateinit var adapter : BookSourceAdapter private lateinit var adapter: BookSourceAdapter
private var sourceLiveDate : LiveData<PagedList<Source>>? = null private var bookSourceLiveDate: LiveData<PagedList<BookSource>>? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
setSupportToolbar(toolbar) setSupportToolbar(toolbar)
initRecyclerView() initRecyclerView()
initDataObservers()
} }
override fun onCompatCreateOptionsMenu(menu: Menu) { override fun onCompatCreateOptionsMenu(menu: Menu) {
@ -41,6 +45,8 @@ class BookSourceFragment : BaseFragment(R.layout.fragment_book_source) {
} }
private fun initDataObservers() { private fun initDataObservers() {
bookSourceLiveDate?.removeObservers(viewLifecycleOwner)
bookSourceLiveDate = LivePagedListBuilder(App.db.sourceDao().observeAll(), 30).build()
bookSourceLiveDate?.observe(viewLifecycleOwner, Observer { adapter.submitList(it) })
} }
} }
Loading…
Cancel
Save