多设备阅读记录叠加

pull/332/head
gedoor 4 years ago
parent 2f0d6fc2af
commit 0f8ee000fa
  1. 15
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  2. 2
      app/src/main/java/io/legado/app/data/dao/ReadRecordDao.kt
  3. 5
      app/src/main/java/io/legado/app/data/entities/ReadRecord.kt

@ -6,6 +6,7 @@ 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 io.legado.app.App
import io.legado.app.data.dao.* import io.legado.app.data.dao.*
import io.legado.app.data.entities.* import io.legado.app.data.entities.*
@ -15,7 +16,7 @@ import io.legado.app.data.entities.*
ReplaceRule::class, SearchBook::class, SearchKeyword::class, Cookie::class, ReplaceRule::class, SearchBook::class, SearchKeyword::class, Cookie::class,
RssSource::class, Bookmark::class, RssArticle::class, RssReadRecord::class, RssSource::class, Bookmark::class, RssArticle::class, RssReadRecord::class,
RssStar::class, TxtTocRule::class, ReadRecord::class, HttpTTS::class], RssStar::class, TxtTocRule::class, ReadRecord::class, HttpTTS::class],
version = 18, version = 19,
exportSchema = true exportSchema = true
) )
abstract class AppDatabase : RoomDatabase() { abstract class AppDatabase : RoomDatabase() {
@ -34,7 +35,8 @@ abstract class AppDatabase : RoomDatabase() {
migration_13_14, migration_13_14,
migration_14_15, migration_14_15,
migration_15_17, migration_15_17,
migration_17_18 migration_17_18,
migration_18_19
) )
.allowMainThreadQueries() .allowMainThreadQueries()
.build() .build()
@ -101,6 +103,15 @@ abstract class AppDatabase : RoomDatabase() {
database.execSQL("CREATE TABLE IF NOT EXISTS `httpTTS` (`id` INTEGER NOT NULL, `name` TEXT NOT NULL, `url` TEXT NOT NULL, PRIMARY KEY(`id`))") database.execSQL("CREATE TABLE IF NOT EXISTS `httpTTS` (`id` INTEGER NOT NULL, `name` TEXT NOT NULL, `url` TEXT NOT NULL, PRIMARY KEY(`id`))")
} }
} }
private val migration_18_19 = object : Migration(18, 19) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("CREATE TABLE IF NOT EXISTS `readRecordNew` (`androidId` TEXT NOT NULL, `bookName` TEXT NOT NULL, `readTime` INTEGER NOT NULL, PRIMARY KEY(`androidId`, `bookName`))")
database.execSQL("INSERT INTO readRecordNew(androidId, bookName, readTime) select '${App.androidId}' as androidId, bookName, readTime from readRecord")
database.execSQL("DROP TABLE readRecord")
database.execSQL("ALTER TABLE readRecordNew RENAME TO readRecord")
}
}
} }
abstract fun bookDao(): BookDao abstract fun bookDao(): BookDao

@ -12,7 +12,7 @@ interface ReadRecordDao {
@get:Query("select sum(readTime) from readRecord") @get:Query("select sum(readTime) from readRecord")
val allTime: Long val allTime: Long
@Query("select readTime from readRecord where bookName = :bookName") @Query("select sum(readTime) from readRecord where bookName = :bookName")
fun getReadTime(bookName: String): Long? fun getReadTime(bookName: String): Long?
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)

@ -1,11 +1,10 @@
package io.legado.app.data.entities package io.legado.app.data.entities
import androidx.room.Entity import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "readRecord") @Entity(tableName = "readRecord", primaryKeys = ["androidId", "bookName"])
data class ReadRecord( data class ReadRecord(
@PrimaryKey var androidId: String = "",
var bookName: String = "", var bookName: String = "",
var readTime: Long = 0L var readTime: Long = 0L
) )
Loading…
Cancel
Save