Merge pull request #2448 from 821938089/little-fix

优化本地书籍查找
pull/2449/head
Horis 2 years ago committed by GitHub
commit aec35d20c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 39
      app/src/main/java/io/legado/app/help/book/BookExtensions.kt
  2. 2
      app/src/main/java/io/legado/app/help/config/AppConfig.kt
  3. 8
      app/src/main/java/io/legado/app/utils/FileDocExtensions.kt

@ -66,7 +66,9 @@ private val localUriCache by lazy {
}
fun Book.getLocalUri(): Uri {
if (isLocal) {
if (!isLocal) {
throw NoStackTraceException("不是本地书籍")
}
var uri = localUriCache[bookUrl]
if (uri != null) {
return uri
@ -84,13 +86,16 @@ fun Book.getLocalUri(): Uri {
}
//不同的设备书籍保存路径可能不一样, uri无效时尝试寻找当前保存路径下的文件
val defaultBookDir = AppConfig.defaultBookTreeUri
if (defaultBookDir.isNullOrBlank()) {
localUriCache[bookUrl] = uri
return uri
}
val importBookDir = AppConfig.importBookPath
// 查找书籍保存目录
if (!defaultBookDir.isNullOrBlank()) {
val treeUri = Uri.parse(defaultBookDir)
val treeFileDoc = FileDoc.fromUri(treeUri, true)
val fileDoc = treeFileDoc.find(originName, 3)
if (!treeFileDoc.exists()) {
appCtx.toastOnUi("书籍保存目录失效,请重新设置!")
} else {
val fileDoc = treeFileDoc.find(originName, 5)
if (fileDoc != null) {
localUriCache[bookUrl] = fileDoc.uri
//更新bookUrl 重启不用再找一遍
@ -98,10 +103,28 @@ fun Book.getLocalUri(): Uri {
save()
return fileDoc.uri
}
}
}
// 查找添加本地选择的目录
if (!importBookDir.isNullOrBlank()) {
val treeUri = if (importBookDir.isUri()) {
Uri.parse(importBookDir)
} else {
Uri.fromFile(File(importBookDir))
}
val treeFileDoc = FileDoc.fromUri(treeUri, true)
val fileDoc = treeFileDoc.find(originName, 5)
if (fileDoc != null) {
localUriCache[bookUrl] = fileDoc.uri
bookUrl = fileDoc.toString()
save()
return fileDoc.uri
}
}
localUriCache[bookUrl] = uri
return uri
}
throw NoStackTraceException("不是本地书籍")
}
fun Book.cacheLocalUri(uri: Uri) {

@ -135,6 +135,7 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
}
}
// 书籍保存位置
var defaultBookTreeUri: String?
get() = appCtx.getPrefString(PreferKey.defaultBookTreeUri)
set(value) {
@ -166,6 +167,7 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
appCtx.putPrefInt(PreferKey.threadCount, value)
}
// 添加本地选择的目录
var importBookPath: String?
get() = appCtx.getPrefString("importBookPath")
set(value) {

@ -221,6 +221,14 @@ fun FileDoc.exists(
}
}
fun FileDoc.exists(): Boolean {
return if (uri.isContentScheme()) {
DocumentFile.fromTreeUri(appCtx, uri)!!.exists()
} else {
FileUtils.exist(uri.path!!)
}
}
/**
* DocumentFile listFiles() 非常的慢,尽量不要使用
*/

Loading…
Cancel
Save