mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -19,6 +19,7 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import splitties.init.appCtx
|
||||
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
@@ -45,24 +46,7 @@ class FileAssociationActivity :
|
||||
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
||||
binding.rotateLoading.show()
|
||||
viewModel.importBookLiveData.observe(this) { uri ->
|
||||
if (uri.isContentScheme()) {
|
||||
val treeUriStr = AppConfig.defaultBookTreeUri
|
||||
if (treeUriStr.isNullOrEmpty()) {
|
||||
localBookTreeSelect.launch {
|
||||
title = "选择保存书籍的文件夹"
|
||||
mode = HandleFileContract.DIR_SYS
|
||||
}
|
||||
} else {
|
||||
importBook(Uri.parse(treeUriStr), uri)
|
||||
}
|
||||
} else {
|
||||
PermissionsCompat.Builder(this)
|
||||
.addPermissions(*Permissions.Group.STORAGE)
|
||||
.rationale(R.string.tip_perm_request_storage)
|
||||
.onGranted {
|
||||
viewModel.importBook(uri)
|
||||
}.request()
|
||||
}
|
||||
importBook(uri)
|
||||
}
|
||||
viewModel.onLineImportLive.observe(this) {
|
||||
startActivity<OnLineImportActivity> {
|
||||
@@ -88,7 +72,7 @@ class FileAssociationActivity :
|
||||
ImportThemeDialog(it.second, true)
|
||||
)
|
||||
"txtRule" -> showDialogFragment(
|
||||
ImportTxtRuleDialog(it.second, true)
|
||||
ImportTxtTocRuleDialog(it.second, true)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -104,11 +88,43 @@ class FileAssociationActivity :
|
||||
}
|
||||
finish()
|
||||
}
|
||||
viewModel.notSupportedLiveData.observe(this) { data ->
|
||||
appCtx.alert(
|
||||
title = appCtx.getString(R.string.draw),
|
||||
message = appCtx.getString(R.string.file_not_supported, data.second)
|
||||
) {
|
||||
okButton {
|
||||
importBook(data.first)
|
||||
}
|
||||
cancelButton()
|
||||
}
|
||||
}
|
||||
intent.data?.let { data ->
|
||||
viewModel.dispatchIndent(data)
|
||||
}
|
||||
}
|
||||
|
||||
private fun importBook(uri: Uri) {
|
||||
if (uri.isContentScheme()) {
|
||||
val treeUriStr = AppConfig.defaultBookTreeUri
|
||||
if (treeUriStr.isNullOrEmpty()) {
|
||||
localBookTreeSelect.launch {
|
||||
title = "选择保存书籍的文件夹"
|
||||
mode = HandleFileContract.DIR_SYS
|
||||
}
|
||||
} else {
|
||||
importBook(Uri.parse(treeUriStr), uri)
|
||||
}
|
||||
} else {
|
||||
PermissionsCompat.Builder(this)
|
||||
.addPermissions(*Permissions.Group.STORAGE)
|
||||
.rationale(R.string.tip_perm_request_storage)
|
||||
.onGranted {
|
||||
viewModel.importBook(uri)
|
||||
}.request()
|
||||
}
|
||||
}
|
||||
|
||||
private fun importBook(treeUri: Uri, uri: Uri) {
|
||||
launch {
|
||||
runCatching {
|
||||
@@ -162,13 +178,4 @@ class FileAssociationActivity :
|
||||
}
|
||||
}
|
||||
|
||||
private fun finallyDialog(title: String, msg: String) {
|
||||
alert(title, msg) {
|
||||
okButton()
|
||||
onDismiss {
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,21 +4,19 @@ import android.app.Application
|
||||
import android.net.Uri
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import io.legado.app.R
|
||||
import io.legado.app.constant.AppPattern.bookFileRegex
|
||||
import io.legado.app.exception.NoStackTraceException
|
||||
import io.legado.app.lib.dialogs.alert
|
||||
import io.legado.app.model.localBook.LocalBook
|
||||
import io.legado.app.utils.isJson
|
||||
import io.legado.app.utils.printOnDebug
|
||||
import io.legado.app.utils.readText
|
||||
import splitties.init.appCtx
|
||||
import java.io.File
|
||||
|
||||
class FileAssociationViewModel(application: Application) : BaseAssociationViewModel(application) {
|
||||
val importBookLiveData = MutableLiveData<Uri>()
|
||||
val onLineImportLive = MutableLiveData<Uri>()
|
||||
val openBookLiveData = MutableLiveData<String>()
|
||||
val notSupportedLiveData = MutableLiveData<Pair<Uri, String>>()
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
fun dispatchIndent(uri: Uri) {
|
||||
@@ -40,19 +38,11 @@ class FileAssociationViewModel(application: Application) : BaseAssociationViewMo
|
||||
content.isJson() -> {
|
||||
importJson(content)
|
||||
}
|
||||
!fileName.matches(bookFileRegex) -> {
|
||||
appCtx.alert(
|
||||
title = appCtx.getString(R.string.draw),
|
||||
message = appCtx.getString(R.string.file_not_supported, fileName)
|
||||
) {
|
||||
okButton {
|
||||
importBookLiveData.postValue(uri)
|
||||
}
|
||||
cancelButton()
|
||||
}
|
||||
fileName.matches(bookFileRegex) -> {
|
||||
importBookLiveData.postValue(uri)
|
||||
}
|
||||
else -> {
|
||||
importBookLiveData.postValue(uri)
|
||||
notSupportedLiveData.postValue(Pair(uri, fileName))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -25,7 +25,7 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||
import io.legado.app.utils.visible
|
||||
import splitties.views.onClick
|
||||
|
||||
class ImportTxtRuleDialog() : BaseDialogFragment(R.layout.dialog_recycler_view) {
|
||||
class ImportTxtTocRuleDialog() : BaseDialogFragment(R.layout.dialog_recycler_view) {
|
||||
|
||||
constructor(source: String, finishOnDismiss: Boolean = false) : this() {
|
||||
arguments = Bundle().apply {
|
||||
@@ -35,7 +35,7 @@ class ImportTxtRuleDialog() : BaseDialogFragment(R.layout.dialog_recycler_view)
|
||||
}
|
||||
|
||||
private val binding by viewBinding(DialogRecyclerViewBinding::bind)
|
||||
private val viewModel by viewModels<ImportTxtRuleViewModel>()
|
||||
private val viewModel by viewModels<ImportTxtTocRuleViewModel>()
|
||||
private val adapter by lazy { SourcesAdapter(requireContext()) }
|
||||
|
||||
override fun onStart() {
|
||||
@@ -12,7 +12,7 @@ import io.legado.app.help.http.okHttpClient
|
||||
import io.legado.app.help.http.text
|
||||
import io.legado.app.utils.*
|
||||
|
||||
class ImportTxtRuleViewModel(app: Application) : BaseViewModel(app) {
|
||||
class ImportTxtTocRuleViewModel(app: Application) : BaseViewModel(app) {
|
||||
|
||||
val errorLiveData = MutableLiveData<String>()
|
||||
val successLiveData = MutableLiveData<Int>()
|
||||
@@ -38,7 +38,7 @@ class OnLineImportActivity :
|
||||
ImportThemeDialog(it.second, true)
|
||||
)
|
||||
"txtRule" -> showDialogFragment(
|
||||
ImportTxtRuleDialog(it.second, true)
|
||||
ImportTxtTocRuleDialog(it.second, true)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -61,15 +61,15 @@ class OnLineImportActivity :
|
||||
"/replaceRule" -> showDialogFragment(
|
||||
ImportReplaceRuleDialog(url, true)
|
||||
)
|
||||
"/textTocRule" -> viewModel.getText(url) { json ->
|
||||
viewModel.importTextTocRule(json, this::finallyDialog)
|
||||
}
|
||||
"/textTocRule" -> showDialogFragment(
|
||||
ImportTxtTocRuleDialog(url, true)
|
||||
)
|
||||
"/httpTTS" -> showDialogFragment(
|
||||
ImportHttpTtsDialog(url, true)
|
||||
)
|
||||
"/theme" -> viewModel.getText(url) { json ->
|
||||
viewModel.importTheme(json, this::finallyDialog)
|
||||
}
|
||||
"/theme" -> showDialogFragment(
|
||||
ImportThemeDialog(url, true)
|
||||
)
|
||||
"/readConfig" -> viewModel.getBytes(url) { bytes ->
|
||||
viewModel.importReadConfig(bytes, this::finallyDialog)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user