1.校验书源:搜索为空时添加分组;2.优化书源分组管理相关代码

pull/1545/head
Xwite 3 years ago
parent cb9289dc14
commit cfb921df4d
  1. 12
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  2. 1
      app/src/main/java/io/legado/app/service/CheckSourceService.kt
  3. 24
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt

@ -3,6 +3,7 @@ package io.legado.app.data.entities
import android.os.Parcelable import android.os.Parcelable
import android.text.TextUtils import android.text.TextUtils
import androidx.room.* import androidx.room.*
import io.legado.app.constant.AppPattern
import io.legado.app.constant.BookType import io.legado.app.constant.BookType
import io.legado.app.data.entities.rule.* import io.legado.app.data.entities.rule.*
import io.legado.app.help.SourceAnalyzer import io.legado.app.help.SourceAnalyzer
@ -118,17 +119,14 @@ data class BookSource(
fun getContentRule() = ruleContent ?: ContentRule() fun getContentRule() = ruleContent ?: ContentRule()
fun addGroup(group: String) { fun addGroup(group: String) {
bookSourceGroup?.let { bookSourceGroup?.splitNotBlank(AppPattern.splitGroupRegex)?.toHashSet()?.let {
if (!it.contains(group)) { it.add(group)
bookSourceGroup = "$it,$group" bookSourceGroup = TextUtils.join(",", it)
}
} ?: let {
bookSourceGroup = group
} }
} }
fun removeGroup(group: String) { fun removeGroup(group: String) {
bookSourceGroup?.splitNotBlank("[,;,;]".toRegex())?.toHashSet()?.let { bookSourceGroup?.splitNotBlank(AppPattern.splitGroupRegex)?.toHashSet()?.let {
it.remove(group) it.remove(group)
bookSourceGroup = TextUtils.join(",", it) bookSourceGroup = TextUtils.join(",", it)
} }

@ -120,6 +120,7 @@ class CheckSourceService : BaseService() {
} }
var books = WebBook.searchBookAwait(this, source, searchWord) var books = WebBook.searchBookAwait(this, source, searchWord)
if (books.isEmpty()) { if (books.isEmpty()) {
source.addGroup("搜索为空")
val exs = source.exploreKinds val exs = source.exploreKinds
var url: String? = null var url: String? = null
for (ex in exs) { for (ex in exs) {

@ -94,16 +94,8 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
execute { execute {
val list = arrayListOf<BookSource>() val list = arrayListOf<BookSource>()
sources.forEach { source -> sources.forEach { source ->
val newGroupList = arrayListOf<String>() source.addGroup(groups)
source.bookSourceGroup?.splitNotBlank(AppPattern.splitGroupRegex)?.forEach { list.add(source)
newGroupList.add(it)
}
groups.splitNotBlank(",", ";", "").forEach {
newGroupList.add(it)
}
val lh = LinkedHashSet(newGroupList)
val newGroup = ArrayList(lh).joinToString(separator = ",")
list.add(source.copy(bookSourceGroup = newGroup))
} }
appDb.bookSourceDao.update(*list.toTypedArray()) appDb.bookSourceDao.update(*list.toTypedArray())
} }
@ -113,16 +105,8 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
execute { execute {
val list = arrayListOf<BookSource>() val list = arrayListOf<BookSource>()
sources.forEach { source -> sources.forEach { source ->
val newGroupList = arrayListOf<String>() source.removeGroup(groups)
source.bookSourceGroup?.splitNotBlank(AppPattern.splitGroupRegex)?.forEach { list.add(source)
newGroupList.add(it)
}
groups.splitNotBlank(",", ";", "").forEach {
newGroupList.remove(it)
}
val lh = LinkedHashSet(newGroupList)
val newGroup = ArrayList(lh).joinToString(separator = ",")
list.add(source.copy(bookSourceGroup = newGroup))
} }
appDb.bookSourceDao.update(*list.toTypedArray()) appDb.bookSourceDao.update(*list.toTypedArray())
} }

Loading…
Cancel
Save