From 70f0086b392ab7bf051129ccd635d0593bdfd952 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 6 Sep 2020 17:27:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=8E=92=E7=89=88=E5=AF=BC?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/model/Debug.kt | 16 ++--- .../ui/book/read/config/BgTextConfigDialog.kt | 58 ++++++++++++++++++- .../legado/app/utils/ThrowableExtensions.kt | 9 +++ 3 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/io/legado/app/utils/ThrowableExtensions.kt diff --git a/app/src/main/java/io/legado/app/model/Debug.kt b/app/src/main/java/io/legado/app/model/Debug.kt index 04123aad5..4e419bef4 100644 --- a/app/src/main/java/io/legado/app/model/Debug.kt +++ b/app/src/main/java/io/legado/app/model/Debug.kt @@ -7,7 +7,7 @@ import io.legado.app.model.rss.Rss import io.legado.app.model.webBook.WebBook import io.legado.app.utils.htmlFormat import io.legado.app.utils.isAbsUrl -import org.jetbrains.anko.getStackTraceString +import io.legado.app.utils.msg import java.text.SimpleDateFormat import java.util.* @@ -77,7 +77,7 @@ object Debug { } } .onError { - log(debugSource, "${it.localizedMessage}\n${it.getStackTraceString()}", state = -1) + log(debugSource, it.msg, state = -1) } } @@ -89,7 +89,7 @@ object Debug { log(debugSource, "︽内容页解析完成", state = 1000) } .onError { - log(debugSource, "${it.localizedMessage}\n${it.getStackTraceString()}", state = -1) + log(debugSource, it.msg, state = -1) } } @@ -149,7 +149,7 @@ object Debug { } } .onError { - log(debugSource, "${it.localizedMessage}\n${it.getStackTraceString()}", state = -1) + log(debugSource, it.msg, state = -1) } tasks.add(explore) } @@ -168,7 +168,7 @@ object Debug { } } .onError { - log(debugSource, "${it.localizedMessage}\n${it.getStackTraceString()}", state = -1) + log(debugSource, it.msg, state = -1) } tasks.add(search) } @@ -182,7 +182,7 @@ object Debug { tocDebug(webBook, book) } .onError { - log(debugSource, "${it.localizedMessage}\n${it.getStackTraceString()}", state = -1) + log(debugSource, it.msg, state = -1) } tasks.add(info) } @@ -201,7 +201,7 @@ object Debug { } } .onError { - log(debugSource, "${it.localizedMessage}\n${it.getStackTraceString()}", state = -1) + log(debugSource, it.msg, state = -1) } tasks.add(chapterList) } @@ -218,7 +218,7 @@ object Debug { log(debugSource, "︽正文页解析完成", state = 1000) } .onError { - log(debugSource, "${it.localizedMessage}\n${it.getStackTraceString()}", state = -1) + log(debugSource, it.msg, state = -1) } tasks.add(content) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index 982a4b19d..fc21a4676 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt @@ -21,15 +21,22 @@ import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.constant.EventBus import io.legado.app.help.ImageLoader import io.legado.app.help.ReadBookConfig +import io.legado.app.help.http.HttpHelper import io.legado.app.help.permission.Permissions import io.legado.app.help.permission.PermissionsCompat +import io.legado.app.lib.dialogs.alert +import io.legado.app.lib.dialogs.customView +import io.legado.app.lib.dialogs.noButton +import io.legado.app.lib.dialogs.okButton import io.legado.app.lib.theme.bottomBackground import io.legado.app.lib.theme.getPrimaryTextColor import io.legado.app.lib.theme.getSecondaryTextColor import io.legado.app.ui.book.read.ReadBookActivityHelp import io.legado.app.ui.filechooser.FileChooserDialog import io.legado.app.ui.filechooser.FilePicker +import io.legado.app.ui.widget.text.AutoCompleteTextView import io.legado.app.utils.* +import kotlinx.android.synthetic.main.dialog_edit_text.view.* import kotlinx.android.synthetic.main.dialog_read_bg_text.* import kotlinx.android.synthetic.main.item_bg_image.view.* import org.jetbrains.anko.sdk27.listeners.onCheckedChange @@ -151,11 +158,18 @@ class BgTextConfigDialog : BaseDialogFragment(), FileChooserDialog.CallBack { postEvent(EventBus.UP_CONFIG, false) } tv_import.onClick { + val importFormNet = "网络导入" + val otherActions = arrayListOf(importFormNet) FilePicker.selectFile( this@BgTextConfigDialog, requestCodeImport, - allowExtensions = arrayOf("zip") - ) + allowExtensions = arrayOf("zip"), + otherActions = otherActions + ) { action -> + when (action) { + importFormNet -> importNetConfigAlert() + } + } } tv_export.onClick { FilePicker.selectFolder(this@BgTextConfigDialog, requestCodeExport) @@ -249,13 +263,51 @@ class BgTextConfigDialog : BaseDialogFragment(), FileChooserDialog.CallBack { } } + @SuppressLint("InflateParams") + private fun importNetConfigAlert() { + alert("输入地址") { + var editText: AutoCompleteTextView? = null + customView { + layoutInflater.inflate(R.layout.dialog_edit_text, null).apply { + editText = edit_view + } + } + okButton { + editText?.text?.toString()?.let { url -> + importNetConfig(url) + } + } + noButton { } + }.show().applyTint() + } + + private fun importNetConfig(url: String) { + execute { + HttpHelper.simpleGetBytesAsync(url)?.let { + importConfig(it) + } ?: throw Exception("获取失败") + }.onError { + longToast(it.msg) + } + } + @Suppress("BlockingMethodInNonBlockingContext") private fun importConfig(uri: Uri) { + execute { + importConfig(uri.readBytes(requireContext())!!) + }.onError { + it.printStackTrace() + longToast("导入失败:${it.localizedMessage}") + } + } + + @Suppress("BlockingMethodInNonBlockingContext") + private fun importConfig(byteArray: ByteArray) { execute { val configZipPath = FileUtils.getPath(requireContext().eCacheDir, configFileName) FileUtils.deleteFile(configZipPath) val zipFile = FileUtils.createFileIfNotExist(configZipPath) - zipFile.writeBytes(uri.readBytes(requireContext())!!) + zipFile.writeBytes(byteArray) val configDirPath = FileUtils.getPath(requireContext().eCacheDir, "readConfig") FileUtils.deleteFile(configDirPath) ZipUtils.unzipFile(zipFile, FileUtils.createFolderIfNotExist(configDirPath)) diff --git a/app/src/main/java/io/legado/app/utils/ThrowableExtensions.kt b/app/src/main/java/io/legado/app/utils/ThrowableExtensions.kt new file mode 100644 index 000000000..fafd84e57 --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/ThrowableExtensions.kt @@ -0,0 +1,9 @@ +package io.legado.app.utils + +val Throwable.msg: String + get() { + val lMsg = this.localizedMessage + return if (lMsg.isNullOrEmpty()) this.stackTraceToString() else { + lMsg + } + } \ No newline at end of file