mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -131,7 +131,7 @@ fun Book.getLocalUri(): Uri {
|
||||
if (!treeFileDoc.exists()) {
|
||||
appCtx.toastOnUi("书籍保存目录失效,请重新设置!")
|
||||
} else {
|
||||
val fileDoc = treeFileDoc.find(originName, 5)
|
||||
val fileDoc = treeFileDoc.find(originName, 5, 100)
|
||||
if (fileDoc != null) {
|
||||
localUriCache[bookUrl] = fileDoc.uri
|
||||
//更新bookUrl 重启不用再找一遍
|
||||
@@ -150,7 +150,7 @@ fun Book.getLocalUri(): Uri {
|
||||
Uri.fromFile(File(importBookDir))
|
||||
}
|
||||
val treeFileDoc = FileDoc.fromUri(treeUri, true)
|
||||
val fileDoc = treeFileDoc.find(originName, 5)
|
||||
val fileDoc = treeFileDoc.find(originName, 5, 100)
|
||||
if (fileDoc != null) {
|
||||
localUriCache[bookUrl] = fileDoc.uri
|
||||
bookUrl = fileDoc.toString()
|
||||
|
||||
@@ -15,6 +15,7 @@ import io.legado.app.R
|
||||
import io.legado.app.constant.PreferKey
|
||||
import io.legado.app.data.appDb
|
||||
import io.legado.app.databinding.DialogEditTextBinding
|
||||
import io.legado.app.help.book.update
|
||||
import io.legado.app.help.config.AppConfig
|
||||
import io.legado.app.lib.dialogs.alert
|
||||
import io.legado.app.lib.permission.Permissions
|
||||
@@ -304,6 +305,11 @@ class ImportBookActivity : BaseImportBookActivity<ImportBookViewModel>(),
|
||||
override fun startRead(fileDoc: FileDoc) {
|
||||
if (!ArchiveUtils.isArchive(fileDoc.name)) {
|
||||
appDb.bookDao.getBookByFileName(fileDoc.name)?.let {
|
||||
val filePath = fileDoc.toString()
|
||||
if (it.bookUrl != filePath) {
|
||||
it.bookUrl = filePath
|
||||
it.update()
|
||||
}
|
||||
startReadBook(it)
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -17,6 +17,7 @@ import java.io.File
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.nio.charset.Charset
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
|
||||
|
||||
data class FileDoc(
|
||||
@@ -166,7 +167,8 @@ fun FileDoc.list(filter: FileDocFilter? = null): ArrayList<FileDoc>? {
|
||||
do {
|
||||
val item = FileDoc(
|
||||
name = cursor.getString(nci),
|
||||
isDir = cursor.getString(mci) == DocumentsContract.Document.MIME_TYPE_DIR,
|
||||
isDir = cursor.getString(mci) ==
|
||||
DocumentsContract.Document.MIME_TYPE_DIR,
|
||||
size = cursor.getLong(sci),
|
||||
lastModified = cursor.getLong(dci),
|
||||
uri = DocumentsContract.buildDocumentUriUsingTree(
|
||||
@@ -216,6 +218,39 @@ fun FileDoc.find(name: String, depth: Int = 0): FileDoc? {
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找文档, 如果存在则返回文档,如果不存在返回空
|
||||
* @param name 文件名
|
||||
* @param depth 查找文件夹深度
|
||||
* @param maxFinds 最大查找文件夹数量
|
||||
*/
|
||||
fun FileDoc.find(name: String, depth: Int = 0, maxFinds: Int = Int.MAX_VALUE): FileDoc? {
|
||||
return find(name, depth, AtomicInteger(maxFinds))
|
||||
}
|
||||
|
||||
private fun FileDoc.find(name: String, depth: Int, maxFinds: AtomicInteger): FileDoc? {
|
||||
if (maxFinds.getAndDecrement() <= 0) {
|
||||
return null
|
||||
}
|
||||
val list = list()
|
||||
list?.forEach {
|
||||
if (it.name == name) {
|
||||
return it
|
||||
}
|
||||
}
|
||||
if (depth > 0) {
|
||||
list?.forEach {
|
||||
if (it.isDir) {
|
||||
val fileDoc = it.find(name, depth - 1, maxFinds)
|
||||
if (fileDoc != null) {
|
||||
return fileDoc
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun FileDoc.createFileIfNotExist(
|
||||
fileName: String,
|
||||
vararg subDirs: String
|
||||
|
||||
Reference in New Issue
Block a user