Merge pull request #2054 from 821938089/fix-bookimport-and-opt

修复添加本地页面选择文件夹后目录没有立刻更新的bug和优化错误提示
pull/2060/head
kunfei 2 years ago committed by GitHub
commit 5c549b7468
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt
  2. 8
      app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt
  3. 4
      app/src/main/java/io/legado/app/ui/book/local/ImportBookViewModel.kt

@ -102,7 +102,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
val filename = "${getExportFileName(book)}.txt" val filename = "${getExportFileName(book)}.txt"
DocumentUtils.delete(doc, filename) DocumentUtils.delete(doc, filename)
val bookDoc = DocumentUtils.createFileIfNotExist(doc, filename) val bookDoc = DocumentUtils.createFileIfNotExist(doc, filename)
?: throw NoStackTraceException("创建文档失败") ?: throw NoStackTraceException("创建文档失败,请尝试重新设置导出文件夹")
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
context.contentResolver.openOutputStream(bookDoc.uri, "wa")?.use { bookOs -> context.contentResolver.openOutputStream(bookDoc.uri, "wa")?.use { bookOs ->
getAllContents(scope, book) { text, srcList -> getAllContents(scope, book) { text, srcList ->

@ -48,10 +48,10 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV
it.uri?.let { uri -> it.uri?.let { uri ->
if (uri.isContentScheme()) { if (uri.isContentScheme()) {
AppConfig.importBookPath = uri.toString() AppConfig.importBookPath = uri.toString()
initRootDoc() initRootDoc(true)
} else { } else {
AppConfig.importBookPath = uri.path AppConfig.importBookPath = uri.path
initRootDoc() initRootDoc(true)
} }
} }
} }
@ -146,10 +146,10 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV
} }
} }
private fun initRootDoc() { private fun initRootDoc(changedFolder: Boolean = false) {
val lastPath = AppConfig.importBookPath val lastPath = AppConfig.importBookPath
when { when {
viewModel.rootDoc != null -> upPath() viewModel.rootDoc != null && !changedFolder -> upPath()
lastPath.isNullOrEmpty() -> { lastPath.isNullOrEmpty() -> {
binding.tvEmptyMsg.visible() binding.tvEmptyMsg.visible()
selectFolder.launch() selectFolder.launch()

@ -4,6 +4,7 @@ import android.app.Application
import android.net.Uri import android.net.Uri
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppLog
import io.legado.app.constant.AppPattern.bookFileRegex import io.legado.app.constant.AppPattern.bookFileRegex
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.model.localBook.LocalBook import io.legado.app.model.localBook.LocalBook
@ -76,6 +77,9 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
uriList.forEach { uriList.forEach {
LocalBook.importFile(Uri.parse(it)) LocalBook.importFile(Uri.parse(it))
} }
}.onError {
context.toastOnUi("添加书架失败,请尝试重新选择文件夹")
AppLog.put("添加书架失败\n${it.localizedMessage}", it)
}.onFinally { }.onFinally {
finally.invoke() finally.invoke()
} }

Loading…
Cancel
Save