优化
This commit is contained in:
@@ -19,6 +19,7 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding
|
|||||||
import kotlinx.coroutines.Dispatchers.IO
|
import kotlinx.coroutines.Dispatchers.IO
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import splitties.init.appCtx
|
||||||
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
@@ -45,24 +46,7 @@ class FileAssociationActivity :
|
|||||||
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
||||||
binding.rotateLoading.show()
|
binding.rotateLoading.show()
|
||||||
viewModel.importBookLiveData.observe(this) { uri ->
|
viewModel.importBookLiveData.observe(this) { uri ->
|
||||||
if (uri.isContentScheme()) {
|
importBook(uri)
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
viewModel.onLineImportLive.observe(this) {
|
viewModel.onLineImportLive.observe(this) {
|
||||||
startActivity<OnLineImportActivity> {
|
startActivity<OnLineImportActivity> {
|
||||||
@@ -88,7 +72,7 @@ class FileAssociationActivity :
|
|||||||
ImportThemeDialog(it.second, true)
|
ImportThemeDialog(it.second, true)
|
||||||
)
|
)
|
||||||
"txtRule" -> showDialogFragment(
|
"txtRule" -> showDialogFragment(
|
||||||
ImportTxtRuleDialog(it.second, true)
|
ImportTxtTocRuleDialog(it.second, true)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,11 +88,43 @@ class FileAssociationActivity :
|
|||||||
}
|
}
|
||||||
finish()
|
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 ->
|
intent.data?.let { data ->
|
||||||
viewModel.dispatchIndent(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) {
|
private fun importBook(treeUri: Uri, uri: Uri) {
|
||||||
launch {
|
launch {
|
||||||
runCatching {
|
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 android.net.Uri
|
||||||
import androidx.documentfile.provider.DocumentFile
|
import androidx.documentfile.provider.DocumentFile
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import io.legado.app.R
|
|
||||||
import io.legado.app.constant.AppPattern.bookFileRegex
|
import io.legado.app.constant.AppPattern.bookFileRegex
|
||||||
import io.legado.app.exception.NoStackTraceException
|
import io.legado.app.exception.NoStackTraceException
|
||||||
import io.legado.app.lib.dialogs.alert
|
|
||||||
import io.legado.app.model.localBook.LocalBook
|
import io.legado.app.model.localBook.LocalBook
|
||||||
import io.legado.app.utils.isJson
|
import io.legado.app.utils.isJson
|
||||||
import io.legado.app.utils.printOnDebug
|
import io.legado.app.utils.printOnDebug
|
||||||
import io.legado.app.utils.readText
|
import io.legado.app.utils.readText
|
||||||
import splitties.init.appCtx
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
class FileAssociationViewModel(application: Application) : BaseAssociationViewModel(application) {
|
class FileAssociationViewModel(application: Application) : BaseAssociationViewModel(application) {
|
||||||
val importBookLiveData = MutableLiveData<Uri>()
|
val importBookLiveData = MutableLiveData<Uri>()
|
||||||
val onLineImportLive = MutableLiveData<Uri>()
|
val onLineImportLive = MutableLiveData<Uri>()
|
||||||
val openBookLiveData = MutableLiveData<String>()
|
val openBookLiveData = MutableLiveData<String>()
|
||||||
|
val notSupportedLiveData = MutableLiveData<Pair<Uri, String>>()
|
||||||
|
|
||||||
@Suppress("BlockingMethodInNonBlockingContext")
|
@Suppress("BlockingMethodInNonBlockingContext")
|
||||||
fun dispatchIndent(uri: Uri) {
|
fun dispatchIndent(uri: Uri) {
|
||||||
@@ -40,19 +38,11 @@ class FileAssociationViewModel(application: Application) : BaseAssociationViewMo
|
|||||||
content.isJson() -> {
|
content.isJson() -> {
|
||||||
importJson(content)
|
importJson(content)
|
||||||
}
|
}
|
||||||
!fileName.matches(bookFileRegex) -> {
|
fileName.matches(bookFileRegex) -> {
|
||||||
appCtx.alert(
|
|
||||||
title = appCtx.getString(R.string.draw),
|
|
||||||
message = appCtx.getString(R.string.file_not_supported, fileName)
|
|
||||||
) {
|
|
||||||
okButton {
|
|
||||||
importBookLiveData.postValue(uri)
|
importBookLiveData.postValue(uri)
|
||||||
}
|
}
|
||||||
cancelButton()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else -> {
|
else -> {
|
||||||
importBookLiveData.postValue(uri)
|
notSupportedLiveData.postValue(Pair(uri, fileName))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+2
-2
@@ -25,7 +25,7 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding
|
|||||||
import io.legado.app.utils.visible
|
import io.legado.app.utils.visible
|
||||||
import splitties.views.onClick
|
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() {
|
constructor(source: String, finishOnDismiss: Boolean = false) : this() {
|
||||||
arguments = Bundle().apply {
|
arguments = Bundle().apply {
|
||||||
@@ -35,7 +35,7 @@ class ImportTxtRuleDialog() : BaseDialogFragment(R.layout.dialog_recycler_view)
|
|||||||
}
|
}
|
||||||
|
|
||||||
private val binding by viewBinding(DialogRecyclerViewBinding::bind)
|
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()) }
|
private val adapter by lazy { SourcesAdapter(requireContext()) }
|
||||||
|
|
||||||
override fun onStart() {
|
override fun onStart() {
|
||||||
+1
-1
@@ -12,7 +12,7 @@ import io.legado.app.help.http.okHttpClient
|
|||||||
import io.legado.app.help.http.text
|
import io.legado.app.help.http.text
|
||||||
import io.legado.app.utils.*
|
import io.legado.app.utils.*
|
||||||
|
|
||||||
class ImportTxtRuleViewModel(app: Application) : BaseViewModel(app) {
|
class ImportTxtTocRuleViewModel(app: Application) : BaseViewModel(app) {
|
||||||
|
|
||||||
val errorLiveData = MutableLiveData<String>()
|
val errorLiveData = MutableLiveData<String>()
|
||||||
val successLiveData = MutableLiveData<Int>()
|
val successLiveData = MutableLiveData<Int>()
|
||||||
@@ -38,7 +38,7 @@ class OnLineImportActivity :
|
|||||||
ImportThemeDialog(it.second, true)
|
ImportThemeDialog(it.second, true)
|
||||||
)
|
)
|
||||||
"txtRule" -> showDialogFragment(
|
"txtRule" -> showDialogFragment(
|
||||||
ImportTxtRuleDialog(it.second, true)
|
ImportTxtTocRuleDialog(it.second, true)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -61,15 +61,15 @@ class OnLineImportActivity :
|
|||||||
"/replaceRule" -> showDialogFragment(
|
"/replaceRule" -> showDialogFragment(
|
||||||
ImportReplaceRuleDialog(url, true)
|
ImportReplaceRuleDialog(url, true)
|
||||||
)
|
)
|
||||||
"/textTocRule" -> viewModel.getText(url) { json ->
|
"/textTocRule" -> showDialogFragment(
|
||||||
viewModel.importTextTocRule(json, this::finallyDialog)
|
ImportTxtTocRuleDialog(url, true)
|
||||||
}
|
)
|
||||||
"/httpTTS" -> showDialogFragment(
|
"/httpTTS" -> showDialogFragment(
|
||||||
ImportHttpTtsDialog(url, true)
|
ImportHttpTtsDialog(url, true)
|
||||||
)
|
)
|
||||||
"/theme" -> viewModel.getText(url) { json ->
|
"/theme" -> showDialogFragment(
|
||||||
viewModel.importTheme(json, this::finallyDialog)
|
ImportThemeDialog(url, true)
|
||||||
}
|
)
|
||||||
"/readConfig" -> viewModel.getBytes(url) { bytes ->
|
"/readConfig" -> viewModel.getBytes(url) { bytes ->
|
||||||
viewModel.importReadConfig(bytes, this::finallyDialog)
|
viewModel.importReadConfig(bytes, this::finallyDialog)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user