|
|
@ -4,7 +4,6 @@ import android.annotation.SuppressLint |
|
|
|
import android.net.Uri |
|
|
|
import android.net.Uri |
|
|
|
import android.os.Build |
|
|
|
import android.os.Build |
|
|
|
import android.os.Bundle |
|
|
|
import android.os.Bundle |
|
|
|
import android.provider.DocumentsContract |
|
|
|
|
|
|
|
import android.view.Menu |
|
|
|
import android.view.Menu |
|
|
|
import android.view.MenuItem |
|
|
|
import android.view.MenuItem |
|
|
|
import androidx.activity.viewModels |
|
|
|
import androidx.activity.viewModels |
|
|
@ -31,7 +30,6 @@ import kotlinx.coroutines.flow.collect |
|
|
|
import kotlinx.coroutines.launch |
|
|
|
import kotlinx.coroutines.launch |
|
|
|
import kotlinx.coroutines.withContext |
|
|
|
import kotlinx.coroutines.withContext |
|
|
|
import java.io.File |
|
|
|
import java.io.File |
|
|
|
import java.util.* |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 导入本地书籍界面 |
|
|
|
* 导入本地书籍界面 |
|
|
@ -43,7 +41,7 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV |
|
|
|
|
|
|
|
|
|
|
|
override val binding by viewBinding(ActivityImportBookBinding::inflate) |
|
|
|
override val binding by viewBinding(ActivityImportBookBinding::inflate) |
|
|
|
override val viewModel by viewModels<ImportBookViewModel>() |
|
|
|
override val viewModel by viewModels<ImportBookViewModel>() |
|
|
|
|
|
|
|
private val bookFileRegex = Regex("(?i).*\\.(txt|epub|umd)") |
|
|
|
private var rootDoc: DocumentFile? = null |
|
|
|
private var rootDoc: DocumentFile? = null |
|
|
|
private val subDocs = arrayListOf<DocumentFile>() |
|
|
|
private val subDocs = arrayListOf<DocumentFile>() |
|
|
|
private val adapter by lazy { ImportBookAdapter(this, this) } |
|
|
|
private val adapter by lazy { ImportBookAdapter(this, this) } |
|
|
@ -191,17 +189,11 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV |
|
|
|
adapter.clearItems() |
|
|
|
adapter.clearItems() |
|
|
|
launch(IO) { |
|
|
|
launch(IO) { |
|
|
|
runCatching { |
|
|
|
runCatching { |
|
|
|
val docList = DocumentUtils.listFiles(lastDoc.uri) |
|
|
|
val docList = DocumentUtils.listFiles(lastDoc.uri) { item -> |
|
|
|
for (i in docList.lastIndex downTo 0) { |
|
|
|
when { |
|
|
|
val item = docList[i] |
|
|
|
item.name.startsWith(".") -> false |
|
|
|
if (item.name.startsWith(".")) { |
|
|
|
item.isDir -> true |
|
|
|
docList.removeAt(i) |
|
|
|
else -> item.name.matches(bookFileRegex) |
|
|
|
} 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 })) |
|
|
|
docList.sortWith(compareBy({ !it.isDir }, { it.name })) |
|
|
@ -217,36 +209,24 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV |
|
|
|
private fun upFiles() { |
|
|
|
private fun upFiles() { |
|
|
|
binding.tvEmptyMsg.gone() |
|
|
|
binding.tvEmptyMsg.gone() |
|
|
|
binding.tvPath.text = path.replace(sdPath, "SD") |
|
|
|
binding.tvPath.text = path.replace(sdPath, "SD") |
|
|
|
val docList = arrayListOf<DocItem>() |
|
|
|
adapter.clearItems() |
|
|
|
File(path).listFiles()?.forEach { |
|
|
|
launch(IO) { |
|
|
|
if (it.isDirectory) { |
|
|
|
kotlin.runCatching { |
|
|
|
if (!it.name.startsWith(".")) |
|
|
|
val docList = DocumentUtils.listFiles(path) { |
|
|
|
docList.add( |
|
|
|
when { |
|
|
|
DocItem( |
|
|
|
it.name.startsWith(".") -> false |
|
|
|
it.name, |
|
|
|
it.isDirectory -> true |
|
|
|
DocumentsContract.Document.MIME_TYPE_DIR, |
|
|
|
else -> it.name.matches(bookFileRegex) |
|
|
|
it.length(), |
|
|
|
} |
|
|
|
Date(it.lastModified()), |
|
|
|
} |
|
|
|
Uri.fromFile(it) |
|
|
|
docList.sortWith(compareBy({ !it.isDir }, { it.name })) |
|
|
|
) |
|
|
|
withContext(Main) { |
|
|
|
) |
|
|
|
adapter.setItems(docList) |
|
|
|
} else if (it.name.endsWith(".txt", true) |
|
|
|
} |
|
|
|
|| it.name.endsWith(".epub", true) |
|
|
|
}.onFailure { |
|
|
|
|| it.name.endsWith(".umd", true) |
|
|
|
toastOnUi("获取文件列表出错\n${it.localizedMessage}") |
|
|
|
) { |
|
|
|
|
|
|
|
docList.add( |
|
|
|
|
|
|
|
DocItem( |
|
|
|
|
|
|
|
it.name, |
|
|
|
|
|
|
|
it.extension, |
|
|
|
|
|
|
|
it.length(), |
|
|
|
|
|
|
|
Date(it.lastModified()), |
|
|
|
|
|
|
|
Uri.fromFile(it) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
docList.sortWith(compareBy({ !it.isDir }, { it.name })) |
|
|
|
|
|
|
|
adapter.setItems(docList) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -298,7 +278,7 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private val find: (docItem: DocItem) -> Unit = { |
|
|
|
private val find: (docItem: FileDoc) -> Unit = { |
|
|
|
launch { |
|
|
|
launch { |
|
|
|
adapter.addItem(it) |
|
|
|
adapter.addItem(it) |
|
|
|
} |
|
|
|
} |
|
|
|