From ba3cdee26787998d81248cbcc5b1e887a23f2a08 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 7 Jun 2019 18:08:32 +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 --- .../java/io/legado/app/constant/DbTable.kt | 7 ------ .../java/io/legado/app/data/AppDatabase.kt | 2 +- .../java/io/legado/app/data/dao/BookDao.kt | 11 ++++----- .../io/legado/app/data/dao/BookSourceDao.kt | 7 ++++-- .../io/legado/app/data/dao/ReplaceRuleDao.kt | 21 ++++++++-------- .../java/io/legado/app/data/entities/Book.kt | 3 +-- .../io/legado/app/data/entities/BookSource.kt | 9 +++---- .../legado/app/data/entities/ReplaceRule.kt | 3 +-- .../ui/main/booksource/BookSourceFragment.kt | 17 +++++++++++-- .../app/ui/sourceedit/SourceEditActivity.kt | 3 ++- .../app/ui/sourceedit/SourceEditAdapter.kt | 24 +++++++++++++++++++ .../app/ui/sourceedit/SourceEditViewModel.kt | 10 ++++++++ 12 files changed, 77 insertions(+), 40 deletions(-) delete mode 100644 app/src/main/java/io/legado/app/constant/DbTable.kt create mode 100644 app/src/main/java/io/legado/app/ui/sourceedit/SourceEditAdapter.kt diff --git a/app/src/main/java/io/legado/app/constant/DbTable.kt b/app/src/main/java/io/legado/app/constant/DbTable.kt deleted file mode 100644 index 11e8b2fb5..000000000 --- a/app/src/main/java/io/legado/app/constant/DbTable.kt +++ /dev/null @@ -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" -} \ 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 819298f55..5e0ebdd07 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -51,5 +51,5 @@ abstract class AppDatabase : RoomDatabase() { abstract fun bookDao(): BookDao abstract fun replaceRuleDao(): ReplaceRuleDao - abstract fun sourceDao() : BookSourceDao + abstract fun bookSourceDao() : 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 a2cd9d7e7..10c65f279 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,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 - @Query("SELECT descUrl FROM ${DbTable.books} WHERE `group` = :group") + @Query("SELECT descUrl FROM books WHERE `group` = :group") fun observeUrlsByGroup(group: Int): LiveData> - @Query("SELECT * FROM ${DbTable.books} WHERE `name` in (:names)") + @Query("SELECT * FROM books WHERE `name` in (:names)") fun findByName(vararg names: String): List - @get:Query("SELECT descUrl FROM ${DbTable.books}") + @get:Query("SELECT descUrl FROM books") val allBookUrls: List - @get:Query("SELECT COUNT(*) FROM ${DbTable.books}") + @get:Query("SELECT COUNT(*) FROM 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 index 8576331a9..b92e379ff 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt @@ -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 + + @Query("select * from book_sources where origin = :key") + fun findByKey(key:String): BookSource? + } \ 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 3435eef62..d4a2073c0 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,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 - @Query("SELECT id FROM ${DbTable.replace_rules} ORDER BY sortOrder ASC") + @Query("SELECT id FROM replace_rules ORDER BY sortOrder ASC") fun observeAllIds(): LiveData> - @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 - @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 - @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 - @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 - @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) 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 4f49cc7ef..78b3694e8 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,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) diff --git a/app/src/main/java/io/legado/app/data/entities/BookSource.kt b/app/src/main/java/io/legado/app/data/entities/BookSource.kt index 4bb90d850..2ac3aea66 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookSource.kt @@ -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 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 6152c3bfd..8b5a0fda6 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,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) 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 f8f3021a3..9ae233d73 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 @@ -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>? = 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. + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt index f4db1d5b4..f896e386a 100644 --- a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt @@ -12,7 +12,8 @@ class SourceEditActivity : BaseActivity() { get() = R.layout.activity_source_edit override fun onViewModelCreated(viewModel: SourceEditViewModel, savedInstanceState: Bundle?) { - + val sourceID = intent.getStringExtra("data") + sourceID?.let { viewModel.setBookSource(sourceID)} } diff --git a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditAdapter.kt b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditAdapter.kt new file mode 100644 index 000000000..58102960b --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditAdapter.kt @@ -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() { + + 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) { + + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt index 4be930291..f0d341fe9 100644 --- a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt @@ -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 = MutableLiveData() + + fun setBookSource(key: String) { + sourceLiveData.value = App.db.bookSourceDao().findByKey(key) + } + } \ No newline at end of file