diff --git a/app/src/main/java/io/legado/app/api/controller/BookController.kt b/app/src/main/java/io/legado/app/api/controller/BookController.kt index 60cb7c9c5..040b1a55f 100644 --- a/app/src/main/java/io/legado/app/api/controller/BookController.kt +++ b/app/src/main/java/io/legado/app/api/controller/BookController.kt @@ -12,6 +12,7 @@ import io.legado.app.help.CacheManager import io.legado.app.help.book.BookHelp import io.legado.app.help.book.ContentProcessor import io.legado.app.help.book.isLocal +import io.legado.app.help.config.AppConfig import io.legado.app.help.glide.ImageLoader import io.legado.app.model.BookCover import io.legado.app.model.localBook.LocalBook @@ -39,7 +40,7 @@ object BookController { return if (books.isEmpty()) { returnData.setErrorMsg("还没有添加小说") } else { - val data = when (appCtx.getPrefInt(PreferKey.bookshelfSort)) { + val data = when (AppConfig.bookshelfSort) { 1 -> books.sortedByDescending { it.latestChapterTime } 2 -> books.sortedWith { o1, o2 -> o1.name.cnCompare(o2.name) diff --git a/app/src/main/java/io/legado/app/data/entities/Book.kt b/app/src/main/java/io/legado/app/data/entities/Book.kt index 876e65fd6..4e6656fad 100644 --- a/app/src/main/java/io/legado/app/data/entities/Book.kt +++ b/app/src/main/java/io/legado/app/data/entities/Book.kt @@ -267,7 +267,10 @@ data class Book( this.tocHtml = this@Book.tocHtml } - fun changeTo(newBook: Book, toc: List): Book { + /** + * 迁移旧的书籍的一些信息到新的书籍中 + */ + fun migrateTo(newBook: Book, toc: List): Book { newBook.durChapterIndex = BookHelp .getDurChapter(durChapterIndex, durChapterTitle, toc, totalChapterNum) newBook.durChapterTitle = runBlocking { @@ -276,6 +279,7 @@ data class Book( ) } newBook.durChapterPos = durChapterPos + newBook.durChapterTime = durChapterTime newBook.group = group newBook.order = order newBook.customCoverUrl = customCoverUrl diff --git a/app/src/main/java/io/legado/app/help/config/AppConfig.kt b/app/src/main/java/io/legado/app/help/config/AppConfig.kt index e46191b26..3e719344e 100644 --- a/app/src/main/java/io/legado/app/help/config/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/config/AppConfig.kt @@ -366,6 +366,12 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { appCtx.putPrefInt(PreferKey.pageTouchSlop, value) } + var bookshelfSort: Int + get() = appCtx.getPrefInt(PreferKey.bookshelfSort, 0) + set(value) { + appCtx.putPrefInt(PreferKey.bookshelfSort, value) + } + private fun getPrefUserAgent(): String { val ua = appCtx.getPrefString(PreferKey.userAgent) if (ua.isNullOrBlank()) { diff --git a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt index b96526874..154af3f3c 100644 --- a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt @@ -214,7 +214,7 @@ class AudioPlayActivity : AudioPlay.stop(this) launch { withContext(IO) { - AudioPlay.book?.changeTo(book, toc) + AudioPlay.book?.migrateTo(book, toc) appDb.bookDao.insert(book) } startActivity { diff --git a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt index 4ad4d49a9..0c0847090 100644 --- a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt @@ -78,7 +78,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) fun changeTo(source: BookSource, book: Book, toc: List) { execute { - AudioPlay.book?.changeTo(book, toc) + AudioPlay.book?.migrateTo(book, toc) appDb.bookDao.insert(book) AudioPlay.book = book AudioPlay.bookSource = source diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt index 484889917..e6e4fa925 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt @@ -168,7 +168,7 @@ class CacheActivity : VMBaseActivity() val booksDownload = books.filter { !it.isAudio } - when (getPrefInt(PreferKey.bookshelfSort)) { + when (AppConfig.bookshelfSort) { 1 -> booksDownload.sortedByDescending { it.latestChapterTime } 2 -> booksDownload.sortedWith { o1, o2 -> o1.name.cnCompare(o2.name) 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 cf051edff..317618a82 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 @@ -235,7 +235,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { changeSourceCoroutine?.cancel() changeSourceCoroutine = execute { bookSource = source - bookData.value?.changeTo(book, toc) + bookData.value?.migrateTo(book, toc) if (inBookshelf) { book.removeType(BookType.updateError) appDb.bookDao.insert(book) diff --git a/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageActivity.kt b/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageActivity.kt index 62ee98767..9ce04123f 100644 --- a/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageActivity.kt @@ -18,6 +18,7 @@ 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.help.config.AppConfig import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.primaryColor import io.legado.app.ui.book.group.GroupManageDialog @@ -134,7 +135,7 @@ class BookshelfManageActivity : binding.recyclerView.addItemDecoration(VerticalDivider(this)) binding.recyclerView.adapter = adapter val itemTouchCallback = ItemTouchCallback(adapter) - itemTouchCallback.isCanDrag = getPrefInt(PreferKey.bookshelfSort) == 3 + itemTouchCallback.isCanDrag = AppConfig.bookshelfSort == 3 val dragSelectTouchHelper: DragSelectTouchHelper = DragSelectTouchHelper(adapter.dragSelectCallback).setSlideArea(16, 50) dragSelectTouchHelper.attachToRecyclerView(binding.recyclerView) @@ -194,7 +195,7 @@ class BookshelfManageActivity : it.name.contains(searchKey) || it.author.contains(searchKey) } } - when (getPrefInt(PreferKey.bookshelfSort)) { + when (AppConfig.bookshelfSort) { 1 -> books.sortedByDescending { it.latestChapterTime } diff --git a/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt b/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt index a28561cad..7eb71ecea 100644 --- a/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt @@ -59,7 +59,7 @@ class BookshelfManageViewModel(application: Application) : BaseViewModel(applica .onFailure { context.toastOnUi("获取目录出错\n${it.localizedMessage}") }.getOrNull()?.let { toc -> - book.changeTo(newBook, toc) + book.migrateTo(newBook, toc) book.removeType(BookType.updateError) appDb.bookDao.insert(newBook) appDb.bookChapterDao.insert(*toc.toTypedArray()) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 43154bd19..098c8fe9f 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -822,7 +822,7 @@ class ReadBookActivity : BaseReadBookActivity(), } else { ReadAloud.stop(this) launch { - ReadBook.book?.changeTo(book, toc) + ReadBook.book?.migrateTo(book, toc) appDb.bookDao.insert(book) } startActivity { 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 1054936e1..5040ac29a 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 @@ -222,7 +222,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { changeSourceCoroutine?.cancel() changeSourceCoroutine = execute { ReadBook.upMsg(context.getString(R.string.loading)) - ReadBook.book?.changeTo(book, toc) + ReadBook.book?.migrateTo(book, toc) book.removeType(BookType.updateError) appDb.bookDao.insert(book) appDb.bookChapterDao.insert(*toc.toTypedArray()) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt index ad3899b0d..f10e2aab9 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt @@ -130,7 +130,7 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment appDb.bookDao.flowUpdateError() else -> appDb.bookDao.flowByGroup(groupId) }.conflate().map { list -> - when (getPrefInt(PreferKey.bookshelfSort)) { + when (AppConfig.bookshelfSort) { 1 -> list.sortedByDescending { it.latestChapterTime } 2 -> list.sortedWith { o1, o2 -> o1.name.cnCompare(o2.name) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt index ecfa099ad..a2cf0d9ff 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt @@ -133,7 +133,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1), AppConst.bookGroupErrorId -> appDb.bookDao.flowUpdateError() else -> appDb.bookDao.flowByGroup(groupId) }.conflate().map { list -> - when (getPrefInt(PreferKey.bookshelfSort)) { + when (AppConfig.bookshelfSort) { 1 -> list.sortedByDescending { it.latestChapterTime }