pull/49/head
kunfei 5 years ago
parent f9b228538d
commit 6c17ec8d90
  1. 56
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  2. 2
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt
  3. 4
      app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt

@ -1,20 +1,26 @@
package io.legado.app.data package io.legado.app.data
import android.content.Context import android.content.Context
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.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase 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.dao.*
import io.legado.app.data.entities.* import io.legado.app.data.entities.*
import io.legado.app.help.FileHelp
import io.legado.app.utils.GSON
import java.io.File
@Database( @Database(
entities = [Book::class, BookGroup::class, BookSource::class, BookChapter::class, ReplaceRule::class, entities = [Book::class, BookGroup::class, BookSource::class, BookChapter::class, ReplaceRule::class,
SearchBook::class, SearchKeyword::class, Cookie::class, RssSource::class, Bookmark::class, SearchBook::class, SearchKeyword::class, Cookie::class, RssSource::class, Bookmark::class,
RssArticle::class], RssArticle::class],
version = 1, version = 2,
exportSchema = true exportSchema = true
) )
abstract class AppDatabase : RoomDatabase() { abstract class AppDatabase : RoomDatabase() {
@ -22,31 +28,45 @@ 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_2: Migration = object : Migration(1, 2) { private val MIGRATION_1_N: Migration = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) { override fun migrate(database: SupportSQLiteDatabase) {
database.run { backup(database)
// 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)")
}
} }
} }
fun createDatabase(context: Context): AppDatabase { fun createDatabase(context: Context): AppDatabase {
return Room.databaseBuilder( return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME)
context.applicationContext, // .fallbackToDestructiveMigration()
AppDatabase::class.java, .addMigrations(MIGRATION_1_N)
DATABASE_NAME
)
// .addMigrations(MIGRATION_1_2)
// .addMigrations(MIGRATION_2_3)
// .addMigrations(MIGRATION_3_4)
// .addMigrations(MIGRATION_4_5)
// .addMigrations(MIGRATION_5_6)
.build() .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 abstract fun bookDao(): BookDao

@ -64,7 +64,7 @@ class ReadRssActivity : VMBaseActivity<ReadRssViewModel>(R.layout.activity_rss_r
}) })
viewModel.contentLiveData.observe(this, Observer { content -> viewModel.contentLiveData.observe(this, Observer { content ->
viewModel.rssArticleLiveData.value?.let { 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) { if (viewModel.rssSourceLiveData.value?.loadWithBaseUrl == true) {
webView.loadDataWithBaseURL( webView.loadDataWithBaseURL(
url, url,

@ -131,11 +131,11 @@ class RssSourceEditActivity :
add(EditEntity("ruleNextPage", rssSource?.ruleNextPage, R.string.r_next)) add(EditEntity("ruleNextPage", rssSource?.ruleNextPage, R.string.r_next))
add(EditEntity("ruleTitle", rssSource?.ruleTitle, R.string.r_title)) add(EditEntity("ruleTitle", rssSource?.ruleTitle, R.string.r_title))
add(EditEntity("rulePubDate", rssSource?.rulePubDate, R.string.r_date)) 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("ruleDescription", rssSource?.ruleDescription, R.string.r_description))
add(EditEntity("ruleImage", rssSource?.ruleImage, R.string.r_image)) add(EditEntity("ruleImage", rssSource?.ruleImage, R.string.r_image))
add(EditEntity("ruleLink", rssSource?.ruleLink, R.string.r_link)) add(EditEntity("ruleLink", rssSource?.ruleLink, R.string.r_link))
add(EditEntity("ruleContent", rssSource?.ruleContent, R.string.r_content)) add(EditEntity("ruleContent", rssSource?.ruleContent, R.string.r_content))
add(EditEntity("header", rssSource?.header, R.string.source_http_header))
} }
adapter.editEntities = sourceEntities adapter.editEntities = sourceEntities
} }
@ -155,11 +155,11 @@ class RssSourceEditActivity :
"ruleNextPage" -> source.ruleNextPage = it.value "ruleNextPage" -> source.ruleNextPage = it.value
"ruleTitle" -> source.ruleTitle = it.value "ruleTitle" -> source.ruleTitle = it.value
"rulePubDate" -> source.rulePubDate = it.value "rulePubDate" -> source.rulePubDate = it.value
"ruleCategories" -> source.ruleCategories = it.value
"ruleDescription" -> source.ruleDescription = it.value "ruleDescription" -> source.ruleDescription = it.value
"ruleImage" -> source.ruleImage = it.value "ruleImage" -> source.ruleImage = it.value
"ruleLink" -> source.ruleLink = it.value "ruleLink" -> source.ruleLink = it.value
"ruleContent" -> source.ruleContent = it.value "ruleContent" -> source.ruleContent = it.value
"header" -> source.header = it.value
} }
} }
return source return source

Loading…
Cancel
Save