Skip to content

Commit

Permalink
Merge pull request #63 from icerockdev/#62-fix-load-next-page-error-s…
Browse files Browse the repository at this point in the history
…tuck

#62 fix load next page error stuck
  • Loading branch information
anton6tak authored Sep 12, 2022
2 parents e8a2a31 + 006f8bd commit 859d9e8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ coroutinesVersion = "1.6.0-native-mt"
mokoMvvmVersion = "0.12.0"
mokoResourcesVersion = "0.18.0"
mokoUnitsVersion = "0.8.0"
mokoPagingVersion = "0.7.1"
mokoPagingVersion = "0.7.2"

[libraries]
appCompat = { module = "androidx.appcompat:appcompat", version.ref = "androidAppCompatVersion" }
Expand Down
42 changes: 23 additions & 19 deletions paging/src/commonMain/kotlin/dev/icerock/moko/paging/Pagination.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import dev.icerock.moko.mvvm.livedata.readOnly
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlin.coroutines.CoroutineContext
Expand Down Expand Up @@ -86,26 +87,29 @@ class Pagination<Item>(

@Suppress("TooGenericExceptionCaught")
try {
loadNextPageDeferred = this.async {
val currentList = mStateStorage.value.dataValue()
?: throw IllegalStateException("Try to load next page when list is empty")
// load next page items
val items = dataSource.loadPage(currentList)
// remove already exist items
val newItems = items.filter { item ->
val existsItem = currentList.firstOrNull { comparator.compare(item, it) == 0 }
existsItem == null
loadNextPageDeferred = coroutineScope {
async {
val currentList = mStateStorage.value.dataValue()
?: throw IllegalStateException("Try to load next page when list is empty")
// load next page items
val items = dataSource.loadPage(currentList)
// remove already exist items
val newItems = items.filter { item ->
val existsItem =
currentList.firstOrNull { comparator.compare(item, it) == 0 }
existsItem == null
}
// append new items to current list
val newList = currentList.plus(newItems)
// mark end of list if no new items
if (newItems.isEmpty()) {
mEndOfList.value = true
} else {
// save
mStateStorage.value = newList.asState()
}
newList
}
// append new items to current list
val newList = currentList.plus(newItems)
// mark end of list if no new items
if (newItems.isEmpty()) {
mEndOfList.value = true
} else {
// save
mStateStorage.value = newList.asState()
}
newList
}
val newList = loadNextPageDeferred!!.await()

Expand Down

0 comments on commit 859d9e8

Please sign in to comment.