This commit is contained in:
kunfei
2023-03-13 19:27:51 +08:00
parent d56f46624e
commit e94d48a2c6
11 changed files with 62 additions and 25 deletions
@@ -0,0 +1,6 @@
package io.legado.app.exception
import io.legado.app.R
import splitties.init.appCtx
class NoBooksDirException: NoStackTraceException(appCtx.getString(R.string.no_books_dir))
@@ -13,6 +13,7 @@ import io.legado.app.data.entities.BaseSource
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.exception.EmptyFileException import io.legado.app.exception.EmptyFileException
import io.legado.app.exception.NoBooksDirException
import io.legado.app.exception.NoStackTraceException import io.legado.app.exception.NoStackTraceException
import io.legado.app.exception.TocEmptyException import io.legado.app.exception.TocEmptyException
import io.legado.app.help.AppWebDav import io.legado.app.help.AppWebDav
@@ -251,7 +252,7 @@ object LocalBook {
source: BaseSource? = null, source: BaseSource? = null,
): Uri { ): Uri {
AppConfig.defaultBookTreeUri AppConfig.defaultBookTreeUri
?: throw NoStackTraceException("没有设置书籍保存位置!") ?: throw NoBooksDirException()
val inputStream = when { val inputStream = when {
str.isAbsUrl() -> AnalyzeUrl(str, source = source).getInputStream() str.isAbsUrl() -> AnalyzeUrl(str, source = source).getInputStream()
str.isDataUrl() -> ByteArrayInputStream( str.isDataUrl() -> ByteArrayInputStream(
@@ -278,7 +279,7 @@ object LocalBook {
): Uri { ): Uri {
inputStream.use { inputStream.use {
val defaultBookTreeUri = AppConfig.defaultBookTreeUri val defaultBookTreeUri = AppConfig.defaultBookTreeUri
if (defaultBookTreeUri.isNullOrBlank()) throw NoStackTraceException("没有设置书籍保存位置!") if (defaultBookTreeUri.isNullOrBlank()) throw NoBooksDirException()
val treeUri = Uri.parse(defaultBookTreeUri) val treeUri = Uri.parse(defaultBookTreeUri)
return if (treeUri.isContentScheme()) { return if (treeUri.isContentScheme()) {
val treeDoc = DocumentFile.fromTreeUri(appCtx, treeUri) val treeDoc = DocumentFile.fromTreeUri(appCtx, treeUri)
@@ -326,7 +327,7 @@ object LocalBook {
if (webDavUrl.isNullOrBlank()) return null if (webDavUrl.isNullOrBlank()) return null
try { try {
AppConfig.defaultBookTreeUri AppConfig.defaultBookTreeUri
?: throw NoStackTraceException("没有设置书籍保存位置!") ?: throw NoBooksDirException()
// 兼容旧版链接 // 兼容旧版链接
val webdav: WebDav = kotlin.runCatching { val webdav: WebDav = kotlin.runCatching {
WebDav.fromPath(webDavUrl) WebDav.fromPath(webDavUrl)
@@ -40,6 +40,7 @@ import io.legado.app.ui.book.read.ReadBookActivity
import io.legado.app.ui.book.search.SearchActivity import io.legado.app.ui.book.search.SearchActivity
import io.legado.app.ui.book.source.edit.BookSourceEditActivity import io.legado.app.ui.book.source.edit.BookSourceEditActivity
import io.legado.app.ui.book.toc.TocActivityResult import io.legado.app.ui.book.toc.TocActivityResult
import io.legado.app.ui.document.HandleFileContract
import io.legado.app.ui.login.SourceLoginActivity import io.legado.app.ui.login.SourceLoginActivity
import io.legado.app.ui.widget.dialog.PhotoDialog import io.legado.app.ui.widget.dialog.PhotoDialog
import io.legado.app.ui.widget.dialog.WaitDialog import io.legado.app.ui.widget.dialog.WaitDialog
@@ -77,6 +78,11 @@ class BookInfoActivity :
} }
} }
} }
private val localBookTreeSelect = registerForActivityResult(HandleFileContract()) {
it.uri?.let { treeUri ->
AppConfig.defaultBookTreeUri = treeUri.toString()
}
}
private val readBookResult = registerForActivityResult( private val readBookResult = registerForActivityResult(
ActivityResultContracts.StartActivityForResult() ActivityResultContracts.StartActivityForResult()
) { ) {
@@ -215,6 +221,16 @@ class BookInfoActivity :
return super.onCompatOptionsItemSelected(item) return super.onCompatOptionsItemSelected(item)
} }
override fun observeLiveBus() {
viewModel.actionLive.observe(this) {
when (it) {
"selectBooksDir" -> localBookTreeSelect.launch {
title = getString(R.string.select_book_folder)
}
}
}
}
private fun upLoadBook( private fun upLoadBook(
book: Book, book: Book,
bookWebDav: RemoteBookWebDav? = AppWebDav.defaultBookWebDav bookWebDav: RemoteBookWebDav? = AppWebDav.defaultBookWebDav
@@ -1,21 +1,21 @@
package io.legado.app.ui.book.info package io.legado.app.ui.book.info
import android.net.Uri
import android.app.Application import android.app.Application
import android.content.Intent import android.content.Intent
import android.net.Uri
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppLog import io.legado.app.constant.AppLog
import io.legado.app.constant.AppPattern import io.legado.app.constant.AppPattern
import io.legado.app.constant.BookSourceType
import io.legado.app.constant.BookType import io.legado.app.constant.BookType
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.exception.NoBooksDirException
import io.legado.app.exception.NoStackTraceException import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.AppWebDav import io.legado.app.help.AppWebDav
import io.legado.app.help.book.* import io.legado.app.help.book.*
@@ -37,6 +37,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
var bookSource: BookSource? = null var bookSource: BookSource? = null
private var changeSourceCoroutine: Coroutine<*>? = null private var changeSourceCoroutine: Coroutine<*>? = null
val waitDialogData = MutableLiveData<Boolean>() val waitDialogData = MutableLiveData<Boolean>()
val actionLive = MutableLiveData<String>()
fun initData(intent: Intent) { fun initData(intent: Intent) {
execute { execute {
@@ -252,24 +253,30 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
/* 导入或者下载在线文件 */ /* 导入或者下载在线文件 */
fun <T> importOrDownloadWebFile(webFile: WebFile, success: ((T) -> Unit)?) { fun <T> importOrDownloadWebFile(webFile: WebFile, success: ((T) -> Unit)?) {
bookSource ?: return bookSource ?: return
execute { execute {
waitDialogData.postValue(true) waitDialogData.postValue(true)
if (webFile.isSupported) { if (webFile.isSupported) {
val book = LocalBook.importFileOnLine(webFile.url, webFile.name, bookSource) val book = LocalBook.importFileOnLine(webFile.url, webFile.name, bookSource)
changeToLocalBook(book) changeToLocalBook(book)
} else { } else {
LocalBook.saveBookFile(webFile.url, webFile.name, bookSource) LocalBook.saveBookFile(webFile.url, webFile.name, bookSource)
} }
}.onSuccess { }.onSuccess {
@Suppress("unchecked_cast") @Suppress("unchecked_cast")
success?.invoke(it as T) success?.invoke(it as T)
}.onError { }.onError {
context.toastOnUi("ImportWebFileError\n${it.localizedMessage}") when (it) {
webFiles.remove(webFile) is NoBooksDirException -> actionLive.postValue("selectBooksDir")
}.onFinally { else -> {
waitDialogData.postValue(false) AppLog.put("ImportWebFileError\n${it.localizedMessage}", it)
} context.toastOnUi("ImportWebFileError\n${it.localizedMessage}")
webFiles.remove(webFile)
}
}
}.onFinally {
waitDialogData.postValue(false)
}
} }
fun deCompress(archiveFileUri: Uri, onSuccess: (List<FileDoc>) -> Unit) { fun deCompress(archiveFileUri: Uri, onSuccess: (List<FileDoc>) -> Unit) {
@@ -289,7 +296,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
fileDoc.uri.inputStream(context).getOrThrow(), fileDoc.uri.inputStream(context).getOrThrow(),
fileDoc.name fileDoc.name
) )
LocalBook.importFile(uri).let { changeToLocalBook(it) } changeToLocalBook(LocalBook.importFile(uri))
} }
fun changeTo(source: BookSource, book: Book, toc: List<BookChapter>) { fun changeTo(source: BookSource, book: Book, toc: List<BookChapter>) {
@@ -1081,4 +1081,5 @@
<string name="server_config">服务器配置</string> <string name="server_config">服务器配置</string>
<string name="sure_upload">Remote webDav url exists, Continue?</string> <string name="sure_upload">Remote webDav url exists, Continue?</string>
<string name="unsupport_archivefile_entry">Cannot find supported files in archive</string> <string name="unsupport_archivefile_entry">Cannot find supported files in archive</string>
<string name="no_books_dir">没有设置书籍保存位置!</string>
</resources> </resources>
@@ -1084,4 +1084,5 @@
<string name="server_config">服务器配置</string> <string name="server_config">服务器配置</string>
<string name="sure_upload">Remote webDav url exists, Continue?</string> <string name="sure_upload">Remote webDav url exists, Continue?</string>
<string name="unsupport_archivefile_entry">Cannot find supported files in archive</string> <string name="unsupport_archivefile_entry">Cannot find supported files in archive</string>
<string name="no_books_dir">没有设置书籍保存位置!</string>
</resources> </resources>
@@ -1084,4 +1084,5 @@
<string name="server_config">服务器配置</string> <string name="server_config">服务器配置</string>
<string name="sure_upload">Remote webDav url exists, Continue?</string> <string name="sure_upload">Remote webDav url exists, Continue?</string>
<string name="unsupport_archivefile_entry">Cannot find supported files in archive</string> <string name="unsupport_archivefile_entry">Cannot find supported files in archive</string>
<string name="no_books_dir">没有设置书籍保存位置!</string>
</resources> </resources>
@@ -1081,4 +1081,5 @@
<string name="server_config">服务器配置</string> <string name="server_config">服务器配置</string>
<string name="sure_upload">远程webDav链接已存在,是否继续</string> <string name="sure_upload">远程webDav链接已存在,是否继续</string>
<string name="unsupport_archivefile_entry">压缩文件内没有支持的文件</string> <string name="unsupport_archivefile_entry">压缩文件内没有支持的文件</string>
<string name="no_books_dir">没有设置书籍保存位置!</string>
</resources> </resources>
@@ -1083,4 +1083,5 @@
<string name="server_config">服务器配置</string> <string name="server_config">服务器配置</string>
<string name="sure_upload">远程webDav链接已存在,是否继续</string> <string name="sure_upload">远程webDav链接已存在,是否继续</string>
<string name="unsupport_archivefile_entry">压缩文件内没有支持的文件</string> <string name="unsupport_archivefile_entry">压缩文件内没有支持的文件</string>
<string name="no_books_dir">没有设置书籍保存位置!</string>
</resources> </resources>
+1
View File
@@ -1083,4 +1083,5 @@
<string name="server_config">服务器配置</string> <string name="server_config">服务器配置</string>
<string name="sure_upload">远程webDav链接已存在,是否继续</string> <string name="sure_upload">远程webDav链接已存在,是否继续</string>
<string name="unsupport_archivefile_entry">压缩文件内没有支持的文件</string> <string name="unsupport_archivefile_entry">压缩文件内没有支持的文件</string>
<string name="no_books_dir">没有设置书籍保存位置!</string>
</resources> </resources>
+1
View File
@@ -1084,4 +1084,5 @@
<string name="server_config">服务器配置</string> <string name="server_config">服务器配置</string>
<string name="sure_upload">Remote webDav url exists, Continue?</string> <string name="sure_upload">Remote webDav url exists, Continue?</string>
<string name="unsupport_archivefile_entry">Cannot find supported files in archive</string> <string name="unsupport_archivefile_entry">Cannot find supported files in archive</string>
<string name="no_books_dir">没有设置书籍保存位置!</string>
</resources> </resources>