pull/32/head
kunfei 5 years ago
parent 3b5fef949d
commit d7fb601d33
  1. 10
      app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt
  2. 86
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  3. 16
      app/src/main/java/io/legado/app/ui/main/booksource/BookSourceAdapter.kt
  4. 2
      app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt
  5. 8
      app/src/main/java/io/legado/app/ui/main/findbook/FindBookAdapter.kt
  6. 23
      app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt
  7. 3
      app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt

@ -10,22 +10,22 @@ interface BookSourceDao {
@Query("select * from book_sources order by customOrder asc")
fun observeAll(): DataSource.Factory<Int, BookSource>
@Query("select * from book_sources where name like :searchKey or `group` like :searchKey or origin like :searchKey order by customOrder asc")
@Query("select * from book_sources where bookSourceName like :searchKey or `bookSourceGroup` like :searchKey or bookSourceOrigin like :searchKey order by customOrder asc")
fun observeSearch(searchKey: String = ""): DataSource.Factory<Int, BookSource>
@Query("select distinct isEnabled from book_sources where name like :searchKey or `group` like :searchKey or origin like :searchKey")
@Query("select distinct enabled from book_sources where bookSourceName like :searchKey or `bookSourceGroup` like :searchKey or bookSourceOrigin like :searchKey")
fun searchIsEnable(searchKey: String = ""): List<Boolean>
@Query("UPDATE book_sources SET isEnabled = :enable where name like :searchKey or `group` like :searchKey or origin like :searchKey")
@Query("UPDATE book_sources SET enabled = :enable where bookSourceName like :searchKey or `bookSourceGroup` like :searchKey or bookSourceOrigin like :searchKey")
fun enableAllSearch(searchKey: String = "", enable: String = "1")
@Query("select * from book_sources where exploreIsEnabled = 1 order by customOrder asc")
@Query("select * from book_sources where enabledExplore = 1 order by customOrder asc")
fun observeFind(): DataSource.Factory<Int, BookSource>
@get:Query("select * from book_sources order by customOrder asc")
val all: List<BookSource>
@Query("select * from book_sources where origin = :key")
@Query("select * from book_sources where bookSourceOrigin = :key")
fun findByKey(key: String): BookSource?
@Query("select count(*) from book_sources")

@ -4,87 +4,29 @@ import android.os.Parcelable
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
import io.legado.app.constant.AppUtils.GSON_CONVERTER
import io.legado.app.data.entities.rule.*
import kotlinx.android.parcel.IgnoredOnParcel
import kotlinx.android.parcel.Parcelize
@Parcelize
@Entity(
tableName = "book_sources",
indices = [(Index(value = ["origin"], unique = false))]
indices = [(Index(value = ["bookSourceOrigin"], unique = false))]
)
data class BookSource(
var bookSourceName: String = "", // 名称
var bookSourceGroup: String? = null, // 分组
@PrimaryKey
var origin: String = "", // 地址,包括 http/https
var name: String = "", // 名称
var type: Int = 0, // 类型,0 文本,1 音频
var group: String? = null, // 分组
var bookSourceOrigin: String = "", // 地址,包括 http/https
var bookSourceType: Int = 0, // 类型,0 文本,1 音频
var customOrder: Int = 0, // 手动排序编号
var enabled: Boolean = true, // 是否启用
var enabledExplore: Boolean = true, //启用发现
var header: String? = null, // header
var loginUrl: String? = null, // 登录地址
var isEnabled: Boolean = true, // 是否启用
var lastUpdateTime: Long = 0, // 最后更新时间,用于排序
var customOrder: Int = 0, // 手动排序编号
var weight: Int = 0, // 智能排序的权重
var exploreIsEnabled: Boolean = true, //启用发现
var exploreRule: String? = null, // 发现规则
var searchRule: String? = null, // 搜索规则
var bookInfoRule: String? = null, // 书籍信息页规则
var chapterRule: String? = null, // 目录页规则
var contentRule: String? = null // 正文页规则
) : Parcelable {
@Transient
@IgnoredOnParcel
var parsedExploreRule: ExploreRule? = null
@Transient
@IgnoredOnParcel
var parsedSearchRule: SearchRule? = null
@Transient
@IgnoredOnParcel
var parsedBookInfoRule: BookInfoRule? = null
@Transient
@IgnoredOnParcel
var parsedChapterRule: ChapterRule? = null
@Transient
@IgnoredOnParcel
var parsedContentRule: ContentRule? = null
fun initAllRules() {
initSearchRule()
initExploreRule()
initBookInfoRule()
initChapterRule()
initContentRule()
}
fun initSearchRule() {
if (searchRule != null) {
parsedSearchRule = GSON_CONVERTER.fromJson(searchRule, SearchRule::class.java)
}
}
fun initExploreRule() {
if (exploreRule != null) {
parsedExploreRule = GSON_CONVERTER.fromJson(exploreRule, ExploreRule::class.java)
}
}
fun initBookInfoRule() {
if (bookInfoRule != null) {
parsedBookInfoRule = GSON_CONVERTER.fromJson(bookInfoRule, BookInfoRule::class.java)
}
}
fun initChapterRule() {
if (chapterRule != null) {
parsedChapterRule = GSON_CONVERTER.fromJson(chapterRule, ChapterRule::class.java)
}
}
fun initContentRule() {
if (contentRule != null) {
parsedContentRule = GSON_CONVERTER.fromJson(contentRule, ContentRule::class.java)
}
}
}
var ruleExplore: String? = null, // 发现规则
var ruleSearch: String? = null, // 搜索规则
var ruleBookInfo: String? = null, // 书籍信息页规则
var ruleToc: String? = null, // 目录页规则
var ruleContent: String? = null // 正文页规则
) : Parcelable

