Merge pull request #2062 from 821938089/fix-bookUrl-change-lead-bug

修复preUpdateJs规则执行后bookUrl改变导致的一些问题
pull/2063/head
kunfei 2 years ago committed by GitHub
commit a929e55cc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 6
      app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt
  3. 8
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
  4. 9
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  5. 5
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt

@ -39,6 +39,12 @@ object BookHelp {
FileUtils.delete(filePath) FileUtils.delete(filePath)
} }
fun updateCacheFolder(oldBook: Book, newBook: Book) {
val oldFolderPath = FileUtils.getPath(downloadDir, cacheFolderName, oldBook.getFolderName())
val newFolderPath = FileUtils.getPath(downloadDir, cacheFolderName, newBook.getFolderName())
FileUtils.move(oldFolderPath, newFolderPath)
}
/** /**
* 清除已删除书的缓存 * 清除已删除书的缓存
*/ */

@ -216,9 +216,9 @@ class BookInfoActivity :
toastOnUi(getString(R.string.upload_book_success)) toastOnUi(getString(R.string.upload_book_success))
else else
toastOnUi(getString(R.string.upload_book_fail)) toastOnUi(getString(R.string.upload_book_fail))
}catch (e : Exception){ } catch (e: Exception) {
toastOnUi(e.localizedMessage) toastOnUi(e.localizedMessage)
}finally { } finally {
waitDialog.dismiss() waitDialog.dismiss()
} }
} }
@ -271,6 +271,8 @@ class BookInfoActivity :
} else { } else {
binding.tvToc.text = getString(R.string.toc_s, chapterList.last().title) binding.tvToc.text = getString(R.string.toc_s, chapterList.last().title)
} }
binding.tvLasted.text =
getString(R.string.lasted_show, chapterList.last().title)
} }
} }
} }

@ -154,6 +154,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
chapterListData.postValue(emptyList()) chapterListData.postValue(emptyList())
} else { } else {
bookSource?.let { bookSource -> bookSource?.let { bookSource ->
val oldBook = book.copy()
val preUpdateJs = bookSource.ruleToc?.preUpdateJs val preUpdateJs = bookSource.ruleToc?.preUpdateJs
if (!preUpdateJs.isNullOrBlank()) { if (!preUpdateJs.isNullOrBlank()) {
AnalyzeRule(book, bookSource).evalJS(preUpdateJs) AnalyzeRule(book, bookSource).evalJS(preUpdateJs)
@ -161,8 +162,13 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
WebBook.getChapterList(this, bookSource, book) WebBook.getChapterList(this, bookSource, book)
.onSuccess(IO) { .onSuccess(IO) {
if (inBookshelf) { if (inBookshelf) {
if (oldBook.bookUrl == book.bookUrl) {
appDb.bookDao.update(book) appDb.bookDao.update(book)
appDb.bookChapterDao.delByBook(book.bookUrl) } else {
appDb.bookDao.insert(book)
BookHelp.updateCacheFolder(oldBook, book)
}
appDb.bookChapterDao.delByBook(oldBook.bookUrl)
appDb.bookChapterDao.insert(*it.toTypedArray()) appDb.bookChapterDao.insert(*it.toTypedArray())
} }
chapterListData.postValue(it) chapterListData.postValue(it)

@ -148,14 +148,21 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} else { } else {
ReadBook.bookSource?.let { ReadBook.bookSource?.let {
viewModelScope.launch(IO) { viewModelScope.launch(IO) {
val oldBook = book.copy()
val preUpdateJs = it.ruleToc?.preUpdateJs val preUpdateJs = it.ruleToc?.preUpdateJs
if (!preUpdateJs.isNullOrBlank()) { if (!preUpdateJs.isNullOrBlank()) {
AnalyzeRule(book, it).evalJS(preUpdateJs) AnalyzeRule(book, it).evalJS(preUpdateJs)
} }
WebBook.getChapterList(viewModelScope, it, book) WebBook.getChapterList(viewModelScope, it, book)
.onSuccess(IO) { cList -> .onSuccess(IO) { cList ->
appDb.bookChapterDao.insert(*cList.toTypedArray()) if (oldBook.bookUrl == book.bookUrl) {
appDb.bookDao.update(book) appDb.bookDao.update(book)
} else {
appDb.bookDao.insert(book)
BookHelp.updateCacheFolder(oldBook, book)
}
appDb.bookChapterDao.delByBook(oldBook.bookUrl)
appDb.bookChapterDao.insert(*cList.toTypedArray())
ReadBook.chapterSize = cList.size ReadBook.chapterSize = cList.size
ReadBook.upMsg(null) ReadBook.upMsg(null)
ReadBook.loadContent(resetPageOffset = true) ReadBook.loadContent(resetPageOffset = true)

@ -11,6 +11,7 @@ import io.legado.app.data.appDb
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.AppWebDav import io.legado.app.help.AppWebDav
import io.legado.app.help.BookHelp
import io.legado.app.help.DefaultData import io.legado.app.help.DefaultData
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.help.config.LocalConfig import io.legado.app.help.config.LocalConfig
@ -116,6 +117,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
waitUpTocBooks.remove(bookUrl) waitUpTocBooks.remove(bookUrl)
upTocAdd(bookUrl) upTocAdd(bookUrl)
execute(context = upTocPool) { execute(context = upTocPool) {
val oldBook = book.copy()
val preUpdateJs = source.ruleToc?.preUpdateJs val preUpdateJs = source.ruleToc?.preUpdateJs
if (!preUpdateJs.isNullOrBlank()) { if (!preUpdateJs.isNullOrBlank()) {
AnalyzeRule(book, source).evalJS(preUpdateJs) AnalyzeRule(book, source).evalJS(preUpdateJs)
@ -129,8 +131,9 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
} else { } else {
upTocAdd(book.bookUrl) upTocAdd(book.bookUrl)
appDb.bookDao.insert(book) appDb.bookDao.insert(book)
BookHelp.updateCacheFolder(oldBook, book)
} }
appDb.bookChapterDao.delByBook(book.bookUrl) appDb.bookChapterDao.delByBook(bookUrl)
appDb.bookChapterDao.insert(*toc.toTypedArray()) appDb.bookChapterDao.insert(*toc.toTypedArray())
addDownload(source, book) addDownload(source, book)
}.onError(upTocPool) { }.onError(upTocPool) {

Loading…
Cancel
Save