Skip to content

Commit

Permalink
修复分页缓存问题
Browse files Browse the repository at this point in the history
  • Loading branch information
dora4 committed Sep 1, 2024
1 parent 640dab6 commit f2d4595
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dcache/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ afterEvaluate {
from(components["release"])
groupId = "com.github.dora4"
artifactId = "dcache-android"
version = "2.4.4"
version = "2.4.5"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ abstract class DoraPageDatabaseCacheRepository<T : OrmTable>(context: Context)
return lastPage == pageNo
}

fun isOutOfPageRange() : Boolean {
val lastPage = if (totalSize % pageSize == 0) totalSize / pageSize - 1 else totalSize / pageSize
return pageNo < 0 || lastPage < pageNo
}

fun observeData(owner: LifecycleOwner, adapter: AdapterDelegate<T>) {
getListLiveData().observe(owner) {
if (pageNo == 0) {
Expand Down Expand Up @@ -228,7 +233,7 @@ abstract class DoraPageDatabaseCacheRepository<T : OrmTable>(context: Context)
if (!checkValuesNotNull()) throw IllegalArgumentException("Query parameter would be null, checkValuesNotNull return false.")
totalSize = (listCacheHolder as DoraListDatabaseCacheHolder<T>)
.queryCacheSize(query()).toInt()
if (isLastPage()) {
if (isOutOfPageRange()) {
listener?.onLoad(OnLoadStateListener.FAILURE)
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ abstract class DoraPageFlowDatabaseCacheRepository<M, T : OrmTable>(context: Con
return lastPage == pageNo
}

fun isOutOfPageRange() : Boolean {
val lastPage = if (totalSize % pageSize == 0) totalSize / pageSize - 1 else totalSize / pageSize
return pageNo < 0 || lastPage < pageNo
}

suspend fun observeData(adapter: AdapterDelegate<T>) {
getListFlowData().collect {
if (pageNo == 0) {
Expand Down Expand Up @@ -232,7 +237,7 @@ abstract class DoraPageFlowDatabaseCacheRepository<M, T : OrmTable>(context: Con
if (!checkValuesNotNull()) throw IllegalArgumentException("Query parameter would be null, checkValuesNotNull return false.")
totalSize = (listCacheHolder as DoraListDatabaseCacheHolder<T>)
.queryCacheSize(query()).toInt()
if (isLastPage()) {
if (isOutOfPageRange()) {
listener?.onLoad(OnLoadStateListener.FAILURE)
return false
}
Expand Down

0 comments on commit f2d4595

Please sign in to comment.