优化阅读时间记录

pull/341/head
gedoor 4 years ago
parent 2f95f1ebb2
commit 9c7bae52a5
  1. 9
      app/src/main/java/io/legado/app/data/dao/ReadRecordDao.kt
  2. 6
      app/src/main/java/io/legado/app/data/entities/ReadRecordShow.kt
  3. 13
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  4. 14
      app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.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<ReadRecord>
@get:Query("select bookName, sum(readTime) as readTime from readRecord group by bookName")
val allShow: List<ReadRecordShow>
@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)
}

@ -0,0 +1,6 @@
package io.legado.app.data.entities
data class ReadRecordShow(
var bookName: String = "",
var readTime: Long = 0L
)

@ -135,7 +135,18 @@ object Restore {
App.db.txtTocRule().insert(*it.toTypedArray())
}
fileToListT<ReadRecord>(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<HttpTTS>(path, "httpTTS.json")?.let {
App.db.httpTTSDao().insert(*it.toTypedArray())

@ -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<ReadRecord>(context, R.layout.item_read_record) {
SimpleRecyclerAdapter<ReadRecordShow>(context, R.layout.item_read_record) {
override fun convert(holder: ItemViewHolder, item: ReadRecord, payloads: MutableList<Any>) {
override fun convert(
holder: ItemViewHolder,
item: ReadRecordShow,
payloads: MutableList<Any>
) {
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()
}
}

Loading…
Cancel
Save