pull/84/head
kunfei 5 years ago
parent 0e84934b5b
commit 9011b95a32
  1. 3
      app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt
  2. 44
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt

@ -53,6 +53,9 @@ interface BookSourceDao {
@Query("select * from book_sources where enabled = 1 and bookSourceGroup like '%' || :group || '%'")
fun getEnabledByGroup(group: String): List<BookSource>
@get:Query("select * from book_sources where bookUrlPattern is not null || bookUrlPattern <> ''")
val hasBookUrlPattern: List<BookSource>
@get:Query("select * from book_sources where bookSourceGroup is null or bookSourceGroup = ''")
val noGroup: List<BookSource>

@ -2,8 +2,14 @@ package io.legado.app.ui.main.bookshelf
import android.app.Application
import io.legado.app.App
import io.legado.app.R
import io.legado.app.base.BaseViewModel
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.model.WebBook
import io.legado.app.utils.NetworkUtils
import kotlinx.coroutines.Dispatchers.IO
class BookshelfViewModel(application: Application) : BaseViewModel(application) {
@ -31,8 +37,42 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
}
}
fun addBookByUrl(bookUrl: String) {
fun addBookByUrl(bookUrls: String) {
execute {
var hasBookUrlPattern: List<BookSource>? = null
val urls = bookUrls.split("\n")
for (url in urls) {
val bookUrl = url.trim()
if (bookUrl.isEmpty()) continue
App.db.bookDao().getBook(bookUrl) ?: continue
val baseUrl = NetworkUtils.getBaseUrl(bookUrl) ?: continue
var source = App.db.bookSourceDao().getBookSource(baseUrl)
if (source == null) {
if (hasBookUrlPattern == null) {
hasBookUrlPattern = App.db.bookSourceDao().hasBookUrlPattern
}
hasBookUrlPattern.forEach { bookSource ->
if (bookUrl.matches(bookSource.bookUrlPattern!!.toRegex())) {
source = bookSource
return@forEach
}
}
}
source?.let { bookSource ->
val book = Book(bookUrl = bookUrl)
WebBook(bookSource).getBookInfo(book, this)
.onSuccess(IO) {
it?.let { book ->
App.db.bookDao().insert(book)
}
}
}
}
}.onSuccess {
toast(R.string.success)
}.onError {
toast(it.localizedMessage ?: "ERROR")
}
}
}

Loading…
Cancel
Save