pull/49/head
kunfei 5 years ago
parent 6c17ec8d90
commit 468daf86f4
  1. 28
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  2. 16
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt
  3. 18
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt

@ -5,7 +5,6 @@ import android.database.Cursor
import androidx.room.Database import androidx.room.Database
import androidx.room.Room import androidx.room.Room
import androidx.room.RoomDatabase import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase import androidx.sqlite.db.SupportSQLiteDatabase
import com.google.gson.JsonArray import com.google.gson.JsonArray
import com.google.gson.JsonObject import com.google.gson.JsonObject
@ -27,22 +26,28 @@ abstract class AppDatabase : RoomDatabase() {
companion object { companion object {
private const val DATABASE_NAME = "legado.db" private const val DATABASE_NAME = "legado.db"
private val MIGRATION_1_N: Migration = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
backup(database)
}
}
fun createDatabase(context: Context): AppDatabase { fun createDatabase(context: Context): AppDatabase {
return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME) return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME)
// .fallbackToDestructiveMigration() .fallbackToDestructiveMigration()
.addMigrations(MIGRATION_1_N) .addCallback(callback)
.build() .build()
} }
val callback = object : Callback() {
override fun onOpen(db: SupportSQLiteDatabase) {
}
override fun onDestructiveMigration(db: SupportSQLiteDatabase) {
}
}
private fun backup(database: SupportSQLiteDatabase) { private fun backup(database: SupportSQLiteDatabase) {
val forms = arrayOf("books") val forms = arrayOf(
"books", "book_groups", "bookmarks", "book_sources", "cookies",
"replace_rules", "rssSources", "search_keywords", "rssArticles"
)
forms.forEach { form -> forms.forEach { form ->
database.query("select * from $form").let { database.query("select * from $form").let {
val ja = JsonArray() val ja = JsonArray()
@ -65,6 +70,7 @@ abstract class AppDatabase : RoomDatabase() {
FileHelp.getFile(FileHelp.getCachePath() + File.separator + "db" + File.separator + form + ".json") FileHelp.getFile(FileHelp.getCachePath() + File.separator + "db" + File.separator + form + ".json")
.writeText(GSON.toJson(ja)) .writeText(GSON.toJson(ja))
} }
database.execSQL("drop table $form")
} }
} }
} }

@ -57,15 +57,11 @@ class ReadRssActivity : VMBaseActivity<ReadRssViewModel>(R.layout.activity_rss_r
@SuppressLint("SetJavaScriptEnabled") @SuppressLint("SetJavaScriptEnabled")
private fun initLiveData() { private fun initLiveData() {
viewModel.rssArticleLiveData.observe(this, Observer { upStarMenu() }) viewModel.rssArticleLiveData.observe(this, Observer { upStarMenu() })
viewModel.rssSourceLiveData.observe(this, Observer {
if (it.enableJs) {
webView.settings.javaScriptEnabled = true
}
})
viewModel.contentLiveData.observe(this, Observer { content -> viewModel.contentLiveData.observe(this, Observer { content ->
viewModel.rssArticleLiveData.value?.let { viewModel.rssArticleLiveData.value?.let {
upJavaScriptEnable()
val url = NetworkUtils.getAbsoluteURL(it.origin, it.link) val url = NetworkUtils.getAbsoluteURL(it.origin, it.link)
if (viewModel.rssSourceLiveData.value?.loadWithBaseUrl == true) { if (viewModel.rssSource?.loadWithBaseUrl == true) {
webView.loadDataWithBaseURL( webView.loadDataWithBaseURL(
url, url,
"<style>img{max-width:100%}</style>$content", "<style>img{max-width:100%}</style>$content",
@ -83,10 +79,18 @@ class ReadRssActivity : VMBaseActivity<ReadRssViewModel>(R.layout.activity_rss_r
} }
}) })
viewModel.urlLiveData.observe(this, Observer { viewModel.urlLiveData.observe(this, Observer {
upJavaScriptEnable()
webView.loadUrl(it.url, it.headerMap) webView.loadUrl(it.url, it.headerMap)
}) })
} }
@SuppressLint("SetJavaScriptEnabled")
private fun upJavaScriptEnable() {
if (viewModel.rssSource?.enableJs == true) {
webView.settings.javaScriptEnabled = true
}
}
private fun upStarMenu() { private fun upStarMenu() {
if (viewModel.rssArticleLiveData.value?.star == true) { if (viewModel.rssArticleLiveData.value?.star == true) {
starMenuItem?.setIcon(R.drawable.ic_star) starMenuItem?.setIcon(R.drawable.ic_star)

@ -12,8 +12,8 @@ import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
class ReadRssViewModel(application: Application) : BaseViewModel(application) { class ReadRssViewModel(application: Application) : BaseViewModel(application) {
var rssArticleLiveData = MutableLiveData<RssArticle>() var rssSource: RssSource? = null
val rssSourceLiveData = MutableLiveData<RssSource>() val rssArticleLiveData = MutableLiveData<RssArticle>()
val contentLiveData = MutableLiveData<String>() val contentLiveData = MutableLiveData<String>()
val urlLiveData = MutableLiveData<AnalyzeUrl>() val urlLiveData = MutableLiveData<AnalyzeUrl>()
@ -21,10 +21,7 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
execute { execute {
val origin = intent.getStringExtra("origin") val origin = intent.getStringExtra("origin")
val link = intent.getStringExtra("link") val link = intent.getStringExtra("link")
val rssSource = App.db.rssSourceDao().getByKey(origin) rssSource = App.db.rssSourceDao().getByKey(origin)
rssSource?.let {
rssSourceLiveData.postValue(it)
}
if (origin != null && link != null) { if (origin != null && link != null) {
App.db.rssArticleDao().get(origin, link)?.let { rssArticle -> App.db.rssArticleDao().get(origin, link)?.let { rssArticle ->
rssArticleLiveData.postValue(rssArticle) rssArticleLiveData.postValue(rssArticle)
@ -32,7 +29,7 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
contentLiveData.postValue(rssArticle.description) contentLiveData.postValue(rssArticle.description)
} else { } else {
rssSource?.let { rssSource?.let {
val ruleContent = rssSource.ruleContent val ruleContent = it.ruleContent
if (!ruleContent.isNullOrBlank()) { if (!ruleContent.isNullOrBlank()) {
loadContent(rssArticle, ruleContent) loadContent(rssArticle, ruleContent)
} else { } else {
@ -46,7 +43,12 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
} }
private fun loadUrl(rssArticle: RssArticle) { private fun loadUrl(rssArticle: RssArticle) {
val analyzeUrl = AnalyzeUrl(rssArticle.link, baseUrl = rssArticle.origin, useWebView = true) val analyzeUrl = AnalyzeUrl(
rssArticle.link,
baseUrl = rssArticle.origin,
useWebView = true,
headerMapF = rssSource?.getHeaderMap()
)
urlLiveData.postValue(analyzeUrl) urlLiveData.postValue(analyzeUrl)
} }

Loading…
Cancel
Save