From 91cd8cd3562ab46f4a45566e04921afb443e692c Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 29 Feb 2020 18:19:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/replacerule/ReplaceRuleActivity.kt | 47 ++++++++++++++----- .../ui/replacerule/ReplaceRuleViewModel.kt | 28 +++++++---- .../rss/source/manage/RssSourceViewModel.kt | 4 +- 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt index 940173bd9..7910b9cb8 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt @@ -9,6 +9,7 @@ import android.view.MenuItem import android.view.SubMenu import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.SearchView +import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.LiveData import androidx.lifecycle.Observer import androidx.recyclerview.widget.DiffUtil @@ -45,7 +46,8 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi override val viewModel: ReplaceRuleViewModel get() = getViewModel(ReplaceRuleViewModel::class.java) private val importRecordKey = "replaceRuleRecordKey" - private val importSource = 132 + private val importRequestCode = 132 + private val exportRequestCode = 65 private lateinit var adapter: ReplaceRuleAdapter private var groups = hashSetOf() private var groupMenu: SubMenu? = null @@ -176,7 +178,7 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi R.id.menu_del_selection -> viewModel.delSelection(adapter.getSelection()) R.id.menu_import_source_onLine -> showImportDialog() R.id.menu_import_source_local -> FilePicker - .selectFile(this, importSource, "text/*", arrayOf("txt", "json")) + .selectFile(this, importRequestCode, "text/*", arrayOf("txt", "json")) } return super.onCompatOptionsItemSelected(item) } @@ -185,7 +187,7 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi when (item?.itemId) { R.id.menu_enable_selection -> viewModel.enableSelection(adapter.getSelection()) R.id.menu_disable_selection -> viewModel.disableSelection(adapter.getSelection()) - R.id.menu_export_selection -> viewModel.exportSelection(adapter.getSelection()) + R.id.menu_export_selection -> FilePicker.selectFolder(this, exportRequestCode) } return false } @@ -232,15 +234,6 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi }.show().applyTint() } - override fun onFilePicked(requestCode: Int, currentPath: String) { - if (requestCode == importSource) { - Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() - viewModel.importSource(File(currentPath).readText()) { msg -> - title_bar.snackbar(msg) - } - } - } - override fun onQueryTextChange(newText: String?): Boolean { observeReplaceRuleData("%$newText%") return false @@ -250,10 +243,25 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi return false } + override fun onFilePicked(requestCode: Int, currentPath: String) { + when (requestCode) { + importRequestCode -> { + Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() + viewModel.importSource(File(currentPath).readText()) { msg -> + title_bar.snackbar(msg) + } + } + exportRequestCode -> viewModel.exportSelection( + adapter.getSelection(), + File(currentPath) + ) + } + } + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) when (requestCode) { - importSource -> if (resultCode == Activity.RESULT_OK) { + importRequestCode -> if (resultCode == Activity.RESULT_OK) { data?.data?.let { uri -> try { uri.readText(this)?.let { @@ -268,6 +276,19 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi } } } + exportRequestCode -> if (resultCode == RESULT_OK) { + data?.data?.let { uri -> + if (uri.toString().isContentPath()) { + DocumentFile.fromTreeUri(this, uri)?.let { + viewModel.exportSelection(adapter.getSelection(), it) + } + } else { + uri.path?.let { + viewModel.exportSelection(adapter.getSelection(), File(it)) + } + } + } + } } } diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt index 42217ec18..a63d7ea38 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt @@ -2,6 +2,7 @@ package io.legado.app.ui.replacerule import android.app.Application import android.text.TextUtils +import androidx.documentfile.provider.DocumentFile import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseViewModel @@ -9,10 +10,7 @@ import io.legado.app.data.entities.ReplaceRule import io.legado.app.help.http.HttpHelper import io.legado.app.help.storage.Backup import io.legado.app.help.storage.ImportOldData -import io.legado.app.utils.FileUtils -import io.legado.app.utils.GSON -import io.legado.app.utils.isAbsUrl -import io.legado.app.utils.splitNotBlank +import io.legado.app.utils.* import org.jetbrains.anko.toast import java.io.File @@ -89,12 +87,24 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application } } - fun exportSelection(rules: LinkedHashSet) { + fun exportSelection(sources: LinkedHashSet, file: File) { execute { - val json = GSON.toJson(rules) - val file = - FileUtils.createFileIfNotExist(Backup.exportPath + File.separator + "exportReplaceRule.json") - file.writeText(json) + val json = GSON.toJson(sources) + FileUtils.createFileIfNotExist(file, "exportReplaceRule.json") + .writeText(json) + }.onSuccess { + context.toast("成功导出至\n${Backup.exportPath}") + }.onError { + context.toast("导出失败\n${it.localizedMessage}") + } + } + + fun exportSelection(sources: LinkedHashSet, doc: DocumentFile) { + execute { + val json = GSON.toJson(sources) + doc.findFile("exportReplaceRule.json")?.delete() + doc.createFile("", "exportReplaceRule.json") + ?.writeText(context, json) }.onSuccess { context.toast("成功导出至\n${Backup.exportPath}") }.onError { diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt index 7a64ea382..9160155b3 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt @@ -83,8 +83,8 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application) fun exportSelection(sources: LinkedHashSet, doc: DocumentFile) { execute { val json = GSON.toJson(sources) - doc.findFile("exportBookSource.json")?.delete() - doc.createFile("", "exportBookSource.json") + doc.findFile("exportRssSource.json")?.delete() + doc.createFile("", "exportRssSource.json") ?.writeText(context, json) }.onSuccess { context.toast("成功导出至\n${Backup.exportPath}")