pull/32/head
gedoor 6 years ago
parent 8cf52988a7
commit ba3cdee267
  1. 7
      app/src/main/java/io/legado/app/constant/DbTable.kt
  2. 2
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  3. 11
      app/src/main/java/io/legado/app/data/dao/BookDao.kt
  4. 7
      app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt
  5. 21
      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. 9
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  8. 3
      app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt
  9. 17
      app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt
  10. 3
      app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt
  11. 24
      app/src/main/java/io/legado/app/ui/sourceedit/SourceEditAdapter.kt
  12. 10
      app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt

@ -1,7 +0,0 @@
package io.legado.app.constant
object DbTable {
const val books = "books"
const val book_sources = "book_sources"
const val replace_rules = "replace_rules"
}

@ -51,5 +51,5 @@ abstract class AppDatabase : RoomDatabase() {
abstract fun bookDao(): BookDao
abstract fun replaceRuleDao(): ReplaceRuleDao
abstract fun sourceDao() : BookSourceDao
abstract fun bookSourceDao() : BookSourceDao
}

@ -6,25 +6,24 @@ 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 ${DbTable.books} WHERE `group` = :group")
@Query("SELECT * FROM books WHERE `group` = :group")
fun observeByGroup(group: Int): DataSource.Factory<Int, Book>
@Query("SELECT descUrl FROM ${DbTable.books} WHERE `group` = :group")
@Query("SELECT descUrl FROM books WHERE `group` = :group")
fun observeUrlsByGroup(group: Int): LiveData<List<String>>
@Query("SELECT * FROM ${DbTable.books} WHERE `name` in (:names)")
@Query("SELECT * FROM books WHERE `name` in (:names)")
fun findByName(vararg names: String): List<Book>
@get:Query("SELECT descUrl FROM ${DbTable.books}")
@get:Query("SELECT descUrl FROM books")
val allBookUrls: List<String>
@get:Query("SELECT COUNT(*) FROM ${DbTable.books}")
@get:Query("SELECT COUNT(*) FROM books")
val allBookCount: Int
@Insert(onConflict = OnConflictStrategy.REPLACE)

@ -3,12 +3,15 @@ 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}")
@Query("select * from book_sources")
fun observeAll(): DataSource.Factory<Int, BookSource>
@Query("select * from book_sources where origin = :key")
fun findByKey(key:String): BookSource?
}

@ -3,41 +3,40 @@ 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 ${DbTable.replace_rules} ORDER BY sortOrder ASC")
@Query("SELECT * FROM replace_rules ORDER BY sortOrder ASC")
fun observeAll(): DataSource.Factory<Int, ReplaceRule>
@Query("SELECT id FROM ${DbTable.replace_rules} ORDER BY sortOrder ASC")
@Query("SELECT id FROM replace_rules ORDER BY sortOrder ASC")
fun observeAllIds(): LiveData<List<Int>>
@get:Query("SELECT MAX(sortOrder) FROM ${DbTable.replace_rules}")
@get:Query("SELECT MAX(sortOrder) FROM replace_rules")
val maxOrder: Int
@get:Query("SELECT * FROM ${DbTable.replace_rules} ORDER BY sortOrder ASC")
@get:Query("SELECT * FROM replace_rules ORDER BY sortOrder ASC")
val all: List<ReplaceRule>
@get:Query("SELECT * FROM ${DbTable.replace_rules} WHERE isEnabled = 1 ORDER BY sortOrder ASC")
@get:Query("SELECT * FROM replace_rules WHERE isEnabled = 1 ORDER BY sortOrder ASC")
val allEnabled: List<ReplaceRule>
@Query("SELECT * FROM ${DbTable.replace_rules} WHERE id = :id")
@Query("SELECT * FROM replace_rules WHERE id = :id")
fun findById(id: Int): ReplaceRule?
@Query("SELECT * FROM ${DbTable.replace_rules} WHERE id in (:ids)")
@Query("SELECT * FROM replace_rules WHERE id in (:ids)")
fun findByIds(vararg ids: Int): List<ReplaceRule>
@Query("SELECT * FROM ${DbTable.replace_rules} WHERE isEnabled = 1 AND scope LIKE '%' || :scope || '%'")
@Query("SELECT * FROM replace_rules WHERE isEnabled = 1 AND scope LIKE '%' || :scope || '%'")
fun findEnabledByScope(scope: String): List<ReplaceRule>
@get:Query("SELECT COUNT(*) - SUM(isEnabled) FROM ${DbTable.replace_rules}")
@get:Query("SELECT COUNT(*) - SUM(isEnabled) FROM replace_rules")
val summary: Int
@Query("UPDATE ${DbTable.replace_rules} SET isEnabled = :enable")
@Query("UPDATE replace_rules SET isEnabled = :enable")
fun enableAll(enable: Boolean)
@Insert(onConflict = OnConflictStrategy.REPLACE)

