添加批量换源

pull/1751/head
kunfei 3 years ago
parent 34f6f482e0
commit 364466e358
  1. 8
      app/src/main/java/io/legado/app/model/webBook/WebBook.kt
  2. 6
      app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt
  3. 12
      app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookViewModel.kt
  4. 17
      app/src/main/java/io/legado/app/ui/book/arrange/SourcePickerDialog.kt
  5. 2
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -262,7 +262,7 @@ object WebBook {
Debug.log(bookSource.bookSourceUrl, "⇒正文规则为空,使用章节链接:${bookChapter.url}")
return bookChapter.url
}
if(bookChapter.isVolume && bookChapter.url.startsWith(bookChapter.title)) {
if (bookChapter.isVolume && bookChapter.url.startsWith(bookChapter.title)) {
Debug.log(bookSource.bookSourceUrl, "⇒一级目录正文不解析规则")
return bookChapter.tag ?: ""
}
@ -322,16 +322,16 @@ object WebBook {
context: CoroutineContext = Dispatchers.IO,
): Coroutine<Pair<BookSource, Book>> {
return Coroutine.async(scope, context) {
preciseSearchAwait(scope, bookSources, name, author)
preciseSearchAwait(scope, name, author, *bookSources.toTypedArray())
?: throw NoStackTraceException("没有搜索到<$name>$author")
}
}
suspend fun preciseSearchAwait(
scope: CoroutineScope,
bookSources: List<BookSource>,
name: String,
author: String
author: String,
vararg bookSources: BookSource
): Pair<BookSource, Book>? {
bookSources.forEach { source ->
kotlin.runCatching {

@ -15,6 +15,7 @@ import io.legado.app.constant.PreferKey
import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookGroup
import io.legado.app.data.entities.BookSource
import io.legado.app.databinding.ActivityArrangeBookBinding
import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.theme.primaryColor
@ -41,6 +42,7 @@ class ArrangeBookActivity : VMBaseActivity<ActivityArrangeBookBinding, ArrangeBo
PopupMenu.OnMenuItemClickListener,
SelectActionBar.CallBack,
ArrangeBookAdapter.CallBack,
SourcePickerDialog.Callback,
GroupSelectDialog.CallBack {
override val binding by viewBinding(ActivityArrangeBookBinding::inflate)
@ -229,4 +231,8 @@ class ArrangeBookActivity : VMBaseActivity<ActivityArrangeBookBinding, ArrangeBo
}
}
override fun sourceOnClick(source: BookSource) {
viewModel.changeSource(adapter.selectedBooks(), source)
}
}

@ -5,6 +5,8 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.webBook.WebBook
class ArrangeBookViewModel(application: Application) : BaseViewModel(application) {
@ -30,8 +32,16 @@ class ArrangeBookViewModel(application: Application) : BaseViewModel(application
}
}
fun changeSource(books: List<Book>, source: BookSource) {
fun changeSource(books: Array<Book>, source: BookSource): Coroutine<Unit> {
return execute {
books.forEach { book ->
WebBook.preciseSearchAwait(this, book.name, book.author, source)?.let {
}
}
}.onFinally {
}
}
}

@ -5,6 +5,7 @@ import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.SearchView
import androidx.core.view.setPadding
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R
import io.legado.app.base.BaseDialogFragment
@ -22,6 +23,7 @@ import io.legado.app.utils.setLayout
import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import splitties.views.onClick
class SourcePickerDialog : BaseDialogFragment(R.layout.dialog_source_picker) {
@ -83,7 +85,7 @@ class SourcePickerDialog : BaseDialogFragment(R.layout.dialog_source_picker) {
override fun getViewBinding(parent: ViewGroup): ItemTextBinding {
return ItemTextBinding.inflate(inflater, parent, false).apply {
root.setPadding(16.dpToPx(), 8.dpToPx(), 16.dpToPx(), 8.dpToPx())
root.setPadding(16.dpToPx())
}
}
@ -97,9 +99,22 @@ class SourcePickerDialog : BaseDialogFragment(R.layout.dialog_source_picker) {
}
override fun registerListener(holder: ItemViewHolder, binding: ItemTextBinding) {
binding.root.onClick {
getItemByLayoutPosition(holder.layoutPosition)?.let {
callback?.sourceOnClick(it)
}
}
}
}
private val callback: Callback?
get() {
return (parentFragment as? Callback) ?: activity as? Callback
}
interface Callback {
fun sourceOnClick(source: BookSource)
}
}

@ -244,7 +244,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
if (!AppConfig.autoChangeSource) return
execute {
val sources = appDb.bookSourceDao.allTextEnabled
WebBook.preciseSearchAwait(this, sources, name, author)?.let {
WebBook.preciseSearchAwait(this, name, author, *sources.toTypedArray())?.let {
it.second.upInfoFromOld(ReadBook.book)
changeTo(it.first, it.second)
} ?: throw NoStackTraceException("自动换源失败")

Loading…
Cancel
Save