添加单章换源

pull/1649/head
kunfei 3 years ago
parent c9cf9b5336
commit 54df563eb5
  1. 25
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 4
      app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt
  3. 26
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceDialog.kt
  4. 18
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceViewModel.kt
  5. 4
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterTocAdapter.kt
  6. 4
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
  7. 4
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  8. 4
      app/src/main/res/layout/dialog_chapter_change_source.xml

@ -265,29 +265,20 @@ object BookHelp {
*/
fun getDurChapter(
oldDurChapterIndex: Int,
oldChapterListSize: Int,
oldDurChapterName: String?,
newChapterList: List<BookChapter>
newChapterList: List<BookChapter>,
oldChapterListSize: Int = 0
): Int {
if (oldChapterListSize == 0) return oldDurChapterIndex
if (oldDurChapterIndex == 0) return oldDurChapterIndex
if (newChapterList.isEmpty()) return oldDurChapterIndex
val oldChapterNum = getChapterNum(oldDurChapterName)
val oldName = getPureChapterName(oldDurChapterName)
val newChapterSize = newChapterList.size
val min = max(
0,
min(
oldDurChapterIndex,
oldDurChapterIndex - oldChapterListSize + newChapterSize
) - 10
)
val max = min(
newChapterSize - 1,
max(
oldDurChapterIndex,
oldDurChapterIndex - oldChapterListSize + newChapterSize
) + 10
)
val durIndex =
if (oldChapterListSize == 0) oldDurChapterIndex
else oldDurChapterIndex * oldChapterListSize / newChapterSize
val min = max(0, min(oldDurChapterIndex, durIndex) - 10)
val max = min(newChapterSize - 1, max(oldDurChapterIndex, durIndex) + 10)
var nameSim = 0.0
var newIndex = 0
var newNum = 0

@ -116,9 +116,9 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
execute {
book.durChapterIndex = BookHelp.getDurChapter(
book.durChapterIndex,
oldTocSize,
book.durChapterTitle,
chapters
chapters,
oldTocSize
)
book.durChapterTitle = chapters[book.durChapterIndex].title
appDb.bookDao.update(book)

@ -21,6 +21,7 @@ import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.SearchBook
import io.legado.app.databinding.DialogChapterChangeSourceBinding
import io.legado.app.help.AppConfig
import io.legado.app.help.BookHelp
import io.legado.app.lib.theme.primaryColor
import io.legado.app.ui.book.source.edit.BookSourceEditActivity
import io.legado.app.ui.book.source.manage.BookSourceActivity
@ -53,6 +54,16 @@ class ChangeChapterSourceDialog() : BaseDialogFragment(R.layout.dialog_chapter_c
registerForActivityResult(StartActivityContract(BookSourceEditActivity::class.java)) {
viewModel.startSearch()
}
private val tocSuccess: (toc: List<BookChapter>) -> Unit = {
tocAdapter.durChapterIndex =
BookHelp.getDurChapter(viewModel.chapterIndex, viewModel.chapterTitle, it)
binding.loadingToc.hide()
tocAdapter.setItems(it)
binding.recyclerViewToc.scrollToPosition(tocAdapter.durChapterIndex - 5)
}
private val contentSuccess: (content: String) -> Unit = {
callBack?.replaceContent(it)
}
private val searchBookAdapter by lazy {
ChangeChapterSourceAdapter(requireContext(), viewModel, this)
}
@ -211,13 +222,6 @@ class ChangeChapterSourceDialog() : BaseDialogFragment(R.layout.dialog_chapter_c
return false
}
private val tocSuccess: (toc: List<BookChapter>) -> Unit = {
tocAdapter.durChapterIndex = viewModel.chapterIndex
binding.loadingToc.hide()
tocAdapter.setItems(it)
binding.recyclerViewToc.scrollToPosition(tocAdapter.durChapterIndex - 5)
}
override fun openToc(searchBook: SearchBook) {
this.searchBook = searchBook
tocAdapter.setItems(null)
@ -259,8 +263,12 @@ class ChangeChapterSourceDialog() : BaseDialogFragment(R.layout.dialog_chapter_c
}
}
override fun clickChapter(bookChapter: BookChapter) {
TODO("Not yet implemented")
override fun clickChapter(bookChapter: BookChapter, nextChapterUrl: String?) {
searchBook?.let {
viewModel.getContent(it.toBook(), bookChapter, nextChapterUrl, contentSuccess) { msg ->
toastOnUi(msg)
}
}
}
private fun changeSource(searchBook: SearchBook) {

@ -294,6 +294,24 @@ class ChangeChapterSourceViewModel(application: Application) : BaseViewModel(app
}
}
fun getContent(
book: Book,
chapter: BookChapter,
nextChapterUrl: String?,
success: (content: String) -> Unit,
error: (msg: String) -> Unit
) {
execute {
val bookSource = appDb.bookSourceDao.getBookSource(book.origin)
?: throw NoStackTraceException("书源不存在")
WebBook.getContentAwait(this, bookSource, book, chapter, nextChapterUrl, false)
}.onSuccess {
success.invoke(it)
}.onError {
error.invoke(it.localizedMessage ?: "获取正文出错")
}
}
fun disableSource(searchBook: SearchBook) {
execute {
appDb.bookSourceDao.getBookSource(searchBook.origin)?.let { source ->

@ -59,12 +59,12 @@ class ChangeChapterTocAdapter(context: Context, val callback: Callback) :
override fun registerListener(holder: ItemViewHolder, binding: ItemChapterListBinding) {
holder.itemView.setOnClickListener {
getItem(holder.layoutPosition)?.let {
callback.clickChapter(it)
callback.clickChapter(it, getItem(holder.layoutPosition + 1)?.url)
}
}
}
interface Callback {
fun clickChapter(bookChapter: BookChapter)
fun clickChapter(bookChapter: BookChapter, nextChapterUrl: String?)
}
}

@ -195,9 +195,9 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
execute {
book.durChapterIndex = BookHelp.getDurChapter(
book.durChapterIndex,
oldTocSize,
book.durChapterTitle,
chapters
chapters,
oldTocSize
)
book.durChapterTitle = chapters[book.durChapterIndex].title
if (inBookshelf) {

@ -185,9 +185,9 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
val oldBook = ReadBook.book!!
book.durChapterIndex = BookHelp.getDurChapter(
oldBook.durChapterIndex,
oldBook.totalChapterNum,
oldBook.durChapterTitle,
chapters
chapters,
oldBook.totalChapterNum
)
book.durChapterTitle = chapters[book.durChapterIndex].title
oldBook.changeTo(book)

@ -60,8 +60,8 @@
<io.legado.app.ui.widget.anima.RotateLoading
android:id="@+id/loading_toc"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_width="48dp"
android:layout_height="48dp"
android:visibility="gone"
app:layout_constraintTop_toTopOf="@+id/recycler_view_toc"
app:layout_constraintLeft_toLeftOf="@+id/recycler_view_toc"

Loading…
Cancel
Save