From 6c17ec8d9080a5749ec18cb79fd5e838b9ad5970 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 18 Nov 2019 09:51:41 +0800 Subject: [PATCH] up --- .../java/io/legado/app/data/AppDatabase.kt | 56 +++++++++++++------ .../legado/app/ui/rss/read/ReadRssActivity.kt | 2 +- .../rss/source/edit/RssSourceEditActivity.kt | 4 +- 3 files changed, 41 insertions(+), 21 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 ee30c1cc4..4b537d59c 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -1,20 +1,26 @@ package io.legado.app.data import android.content.Context +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 import io.legado.app.data.dao.* import io.legado.app.data.entities.* +import io.legado.app.help.FileHelp +import io.legado.app.utils.GSON +import java.io.File @Database( entities = [Book::class, BookGroup::class, BookSource::class, BookChapter::class, ReplaceRule::class, SearchBook::class, SearchKeyword::class, Cookie::class, RssSource::class, Bookmark::class, RssArticle::class], - version = 1, + version = 2, exportSchema = true ) abstract class AppDatabase : RoomDatabase() { @@ -22,31 +28,45 @@ abstract class AppDatabase : RoomDatabase() { companion object { private const val DATABASE_NAME = "legado.db" - private val MIGRATION_1_2: Migration = object : Migration(1, 2) { + private val MIGRATION_1_N: Migration = object : Migration(1, 2) { override fun migrate(database: SupportSQLiteDatabase) { - database.run { - // execSQL("ALTER TABLE parsers ADD COLUMN fulltextScript TEXT") - // execSQL("ALTER TABLE feeds ADD COLUMN lastUpdateTime INTEGER NOT NULL DEFAULT 0") - // execSQL("DELETE FROM entries WHERE rowid NOT IN (SELECT MIN(rowid) FROM entries GROUP BY link)") - // execSQL("CREATE UNIQUE INDEX index_entries_link ON entries(link)") - } + backup(database) } } fun createDatabase(context: Context): AppDatabase { - return Room.databaseBuilder( - context.applicationContext, - AppDatabase::class.java, - DATABASE_NAME - ) - // .addMigrations(MIGRATION_1_2) - // .addMigrations(MIGRATION_2_3) - // .addMigrations(MIGRATION_3_4) - // .addMigrations(MIGRATION_4_5) - // .addMigrations(MIGRATION_5_6) + return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME) +// .fallbackToDestructiveMigration() + .addMigrations(MIGRATION_1_N) .build() } + private fun backup(database: SupportSQLiteDatabase) { + val forms = arrayOf("books") + forms.forEach { form -> + database.query("select * from $form").let { + val ja = JsonArray() + while (it.moveToNext()) { + val jo = JsonObject() + for (i in 0 until it.columnCount) { + if (!it.isNull(i)) { + when (it.getType(i)) { + Cursor.FIELD_TYPE_FLOAT -> + jo.addProperty(it.getColumnName(i), it.getFloat(i)) + Cursor.FIELD_TYPE_INTEGER -> + jo.addProperty(it.getColumnName(i), it.getInt(i)) + else -> jo.addProperty(it.getColumnName(i), it.getString(i)) + } + } + } + ja.add(jo) + } + it.close() + FileHelp.getFile(FileHelp.getCachePath() + File.separator + "db" + File.separator + form + ".json") + .writeText(GSON.toJson(ja)) + } + } + } } abstract fun bookDao(): BookDao 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 928f8b04e..e797da671 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 @@ -64,7 +64,7 @@ class ReadRssActivity : VMBaseActivity(R.layout.activity_rss_r }) viewModel.contentLiveData.observe(this, Observer { content -> viewModel.rssArticleLiveData.value?.let { - val url = NetworkUtils.getAbsoluteURL(it.origin, it.link ?: "") + val url = NetworkUtils.getAbsoluteURL(it.origin, it.link) if (viewModel.rssSourceLiveData.value?.loadWithBaseUrl == true) { webView.loadDataWithBaseURL( url, diff --git a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt index c993073ef..2599fe1f3 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt @@ -131,11 +131,11 @@ class RssSourceEditActivity : add(EditEntity("ruleNextPage", rssSource?.ruleNextPage, R.string.r_next)) add(EditEntity("ruleTitle", rssSource?.ruleTitle, R.string.r_title)) add(EditEntity("rulePubDate", rssSource?.rulePubDate, R.string.r_date)) - add(EditEntity("ruleCategories", rssSource?.ruleCategories, R.string.r_categories)) add(EditEntity("ruleDescription", rssSource?.ruleDescription, R.string.r_description)) add(EditEntity("ruleImage", rssSource?.ruleImage, R.string.r_image)) add(EditEntity("ruleLink", rssSource?.ruleLink, R.string.r_link)) add(EditEntity("ruleContent", rssSource?.ruleContent, R.string.r_content)) + add(EditEntity("header", rssSource?.header, R.string.source_http_header)) } adapter.editEntities = sourceEntities } @@ -155,11 +155,11 @@ class RssSourceEditActivity : "ruleNextPage" -> source.ruleNextPage = it.value "ruleTitle" -> source.ruleTitle = it.value "rulePubDate" -> source.rulePubDate = it.value - "ruleCategories" -> source.ruleCategories = it.value "ruleDescription" -> source.ruleDescription = it.value "ruleImage" -> source.ruleImage = it.value "ruleLink" -> source.ruleLink = it.value "ruleContent" -> source.ruleContent = it.value + "header" -> source.header = it.value } } return source