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 d7619e9a8..991eaf61c 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 @@ -2,6 +2,7 @@ package io.legado.app.data.dao import androidx.room.* import io.legado.app.data.entities.ReadRecord +import io.legado.app.data.entities.ReadRecordShow @Dao interface ReadRecordDao { @@ -9,12 +10,18 @@ interface ReadRecordDao { @get:Query("select * from readRecord") val all: List + @get:Query("select bookName, sum(readTime) as readTime from readRecord group by bookName") + val allShow: List + @get:Query("select sum(readTime) from readRecord") val allTime: Long @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") + fun getReadTime(androidId: String, bookName: String): Long? + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg readRecord: ReadRecord) @@ -24,4 +31,6 @@ interface ReadRecordDao { @Delete fun delete(vararg record: ReadRecord) + @Query("delete from readRecord where bookName = :bookName") + fun deleteByName(bookName: String) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/ReadRecordShow.kt b/app/src/main/java/io/legado/app/data/entities/ReadRecordShow.kt new file mode 100644 index 000000000..79795b0d9 --- /dev/null +++ b/app/src/main/java/io/legado/app/data/entities/ReadRecordShow.kt @@ -0,0 +1,6 @@ +package io.legado.app.data.entities + +data class ReadRecordShow( + 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 e8800f542..63202a5c5 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 @@ -135,7 +135,18 @@ object Restore { App.db.txtTocRule().insert(*it.toTypedArray()) } fileToListT(path, "readRecord.json")?.let { - App.db.readRecordDao().insert(*it.toTypedArray()) + it.forEach { readRecord -> + //判断是不是本机记录 + if (readRecord.androidId != App.androidId) { + App.db.readRecordDao().insert(readRecord) + } else { + val time = App.db.readRecordDao() + .getReadTime(readRecord.androidId, readRecord.bookName) + if (time == null || time < readRecord.readTime) { + App.db.readRecordDao().insert(readRecord) + } + } + } } fileToListT(path, "httpTTS.json")?.let { App.db.httpTTSDao().insert(*it.toTypedArray()) diff --git a/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt b/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt index 51e575dff..01a71f4ad 100644 --- a/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt +++ b/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt @@ -8,7 +8,7 @@ import io.legado.app.R import io.legado.app.base.BaseActivity import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter -import io.legado.app.data.entities.ReadRecord +import io.legado.app.data.entities.ReadRecordShow import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.noButton import io.legado.app.lib.dialogs.okButton @@ -45,7 +45,7 @@ class ReadRecordActivity : BaseActivity(R.layout.activity_read_record) { withContext(Main) { tv_read_time.text = formatDuring(allTime) } - val readRecords = App.db.readRecordDao().all + val readRecords = App.db.readRecordDao().allShow withContext(Main) { adapter.setItems(readRecords) } @@ -53,9 +53,13 @@ class ReadRecordActivity : BaseActivity(R.layout.activity_read_record) { } inner class RecordAdapter(context: Context) : - SimpleRecyclerAdapter(context, R.layout.item_read_record) { + SimpleRecyclerAdapter(context, R.layout.item_read_record) { - override fun convert(holder: ItemViewHolder, item: ReadRecord, payloads: MutableList) { + override fun convert( + holder: ItemViewHolder, + item: ReadRecordShow, + payloads: MutableList + ) { holder.itemView.apply { tv_book_name.text = item.bookName tv_read_time.text = formatDuring(item.readTime) @@ -69,7 +73,7 @@ class ReadRecordActivity : BaseActivity(R.layout.activity_read_record) { alert(R.string.delete, R.string.sure_del) { okButton { getItem(holder.layoutPosition)?.let { - App.db.readRecordDao().delete(it) + App.db.readRecordDao().deleteByName(it.bookName) initData() } }