mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-17 16:47:01 +05:30
desktop: wire in API into desktop client
This commit is contained in:
parent
2fa682048a
commit
2abbda7641
3 changed files with 101 additions and 15 deletions
31
desktop/src/jvmMain/kotlin/Paging.kt
Normal file
31
desktop/src/jvmMain/kotlin/Paging.kt
Normal file
|
@ -0,0 +1,31 @@
|
|||
import androidx.paging.PagingConfig
|
||||
import androidx.paging.cachedIn
|
||||
import com.kuuurt.paging.multiplatform.Pager
|
||||
import com.kuuurt.paging.multiplatform.PagingResult
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.FlowPreview
|
||||
|
||||
@OptIn(FlowPreview::class, ExperimentalCoroutinesApi::class)
|
||||
class Paging(
|
||||
private val coroutineScope: CoroutineScope,
|
||||
) {
|
||||
private val api = Api()
|
||||
private val pager =
|
||||
Pager(
|
||||
clientScope = coroutineScope,
|
||||
config = PagingConfig(20),
|
||||
initialKey = 1,
|
||||
getItems = { currentKey, _ ->
|
||||
val items = api.api.getHottestPosts(currentKey)
|
||||
PagingResult(
|
||||
items = items,
|
||||
currentKey = currentKey,
|
||||
prevKey = { if (currentKey == 1) null else currentKey - 1 },
|
||||
nextKey = { currentKey + 1 },
|
||||
)
|
||||
}
|
||||
)
|
||||
val pagingData
|
||||
get() = pager.pagingData.cachedIn(coroutineScope)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue