This commit is contained in:
kunfei
2023-05-03 22:01:07 +08:00
parent 44a3591b1b
commit d70b685f47
12 changed files with 52 additions and 27 deletions
@@ -110,10 +110,13 @@ interface BookSourceDao {
) )
fun getEnabledByGroup(group: String): List<BookSource> fun getEnabledByGroup(group: String): List<BookSource>
@Query("select * from book_sources where enabled = 1 and bookSourceType = :type") @Query("select * from book_sources where bookUrlPattern != 'NONE' and bookSourceType = :type")
fun getEnabledByType(type: Int): List<BookSource> fun getEnabledByType(type: Int): List<BookSource>
@get:Query("select * from book_sources where enabled = 1 and trim(bookUrlPattern) <> '' order by enabled desc, customOrder") @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")
val hasBookUrlPattern: List<BookSource> val hasBookUrlPattern: List<BookSource>
@get:Query("select * from book_sources where bookSourceGroup is null or bookSourceGroup = ''") @get:Query("select * from book_sources where bookSourceGroup is null or bookSourceGroup = ''")
@@ -12,8 +12,25 @@ import io.legado.app.constant.PreferKey
import io.legado.app.help.DefaultData import io.legado.app.help.DefaultData
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.ui.book.read.page.provider.ChapterProvider import io.legado.app.ui.book.read.page.provider.ChapterProvider
import io.legado.app.utils.* import io.legado.app.utils.BitmapUtils
import io.legado.app.utils.FileUtils
import io.legado.app.utils.GSON
import io.legado.app.utils.compress.ZipUtils import io.legado.app.utils.compress.ZipUtils
import io.legado.app.utils.createFolderReplace
import io.legado.app.utils.externalCache
import io.legado.app.utils.externalFiles
import io.legado.app.utils.fromJsonArray
import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.getCompatColor
import io.legado.app.utils.getFile
import io.legado.app.utils.getMeanColor
import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.getPrefInt
import io.legado.app.utils.hexString
import io.legado.app.utils.printOnDebug
import io.legado.app.utils.putPrefBoolean
import io.legado.app.utils.putPrefInt
import io.legado.app.utils.resizeAndRecycle
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import splitties.init.appCtx import splitties.init.appCtx
@@ -394,7 +411,6 @@ object ReadBookConfig {
zipFile.writeBytes(byteArray) zipFile.writeBytes(byteArray)
val configDir = appCtx.externalCache.getFile("readConfig") val configDir = appCtx.externalCache.getFile("readConfig")
configDir.createFolderReplace() configDir.createFolderReplace()
@Suppress("BlockingMethodInNonBlockingContext")
ZipUtils.unZipToPath(zipFile, configDir) ZipUtils.unZipToPath(zipFile, configDir)
val configFile = configDir.getFile(configFileName) val configFile = configDir.getFile(configFileName)
val config: Config = GSON.fromJsonObject<Config>(configFile.readText()).getOrThrow() val config: Config = GSON.fromJsonObject<Config>(configFile.readText()).getOrThrow()
@@ -259,7 +259,6 @@ open class WebDav(
* @param savedPath 本地的完整路径,包括最后的文件名 * @param savedPath 本地的完整路径,包括最后的文件名
* @param replaceExisting 是否替换本地的同名文件 * @param replaceExisting 是否替换本地的同名文件
*/ */
@Suppress("BlockingMethodInNonBlockingContext")
@Throws(WebDavException::class) @Throws(WebDavException::class)
suspend fun downloadTo(savedPath: String, replaceExisting: Boolean) { suspend fun downloadTo(savedPath: String, replaceExisting: Boolean) {
val file = File(savedPath) val file = File(savedPath)
@@ -123,7 +123,7 @@ class AddToBookshelfDialog() : BaseDialogFragment(R.layout.dialog_add_to_bookshe
} }
} }
} }
appDb.bookSourceDao.getBookSource(baseUrl)?.let { source -> appDb.bookSourceDao.getBookSourceAddBook(baseUrl)?.let { source ->
getBookInfo(bookUrl, source)?.let { book -> getBookInfo(bookUrl, source)?.let { book ->
return@execute book return@execute book
} }
@@ -36,7 +36,6 @@ class OnLineImportViewModel(app: Application) : BaseAssociationViewModel(app) {
fun getBytes(url: String, success: (bytes: ByteArray) -> Unit) { fun getBytes(url: String, success: (bytes: ByteArray) -> Unit) {
execute { execute {
@Suppress("BlockingMethodInNonBlockingContext")
okHttpClient.newCallResponseBody { okHttpClient.newCallResponseBody {
if (url.endsWith("#requestWithoutUA")) { if (url.endsWith("#requestWithoutUA")) {
url(url.substringBeforeLast("#requestWithoutUA")) url(url.substringBeforeLast("#requestWithoutUA"))
@@ -88,7 +87,6 @@ class OnLineImportViewModel(app: Application) : BaseAssociationViewModel(app) {
when (rs.contentType()) { when (rs.contentType()) {
"application/zip".toMediaType(), "application/zip".toMediaType(),
"application/octet-stream".toMediaType() -> { "application/octet-stream".toMediaType() -> {
@Suppress("BlockingMethodInNonBlockingContext")
importReadConfig(rs.bytes(), finally) importReadConfig(rs.bytes(), finally)
} }
else -> { else -> {
@@ -123,7 +123,6 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
} }
} }
@Suppress("BlockingMethodInNonBlockingContext")
private suspend fun export(doc: DocumentFile, book: Book) { private suspend fun export(doc: DocumentFile, book: Book) {
val filename = book.getExportFileName("txt") val filename = book.getExportFileName("txt")
DocumentUtils.delete(doc, filename) DocumentUtils.delete(doc, filename)
@@ -288,7 +287,6 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
} }
} }
@Suppress("BlockingMethodInNonBlockingContext")
private suspend fun exportEpub(doc: DocumentFile, book: Book) { private suspend fun exportEpub(doc: DocumentFile, book: Book) {
val filename = book.getExportFileName("epub") val filename = book.getExportFileName("epub")
DocumentUtils.delete(doc, filename) DocumentUtils.delete(doc, filename)
@@ -232,7 +232,6 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
}) })
} }
@Suppress("BlockingMethodInNonBlockingContext")
private fun exportConfig(uri: Uri) { private fun exportConfig(uri: Uri) {
val exportFileName = if (ReadBookConfig.config.name.isBlank()) { val exportFileName = if (ReadBookConfig.config.name.isBlank()) {
configFileName configFileName
@@ -324,7 +323,6 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
private fun importNetConfig(url: String) { private fun importNetConfig(url: String) {
execute { execute {
@Suppress("BlockingMethodInNonBlockingContext")
okHttpClient.newCallResponseBody { okHttpClient.newCallResponseBody {
url(url) url(url)
}.bytes().let { }.bytes().let {
@@ -337,7 +335,6 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
private fun importConfig(uri: Uri) { private fun importConfig(uri: Uri) {
execute { execute {
@Suppress("BlockingMethodInNonBlockingContext")
importConfig(uri.readBytes(requireContext())) importConfig(uri.readBytes(requireContext()))
}.onError { }.onError {
it.printOnDebug() it.printOnDebug()
@@ -345,7 +342,6 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
} }
} }
@Suppress("BlockingMethodInNonBlockingContext", "BlockingMethodInNonBlockingContext")
private fun importConfig(byteArray: ByteArray) { private fun importConfig(byteArray: ByteArray) {
execute { execute {
ReadBookConfig.import(byteArray).getOrThrow() ReadBookConfig.import(byteArray).getOrThrow()
@@ -88,7 +88,6 @@ object ImageProvider {
if (book.isEpub) { if (book.isEpub) {
EpubFile.getImage(book, src)?.use { input -> EpubFile.getImage(book, src)?.use { input ->
val newFile = FileUtils.createFileIfNotExist(vFile.absolutePath) val newFile = FileUtils.createFileIfNotExist(vFile.absolutePath)
@Suppress("BlockingMethodInNonBlockingContext")
FileOutputStream(newFile).use { output -> FileOutputStream(newFile).use { output ->
input.copyTo(output) input.copyTo(output)
} }
@@ -96,7 +95,6 @@ object ImageProvider {
} else if (book.isPdf) { } else if (book.isPdf) {
PdfFile.getImage(book, src)?.use { input -> PdfFile.getImage(book, src)?.use { input ->
val newFile = FileUtils.createFileIfNotExist(vFile.absolutePath) val newFile = FileUtils.createFileIfNotExist(vFile.absolutePath)
@Suppress("BlockingMethodInNonBlockingContext")
FileOutputStream(newFile).use { output -> FileOutputStream(newFile).use { output ->
input.copyTo(output) input.copyTo(output)
} }
@@ -170,7 +168,6 @@ object ImageProvider {
} }
return null return null
} }
@Suppress("BlockingMethodInNonBlockingContext")
return kotlin.runCatching { return kotlin.runCatching {
val bitmap = BitmapUtils.decodeBitmap(vFile.absolutePath, width, height) val bitmap = BitmapUtils.decodeBitmap(vFile.absolutePath, width, height)
?: SvgUtils.createBitmap(vFile.absolutePath, width, height) ?: SvgUtils.createBitmap(vFile.absolutePath, width, height)
@@ -15,9 +15,14 @@ import io.legado.app.help.IntentData
import io.legado.app.help.http.newCallResponseBody import io.legado.app.help.http.newCallResponseBody
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.* import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.FileUtils
import io.legado.app.utils.isContentScheme
import io.legado.app.utils.printOnDebug
import io.legado.app.utils.toastOnUi
import io.legado.app.utils.writeBytes
import java.io.File import java.io.File
import java.util.* import java.util.Date
class WebViewModel(application: Application) : BaseViewModel(application) { class WebViewModel(application: Application) : BaseViewModel(application) {
var baseUrl: String = "" var baseUrl: String = ""
@@ -75,7 +80,6 @@ class WebViewModel(application: Application) : BaseViewModel(application) {
private suspend fun webData2bitmap(data: String): ByteArray? { private suspend fun webData2bitmap(data: String): ByteArray? {
return if (URLUtil.isValidUrl(data)) { return if (URLUtil.isValidUrl(data)) {
@Suppress("BlockingMethodInNonBlockingContext")
okHttpClient.newCallResponseBody { okHttpClient.newCallResponseBody {
url(data) url(data)
}.bytes() }.bytes()
@@ -12,12 +12,24 @@ import io.legado.app.help.http.newCallResponseBody
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text import io.legado.app.help.http.text
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.utils.* import io.legado.app.utils.FileUtils
import io.legado.app.utils.GSON
import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.fromJsonArray
import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.isJsonArray
import io.legado.app.utils.printOnDebug
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.OutputStreamWriter import java.io.OutputStreamWriter
import kotlin.collections.List
import kotlin.collections.Map
import kotlin.collections.forEach
import kotlin.collections.hashMapOf
import kotlin.collections.set
class BookshelfViewModel(application: Application) : BaseViewModel(application) { class BookshelfViewModel(application: Application) : BaseViewModel(application) {
@@ -33,7 +45,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
if (bookUrl.isEmpty()) continue if (bookUrl.isEmpty()) continue
if (appDb.bookDao.getBook(bookUrl) != null) continue if (appDb.bookDao.getBook(bookUrl) != null) continue
val baseUrl = NetworkUtils.getBaseUrl(bookUrl) ?: continue val baseUrl = NetworkUtils.getBaseUrl(bookUrl) ?: continue
var source = appDb.bookSourceDao.getBookSource(baseUrl) var source = appDb.bookSourceDao.getBookSourceAddBook(baseUrl)
if (source == null) { if (source == null) {
hasBookUrlPattern.forEach { bookSource -> hasBookUrlPattern.forEach { bookSource ->
if (bookUrl.matches(bookSource.bookUrlPattern!!.toRegex())) { if (bookUrl.matches(bookSource.bookUrlPattern!!.toRegex())) {
@@ -75,7 +87,6 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
val path = "${context.filesDir}/books.json" val path = "${context.filesDir}/books.json"
FileUtils.delete(path) FileUtils.delete(path)
val file = FileUtils.createFileWithReplace(path) val file = FileUtils.createFileWithReplace(path)
@Suppress("BlockingMethodInNonBlockingContext")
FileOutputStream(file).use { out -> FileOutputStream(file).use { out ->
val writer = JsonWriter(OutputStreamWriter(out, "UTF-8")) val writer = JsonWriter(OutputStreamWriter(out, "UTF-8"))
writer.setIndent(" ") writer.setIndent(" ")
@@ -19,11 +19,15 @@ import io.legado.app.help.http.newCallResponseBody
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.rss.Rss import io.legado.app.model.rss.Rss
import io.legado.app.utils.* import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.FileUtils
import io.legado.app.utils.isContentScheme
import io.legado.app.utils.toastOnUi
import io.legado.app.utils.writeBytes
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import splitties.init.appCtx import splitties.init.appCtx
import java.io.File import java.io.File
import java.util.* import java.util.Date
class ReadRssViewModel(application: Application) : BaseViewModel(application) { class ReadRssViewModel(application: Application) : BaseViewModel(application) {
@@ -156,7 +160,6 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
private suspend fun webData2bitmap(data: String): ByteArray? { private suspend fun webData2bitmap(data: String): ByteArray? {
return if (URLUtil.isValidUrl(data)) { return if (URLUtil.isValidUrl(data)) {
@Suppress("BlockingMethodInNonBlockingContext")
okHttpClient.newCallResponseBody { okHttpClient.newCallResponseBody {
url(data) url(data)
}.bytes() }.bytes()
@@ -13,7 +13,7 @@ import java.io.*
import java.util.zip.* import java.util.zip.*
@SuppressLint("ObsoleteSdkInt") @SuppressLint("ObsoleteSdkInt")
@Suppress("unused", "BlockingMethodInNonBlockingContext", "MemberVisibilityCanBePrivate") @Suppress("unused", "MemberVisibilityCanBePrivate")
object ZipUtils { object ZipUtils {
fun zipString(text: String): ByteArray { fun zipString(text: String): ByteArray {