diff --git a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt index 27c2fd6b0..49eac85a3 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt @@ -168,7 +168,7 @@ interface BookSourceDao { @Query("select * from book_sources where enabled = 1 and bookSourceUrl = :baseUrl") fun getBookSourceAddBook(baseUrl: String): BookSource? - @get:Query("select * from book_sources where enabled = 1 and trim(bookUrlPattern) <> '' and trim(bookUrlPattern) <> 'NONE' order by enabled desc, customOrder") + @get:Query("select * from book_sources where enabled = 1 and trim(bookUrlPattern) <> '' and trim(bookUrlPattern) <> 'NONE' order by customOrder") val hasBookUrlPattern: List @get:Query("select * from book_sources where bookSourceGroup is null or bookSourceGroup = ''") 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 bdc5d5f4e..f371a8706 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 @@ -27,6 +27,7 @@ import io.legado.app.ui.book.search.SearchActivity import io.legado.app.ui.file.HandleFileContract import io.legado.app.ui.main.MainFragmentInterface import io.legado.app.ui.main.MainViewModel +import io.legado.app.ui.widget.dialog.WaitDialog import io.legado.app.utils.* abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment(layoutId), @@ -66,6 +67,13 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment private var groupsLiveData: LiveData>? = null + private val waitDialog by lazy { + WaitDialog(requireContext()).apply { + setOnCancelListener { + viewModel.addBookJob?.cancel() + } + } + } abstract fun gotoTop() @@ -82,7 +90,7 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment configBookshelf() R.id.menu_group_manage -> showDialogFragment() R.id.menu_add_local -> startActivity() - R.id.menu_add_url -> addBookByUrl() + R.id.menu_add_url -> showAddBookByUrlAlert() R.id.menu_bookshelf_manage -> startActivity { putExtra("groupId", groupId) } @@ -117,8 +125,18 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment + if (count < 0) { + waitDialog.dismiss() + } else { + waitDialog.setText("添加中... ($count)") + } + } + } + @SuppressLint("InflateParams") - fun addBookByUrl() { + fun showAddBookByUrlAlert() { alert(titleResource = R.string.add_book_url) { val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply { editView.hint = "url" @@ -126,6 +144,8 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment? = null fun addBookByUrl(bookUrls: String) { var successCount = 0 - execute { + addBookJob = execute { val hasBookUrlPattern: List by lazy { appDb.bookSourceDao.hasBookUrlPattern } @@ -43,41 +47,48 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) for (url in urls) { val bookUrl = url.trim() if (bookUrl.isEmpty()) continue - if (appDb.bookDao.getBook(bookUrl) != null) continue + if (appDb.bookDao.getBook(bookUrl) != null) { + successCount++ + continue + } val baseUrl = NetworkUtils.getBaseUrl(bookUrl) ?: continue var source = appDb.bookSourceDao.getBookSourceAddBook(baseUrl) if (source == null) { - hasBookUrlPattern.forEach { bookSource -> - if (bookUrl.matches(bookSource.bookUrlPattern!!.toRegex())) { - source = bookSource - return@forEach + for (bookSource in hasBookUrlPattern) { + try { + if (bookUrl.matches(bookSource.bookUrlPattern!!.toRegex())) { + source = bookSource + break + } + } catch (_: Exception) { } } } - source?.let { bookSource -> - val book = Book( - bookUrl = bookUrl, - origin = bookSource.bookSourceUrl, - originName = bookSource.bookSourceName - ) - WebBook.getBookInfo(this, bookSource, book) - .onSuccess(IO) { - it.order = appDb.bookDao.minOrder - 1 - it.save() - successCount++ - }.onError { - throw it - } + val bookSource = source ?: continue + val book = Book( + bookUrl = bookUrl, + origin = bookSource.bookSourceUrl, + originName = bookSource.bookSourceName + ) + kotlin.runCatching { + WebBook.getBookInfoAwait(bookSource, book) + }.onSuccess { + it.order = appDb.bookDao.minOrder - 1 + it.save() + successCount++ + addBookProgressLiveData.postValue(successCount) } } }.onSuccess { if (successCount > 0) { context.toastOnUi(R.string.success) } else { - context.toastOnUi("ERROR") + context.toastOnUi("添加网址失败") } }.onError { - context.toastOnUi(it.localizedMessage ?: "ERROR") + AppLog.put("添加网址出错\n${it.localizedMessage}", it, true) + }.onFinally { + addBookProgressLiveData.postValue(-1) } } @@ -121,9 +132,11 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) importBookshelf(it, groupId) } } + text.isJsonArray() -> { importBookshelfByJson(text, groupId) } + else -> { throw NoStackTraceException("格式不对") }