From 9c7bae52a5e63a6a967648790d6e24ff494bab45 Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 26 Aug 2020 09:36:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=98=85=E8=AF=BB=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/data/dao/ReadRecordDao.kt | 9 +++++++++ .../io/legado/app/data/entities/ReadRecordShow.kt | 6 ++++++ .../java/io/legado/app/help/storage/Restore.kt | 13 ++++++++++++- .../io/legado/app/ui/about/ReadRecordActivity.kt | 14 +++++++++----- 4 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/io/legado/app/data/entities/ReadRecordShow.kt 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() } }