Skip to content

Commit f2b0416

Browse files
committed
aa
1 parent 3744cb6 commit f2b0416

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

app/src/main/java/ceui/pixiv/db/RemoteKey.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ import androidx.room.PrimaryKey
66
@Entity(tableName = "remote_keys")
77
data class RemoteKey(
88
@PrimaryKey val recordType: Int,
9-
val nextPageUrl: String?
9+
val nextPageUrl: String?,
10+
val lastUpdatedTime: Long,
1011
)

app/src/main/java/ceui/pixiv/paging/PagingRemoteMediator.kt

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import ceui.lisa.database.AppDatabase
1010
import ceui.lisa.models.ModelObject
1111
import ceui.pixiv.db.GeneralEntity
1212
import ceui.pixiv.db.RemoteKey
13+
import kotlinx.coroutines.delay
14+
import timber.log.Timber
1315

1416
@OptIn(ExperimentalPagingApi::class)
1517
class PagingRemoteMediator<ObjectT : ModelObject>(
@@ -26,12 +28,28 @@ class PagingRemoteMediator<ObjectT : ModelObject>(
2628
val generalDao = db.generalDao()
2729
val remoteKeyDao = db.remoteKeyDao()
2830

31+
val cacheTimeoutMs = 5 * 60 * 1000L // 5分钟
32+
val now = System.currentTimeMillis()
33+
34+
val remoteKey = remoteKeyDao.getRemoteKey(recordType)
35+
val shouldSkipNetwork = if (loadType == LoadType.REFRESH) {
36+
remoteKey?.lastUpdatedTime?.let {
37+
now - it < cacheTimeoutMs
38+
} ?: false
39+
} else {
40+
false
41+
}
42+
43+
if (shouldSkipNetwork) {
44+
delay(800L)
45+
return MediatorResult.Success(endOfPaginationReached = false)
46+
}
47+
2948
val nextPageUrl = when (loadType) {
3049
LoadType.REFRESH -> null
3150
LoadType.PREPEND -> return MediatorResult.Success(endOfPaginationReached = true)
3251
LoadType.APPEND -> {
33-
val key = remoteKeyDao.getRemoteKey(recordType)
34-
key?.nextPageUrl
52+
remoteKey?.nextPageUrl
3553
?: return MediatorResult.Success(endOfPaginationReached = true)
3654
}
3755
}
@@ -59,7 +77,12 @@ class PagingRemoteMediator<ObjectT : ModelObject>(
5977
}
6078

6179
generalDao.insertAll(entities)
62-
remoteKeyDao.insert(RemoteKey(recordType, newNextPageUrl))
80+
val newRemoteKey = RemoteKey(
81+
recordType = recordType,
82+
nextPageUrl = newNextPageUrl,
83+
lastUpdatedTime = now
84+
)
85+
remoteKeyDao.insert(newRemoteKey)
6386
}
6487

6588
return MediatorResult.Success(endOfPaginationReached = newNextPageUrl == null)

0 commit comments

Comments
 (0)