From f363d0f90df9e684e78e92b81dc0125b8ee1fe1e Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 15 Mar 2021 11:35:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/data/AppDatabase.kt | 16 ++++++++++++++-- .../java/io/legado/app/data/dao/ReadRecordDao.kt | 2 +- .../io/legado/app/data/entities/ReadRecord.kt | 4 ++-- .../java/io/legado/app/help/storage/Restore.kt | 4 ++-- 4 files changed, 19 insertions(+), 7 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 a1e9db1a1..451c659c3 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -23,7 +23,7 @@ val appDb by lazy { RssSource::class, Bookmark::class, RssArticle::class, RssReadRecord::class, RssStar::class, TxtTocRule::class, ReadRecord::class, HttpTTS::class, Cache::class, RuleSub::class, EpubChapter::class], - version = 30, + version = 31, exportSchema = true ) abstract class AppDatabase : RoomDatabase() { @@ -59,7 +59,7 @@ abstract class AppDatabase : RoomDatabase() { migration_14_15, migration_15_17, migration_17_18, migration_18_19, migration_19_20, migration_20_21, migration_21_22, migration_22_23, migration_23_24, migration_24_25, migration_25_26, migration_26_27, - migration_27_28, migration_28_29, migration_29_30 + migration_27_28, migration_28_29, migration_29_30, migration_30_31 ) .allowMainThreadQueries() .addCallback(dbCallback) @@ -278,6 +278,18 @@ abstract class AppDatabase : RoomDatabase() { database.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS `index_epubChapters_bookUrl_href` ON `epubChapters` (`bookUrl`, `href`)") } } + + private val migration_30_31 = object : Migration(30, 31) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("ALTER TABLE readRecord RENAME TO readRecord1") + database.execSQL( + """ + CREATE TABLE IF NOT EXISTS `readRecord` (`deviceId` TEXT NOT NULL, `bookName` TEXT NOT NULL, `readTime` INTEGER NOT NULL, PRIMARY KEY(`deviceId`, `bookName`)) + """ + ) + database.execSQL("insert into readRecord (deviceId, bookName, readTime) select androidId, bookName, readTime from readRecord1") + } + } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/dao/ReadRecordDao.kt b/app/src/main/java/io/legado/app/data/dao/ReadRecordDao.kt index 9363e2a56..7aaaf916e 100644 --- a/app/src/main/java/io/legado/app/data/dao/ReadRecordDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/ReadRecordDao.kt @@ -19,7 +19,7 @@ interface ReadRecordDao { @Query("select sum(readTime) from readRecord where bookName = :bookName") fun getReadTime(bookName: String): Long? - @Query("select readTime from readRecord where androidId = :androidId and bookName = :bookName") + @Query("select readTime from readRecord where deviceId = :androidId and bookName = :bookName") fun getReadTime(androidId: String, bookName: String): Long? @Insert(onConflict = OnConflictStrategy.REPLACE) diff --git a/app/src/main/java/io/legado/app/data/entities/ReadRecord.kt b/app/src/main/java/io/legado/app/data/entities/ReadRecord.kt index 60f577477..2b2b5caf4 100644 --- a/app/src/main/java/io/legado/app/data/entities/ReadRecord.kt +++ b/app/src/main/java/io/legado/app/data/entities/ReadRecord.kt @@ -2,9 +2,9 @@ package io.legado.app.data.entities import androidx.room.Entity -@Entity(tableName = "readRecord", primaryKeys = ["androidId", "bookName"]) +@Entity(tableName = "readRecord", primaryKeys = ["deviceId", "bookName"]) data class ReadRecord( - var androidId: String = "", + var deviceId: String = "", var bookName: String = "", var readTime: Long = 0L ) \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index 16340aeea..435946e38 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.kt @@ -149,11 +149,11 @@ object Restore { fileToListT(path, "readRecord.json")?.let { it.forEach { readRecord -> //判断是不是本机记录 - if (readRecord.androidId != androidId) { + if (readRecord.deviceId != androidId) { appDb.readRecordDao.insert(readRecord) } else { val time = appDb.readRecordDao - .getReadTime(readRecord.androidId, readRecord.bookName) + .getReadTime(readRecord.deviceId, readRecord.bookName) if (time == null || time < readRecord.readTime) { appDb.readRecordDao.insert(readRecord) }