This commit is contained in:
Horis
2025-12-21 11:01:32 +08:00
parent 4d2bc46caf
commit e8d876b642
2 changed files with 32 additions and 24 deletions
@@ -21,8 +21,8 @@ import io.legado.app.utils.applyTint
import io.legado.app.utils.startActivityForBook import io.legado.app.utils.startActivityForBook
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
abstract class BaseImportBookActivity<VM : ViewModel> : abstract class BaseImportBookActivity<VM : ViewModel> :
VMBaseActivity<ActivityImportBookBinding, VM>() { VMBaseActivity<ActivityImportBookBinding, VM>() {
@@ -49,7 +49,7 @@ abstract class BaseImportBookActivity<VM : ViewModel> :
/** /**
* 设置书籍保存位置 * 设置书籍保存位置
*/ */
protected suspend fun setBookStorage() = suspendCoroutine { block -> protected suspend fun setBookStorage() = suspendCancellableCoroutine sc@{ block ->
localBookTreeSelectListener = { localBookTreeSelectListener = {
localBookTreeSelectListener = null localBookTreeSelectListener = null
block.resume(it) block.resume(it)
@@ -58,7 +58,7 @@ abstract class BaseImportBookActivity<VM : ViewModel> :
if (!AppConfig.defaultBookTreeUri.isNullOrBlank()) { if (!AppConfig.defaultBookTreeUri.isNullOrBlank()) {
localBookTreeSelectListener = null localBookTreeSelectListener = null
block.resume(true) block.resume(true)
return@suspendCoroutine return@sc
} }
//测试读写?? //测试读写??
val storageHelp = String(assets.open("storageHelp.md").readBytes()) val storageHelp = String(assets.open("storageHelp.md").readBytes())
@@ -8,6 +8,7 @@ import android.view.MenuItem
import androidx.activity.addCallback import androidx.activity.addCallback
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.PopupMenu
import androidx.core.net.toUri
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
@@ -166,34 +167,35 @@ class ImportBookActivity : BaseImportBookActivity<ImportBookViewModel>(),
selectFolder.launch() selectFolder.launch()
} else { } else {
val rootUri = if (lastPath.isUri()) { val rootUri = if (lastPath.isUri()) {
Uri.parse(lastPath) lastPath.toUri()
} else { } else {
Uri.fromFile(File(lastPath)) Uri.fromFile(File(lastPath))
} }
when { when {
rootUri.isContentScheme() -> { rootUri.isContentScheme() -> initRootPath(rootUri)
kotlin.runCatching {
val doc = DocumentFile.fromTreeUri(this, rootUri)
if (doc == null || doc.name.isNullOrEmpty()) {
binding.tvEmptyMsg.visible()
selectFolder.launch()
} else {
viewModel.subDocs.clear()
viewModel.rootDoc = FileDoc.fromDocumentFile(doc)
upDocs(viewModel.rootDoc!!)
}
}.onFailure {
binding.tvEmptyMsg.visible()
selectFolder.launch()
}
}
else -> initRootPath(rootUri.path!!) else -> initRootPath(rootUri.path!!)
} }
} }
} }
} }
private fun initRootPath(rootUri: Uri) {
kotlin.runCatching {
val doc = DocumentFile.fromTreeUri(this, rootUri)
if (doc == null || doc.name.isNullOrEmpty()) {
binding.tvEmptyMsg.visible()
selectFolder.launch()
} else {
viewModel.subDocs.clear()
viewModel.rootDoc = FileDoc.fromDocumentFile(doc)
upPath()
}
}.onFailure {
binding.tvEmptyMsg.visible()
selectFolder.launch()
}
}
private fun initRootPath(path: String) { private fun initRootPath(path: String) {
binding.tvEmptyMsg.visible() binding.tvEmptyMsg.visible()
PermissionsCompat.Builder() PermissionsCompat.Builder()
@@ -201,9 +203,15 @@ class ImportBookActivity : BaseImportBookActivity<ImportBookViewModel>(),
.rationale(R.string.tip_perm_request_storage) .rationale(R.string.tip_perm_request_storage)
.onGranted { .onGranted {
kotlin.runCatching { kotlin.runCatching {
viewModel.rootDoc = FileDoc.fromFile(File(path)) val file = File(path)
viewModel.subDocs.clear() if (!file.isDirectory) {
upPath() binding.tvEmptyMsg.visible()
selectFolder.launch()
} else {
viewModel.subDocs.clear()
viewModel.rootDoc = FileDoc.fromFile(file)
upPath()
}
}.onFailure { }.onFailure {
binding.tvEmptyMsg.visible() binding.tvEmptyMsg.visible()
selectFolder.launch() selectFolder.launch()