From 7c29f4cdaf68d673dc102332f0dd89e80bbbcf02 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 15 Oct 2022 20:29:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9B=B4=E6=96=B0=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E5=88=86=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/constant/AppConst.kt | 1 + .../java/io/legado/app/constant/BookType.kt | 13 +++---- .../java/io/legado/app/data/AppDatabase.kt | 7 +++- .../java/io/legado/app/data/dao/BookDao.kt | 3 ++ .../io/legado/app/data/dao/BookGroupDao.kt | 1 + .../io/legado/app/help/book/BookExtensions.kt | 39 +++++++++++++++---- .../ui/book/manage/BookshelfManageActivity.kt | 1 + .../searchContent/SearchContentActivity.kt | 2 +- .../io/legado/app/ui/main/MainViewModel.kt | 6 +++ .../bookshelf/style1/books/BooksFragment.kt | 1 + 10 files changed, 56 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/AppConst.kt b/app/src/main/java/io/legado/app/constant/AppConst.kt index 8a6339bdf..be8cd07e5 100644 --- a/app/src/main/java/io/legado/app/constant/AppConst.kt +++ b/app/src/main/java/io/legado/app/constant/AppConst.kt @@ -43,6 +43,7 @@ object AppConst { const val bookGroupAudioId = -3L const val bookGroupNetNoneId = -4L const val bookGroupLocalNoneId = -5L + const val bookGroupErrorId = -11L const val notificationIdRead = -1122391 const val notificationIdAudio = -1122392 diff --git a/app/src/main/java/io/legado/app/constant/BookType.kt b/app/src/main/java/io/legado/app/constant/BookType.kt index 15515981c..9b998d0e8 100644 --- a/app/src/main/java/io/legado/app/constant/BookType.kt +++ b/app/src/main/java/io/legado/app/constant/BookType.kt @@ -1,7 +1,5 @@ package io.legado.app.constant -import androidx.annotation.IntDef - /** * 以二进制位来区分,可能一本书籍包含多个类型,每一位代表一个类型,数值为2的n次方 * 以二进制位来区分,数据库查询更高效, 数值>=8和老版本类型区分开 @@ -12,6 +10,11 @@ object BookType { */ const val text = 0b1000 + /** + * 16 更新失败 + */ + const val updateError = 0b10000 + /** * 32 音频 */ @@ -32,12 +35,6 @@ object BookType { */ const val local = 0b100000000 - - @Target(AnnotationTarget.VALUE_PARAMETER) - @Retention(AnnotationRetention.SOURCE) - @IntDef(text, audio, image, webFile) - annotation class Type - /** * 本地书籍书源标志 */ diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt index 01351d2eb..b826093b6 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -105,9 +105,14 @@ abstract class AppDatabase : RoomDatabase() { ) db.execSQL( """insert into book_groups(groupId, groupName, 'order', show) - select ${AppConst.bookGroupLocalNoneId}, '本地未分组', -8, 0 + select ${AppConst.bookGroupLocalNoneId}, '本地未分组', -6, 0 where not exists (select * from book_groups where groupId = ${AppConst.bookGroupLocalNoneId})""" ) + db.execSQL( + """insert into book_groups(groupId, groupName, 'order', show) + select ${AppConst.bookGroupErrorId}, '更新失败', -1, 1 + where not exists (select * from book_groups where groupId = ${AppConst.bookGroupErrorId})""" + ) db.execSQL("update book_sources set loginUi = null where loginUi = 'null'") db.execSQL("update rssSources set loginUi = null where loginUi = 'null'") db.execSQL("update httpTTS set loginUi = null where loginUi = 'null'") diff --git a/app/src/main/java/io/legado/app/data/dao/BookDao.kt b/app/src/main/java/io/legado/app/data/dao/BookDao.kt index 4dd905c33..082bfba0a 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookDao.kt @@ -53,6 +53,9 @@ interface BookDao { @Query("SELECT * FROM books WHERE name like '%'||:key||'%' or author like '%'||:key||'%'") fun flowSearch(key: String): Flow> + @Query("SELECT * FROM books where type & ${BookType.updateError} > 0 order by durChapterTime desc") + fun flowUpdateError(): Flow> + @Query("SELECT * FROM books WHERE (`group` & :group) > 0") fun getBooksByGroup(group: Long): List diff --git a/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt b/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt index af3982275..5e1fe7684 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt @@ -25,6 +25,7 @@ interface BookGroupDao { or (groupId = -1 and show > 0) or (groupId = -2 and show > 0 and (select count(*) from books where type & ${BookType.local} > 0) > 0) or (groupId = -3 and show > 0 and (select count(*) from books where type & ${BookType.audio} > 0) > 0) + or (groupId = -11 and show > 0 and (select count(*) from books where type & ${BookType.updateError} > 0) > 0) or (groupId = -4 and show > 0 and ( select count(*) from books diff --git a/app/src/main/java/io/legado/app/help/book/BookExtensions.kt b/app/src/main/java/io/legado/app/help/book/BookExtensions.kt index 15b6c7912..179d09b35 100644 --- a/app/src/main/java/io/legado/app/help/book/BookExtensions.kt +++ b/app/src/main/java/io/legado/app/help/book/BookExtensions.kt @@ -1,3 +1,5 @@ +@file:Suppress("unused") + package io.legado.app.help.book import android.net.Uri @@ -6,12 +8,12 @@ import io.legado.app.constant.BookSourceType import io.legado.app.constant.BookType import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookSource -import io.legado.app.help.config.AppConfig.defaultBookTreeUri import io.legado.app.exception.NoStackTraceException -import io.legado.app.utils.isContentScheme +import io.legado.app.help.config.AppConfig.defaultBookTreeUri import io.legado.app.utils.getFile -import java.io.File +import io.legado.app.utils.isContentScheme import splitties.init.appCtx +import java.io.File val Book.isAudio: Boolean @@ -54,11 +56,11 @@ val Book.isOnLineTxt: Boolean fun Book.getLocalUri(): Uri { if (isLocal) { - val originBookUri = if (bookUrl.isContentScheme()) { - Uri.parse(bookUrl) - } else { - Uri.fromFile(File(bookUrl)) - } + val originBookUri = if (bookUrl.isContentScheme()) { + Uri.parse(bookUrl) + } else { + Uri.fromFile(File(bookUrl)) + } //不同的设备书籍保存路径可能不一样 优先尝试寻找当前保存路径下的文件 defaultBookTreeUri ?: return originBookUri val treeUri = Uri.parse(defaultBookTreeUri) @@ -84,6 +86,27 @@ fun Book.getRemoteUrl(): String? { return null } +fun Book.setType(vararg types: Int) { + type = 0 + addType(*types) +} + +fun Book.addType(vararg types: Int) { + types.forEach { + type = type or it + } +} + +fun Book.removeType(vararg types: Int) { + types.forEach { + type = type and it.inv() + } +} + +fun Book.clearType() { + type = 0 +} + fun Book.upType() { if (type < 8) { type = when (type) { 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 b7b4d6302..62ee98767 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 @@ -184,6 +184,7 @@ class BookshelfManageActivity : AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio() AppConst.bookGroupNetNoneId -> appDb.bookDao.flowNetNoGroup() AppConst.bookGroupLocalNoneId -> appDb.bookDao.flowLocalNoGroup() + AppConst.bookGroupErrorId -> appDb.bookDao.flowUpdateError() else -> appDb.bookDao.flowByGroup(viewModel.groupId) }.conflate().map { list -> val books = if (searchKey.isNullOrBlank()) { diff --git a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt index 4b65b2695..477f273ac 100644 --- a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt @@ -193,7 +193,7 @@ class SearchContentActivity : } } - val isLocalBook: Boolean + private val isLocalBook: Boolean get() = viewModel.book?.isLocal == true override fun openSearchResult(searchResult: SearchResult, index: Int) { 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 b6629fa99..d09ef387d 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 @@ -5,6 +5,7 @@ import androidx.lifecycle.viewModelScope import io.legado.app.base.BaseViewModel import io.legado.app.constant.AppConst import io.legado.app.constant.AppLog +import io.legado.app.constant.BookType import io.legado.app.constant.EventBus import io.legado.app.data.appDb import io.legado.app.data.entities.Book @@ -12,7 +13,9 @@ import io.legado.app.data.entities.BookSource import io.legado.app.help.AppWebDav import io.legado.app.help.DefaultData import io.legado.app.help.book.BookHelp +import io.legado.app.help.book.addType import io.legado.app.help.book.isLocal +import io.legado.app.help.book.removeType import io.legado.app.help.config.AppConfig import io.legado.app.help.config.LocalConfig import io.legado.app.model.CacheBook @@ -126,6 +129,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) { WebBook.getBookInfoAwait(source, book) } val toc = WebBook.getChapterListAwait(source, book).getOrThrow() + book.removeType(BookType.updateError) if (book.bookUrl == bookUrl) { appDb.bookDao.update(book) } else { @@ -137,6 +141,8 @@ class MainViewModel(application: Application) : BaseViewModel(application) { appDb.bookChapterDao.insert(*toc.toTypedArray()) addDownload(source, book) }.onError(upTocPool) { + book.addType(BookType.updateError) + appDb.bookDao.update(book) AppLog.put("${book.name} 更新目录失败\n${it.localizedMessage}", it) }.onCancel(upTocPool) { upTocCancel(bookUrl) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt index 91fa9318a..a2a434497 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt @@ -118,6 +118,7 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books), AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio() AppConst.bookGroupNetNoneId -> appDb.bookDao.flowNetNoGroup() AppConst.bookGroupLocalNoneId -> appDb.bookDao.flowLocalNoGroup() + AppConst.bookGroupErrorId -> appDb.bookDao.flowUpdateError() else -> appDb.bookDao.flowByGroup(groupId) }.conflate().map { list -> when (getPrefInt(PreferKey.bookshelfSort)) {