diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index 0d607a5f1..8e403ebff 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -39,6 +39,12 @@ object BookHelp { 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) + } + /** * 清除已删除书的缓存 */ diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index eb79ef5e7..17be57e48 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -216,9 +216,9 @@ class BookInfoActivity : toastOnUi(getString(R.string.upload_book_success)) else toastOnUi(getString(R.string.upload_book_fail)) - }catch (e : Exception){ + } catch (e: Exception) { toastOnUi(e.localizedMessage) - }finally { + } finally { waitDialog.dismiss() } } @@ -271,6 +271,8 @@ class BookInfoActivity : } else { binding.tvToc.text = getString(R.string.toc_s, chapterList.last().title) } + binding.tvLasted.text = + getString(R.string.lasted_show, chapterList.last().title) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt index 73a737bb1..f915600b0 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt @@ -154,6 +154,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { chapterListData.postValue(emptyList()) } else { bookSource?.let { bookSource -> + val oldBook = book.copy() val preUpdateJs = bookSource.ruleToc?.preUpdateJs if (!preUpdateJs.isNullOrBlank()) { AnalyzeRule(book, bookSource).evalJS(preUpdateJs) @@ -161,8 +162,13 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { WebBook.getChapterList(this, bookSource, book) .onSuccess(IO) { if (inBookshelf) { - appDb.bookDao.update(book) - appDb.bookChapterDao.delByBook(book.bookUrl) + if (oldBook.bookUrl == book.bookUrl) { + appDb.bookDao.update(book) + } else { + appDb.bookDao.insert(book) + BookHelp.updateCacheFolder(oldBook, book) + } + appDb.bookChapterDao.delByBook(oldBook.bookUrl) appDb.bookChapterDao.insert(*it.toTypedArray()) } chapterListData.postValue(it) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index ef1ea642a..ec86f9cfd 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -148,14 +148,21 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } else { ReadBook.bookSource?.let { viewModelScope.launch(IO) { + val oldBook = book.copy() val preUpdateJs = it.ruleToc?.preUpdateJs if (!preUpdateJs.isNullOrBlank()) { AnalyzeRule(book, it).evalJS(preUpdateJs) } WebBook.getChapterList(viewModelScope, it, book) .onSuccess(IO) { cList -> + if (oldBook.bookUrl == book.bookUrl) { + appDb.bookDao.update(book) + } else { + appDb.bookDao.insert(book) + BookHelp.updateCacheFolder(oldBook, book) + } + appDb.bookChapterDao.delByBook(oldBook.bookUrl) appDb.bookChapterDao.insert(*cList.toTypedArray()) - appDb.bookDao.update(book) ReadBook.chapterSize = cList.size ReadBook.upMsg(null) ReadBook.loadContent(resetPageOffset = true) diff --git a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt index 2bef7753b..69dea8cfe 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt @@ -11,6 +11,7 @@ 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.AppWebDav +import io.legado.app.help.BookHelp import io.legado.app.help.DefaultData import io.legado.app.help.config.AppConfig import io.legado.app.help.config.LocalConfig @@ -116,6 +117,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) { waitUpTocBooks.remove(bookUrl) upTocAdd(bookUrl) execute(context = upTocPool) { + val oldBook = book.copy() val preUpdateJs = source.ruleToc?.preUpdateJs if (!preUpdateJs.isNullOrBlank()) { AnalyzeRule(book, source).evalJS(preUpdateJs) @@ -129,8 +131,9 @@ class MainViewModel(application: Application) : BaseViewModel(application) { } else { upTocAdd(book.bookUrl) appDb.bookDao.insert(book) + BookHelp.updateCacheFolder(oldBook, book) } - appDb.bookChapterDao.delByBook(book.bookUrl) + appDb.bookChapterDao.delByBook(bookUrl) appDb.bookChapterDao.insert(*toc.toTypedArray()) addDownload(source, book) }.onError(upTocPool) {