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.toastOnUi
import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
abstract class BaseImportBookActivity<VM : ViewModel> :
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 = null
block.resume(it)
@@ -58,7 +58,7 @@ abstract class BaseImportBookActivity<VM : ViewModel> :
if (!AppConfig.defaultBookTreeUri.isNullOrBlank()) {
localBookTreeSelectListener = null
block.resume(true)
return@suspendCoroutine
return@sc
}
//测试读写??
val storageHelp = String(assets.open("storageHelp.md").readBytes())
@@ -8,6 +8,7 @@ import android.view.MenuItem
import androidx.activity.addCallback
import androidx.activity.viewModels
import androidx.appcompat.widget.PopupMenu
import androidx.core.net.toUri
import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
@@ -166,34 +167,35 @@ class ImportBookActivity : BaseImportBookActivity<ImportBookViewModel>(),
selectFolder.launch()
} else {
val rootUri = if (lastPath.isUri()) {
Uri.parse(lastPath)
lastPath.toUri()
} else {
Uri.fromFile(File(lastPath))
}
when {
rootUri.isContentScheme() -> {
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()
}
}
rootUri.isContentScheme() -> initRootPath(rootUri)
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) {
binding.tvEmptyMsg.visible()
PermissionsCompat.Builder()
@@ -201,9 +203,15 @@ class ImportBookActivity : BaseImportBookActivity<ImportBookViewModel>(),
.rationale(R.string.tip_perm_request_storage)
.onGranted {
kotlin.runCatching {
viewModel.rootDoc = FileDoc.fromFile(File(path))
viewModel.subDocs.clear()
upPath()
val file = File(path)
if (!file.isDirectory) {
binding.tvEmptyMsg.visible()
selectFolder.launch()
} else {
viewModel.subDocs.clear()
viewModel.rootDoc = FileDoc.fromFile(file)
upPath()
}
}.onFailure {
binding.tvEmptyMsg.visible()
selectFolder.launch()