From 468daf86f451dff0960ef4d4503dcc38bb911860 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 18 Nov 2019 10:35:14 +0800 Subject: [PATCH] up --- .../java/io/legado/app/data/AppDatabase.kt | 28 +++++++++++-------- .../legado/app/ui/rss/read/ReadRssActivity.kt | 16 +++++++---- .../app/ui/rss/read/ReadRssViewModel.kt | 18 ++++++------ 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt index 4b537d59c..555f827d2 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.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") } } } diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt index e797da671..5bee434a2 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt @@ -57,15 +57,11 @@ class ReadRssActivity : VMBaseActivity(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, "$content", @@ -83,10 +79,18 @@ class ReadRssActivity : VMBaseActivity(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) diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt index 02a5ccc89..51e1ca960 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt @@ -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() - val rssSourceLiveData = MutableLiveData() + var rssSource: RssSource? = null + val rssArticleLiveData = MutableLiveData() val contentLiveData = MutableLiveData() val urlLiveData = MutableLiveData() @@ -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) }