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.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.google.gson.JsonArray
import com.google.gson.JsonObject
@ -27,22 +26,28 @@ abstract class AppDatabase : RoomDatabase() {
companion object {
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 {
return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME)
// .fallbackToDestructiveMigration()
.addMigrations(MIGRATION_1_N)
.fallbackToDestructiveMigration()
.addCallback(callback)
.build()
}
val callback = object : Callback() {
override fun onOpen(db: SupportSQLiteDatabase) {
}
override fun onDestructiveMigration(db: 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 ->
database.query("select * from $form").let {
val ja = JsonArray()
@ -65,6 +70,7 @@ abstract class AppDatabase : RoomDatabase() {
FileHelp.getFile(FileHelp.getCachePath() + File.separator + "db" + File.separator + form + ".json")
.writeText(GSON.toJson(ja))
}
database.execSQL("drop table $form")
}
}
}

@ -57,15 +57,11 @@ class ReadRssActivity : VMBaseActivity<ReadRssViewModel>(R.layout.activity_rss_r
@SuppressLint("SetJavaScriptEnabled")
private fun initLiveData() {
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.rssArticleLiveData.value?.let {
upJavaScriptEnable()
val url = NetworkUtils.getAbsoluteURL(it.origin, it.link)
if (viewModel.rssSourceLiveData.value?.loadWithBaseUrl == true) {
if (viewModel.rssSource?.loadWithBaseUrl == true) {
webView.loadDataWithBaseURL(
url,
"<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 {
upJavaScriptEnable()
webView.loadUrl(it.url, it.headerMap)
})
}
@SuppressLint("SetJavaScriptEnabled")
private fun upJavaScriptEnable() {
if (viewModel.rssSource?.enableJs == true) {
webView.settings.javaScriptEnabled = true
}
}
private fun upStarMenu() {
if (viewModel.rssArticleLiveData.value?.star == true) {
starMenuItem?.setIcon(R.drawable.ic_star)

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

Loading…
Cancel
Save