mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -3,16 +3,13 @@
|
||||
package io.legado.app.help.book
|
||||
|
||||
import android.net.Uri
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import io.legado.app.constant.BookSourceType
|
||||
import io.legado.app.constant.BookType
|
||||
import io.legado.app.data.entities.Book
|
||||
import io.legado.app.data.entities.BookSource
|
||||
import io.legado.app.exception.NoStackTraceException
|
||||
import io.legado.app.help.config.AppConfig.defaultBookTreeUri
|
||||
import io.legado.app.utils.getFile
|
||||
import io.legado.app.utils.isContentScheme
|
||||
import splitties.init.appCtx
|
||||
import io.legado.app.utils.*
|
||||
import java.io.File
|
||||
|
||||
|
||||
@@ -64,17 +61,8 @@ fun Book.getLocalUri(): Uri {
|
||||
//不同的设备书籍保存路径可能不一样 优先尝试寻找当前保存路径下的文件
|
||||
defaultBookTreeUri ?: return originBookUri
|
||||
val treeUri = Uri.parse(defaultBookTreeUri)
|
||||
return if (treeUri.isContentScheme()) {
|
||||
DocumentFile.fromTreeUri(appCtx, treeUri)?.run {
|
||||
findFile(originName)?.let {
|
||||
if (it.exists()) it.uri else originBookUri
|
||||
} ?: originBookUri
|
||||
} ?: originBookUri
|
||||
} else {
|
||||
val treeFile = File(treeUri.path!!)
|
||||
val file = treeFile.getFile(originName)
|
||||
if (file.exists()) Uri.fromFile(file) else originBookUri
|
||||
}
|
||||
val treeFileDoc = FileDoc.fromUri(treeUri, true)
|
||||
return treeFileDoc.find(originName)?.uri ?: originBookUri
|
||||
}
|
||||
throw NoStackTraceException("不是本地书籍")
|
||||
}
|
||||
|
||||
@@ -145,6 +145,10 @@ data class FileDoc(
|
||||
|
||||
companion object {
|
||||
|
||||
fun fromUri(uri: Uri, isDir: Boolean, name: String = ""): FileDoc {
|
||||
return FileDoc(name, isDir, 0, 0, uri)
|
||||
}
|
||||
|
||||
fun fromDocumentFile(doc: DocumentFile): FileDoc {
|
||||
return FileDoc(
|
||||
name = doc.name ?: "",
|
||||
@@ -168,6 +172,19 @@ data class FileDoc(
|
||||
}
|
||||
}
|
||||
|
||||
fun FileDoc.list(filter: ((file: FileDoc) -> Boolean)? = null): ArrayList<FileDoc>? {
|
||||
if (isDir) {
|
||||
return DocumentUtils.listFiles(uri, filter)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun FileDoc.find(name: String): FileDoc? {
|
||||
return list {
|
||||
it.name == name
|
||||
}?.firstOrNull()
|
||||
}
|
||||
|
||||
@Throws(Exception::class)
|
||||
fun DocumentFile.writeText(context: Context, data: String, charset: Charset = Charsets.UTF_8) {
|
||||
uri.writeText(context, data, charset)
|
||||
|
||||
Reference in New Issue
Block a user