From 2f0ac01959bf3f4748f3a7bafe6f9a3a3ed2fa41 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 7 Jan 2020 21:33:14 +0800 Subject: [PATCH] up --- .../book/source/manage/BookSourceActivity.kt | 20 ++--- .../app/ui/replacerule/ReplaceRuleActivity.kt | 84 ++++++++++++++++++- .../ui/replacerule/ReplaceRuleViewModel.kt | 13 ++- .../java/io/legado/app/utils/UriExtensions.kt | 61 ++++++++++++++ 4 files changed, 160 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/io/legado/app/utils/UriExtensions.kt diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index a6c2b7e05..314eb3621 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -9,7 +9,6 @@ import android.view.MenuItem import android.view.SubMenu import androidx.appcompat.widget.SearchView import androidx.core.content.ContextCompat -import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.LiveData import androidx.lifecycle.Observer import androidx.recyclerview.widget.DiffUtil @@ -291,23 +290,16 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity } importSource -> if (resultCode == Activity.RESULT_OK) { data?.data?.let { uri -> - if (DocumentFile.isDocumentUri(this, uri)) { - DocumentUtils.readText(this, uri)?.let { - viewModel.importSource(it) { msg -> - title_bar.snackbar(msg) - } - } ?: toast("读取文件失败") - } else { - val path = FileUtils.getPath(this, uri) - if (path != null) { + try { + uri.readText(this)?.let { Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE) .show() - viewModel.importSourceFromFilePath(path) { msg -> - title_bar.snackbar(msg) + viewModel.importSource(it) { msg -> + toast(msg) } - } else { - toast(R.string.uri_to_path_fail) } + } catch (e: Exception) { + e.localizedMessage?.let { toast(it) } } } } 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 806336698..9ba3a5a0f 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 @@ -1,6 +1,8 @@ package io.legado.app.ui.replacerule +import android.annotation.SuppressLint import android.app.Activity +import android.content.Intent import android.os.Bundle import android.view.Menu import android.view.MenuItem @@ -13,18 +15,25 @@ import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager +import com.google.android.material.snackbar.Snackbar import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.data.entities.ReplaceRule import io.legado.app.help.ItemTouchCallback +import io.legado.app.lib.dialogs.alert +import io.legado.app.lib.dialogs.cancelButton +import io.legado.app.lib.dialogs.customView +import io.legado.app.lib.dialogs.okButton import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.primaryTextColor +import io.legado.app.lib.theme.view.ATEAutoCompleteTextView import io.legado.app.ui.replacerule.edit.ReplaceEditDialog -import io.legado.app.utils.getViewModel -import io.legado.app.utils.splitNotBlank +import io.legado.app.utils.* import kotlinx.android.synthetic.main.activity_replace_rule.* +import kotlinx.android.synthetic.main.dialog_edit_text.view.* import kotlinx.android.synthetic.main.view_search.* +import org.jetbrains.anko.toast class ReplaceRuleActivity : VMBaseActivity(R.layout.activity_replace_rule), @@ -32,7 +41,7 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi ReplaceRuleAdapter.CallBack { override val viewModel: ReplaceRuleViewModel get() = getViewModel(ReplaceRuleViewModel::class.java) - + private val importSource = 132 private lateinit var adapter: ReplaceRuleAdapter private var groups = hashSetOf() private var groupMenu: SubMenu? = null @@ -68,6 +77,8 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi R.id.menu_enable_selection -> viewModel.enableSelection(adapter.getSelectionIds()) R.id.menu_disable_selection -> viewModel.disableSelection(adapter.getSelectionIds()) R.id.menu_del_selection -> viewModel.delSelection(adapter.getSelectionIds()) + R.id.menu_import_source_onLine -> showImportDialog() + R.id.menu_import_source_local -> selectFileSys() R.id.menu_export_selection -> viewModel.exportSelection(adapter.getSelectionIds()) } return super.onCompatOptionsItemSelected(item) @@ -134,6 +145,52 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi } } + @SuppressLint("InflateParams") + private fun showImportDialog() { + val aCache = ACache.get(this, cacheDir = false) + val cacheUrls: MutableList = aCache + .getAsString("replaceRuleUrl") + ?.splitNotBlank(",") + ?.toMutableList() ?: mutableListOf() + alert(titleResource = R.string.import_replace_rule_on_line) { + var editText: ATEAutoCompleteTextView? = null + customView { + layoutInflater.inflate(R.layout.dialog_edit_text, null).apply { + editText = edit_view + edit_view.setFilterValues(cacheUrls) { + cacheUrls.remove(it) + aCache.put("replaceRuleUrl", cacheUrls.joinToString(",")) + } + } + } + okButton { + val text = editText?.text?.toString() + text?.let { + if (!cacheUrls.contains(it)) { + cacheUrls.add(0, it) + aCache.put("replaceRuleUrl", cacheUrls.joinToString(",")) + } + Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() + viewModel.importSource(it) { msg -> + title_bar.snackbar(msg) + } + } + } + cancelButton() + }.show().applyTint() + } + + private fun selectFileSys() { + try { + val intent = Intent(Intent.ACTION_GET_CONTENT) + intent.addCategory(Intent.CATEGORY_OPENABLE) + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + intent.type = "text/*"//设置类型 + startActivityForResult(intent, importSource) + } catch (e: Exception) { + + } + } override fun onQueryTextChange(newText: String?): Boolean { observeReplaceRuleData("%$newText%") @@ -144,6 +201,27 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi return false } + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + when (requestCode) { + importSource -> if (resultCode == Activity.RESULT_OK) { + data?.data?.let { uri -> + try { + uri.readText(this)?.let { + Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE) + .show() + viewModel.importSource(it) { msg -> + toast(msg) + } + } + } catch (e: Exception) { + e.localizedMessage?.let { toast(it) } + } + } + } + } + } + override fun update(vararg rule: ReplaceRule) { viewModel.update(*rule) } 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 7b80d5475..5c5f0e6a5 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 @@ -3,6 +3,7 @@ package io.legado.app.ui.replacerule import android.app.Application import android.text.TextUtils import io.legado.app.App +import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.ReplaceRule import io.legado.app.help.FileHelp @@ -14,6 +15,15 @@ import java.io.File class ReplaceRuleViewModel(application: Application) : BaseViewModel(application) { + fun importSource(text: String, toast: (msg: String) -> Unit) { + execute { + + }.onError { + toast(it.localizedMessage ?: "ERROR") + }.onSuccess { + toast(context.getString(R.string.success)) + } + } fun update(vararg rule: ReplaceRule) { execute { @@ -68,7 +78,8 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application App.db.replaceRuleDao().findById(it) }.let { val json = GSON.toJson(it) - val file = FileHelp.getFile(Backup.exportPath + File.separator + "exportReplaceRule.json") + val file = + FileHelp.getFile(Backup.exportPath + File.separator + "exportReplaceRule.json") file.writeText(json) } }.onSuccess { diff --git a/app/src/main/java/io/legado/app/utils/UriExtensions.kt b/app/src/main/java/io/legado/app/utils/UriExtensions.kt new file mode 100644 index 000000000..297a5a612 --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/UriExtensions.kt @@ -0,0 +1,61 @@ +package io.legado.app.utils + +import android.content.Context +import android.net.Uri +import androidx.documentfile.provider.DocumentFile +import java.io.File + + +@Throws(Exception::class) +fun Uri.readBytes(context: Context): ByteArray? { + if (DocumentFile.isDocumentUri(context, this)) { + return DocumentUtils.readBytes(context, this) + } else { + val path = FileUtils.getPath(context, this) + if (path?.isNotEmpty() == true) { + return File(path).readBytes() + } + } + return null +} + +@Throws(Exception::class) +fun Uri.readText(context: Context): String? { + if (DocumentFile.isDocumentUri(context, this)) { + return DocumentUtils.readText(context, this) + } else { + val path = FileUtils.getPath(context, this) + if (path?.isNotEmpty() == true) { + return File(path).readText() + } + } + return null +} + +@Throws(Exception::class) +fun Uri.writeBytes(context: Context, byteArray: ByteArray): Boolean { + if (DocumentFile.isDocumentUri(context, this)) { + return DocumentUtils.writeBytes(context, byteArray, this) + } else { + val path = FileUtils.getPath(context, this) + if (path?.isNotEmpty() == true) { + File(path).writeBytes(byteArray) + return true + } + } + return false +} + +@Throws(Exception::class) +fun Uri.writeText(context: Context, text: String): Boolean { + if (DocumentFile.isDocumentUri(context, this)) { + return DocumentUtils.writeText(context, text, this) + } else { + val path = FileUtils.getPath(context, this) + if (path?.isNotEmpty() == true) { + File(path).writeText(text) + return true + } + } + return false +} \ No newline at end of file