@ -3,11 +3,10 @@ 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 = DbTable.books, indices = [(Index(value = ["descUrl"], unique = true))])
@Entity(tableName = "books", indices = [(Index(value = ["descUrl"], unique = true))])
data class Book(@PrimaryKey
var descUrl: String = "", // 详情页Url(本地书源存储完整文件路径)
var tocUrl: String = "", // 目录页Url (toc=table of Contents)

@ -3,22 +3,19 @@ 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 = DbTable.book_sources,
tableName = "book_sources",
indices = [(Index(value = ["sourceId"])), (Index(value = ["origin"], unique = false))]
)
data class BookSource (
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "sourceId")
var id: Int = 0, // 编号
var name: String = "", // 名称
@PrimaryKey
var origin: String = "", // 地址,包括 http/https
var name: String = "", // 名称
var type: Int = 0, // 类型,0 文本,1 音频
var group: String? = null, // 分组
var header: String? = null, // header

@ -5,11 +5,10 @@ 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 = DbTable.replace_rules,
@Entity(tableName = "replace_rules",
indices = [(Index(value = ["id"]))])
data class ReplaceRule(
@PrimaryKey(autoGenerate = true)

@ -18,7 +18,7 @@ import kotlinx.android.synthetic.main.fragment_book_source.*
import kotlinx.android.synthetic.main.view_titlebar.*
import org.jetbrains.anko.startActivity
class BookSourceFragment : BaseFragment(R.layout.fragment_book_source) {
class BookSourceFragment : BaseFragment(R.layout.fragment_book_source), BookSourceAdapter.CallBack {
private lateinit var adapter: BookSourceAdapter
private var bookSourceLiveDate: LiveData<PagedList<BookSource>>? = null
@ -44,12 +44,25 @@ class BookSourceFragment : BaseFragment(R.layout.fragment_book_source) {
private fun initRecyclerView() {
recycler_view.layoutManager = LinearLayoutManager(context)
adapter = BookSourceAdapter()
adapter.callBack = this
recycler_view.adapter = adapter
}
private fun initDataObservers() {
bookSourceLiveDate?.removeObservers(viewLifecycleOwner)
bookSourceLiveDate = LivePagedListBuilder(App.db.sourceDao().observeAll(), 30).build()
bookSourceLiveDate = LivePagedListBuilder(App.db.bookSourceDao().observeAll(), 30).build()
bookSourceLiveDate?.observe(viewLifecycleOwner, Observer { adapter.submitList(it) })
}
override fun del(bookSource: BookSource) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun update(bookSource: BookSource) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun edit(bookSource: BookSource) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}

@ -12,7 +12,8 @@ class SourceEditActivity : BaseActivity<SourceEditViewModel>() {
get() = R.layout.activity_source_edit
override fun onViewModelCreated(viewModel: SourceEditViewModel, savedInstanceState: Bundle?) {
val sourceID = intent.getStringExtra("data")
sourceID?.let { viewModel.setBookSource(sourceID)}
}

@ -0,0 +1,24 @@
package io.legado.app.ui.sourceedit
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
class SourceEditAdapter : RecyclerView.Adapter<SourceEditAdapter.MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun getItemCount(): Int {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
}
}

@ -2,7 +2,17 @@ package io.legado.app.ui.sourceedit
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import io.legado.app.App
import io.legado.app.data.dao.BookSourceDao
import io.legado.app.data.entities.BookSource
class SourceEditViewModel(application: Application) : AndroidViewModel(application) {
private val sourceLiveData:MutableLiveData<BookSource> = MutableLiveData()
fun setBookSource(key: String) {
sourceLiveData.value = App.db.bookSourceDao().findByKey(key)
}
}
Loading…
Cancel
Save