@@ -10,6 +10,8 @@ import ceui.lisa.database.AppDatabase
1010import ceui.lisa.models.ModelObject
1111import ceui.pixiv.db.GeneralEntity
1212import ceui.pixiv.db.RemoteKey
13+ import kotlinx.coroutines.delay
14+ import timber.log.Timber
1315
1416@OptIn(ExperimentalPagingApi ::class )
1517class 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