@ -21,13 +21,13 @@ class BookSourceAdapter : PagedListAdapter<BookSource, BookSourceAdapter.MyViewH
@JvmField
val DIFF_CALLBACK = object : DiffUtil.ItemCallback<BookSource>() {
override fun areItemsTheSame(oldItem: BookSource, newItem: BookSource): Boolean =
oldItem.origin == newItem.origin
oldItem.bookSourceOrigin == newItem.bookSourceOrigin
override fun areContentsTheSame(oldItem: BookSource, newItem: BookSource): Boolean =
oldItem.origin == newItem.origin
&& oldItem.name == newItem.name
&& oldItem.group == newItem.group
&& oldItem.isEnabled == newItem.isEnabled
oldItem.bookSourceOrigin == newItem.bookSourceOrigin
&& oldItem.bookSourceName == newItem.bookSourceName
&& oldItem.bookSourceGroup == newItem.bookSourceGroup
&& oldItem.enabled == newItem.enabled
}
}
@ -76,10 +76,10 @@ class BookSourceAdapter : PagedListAdapter<BookSource, BookSourceAdapter.MyViewH
}
fun bind(bookSource: BookSource, callBack: CallBack?) = with(itemView) {
cb_book_source.text = String.format("%s (%s)", bookSource.name, bookSource.group)
cb_book_source.isChecked = bookSource.isEnabled
cb_book_source.text = String.format("%s (%s)", bookSource.bookSourceName, bookSource.bookSourceGroup)
cb_book_source.isChecked = bookSource.enabled
cb_book_source.setOnClickListener {
bookSource.isEnabled = cb_book_source.isChecked
bookSource.enabled = cb_book_source.isChecked
callBack?.update(bookSource)
}
iv_edit_source.onClick { callBack?.edit(bookSource) }

@ -124,6 +124,6 @@ class BookSourceFragment : BaseFragment(R.layout.fragment_book_source), BookSour
}
override fun edit(bookSource: BookSource) {
context?.startActivity<SourceEditActivity>(Pair("data", bookSource.origin))
context?.startActivity<SourceEditActivity>(Pair("data", bookSource.bookSourceOrigin))
}
}

