From 92b9a6e1cddcd1ddb24e276f8374d1d71f1af9c9 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 11 Aug 2020 14:41:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AF=BC=E5=85=A5,=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E6=97=B6=E5=A2=9E=E5=8A=A0=E7=A1=AE=E8=AE=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 85 ++++++++------ .../java/io/legado/app/base/BaseActivity.kt | 24 ++-- .../main/java/io/legado/app/constant/Theme.kt | 2 +- .../legado/app/help/storage/ImportOldData.kt | 16 +-- .../io/legado/app/help/storage/OldReplace.kt | 16 ++- .../FileAssociationActivity.kt | 10 +- .../FileAssociationViewModel.kt | 2 +- .../association/ImportBookSourceActivity.kt | 80 +++++++++++++ .../association/ImportBookSourceViewModel.kt | 105 ++++++++++++++++++ .../association/ImportReplaceRuleActivity.kt | 80 +++++++++++++ .../association/ImportReplaceRuleViewModel.kt | 64 +++++++++++ .../ui/association/ImportRssSourceActivity.kt | 80 +++++++++++++ .../association/ImportRssSourceViewModel.kt | 96 ++++++++++++++++ .../book/source/manage/BookSourceActivity.kt | 53 +-------- .../book/source/manage/BookSourceViewModel.kt | 103 +---------------- .../app/ui/replacerule/ReplaceRuleActivity.kt | 50 +-------- .../ui/replacerule/ReplaceRuleViewModel.kt | 51 +-------- .../ui/rss/source/manage/RssSourceActivity.kt | 54 +-------- .../rss/source/manage/RssSourceViewModel.kt | 96 +--------------- ...ociation.xml => activity_translucence.xml} | 5 +- 20 files changed, 628 insertions(+), 444 deletions(-) rename app/src/main/java/io/legado/app/ui/{config => association}/FileAssociationActivity.kt (82%) rename app/src/main/java/io/legado/app/ui/{config => association}/FileAssociationViewModel.kt (98%) create mode 100644 app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt create mode 100644 app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt create mode 100644 app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleActivity.kt create mode 100644 app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt create mode 100644 app/src/main/java/io/legado/app/ui/association/ImportRssSourceActivity.kt create mode 100644 app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt rename app/src/main/res/layout/{activity_file_association.xml => activity_translucence.xml} (57%) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5d5b115af..d1f545692 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -199,47 +199,15 @@ - - - - - - - - - + android:launchMode="singleTop" /> - - - - - - - - - + android:launchMode="singleTop" /> - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/io/legado/app/base/BaseActivity.kt b/app/src/main/java/io/legado/app/base/BaseActivity.kt index 7a7d8a6a8..e0de2b618 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -24,7 +24,8 @@ import kotlinx.coroutines.cancel abstract class BaseActivity( private val layoutID: Int, private val fullScreen: Boolean = true, - private val theme: Theme = Theme.Auto + private val theme: Theme = Theme.Auto, + private val transparent: Boolean = false ) : AppCompatActivity(), CoroutineScope by MainScope() { @@ -92,14 +93,23 @@ abstract class BaseActivity( } private fun initTheme() { - ATH.applyBackgroundTint(window.decorView) when (theme) { - Theme.Dark -> setTheme(R.style.AppTheme_Dark) - Theme.Light -> setTheme(R.style.AppTheme_Light) - else -> if (ColorUtils.isColorLight(primaryColor)) { - setTheme(R.style.AppTheme_Light) - } else { + Theme.Transparent -> setTheme(R.style.AppTheme_Transparent) + Theme.Dark -> { setTheme(R.style.AppTheme_Dark) + ATH.applyBackgroundTint(window.decorView) + } + Theme.Light -> { + setTheme(R.style.AppTheme_Light) + ATH.applyBackgroundTint(window.decorView) + } + else -> { + if (ColorUtils.isColorLight(primaryColor)) { + setTheme(R.style.AppTheme_Light) + } else { + setTheme(R.style.AppTheme_Dark) + } + ATH.applyBackgroundTint(window.decorView) } } } diff --git a/app/src/main/java/io/legado/app/constant/Theme.kt b/app/src/main/java/io/legado/app/constant/Theme.kt index b8587997b..a4f313a80 100644 --- a/app/src/main/java/io/legado/app/constant/Theme.kt +++ b/app/src/main/java/io/legado/app/constant/Theme.kt @@ -3,7 +3,7 @@ package io.legado.app.constant import io.legado.app.help.AppConfig enum class Theme { - Dark, Light, Auto; + Dark, Light, Auto, Transparent; companion object { fun getTheme(): Theme { diff --git a/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt b/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt index da769dbe2..a1741c845 100644 --- a/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt +++ b/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt @@ -5,7 +5,6 @@ import android.net.Uri import androidx.documentfile.provider.DocumentFile import io.legado.app.App import io.legado.app.data.entities.BookSource -import io.legado.app.data.entities.ReplaceRule import io.legado.app.utils.DocumentUtils import io.legado.app.utils.FileUtils import org.jetbrains.anko.toast @@ -102,17 +101,8 @@ object ImportOldData { } fun importOldReplaceRule(json: String): Int { - val replaceRules = mutableListOf() - val items: List> = Restore.jsonPath.parse(json).read("$") - for (item in items) { - val jsonItem = Restore.jsonPath.parse(item) - OldReplace.jsonToReplaceRule(jsonItem.jsonString())?.let { - if (it.isValid()){ - replaceRules.add(it) - } - } - } - App.db.replaceRuleDao().insert(*replaceRules.toTypedArray()) - return replaceRules.size + val rules = OldReplace.jsonToReplaceRules(json) + App.db.replaceRuleDao().insert(*rules.toTypedArray()) + return rules.size } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/storage/OldReplace.kt b/app/src/main/java/io/legado/app/help/storage/OldReplace.kt index d658106dc..1b6a9a89e 100644 --- a/app/src/main/java/io/legado/app/help/storage/OldReplace.kt +++ b/app/src/main/java/io/legado/app/help/storage/OldReplace.kt @@ -5,7 +5,21 @@ import io.legado.app.utils.* object OldReplace { - fun jsonToReplaceRule(json: String): ReplaceRule? { + fun jsonToReplaceRules(json: String): List { + val replaceRules = mutableListOf() + val items: List> = Restore.jsonPath.parse(json).read("$") + for (item in items) { + val jsonItem = Restore.jsonPath.parse(item) + jsonToReplaceRule(jsonItem.jsonString())?.let { + if (it.isValid()) { + replaceRules.add(it) + } + } + } + return replaceRules + } + + private fun jsonToReplaceRule(json: String): ReplaceRule? { var replaceRule: ReplaceRule? = null runCatching { replaceRule = GSON.fromJsonObject(json.trim()) diff --git a/app/src/main/java/io/legado/app/ui/config/FileAssociationActivity.kt b/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt similarity index 82% rename from app/src/main/java/io/legado/app/ui/config/FileAssociationActivity.kt rename to app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt index 80390c639..e52e9a25d 100644 --- a/app/src/main/java/io/legado/app/ui/config/FileAssociationActivity.kt +++ b/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt @@ -1,15 +1,19 @@ -package io.legado.app.ui.config +package io.legado.app.ui.association import android.content.Intent import android.os.Bundle import io.legado.app.R import io.legado.app.base.VMBaseActivity +import io.legado.app.constant.Theme import io.legado.app.ui.main.MainActivity import io.legado.app.utils.getViewModel -class FileAssociationActivity : - VMBaseActivity(R.layout.activity_file_association) { +class FileAssociationActivity : VMBaseActivity( + R.layout.activity_translucence, + theme = Theme.Transparent +) { + override val viewModel: FileAssociationViewModel get() = getViewModel(FileAssociationViewModel::class.java) diff --git a/app/src/main/java/io/legado/app/ui/config/FileAssociationViewModel.kt b/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt similarity index 98% rename from app/src/main/java/io/legado/app/ui/config/FileAssociationViewModel.kt rename to app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt index 4f897c791..d1e0c0dc5 100644 --- a/app/src/main/java/io/legado/app/ui/config/FileAssociationViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt @@ -1,4 +1,4 @@ -package io.legado.app.ui.config +package io.legado.app.ui.association import android.app.Application import android.content.Intent diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt new file mode 100644 index 000000000..53a4ba64b --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt @@ -0,0 +1,80 @@ +package io.legado.app.ui.association + +import android.os.Bundle +import androidx.lifecycle.Observer +import io.legado.app.R +import io.legado.app.base.VMBaseActivity +import io.legado.app.constant.Theme +import io.legado.app.data.entities.BookSource +import io.legado.app.help.SourceHelp +import io.legado.app.lib.dialogs.alert +import io.legado.app.lib.dialogs.noButton +import io.legado.app.lib.dialogs.okButton +import io.legado.app.utils.applyTint +import io.legado.app.utils.getViewModel +import org.jetbrains.anko.toast + +class ImportBookSourceActivity : VMBaseActivity( + R.layout.activity_translucence, + theme = Theme.Transparent +) { + + override val viewModel: ImportBookSourceViewModel + get() = getViewModel(ImportBookSourceViewModel::class.java) + + override fun onActivityCreated(savedInstanceState: Bundle?) { + viewModel.errorLiveData.observe(this, Observer { + errorDialog(it) + }) + viewModel.successLiveData.observe(this, Observer { + successDialog(it) + }) + initData() + } + + private fun initData() { + intent.getStringExtra("source")?.let { + viewModel.importSource(it) + return + } + intent.getStringExtra("filePath")?.let { + viewModel.importSourceFromFilePath(it) + return + } + intent.data?.let { + when (it.path) { + "/importonline" -> it.getQueryParameter("src")?.let { url -> + if (url.startsWith("http", false)) { + viewModel.importSource(url) + } else { + viewModel.importSourceFromFilePath(url) + } + } + else -> { + toast("格式不对") + } + } + } + } + + private fun errorDialog(msg: String) { + alert("导入出错", msg) { + okButton { } + }.show().applyTint().setOnDismissListener { + finish() + } + } + + private fun successDialog(allSource: ArrayList) { + alert("解析结果", "共${allSource.size}个书源,是否确认导入?") { + okButton { + SourceHelp.insertBookSource(*allSource.toTypedArray()) + } + noButton { + + } + }.show().applyTint().setOnDismissListener { + finish() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt new file mode 100644 index 000000000..88dd8a91f --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt @@ -0,0 +1,105 @@ +package io.legado.app.ui.association + +import android.app.Application +import android.net.Uri +import androidx.documentfile.provider.DocumentFile +import androidx.lifecycle.MutableLiveData +import com.jayway.jsonpath.JsonPath +import io.legado.app.base.BaseViewModel +import io.legado.app.data.entities.BookSource +import io.legado.app.help.http.HttpHelper +import io.legado.app.help.storage.OldRule +import io.legado.app.help.storage.Restore +import io.legado.app.utils.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import java.io.File + +class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { + + val errorLiveData = MutableLiveData() + val successLiveData = MutableLiveData>() + + private val allSources = arrayListOf() + + fun importSourceFromFilePath(path: String) { + execute { + val content = if (path.isContentPath()) { + //在前面被解码了,如果不进行编码,中文会无法识别 + val newPath = Uri.encode(path, ":/.") + DocumentFile.fromSingleUri(context, Uri.parse(newPath))?.readText(context) + } else { + val file = File(path) + if (file.exists()) { + file.readText() + } else { + null + } + } + if (content != null) { + importSource(content) + } else { + withContext(Dispatchers.Main) { + errorLiveData.postValue("打开文件出错") + } + } + }.onError { + errorLiveData.postValue(it.localizedMessage ?: "打开文件出错") + } + } + + fun importSource(text: String) { + execute { + val text1 = text.trim() + when { + text1.isJsonObject() -> { + val json = JsonPath.parse(text1) + val urls = json.read>("$.sourceUrls") + if (!urls.isNullOrEmpty()) { + urls.forEach { + importSourceUrl(it) + } + } else { + OldRule.jsonToBookSource(text1)?.let { + allSources.add(it) + } + } + } + text1.isJsonArray() -> { + val items: List> = Restore.jsonPath.parse(text1).read("$") + for (item in items) { + val jsonItem = Restore.jsonPath.parse(item) + OldRule.jsonToBookSource(jsonItem.jsonString())?.let { + allSources.add(it) + } + } + } + text1.isAbsUrl() -> { + importSourceUrl(text1) + } + else -> throw Exception("格式不对") + } + }.onError { + it.printStackTrace() + errorLiveData.postValue(it.localizedMessage ?: "") + }.onSuccess { + successLiveData.postValue(allSources) + } + } + + private fun importSourceUrl(url: String) { + HttpHelper.simpleGet(url, "UTF-8").let { body -> + if (body == null) { + throw Exception("访问网站失败") + } + val items: List> = Restore.jsonPath.parse(body).read("$") + for (item in items) { + val jsonItem = Restore.jsonPath.parse(item) + OldRule.jsonToBookSource(jsonItem.jsonString())?.let { source -> + allSources.add(source) + } + } + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleActivity.kt new file mode 100644 index 000000000..55ff9e955 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleActivity.kt @@ -0,0 +1,80 @@ +package io.legado.app.ui.association + +import android.os.Bundle +import androidx.lifecycle.Observer +import io.legado.app.App +import io.legado.app.R +import io.legado.app.base.VMBaseActivity +import io.legado.app.constant.Theme +import io.legado.app.data.entities.ReplaceRule +import io.legado.app.lib.dialogs.alert +import io.legado.app.lib.dialogs.noButton +import io.legado.app.lib.dialogs.okButton +import io.legado.app.utils.applyTint +import io.legado.app.utils.getViewModel +import org.jetbrains.anko.toast + +class ImportReplaceRuleActivity : VMBaseActivity( + R.layout.activity_translucence, + theme = Theme.Transparent +) { + + override val viewModel: ImportReplaceRuleViewModel + get() = getViewModel(ImportReplaceRuleViewModel::class.java) + + override fun onActivityCreated(savedInstanceState: Bundle?) { + viewModel.errorLiveData.observe(this, Observer { + errorDialog(it) + }) + viewModel.successLiveData.observe(this, Observer { + successDialog(it) + }) + initData() + } + + private fun initData() { + intent.getStringExtra("source")?.let { + viewModel.import(it) + return + } + intent.getStringExtra("filePath")?.let { + viewModel.importFromFilePath(it) + return + } + intent.data?.let { + when (it.path) { + "/importonline" -> it.getQueryParameter("src")?.let { url -> + if (url.startsWith("http", false)) { + viewModel.import(url) + } else { + viewModel.importFromFilePath(url) + } + } + else -> { + toast("格式不对") + } + } + } + } + + private fun errorDialog(msg: String) { + alert("导入出错", msg) { + okButton { } + }.show().applyTint().setOnDismissListener { + finish() + } + } + + private fun successDialog(allSource: ArrayList) { + alert("解析结果", "共${allSource.size}个替换规则,是否确认导入?") { + okButton { + App.db.replaceRuleDao().insert(*allSource.toTypedArray()) + } + noButton { + + } + }.show().applyTint().setOnDismissListener { + finish() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt new file mode 100644 index 000000000..fed2bef76 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt @@ -0,0 +1,64 @@ +package io.legado.app.ui.association + +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.base.BaseViewModel +import io.legado.app.data.entities.ReplaceRule +import io.legado.app.help.http.HttpHelper +import io.legado.app.help.storage.OldReplace +import io.legado.app.utils.isAbsUrl +import io.legado.app.utils.isContentPath +import io.legado.app.utils.readText +import java.io.File + +class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) { + val errorLiveData = MutableLiveData() + val successLiveData = MutableLiveData>() + + private val allRules = arrayListOf() + + fun importFromFilePath(path: String) { + execute { + val content = if (path.isContentPath()) { + //在前面被解码了,如果不进行编码,中文会无法识别 + val newPath = Uri.encode(path, ":/.") + DocumentFile.fromSingleUri(context, Uri.parse(newPath))?.readText(context) + } else { + val file = File(path) + if (file.exists()) { + file.readText() + } else { + null + } + } + if (content != null) { + import(content) + } else { + errorLiveData.postValue("打开文件出错") + } + }.onError { + errorLiveData.postValue(it.localizedMessage ?: "打开文件出错") + } + } + + fun import(text: String) { + execute { + if (text.isAbsUrl()) { + HttpHelper.simpleGet(text)?.let { + val rules = OldReplace.jsonToReplaceRules(it) + allRules.addAll(rules) + } + } else { + val rules = OldReplace.jsonToReplaceRules(text) + allRules.addAll(rules) + } + }.onError { + errorLiveData.postValue(it.localizedMessage ?: "ERROR") + }.onSuccess { + errorLiveData.postValue(context.getString(R.string.success)) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceActivity.kt b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceActivity.kt new file mode 100644 index 000000000..9e5ffcee4 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceActivity.kt @@ -0,0 +1,80 @@ +package io.legado.app.ui.association + +import android.os.Bundle +import androidx.lifecycle.Observer +import io.legado.app.R +import io.legado.app.base.VMBaseActivity +import io.legado.app.constant.Theme +import io.legado.app.data.entities.RssSource +import io.legado.app.help.SourceHelp +import io.legado.app.lib.dialogs.alert +import io.legado.app.lib.dialogs.noButton +import io.legado.app.lib.dialogs.okButton +import io.legado.app.utils.applyTint +import io.legado.app.utils.getViewModel +import org.jetbrains.anko.toast + +class ImportRssSourceActivity : VMBaseActivity( + R.layout.activity_translucence, + theme = Theme.Transparent +) { + + override val viewModel: ImportRssSourceViewModel + get() = getViewModel(ImportRssSourceViewModel::class.java) + + override fun onActivityCreated(savedInstanceState: Bundle?) { + viewModel.errorLiveData.observe(this, Observer { + errorDialog(it) + }) + viewModel.successLiveData.observe(this, Observer { + successDialog(it) + }) + initData() + } + + private fun initData() { + intent.getStringExtra("source")?.let { + viewModel.importSource(it) + return + } + intent.getStringExtra("filePath")?.let { + viewModel.importSourceFromFilePath(it) + return + } + intent.data?.let { + when (it.path) { + "/importonline" -> it.getQueryParameter("src")?.let { url -> + if (url.startsWith("http", false)) { + viewModel.importSource(url) + } else { + viewModel.importSourceFromFilePath(url) + } + } + else -> { + toast("格式不对") + } + } + } + } + + private fun errorDialog(msg: String) { + alert("导入出错", msg) { + okButton { } + }.show().applyTint().setOnDismissListener { + finish() + } + } + + private fun successDialog(allSource: ArrayList) { + alert("解析结果", "共${allSource.size}个订阅源,是否确认导入?") { + okButton { + SourceHelp.insertRssSource(*allSource.toTypedArray()) + } + noButton { + + } + }.show().applyTint().setOnDismissListener { + finish() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt new file mode 100644 index 000000000..98bacf4da --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt @@ -0,0 +1,96 @@ +package io.legado.app.ui.association + +import android.app.Application +import android.net.Uri +import androidx.documentfile.provider.DocumentFile +import androidx.lifecycle.MutableLiveData +import com.jayway.jsonpath.JsonPath +import io.legado.app.base.BaseViewModel +import io.legado.app.data.entities.RssSource +import io.legado.app.help.http.HttpHelper +import io.legado.app.help.storage.Restore +import io.legado.app.utils.* +import java.io.File + +class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) { + + val errorLiveData = MutableLiveData() + val successLiveData = MutableLiveData>() + + private val allSources = arrayListOf() + + fun importSourceFromFilePath(path: String) { + execute { + val content = if (path.isContentPath()) { + //在前面被解码了,如果不进行编码,中文会无法识别 + val newPath = Uri.encode(path, ":/.") + DocumentFile.fromSingleUri(context, Uri.parse(newPath))?.readText(context) + } else { + val file = File(path) + if (file.exists()) { + file.readText() + } else { + null + } + } + if (null != content) { + GSON.fromJsonArray(content)?.let { + allSources.addAll(it) + } + } + }.onSuccess { + successLiveData.postValue(allSources) + } + } + + fun importSource(text: String) { + execute { + val text1 = text.trim() + when { + text1.isJsonObject() -> { + val json = JsonPath.parse(text1) + val urls = json.read>("$.sourceUrls") + if (!urls.isNullOrEmpty()) { + urls.forEach { + importSourceUrl(it) + } + } else { + GSON.fromJsonArray(text1)?.let { + allSources.addAll(it) + } + } + } + text1.isJsonArray() -> { + val items: List> = Restore.jsonPath.parse(text1).read("$") + for (item in items) { + val jsonItem = Restore.jsonPath.parse(item) + GSON.fromJsonObject(jsonItem.jsonString())?.let { + allSources.add(it) + } + } + } + text1.isAbsUrl() -> { + importSourceUrl(text1) + } + else -> throw Exception("格式不对") + } + }.onError { + errorLiveData.postValue("ImportError:${it.localizedMessage}") + }.onSuccess { + successLiveData.postValue(allSources) + } + } + + private fun importSourceUrl(url: String) { + HttpHelper.simpleGet(url, "UTF-8")?.let { body -> + val items: List> = Restore.jsonPath.parse(body).read("$") + for (item in items) { + val jsonItem = Restore.jsonPath.parse(item) + GSON.fromJsonObject(jsonItem.jsonString())?.let { source -> + allSources.add(source) + } + } + } + } + +} \ No newline at end of file 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 9b6b8a5d4..c1316e237 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 @@ -15,7 +15,6 @@ import androidx.lifecycle.Observer import androidx.recyclerview.widget.DiffUtil 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 @@ -25,6 +24,7 @@ import io.legado.app.lib.dialogs.* import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.primaryTextColor import io.legado.app.service.help.CheckSource +import io.legado.app.ui.association.ImportBookSourceActivity import io.legado.app.ui.book.source.edit.BookSourceEditActivity import io.legado.app.ui.filechooser.FileChooserDialog import io.legado.app.ui.filechooser.FilePicker @@ -38,7 +38,6 @@ import kotlinx.android.synthetic.main.dialog_edit_text.view.* import kotlinx.android.synthetic.main.view_search.* import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivityForResult -import org.jetbrains.anko.toast import java.io.File import java.text.Collator @@ -60,7 +59,6 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity private var sort = 0 override fun onActivityCreated(savedInstanceState: Bundle?) { - initUriScheme() initRecyclerView() initSearchView() initLiveDataBookSource() @@ -128,28 +126,6 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity return super.onCompatOptionsItemSelected(item) } - private fun initUriScheme() { - intent.data?.let { - when (it.path) { - "/importonline" -> it.getQueryParameter("src")?.let { url -> - Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() - if (url.startsWith("http", false)) { - viewModel.importSource(url) { msg -> - title_bar.snackbar(msg) - } - } else { - viewModel.importSourceFromFilePath(url) { msg -> - title_bar.snackbar(msg) - } - } - } - else -> { - toast("格式不对") - } - } - } - } - private fun initRecyclerView() { ATH.applyEdgeEffectColor(recycler_view) recycler_view.layoutManager = LinearLayoutManager(this) @@ -309,10 +285,7 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity cacheUrls.add(0, it) aCache.put(importRecordKey, cacheUrls.joinToString(",")) } - Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() - viewModel.importSource(it) { msg -> - title_bar.snackbar(msg) - } + startActivity(Pair("source", it)) } } cancelButton() @@ -365,10 +338,7 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity File(currentPath) ) importRequestCode -> { - Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() - viewModel.importSourceFromFilePath(currentPath) { msg -> - title_bar.snackbar(msg) - } + startActivity(Pair("filePath", currentPath)) } } } @@ -378,25 +348,12 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity when (requestCode) { qrRequestCode -> if (resultCode == RESULT_OK) { data?.getStringExtra("result")?.let { - Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() - viewModel.importSource(it) { msg -> - title_bar.snackbar(msg) - } + startActivity("source" to it) } } importRequestCode -> 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 -> - title_bar.snackbar(msg) - } - } - } catch (e: Exception) { - toast(e.localizedMessage ?: "ERROR") - } + startActivity("filePath" to uri.toString()) } } exportRequestCode -> { diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt index 7e2a75d6e..866122b9c 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt @@ -1,20 +1,15 @@ package io.legado.app.ui.book.source.manage import android.app.Application -import android.net.Uri import android.text.TextUtils import androidx.documentfile.provider.DocumentFile -import com.jayway.jsonpath.JsonPath import io.legado.app.App import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.BookSource -import io.legado.app.help.SourceHelp -import io.legado.app.help.http.HttpHelper -import io.legado.app.help.storage.OldRule -import io.legado.app.help.storage.Restore.jsonPath -import io.legado.app.utils.* -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext +import io.legado.app.utils.FileUtils +import io.legado.app.utils.GSON +import io.legado.app.utils.splitNotBlank +import io.legado.app.utils.writeText import org.jetbrains.anko.toast import java.io.File @@ -166,94 +161,4 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) } } - fun importSourceFromFilePath(path: String, finally: (msg: String) -> Unit) { - execute { - val content = if (path.isContentPath()) { - //在前面被解码了,如果不进行编码,中文会无法识别 - val newPath = Uri.encode(path, ":/.") - DocumentFile.fromSingleUri(context, Uri.parse(newPath))?.readText(context) - } else { - val file = File(path) - if (file.exists()) { - file.readText() - } else { - null - } - } - if (content != null) { - importSource(content, finally) - } else { - withContext(Dispatchers.Main) { - finally("打开文件出错") - } - } - }.onError { - finally(it.localizedMessage ?: "打开文件出错") - } - } - - fun importSource(text: String, finally: (msg: String) -> Unit) { - execute { - val text1 = text.trim() - when { - text1.isJsonObject() -> { - val json = JsonPath.parse(text1) - val urls = json.read>("$.sourceUrls") - var count = 0 - if (!urls.isNullOrEmpty()) { - urls.forEach { - count += importSourceUrl(it) - } - } else { - OldRule.jsonToBookSource(text1)?.let { - SourceHelp.insertBookSource(it) - count = 1 - } - } - "导入${count}条" - } - text1.isJsonArray() -> { - val bookSources = mutableListOf() - val items: List> = jsonPath.parse(text1).read("$") - for (item in items) { - val jsonItem = jsonPath.parse(item) - OldRule.jsonToBookSource(jsonItem.jsonString())?.let { - bookSources.add(it) - } - } - SourceHelp.insertBookSource(*bookSources.toTypedArray()) - "导入${bookSources.size}条" - } - text1.isAbsUrl() -> { - val count = importSourceUrl(text1) - "导入${count}条" - } - else -> "格式不对" - } - }.onError { - it.printStackTrace() - finally(it.localizedMessage ?: "") - }.onSuccess { - finally(it) - } - } - - private fun importSourceUrl(url: String): Int { - HttpHelper.simpleGet(url, "UTF-8").let { body -> - if (body == null) { - toast("访问网站失败") - return 0 - } - val bookSources = mutableListOf() - val items: List> = jsonPath.parse(body).read("$") - for (item in items) { - val jsonItem = jsonPath.parse(item) - OldRule.jsonToBookSource(jsonItem.jsonString())?.let { source -> - bookSources.add(source) - } - } - SourceHelp.insertBookSource(*bookSources.toTypedArray()) - return bookSources.size - } - } } \ No newline at end of file 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 e4b509efb..b2e39c4a8 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 @@ -15,7 +15,6 @@ import androidx.lifecycle.Observer import androidx.recyclerview.widget.DiffUtil 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 @@ -26,6 +25,7 @@ import io.legado.app.help.coroutine.Coroutine import io.legado.app.lib.dialogs.* import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.primaryTextColor +import io.legado.app.ui.association.ImportReplaceRuleActivity import io.legado.app.ui.filechooser.FileChooserDialog import io.legado.app.ui.filechooser.FilePicker import io.legado.app.ui.replacerule.edit.ReplaceEditDialog @@ -36,7 +36,7 @@ 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 +import org.jetbrains.anko.startActivity import java.io.File @@ -57,7 +57,6 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi private var dataInit = false override fun onActivityCreated(savedInstanceState: Bundle?) { - initUriScheme() initRecyclerView() initSearchView() initSelectActionView() @@ -76,29 +75,6 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi return super.onPrepareOptionsMenu(menu) } - private fun initUriScheme() { - intent.data?.let { - when (it.path) { - "/importonline" -> it.getQueryParameter("src")?.let { url -> - Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() - if (url.startsWith("http", false)){ - viewModel.importSource(url) { msg -> - title_bar.snackbar(msg) - } - } - else{ - viewModel.importSourceFromFilePath(url) { msg -> - title_bar.snackbar(msg) - } - } - } - else -> { - toast("格式不对") - } - } - } - } - private fun initRecyclerView() { ATH.applyEdgeEffectColor(recycler_view) recycler_view.layoutManager = LinearLayoutManager(this) @@ -242,10 +218,7 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi cacheUrls.add(0, it) aCache.put(importRecordKey, cacheUrls.joinToString(",")) } - Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() - viewModel.importSource(it) { msg -> - title_bar.snackbar(msg) - } + startActivity("source" to it) } } cancelButton() @@ -264,10 +237,7 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi 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) - } + startActivity("filePath" to currentPath) } exportRequestCode -> viewModel.exportSelection( adapter.getSelection(), @@ -281,17 +251,7 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi when (requestCode) { importRequestCode -> 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 -> - title_bar.snackbar(msg) - } - } - } catch (e: Exception) { - toast(e.localizedMessage ?: "ERROR") - } + startActivity("filePath" to uri.toString()) } } exportRequestCode -> if (resultCode == RESULT_OK) { 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 d11d04d4a..afbbcdcf6 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 @@ -1,62 +1,19 @@ package io.legado.app.ui.replacerule import android.app.Application -import android.net.Uri 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 import io.legado.app.data.entities.ReplaceRule -import io.legado.app.help.http.HttpHelper -import io.legado.app.help.storage.ImportOldData -import io.legado.app.utils.* -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext +import io.legado.app.utils.FileUtils +import io.legado.app.utils.GSON +import io.legado.app.utils.splitNotBlank +import io.legado.app.utils.writeText import org.jetbrains.anko.toast import java.io.File class ReplaceRuleViewModel(application: Application) : BaseViewModel(application) { - fun importSourceFromFilePath(path: String, finally: (msg: String) -> Unit) { - execute { - val content = if (path.isContentPath()) { - //在前面被解码了,如果不进行编码,中文会无法识别 - val newPath = Uri.encode(path, ":/.") - DocumentFile.fromSingleUri(context, Uri.parse(newPath))?.readText(context) - } else { - val file = File(path) - if (file.exists()) { - file.readText() - } else { - null - } - } - if (content != null) { - importSource(content, finally) - } else { - withContext(Dispatchers.Main) { - finally("打开文件出错") - } - } - }.onError { - finally(it.localizedMessage ?: "打开文件出错") - } - } - fun importSource(text: String, showMsg: (msg: String) -> Unit) { - execute { - if (text.isAbsUrl()) { - HttpHelper.simpleGet(text)?.let { - ImportOldData.importOldReplaceRule(it) - } - } else { - ImportOldData.importOldReplaceRule(text) - } - }.onError { - showMsg(it.localizedMessage ?: "ERROR") - }.onSuccess { - showMsg(context.getString(R.string.success)) - } - } fun update(vararg rule: ReplaceRule) { execute { diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt index 1480c93cf..0b7a54564 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt @@ -15,7 +15,6 @@ import androidx.lifecycle.Observer import androidx.recyclerview.widget.DiffUtil 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 @@ -24,6 +23,7 @@ import io.legado.app.help.ItemTouchCallback import io.legado.app.lib.dialogs.* import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.primaryTextColor +import io.legado.app.ui.association.ImportRssSourceActivity import io.legado.app.ui.filechooser.FileChooserDialog import io.legado.app.ui.filechooser.FilePicker import io.legado.app.ui.qrcode.QrCodeActivity @@ -37,7 +37,6 @@ import kotlinx.android.synthetic.main.dialog_edit_text.view.* import kotlinx.android.synthetic.main.view_search.* import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivityForResult -import org.jetbrains.anko.toast import java.io.File import java.text.Collator import java.util.* @@ -60,7 +59,6 @@ class RssSourceActivity : VMBaseActivity(R.layout.activity_r private var groupMenu: SubMenu? = null override fun onActivityCreated(savedInstanceState: Bundle?) { - initUriScheme() initRecyclerView() initSearchView() initLiveDataGroup() @@ -112,29 +110,6 @@ class RssSourceActivity : VMBaseActivity(R.layout.activity_r return true } - private fun initUriScheme() { - intent.data?.let { - when (it.path) { - "/importonline" -> it.getQueryParameter("src")?.let { url -> - Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() - if (url.startsWith("http", false)){ - viewModel.importSource(url) { msg -> - title_bar.snackbar(msg) - } - } - else{ - viewModel.importSourceFromFilePath(url) { msg -> - title_bar.snackbar(msg) - } - } - } - else -> { - toast("格式不对") - } - } - } - } - private fun initRecyclerView() { ATH.applyEdgeEffectColor(recycler_view) recycler_view.layoutManager = LinearLayoutManager(this) @@ -256,10 +231,7 @@ class RssSourceActivity : VMBaseActivity(R.layout.activity_r cacheUrls.add(0, it) aCache.put(importRecordKey, cacheUrls.joinToString(",")) } - Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() - viewModel.importSource(it) { msg -> - title_bar.snackbar(msg) - } + startActivity("source" to it) } } cancelButton() @@ -269,10 +241,7 @@ class RssSourceActivity : VMBaseActivity(R.layout.activity_r override fun onFilePicked(requestCode: Int, currentPath: String) { when (requestCode) { importRequestCode -> { - Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() - viewModel.importSourceFromFilePath(currentPath) { msg -> - title_bar.snackbar(msg) - } + startActivity("filePath" to currentPath) } exportRequestCode -> viewModel.exportSelection( adapter.getSelection(), @@ -286,25 +255,12 @@ class RssSourceActivity : VMBaseActivity(R.layout.activity_r when (requestCode) { importRequestCode -> 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 -> - title_bar.snackbar(msg) - } - } - } catch (e: Exception) { - toast(e.localizedMessage ?: "ERROR") - } + startActivity("filePath" to uri.toString()) } } qrRequestCode -> if (resultCode == RESULT_OK) { data?.getStringExtra("result")?.let { - Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE) - viewModel.importSource(it) { msg -> - title_bar.snackbar(msg) - } + startActivity("source" to it) } } exportRequestCode -> if (resultCode == RESULT_OK) { 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 a15cb2e77..5c72b503d 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 @@ -1,18 +1,15 @@ package io.legado.app.ui.rss.source.manage import android.app.Application -import android.net.Uri import android.text.TextUtils import androidx.documentfile.provider.DocumentFile -import com.jayway.jsonpath.JsonPath import io.legado.app.App -import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.RssSource -import io.legado.app.help.SourceHelp -import io.legado.app.help.http.HttpHelper -import io.legado.app.help.storage.Restore.jsonPath -import io.legado.app.utils.* +import io.legado.app.utils.FileUtils +import io.legado.app.utils.GSON +import io.legado.app.utils.splitNotBlank +import io.legado.app.utils.writeText import org.jetbrains.anko.toast import java.io.File @@ -147,89 +144,4 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application) } } - fun importSourceFromFilePath(path: String, finally: (msg: String) -> Unit) { - execute { - val content = if (path.isContentPath()) { - //在前面被解码了,如果不进行编码,中文会无法识别 - val newPath = Uri.encode(path, ":/.") - DocumentFile.fromSingleUri(context, Uri.parse(newPath))?.readText(context) - } else { - val file = File(path) - if (file.exists()) { - file.readText() - } else { - null - } - } - if (null != content) { - GSON.fromJsonArray(content)?.let { - SourceHelp.insertRssSource(*it.toTypedArray()) - } - } - }.onSuccess { - finally.invoke(context.getString(R.string.success)) - } - } - - fun importSource(text: String, finally: (msg: String) -> Unit) { - execute { - val text1 = text.trim() - when { - text1.isJsonObject() -> { - val json = JsonPath.parse(text1) - val urls = json.read>("$.sourceUrls") - var count = 0 - if (!urls.isNullOrEmpty()) { - urls.forEach { - count += importSourceUrl(it) - } - } else { - GSON.fromJsonArray(text1)?.let { - SourceHelp.insertRssSource(*it.toTypedArray()) - count = 1 - } - } - "导入${count}条" - } - text1.isJsonArray() -> { - val rssSources = mutableListOf() - val items: List> = jsonPath.parse(text1).read("$") - for (item in items) { - val jsonItem = jsonPath.parse(item) - GSON.fromJsonObject(jsonItem.jsonString())?.let { - rssSources.add(it) - } - } - SourceHelp.insertRssSource(*rssSources.toTypedArray()) - "导入${rssSources.size}条" - } - text1.isAbsUrl() -> { - val count = importSourceUrl(text1) - "导入${count}条" - } - else -> "格式不对" - } - }.onError { - finally(it.localizedMessage ?: "") - }.onSuccess { - finally(it) - } - } - - private fun importSourceUrl(url: String): Int { - HttpHelper.simpleGet(url, "UTF-8")?.let { body -> - val sources = mutableListOf() - val items: List> = jsonPath.parse(body).read("$") - for (item in items) { - val jsonItem = jsonPath.parse(item) - GSON.fromJsonObject(jsonItem.jsonString())?.let { source -> - sources.add(source) - } - } - SourceHelp.insertRssSource(*sources.toTypedArray()) - return sources.size - } - return 0 - } - } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_file_association.xml b/app/src/main/res/layout/activity_translucence.xml similarity index 57% rename from app/src/main/res/layout/activity_file_association.xml rename to app/src/main/res/layout/activity_translucence.xml index 2a7adee89..be20a795a 100644 --- a/app/src/main/res/layout/activity_file_association.xml +++ b/app/src/main/res/layout/activity_translucence.xml @@ -1,8 +1,7 @@ + android:background="@color/transparent30"> - + \ No newline at end of file