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

View File

@ -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))

View File

@ -13,6 +13,7 @@ import io.legado.app.data.entities.BaseSource
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
import io.legado.app.exception.EmptyFileException
import io.legado.app.exception.NoBooksDirException
import io.legado.app.exception.NoStackTraceException
import io.legado.app.exception.TocEmptyException
import io.legado.app.help.AppWebDav
@ -160,7 +161,7 @@ object LocalBook {
//updateTime变量不要修改,否则会导致读取不到缓存
val (fileName, _, _, updateTime, _) = FileDoc.fromUri(uri, false).apply {
if (size == 0L) throw EmptyFileException("Unexpected empty File")
bookUrl = toString()
}
var book = appDb.bookDao.getBook(bookUrl)
@ -251,7 +252,7 @@ object LocalBook {
source: BaseSource? = null,
): Uri {
AppConfig.defaultBookTreeUri
?: throw NoStackTraceException("没有设置书籍保存位置!")
?: throw NoBooksDirException()
val inputStream = when {
str.isAbsUrl() -> AnalyzeUrl(str, source = source).getInputStream()
str.isDataUrl() -> ByteArrayInputStream(
@ -278,7 +279,7 @@ object LocalBook {
): Uri {
inputStream.use {
val defaultBookTreeUri = AppConfig.defaultBookTreeUri
if (defaultBookTreeUri.isNullOrBlank()) throw NoStackTraceException("没有设置书籍保存位置!")
if (defaultBookTreeUri.isNullOrBlank()) throw NoBooksDirException()
val treeUri = Uri.parse(defaultBookTreeUri)
return if (treeUri.isContentScheme()) {
val treeDoc = DocumentFile.fromTreeUri(appCtx, treeUri)
@ -326,7 +327,7 @@ object LocalBook {
if (webDavUrl.isNullOrBlank()) return null
try {
AppConfig.defaultBookTreeUri
?: throw NoStackTraceException("没有设置书籍保存位置!")
?: throw NoBooksDirException()
// 兼容旧版链接
val webdav: WebDav = kotlin.runCatching {
WebDav.fromPath(webDavUrl)

View File

@ -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.source.edit.BookSourceEditActivity
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.widget.dialog.PhotoDialog
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(
ActivityResultContracts.StartActivityForResult()
) {
@ -215,6 +221,16 @@ class BookInfoActivity :
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(
book: Book,
bookWebDav: RemoteBookWebDav? = AppWebDav.defaultBookWebDav

View File

@ -1,21 +1,21 @@
package io.legado.app.ui.book.info
import android.net.Uri
import android.app.Application
import android.content.Intent
import android.net.Uri
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import io.legado.app.R
import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppLog
import io.legado.app.constant.AppPattern
import io.legado.app.constant.BookSourceType
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
import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.BookSource
import io.legado.app.exception.NoBooksDirException
import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.AppWebDav
import io.legado.app.help.book.*
@ -37,6 +37,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
var bookSource: BookSource? = null
private var changeSourceCoroutine: Coroutine<*>? = null
val waitDialogData = MutableLiveData<Boolean>()
val actionLive = MutableLiveData<String>()
fun initData(intent: Intent) {
execute {
@ -252,24 +253,30 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
/* 导入或者下载在线文件 */
fun <T> importOrDownloadWebFile(webFile: WebFile, success: ((T) -> Unit)?) {
bookSource ?: return
execute {
waitDialogData.postValue(true)
if (webFile.isSupported) {
val book = LocalBook.importFileOnLine(webFile.url, webFile.name, bookSource)
changeToLocalBook(book)
} else {
LocalBook.saveBookFile(webFile.url, webFile.name, bookSource)
}
}.onSuccess {
@Suppress("unchecked_cast")
success?.invoke(it as T)
}.onError {
context.toastOnUi("ImportWebFileError\n${it.localizedMessage}")
webFiles.remove(webFile)
}.onFinally {
waitDialogData.postValue(false)
}
bookSource ?: return
execute {
waitDialogData.postValue(true)
if (webFile.isSupported) {
val book = LocalBook.importFileOnLine(webFile.url, webFile.name, bookSource)
changeToLocalBook(book)
} else {
LocalBook.saveBookFile(webFile.url, webFile.name, bookSource)
}
}.onSuccess {
@Suppress("unchecked_cast")
success?.invoke(it as T)
}.onError {
when (it) {
is NoBooksDirException -> actionLive.postValue("selectBooksDir")
else -> {
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) {
@ -289,7 +296,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
fileDoc.uri.inputStream(context).getOrThrow(),
fileDoc.name
)
LocalBook.importFile(uri).let { changeToLocalBook(it) }
changeToLocalBook(LocalBook.importFile(uri))
}
fun changeTo(source: BookSource, book: Book, toc: List<BookChapter>) {

View File

@ -1081,4 +1081,5 @@
<string name="server_config">服务器配置</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="no_books_dir">没有设置书籍保存位置!</string>
</resources>

View File

@ -1084,4 +1084,5 @@
<string name="server_config">服务器配置</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="no_books_dir">没有设置书籍保存位置!</string>
</resources>

View File

@ -1084,4 +1084,5 @@
<string name="server_config">服务器配置</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="no_books_dir">没有设置书籍保存位置!</string>
</resources>

View File

@ -1081,4 +1081,5 @@
<string name="server_config">服务器配置</string>
<string name="sure_upload">远程webDav链接已存在是否继续</string>
<string name="unsupport_archivefile_entry">压缩文件内没有支持的文件</string>
<string name="no_books_dir">没有设置书籍保存位置!</string>
</resources>

View File

@ -1083,4 +1083,5 @@
<string name="server_config">服务器配置</string>
<string name="sure_upload">远程webDav链接已存在是否继续</string>
<string name="unsupport_archivefile_entry">压缩文件内没有支持的文件</string>
<string name="no_books_dir">没有设置书籍保存位置!</string>
</resources>

View File

@ -1083,4 +1083,5 @@
<string name="server_config">服务器配置</string>
<string name="sure_upload">远程webDav链接已存在是否继续</string>
<string name="unsupport_archivefile_entry">压缩文件内没有支持的文件</string>
<string name="no_books_dir">没有设置书籍保存位置!</string>
</resources>

View File

@ -1084,4 +1084,5 @@
<string name="server_config">服务器配置</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="no_books_dir">没有设置书籍保存位置!</string>
</resources>