@ -17,11 +17,11 @@ class FindBookAdapter:PagedListAdapter<BookSource, FindBookAdapter.MyViewHolder>
companion object {
var DIFF_CALLBACK = object : DiffUtil.ItemCallback<BookSource>() {
override fun areItemsTheSame(oldItem: BookSource, newItem: BookSource): Boolean =
oldItem.origin == newItem.origin
oldItem.bookSourceOrigin == newItem.bookSourceOrigin
override fun areContentsTheSame(oldItem: BookSource, newItem: BookSource): Boolean =
oldItem.origin == newItem.origin
&& oldItem.name == newItem.name
oldItem.bookSourceOrigin == newItem.bookSourceOrigin
&& oldItem.bookSourceName == newItem.bookSourceName
}
}
@ -40,7 +40,7 @@ class FindBookAdapter:PagedListAdapter<BookSource, FindBookAdapter.MyViewHolder>
fun bind(bookSource: BookSource) = with(itemView) {
val bgShape: GradientDrawable? = tv_name.background as? GradientDrawable
bgShape?.setStroke(2, ColorUtils.getRandomColor())
tv_name.text = bookSource.name
tv_name.text = bookSource.bookSourceName
}
}
}

@ -10,9 +10,6 @@ import io.legado.app.base.BaseActivity
import io.legado.app.data.entities.BookSource
import io.legado.app.utils.getViewModel
import kotlinx.android.synthetic.main.activity_source_edit.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jetbrains.anko.toast
class SourceEditActivity : BaseActivity<SourceEditViewModel>() {
@ -67,38 +64,38 @@ class SourceEditActivity : BaseActivity<SourceEditViewModel>() {
private fun upRecyclerView(bookSource: BookSource?) {
bookSource?.let {
cb_is_enable.isChecked = it.isEnabled
cb_is_enable_find.isChecked = it.exploreIsEnabled
cb_is_enable.isChecked = it.enabled
cb_is_enable_find.isChecked = it.enabledExplore
}
sourceEditEntities.clear()
sourceEditEntities.add(SourceEditEntity("origin", bookSource?.origin, R.string.book_source_url))
sourceEditEntities.add(SourceEditEntity("name", bookSource?.name, R.string.book_source_name))
sourceEditEntities.add(SourceEditEntity("group", bookSource?.group, R.string.book_source_group))
sourceEditEntities.add(SourceEditEntity("origin", bookSource?.bookSourceOrigin, R.string.book_source_url))
sourceEditEntities.add(SourceEditEntity("name", bookSource?.bookSourceName, R.string.book_source_name))
sourceEditEntities.add(SourceEditEntity("group", bookSource?.bookSourceGroup, R.string.book_source_group))
adapter.sourceEditEntities = sourceEditEntities
adapter.notifyDataSetChanged()
}
private fun getSource(): BookSource? {
val bookSource = BookSource()
bookSource.isEnabled = cb_is_enable.isChecked
bookSource.exploreIsEnabled = cb_is_enable_find.isChecked
bookSource.enabled = cb_is_enable.isChecked
bookSource.enabledExplore = cb_is_enable_find.isChecked
for (entity in adapter.sourceEditEntities) {
when (entity.key) {
"origin" -> {
if (entity.value == null) {
return null
} else {
bookSource.origin = entity.value!!
bookSource.bookSourceOrigin = entity.value!!
}
}
"name" -> {
if (entity.value == null) {
return null
} else {
bookSource.name = entity.value!!
bookSource.bookSourceName = entity.value!!
}
}
"group" -> bookSource.group = entity.value
"group" -> bookSource.bookSourceGroup = entity.value
}
}
return bookSource

@ -5,7 +5,6 @@ import androidx.lifecycle.MutableLiveData
import io.legado.app.App
import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.BookSource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
@ -21,7 +20,7 @@ class SourceEditViewModel(application: Application) : BaseViewModel(application)
fun save(bookSource: BookSource, finally: (() -> Unit)? = null) {
launch(IO) {
val source = App.db.bookSourceDao().findByKey(bookSource.origin)
val source = App.db.bookSourceDao().findByKey(bookSource.bookSourceOrigin)
if (source == null) {
bookSource.customOrder = App.db.bookSourceDao().allCount()
}

Loading…
Cancel
Save