Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
Fixes refresh for iOS, Adds for iOS to listen to loading states
Browse files Browse the repository at this point in the history
  • Loading branch information
kuuuurt committed Sep 8, 2022
1 parent 51c938a commit 64dd9c4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [0.5.0] - 2022-09-08
### What's new
- Fixes refresh for iOS
- Adds `pagingState` for iOS to listen to loading states
- Library Updates:
- Kotlin to 1.7.10
- Coroutines to 1.6.3
Expand Down
25 changes: 22 additions & 3 deletions paging/src/iosMain/kotlin/com/kuuurt/paging/multiplatform/Pager.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kuuurt.paging.multiplatform

import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
Expand All @@ -23,7 +24,10 @@ actual class Pager<K : Any, V : Any> actual constructor(
) {

private val _pagingData = MutableStateFlow<PagingData<V>?>(null)
actual val pagingData: Flow<PagingData<V>> get() = _pagingData.filterNotNull()
actual val pagingData: Flow<PagingData<V>> = _pagingData.filterNotNull()

private val _pagingState = MutableStateFlow<PagingState>(PagingState.Success)
val pagingState: Flow<PagingState> = _pagingState.filterNotNull()

private val _hasNextPage = MutableStateFlow(true)
val hasNextPage: Boolean
Expand All @@ -37,7 +41,7 @@ actual class Pager<K : Any, V : Any> actual constructor(

fun refresh() {
currentPagingResult.value = null
_hasNextPage.value = false
_hasNextPage.value = true
loadNext()
}

Expand All @@ -61,13 +65,21 @@ actual class Pager<K : Any, V : Any> actual constructor(
}

if (key != null && hasNextPage) {
clientScope.launch {
clientScope.launch(CoroutineExceptionHandler { _, throwable ->
_pagingState.value = PagingState.Error(throwable)
}) {
_pagingState.value = if (currentPagingResult.value?.items?.isEmpty() == true) {
PagingState.LoadingInitial
} else {
PagingState.LoadingMore
}
val newPagingResult = getItems(key, config.pageSize)
_pagingData.value = _pagingData.value?.toMutableList()?.apply {
addAll(newPagingResult.items)
}?.toPagingData() ?: newPagingResult.items.toPagingData()
_hasNextPage.value = newPagingResult.items.size >= config.pageSize
currentPagingResult.value = newPagingResult
_pagingState.value = PagingState.Success
}
}
}
Expand All @@ -76,4 +88,11 @@ actual class Pager<K : Any, V : Any> actual constructor(
PREVIOUS,
NEXT
}
}

sealed interface PagingState {
object Success : PagingState
object LoadingMore : PagingState
object LoadingInitial : PagingState
data class Error(val error: Throwable) : PagingState
}

0 comments on commit 64dd9c4

Please sign in to comment.