mirror of
https://github.com/gedoor/legado.git
synced 2024-09-01 09:34:25 +08:00
优化
This commit is contained in:
parent
468c574c25
commit
92ec2d1057
@ -190,22 +190,26 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV
|
||||
adapter.selectedUris.clear()
|
||||
adapter.clearItems()
|
||||
launch(IO) {
|
||||
val docList = DocumentUtils.listFiles(lastDoc.uri)
|
||||
for (i in docList.lastIndex downTo 0) {
|
||||
val item = docList[i]
|
||||
if (item.name.startsWith(".")) {
|
||||
docList.removeAt(i)
|
||||
} else if (!item.isDir
|
||||
&& !item.name.endsWith(".txt", true)
|
||||
&& !item.name.endsWith(".epub", true)
|
||||
&& !item.name.endsWith(".umd", true)
|
||||
) {
|
||||
docList.removeAt(i)
|
||||
runCatching {
|
||||
val docList = DocumentUtils.listFiles(lastDoc.uri)
|
||||
for (i in docList.lastIndex downTo 0) {
|
||||
val item = docList[i]
|
||||
if (item.name.startsWith(".")) {
|
||||
docList.removeAt(i)
|
||||
} else if (!item.isDir
|
||||
&& !item.name.endsWith(".txt", true)
|
||||
&& !item.name.endsWith(".epub", true)
|
||||
&& !item.name.endsWith(".umd", true)
|
||||
) {
|
||||
docList.removeAt(i)
|
||||
}
|
||||
}
|
||||
}
|
||||
docList.sortWith(compareBy({ !it.isDir }, { it.name }))
|
||||
withContext(Main) {
|
||||
adapter.setItems(docList)
|
||||
docList.sortWith(compareBy({ !it.isDir }, { it.name }))
|
||||
withContext(Main) {
|
||||
adapter.setItems(docList)
|
||||
}
|
||||
}.onFailure {
|
||||
toastOnUi("获取文件列表出错\n${it.localizedMessage}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import io.legado.app.model.localBook.LocalBook
|
||||
import io.legado.app.utils.DocItem
|
||||
import io.legado.app.utils.DocumentUtils
|
||||
import io.legado.app.utils.isContentScheme
|
||||
import io.legado.app.utils.toastOnUi
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
|
||||
@ -47,17 +48,21 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
|
||||
find: (docItem: DocItem) -> Unit,
|
||||
finally: (() -> Unit)? = null
|
||||
) {
|
||||
val docList = DocumentUtils.listFiles(documentFile.uri)
|
||||
docList.forEach { docItem ->
|
||||
if (docItem.isDir) {
|
||||
DocumentFile.fromSingleUri(context, docItem.uri)?.let {
|
||||
scanDoc(it, false, find)
|
||||
kotlin.runCatching {
|
||||
val docList = DocumentUtils.listFiles(documentFile.uri)
|
||||
docList.forEach { docItem ->
|
||||
if (docItem.isDir) {
|
||||
DocumentFile.fromSingleUri(context, docItem.uri)?.let {
|
||||
scanDoc(it, false, find)
|
||||
}
|
||||
} else if (docItem.name.endsWith(".txt", true)
|
||||
|| docItem.name.endsWith(".epub", true)
|
||||
) {
|
||||
find(docItem)
|
||||
}
|
||||
} else if (docItem.name.endsWith(".txt", true)
|
||||
|| docItem.name.endsWith(".epub", true)
|
||||
) {
|
||||
find(docItem)
|
||||
}
|
||||
}.onFailure {
|
||||
context.toastOnUi("扫描文件夹出错\n${it.localizedMessage}")
|
||||
}
|
||||
if (isRoot) {
|
||||
finally?.invoke()
|
||||
|
@ -7,7 +7,6 @@ import android.provider.DocumentsContract
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import io.legado.app.model.NoStackTraceException
|
||||
import splitties.init.appCtx
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import java.nio.charset.Charset
|
||||
import java.util.*
|
||||
@ -95,6 +94,7 @@ object DocumentUtils {
|
||||
} ?: throw NoStackTraceException("打开文件失败\n${uri}")
|
||||
}
|
||||
|
||||
@Throws(Exception::class)
|
||||
fun listFiles(uri: Uri, regex: Regex? = null): ArrayList<DocItem> {
|
||||
val docList = arrayListOf<DocItem>()
|
||||
var cursor: Cursor? = null
|
||||
@ -133,33 +133,30 @@ object DocumentUtils {
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e)
|
||||
} finally {
|
||||
cursor?.close()
|
||||
}
|
||||
return docList
|
||||
}
|
||||
|
||||
@Throws(Exception::class)
|
||||
fun listFiles(path: String, regex: Regex? = null): ArrayList<DocItem> {
|
||||
val docItems = arrayListOf<DocItem>()
|
||||
kotlin.runCatching {
|
||||
val file = File(path)
|
||||
file.listFiles { pathName ->
|
||||
regex?.let {
|
||||
pathName.name.matches(it)
|
||||
} ?: true
|
||||
}?.forEach {
|
||||
docItems.add(
|
||||
DocItem(
|
||||
it.name,
|
||||
it.extension,
|
||||
it.length(),
|
||||
Date(it.lastModified()),
|
||||
Uri.parse(it.absolutePath)
|
||||
)
|
||||
val file = File(path)
|
||||
file.listFiles { pathName ->
|
||||
regex?.let {
|
||||
pathName.name.matches(it)
|
||||
} ?: true
|
||||
}?.forEach {
|
||||
docItems.add(
|
||||
DocItem(
|
||||
it.name,
|
||||
it.extension,
|
||||
it.length(),
|
||||
Date(it.lastModified()),
|
||||
Uri.parse(it.absolutePath)
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
return docItems
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user