feat: 优化

pull/219/head
kunfei 5 years ago
parent b04442f3ec
commit b57a63f2d2
  1. 44
      app/src/main/java/io/legado/app/help/SourceHelp.kt
  2. 9
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.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
}
}

@ -9,6 +9,7 @@ import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.RssSource 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.http.HttpHelper
import io.legado.app.help.storage.Restore.jsonPath import io.legado.app.help.storage.Restore.jsonPath
import io.legado.app.utils.* import io.legado.app.utils.*
@ -162,7 +163,7 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
} }
if (null != content) { if (null != content) {
GSON.fromJsonArray<RssSource>(content)?.let { GSON.fromJsonArray<RssSource>(content)?.let {
App.db.rssSourceDao().insert(*it.toTypedArray()) SourceHelp.insertRssSource(*it.toTypedArray())
} }
} }
}.onSuccess { }.onSuccess {
@ -184,7 +185,7 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
} }
} else { } else {
GSON.fromJsonArray<RssSource>(text1)?.let { GSON.fromJsonArray<RssSource>(text1)?.let {
App.db.rssSourceDao().insert(*it.toTypedArray()) SourceHelp.insertRssSource(*it.toTypedArray())
count = 1 count = 1
} }
} }
@ -199,7 +200,7 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
rssSources.add(it) rssSources.add(it)
} }
} }
App.db.rssSourceDao().insert(*rssSources.toTypedArray()) SourceHelp.insertRssSource(*rssSources.toTypedArray())
"导入${rssSources.size}" "导入${rssSources.size}"
} }
text1.isAbsUrl() -> { text1.isAbsUrl() -> {
@ -225,7 +226,7 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
sources.add(source) sources.add(source)
} }
} }
App.db.rssSourceDao().insert(*sources.toTypedArray()) SourceHelp.insertRssSource(*sources.toTypedArray())
return sources.size return sources.size
} }
return 0 return 0

Loading…
Cancel
Save