From b57a63f2d2eaf729b94992e832f662413939e2c5 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 14 May 2020 13:16:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/SourceHelp.kt | 44 +++++++++++++++++++ .../rss/source/manage/RssSourceViewModel.kt | 9 ++-- 2 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/io/legado/app/help/SourceHelp.kt diff --git a/app/src/main/java/io/legado/app/help/SourceHelp.kt b/app/src/main/java/io/legado/app/help/SourceHelp.kt new file mode 100644 index 000000000..bfb1ce104 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/SourceHelp.kt @@ -0,0 +1,44 @@ +package io.legado.app.help + +import android.os.Handler +import android.os.Looper +import io.legado.app.App +import io.legado.app.data.entities.BookSource +import io.legado.app.data.entities.RssSource +import io.legado.app.utils.splitNotBlank +import org.jetbrains.anko.toast + +object SourceHelp { + + private val handler = Handler(Looper.getMainLooper()) + private val list18Plus by lazy { + String(App.INSTANCE.assets.open("18PlusList.txt").readBytes()).splitNotBlank("\n") + } + + fun insertRssSource(vararg rssSources: RssSource) { + rssSources.forEach { rssSource -> + if (is18Plus(rssSource.sortUrl)) { + handler.post { + App.INSTANCE.toast("${rssSource.sourceName}是18+网址,禁止导入.") + } + } else { + App.db.rssSourceDao().insert(rssSource) + } + } + } + + fun insertBookSource(vararg bookSources: BookSource) { + App.db.bookSourceDao().insert(*bookSources) + } + + private fun is18Plus(url: String?): Boolean { + url ?: return false + list18Plus.forEach { + if (url.contains(it)) { + return true + } + } + return false + } + +} \ No newline at end of file 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 1ee781220..a15cb2e77 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 @@ -9,6 +9,7 @@ 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.* @@ -162,7 +163,7 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application) } if (null != content) { GSON.fromJsonArray(content)?.let { - App.db.rssSourceDao().insert(*it.toTypedArray()) + SourceHelp.insertRssSource(*it.toTypedArray()) } } }.onSuccess { @@ -184,7 +185,7 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application) } } else { GSON.fromJsonArray(text1)?.let { - App.db.rssSourceDao().insert(*it.toTypedArray()) + SourceHelp.insertRssSource(*it.toTypedArray()) count = 1 } } @@ -199,7 +200,7 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application) rssSources.add(it) } } - App.db.rssSourceDao().insert(*rssSources.toTypedArray()) + SourceHelp.insertRssSource(*rssSources.toTypedArray()) "导入${rssSources.size}条" } text1.isAbsUrl() -> { @@ -225,7 +226,7 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application) sources.add(source) } } - App.db.rssSourceDao().insert(*sources.toTypedArray()) + SourceHelp.insertRssSource(*sources.toTypedArray()) return sources.size } return 0