I am using paging 3 library for get objects, and I need add some additional elements to top of list I can do this by calling pagingData.insertHeaderItem(...).insertHeaderItem(...).insertHeaderItem(...) etc
but I need populate header items from list: val list = listOf(Obj("name1"), Obj("name2"), Obj("name3")) how I can do it?
example
fun fetchObjs(): Flow<PagingData<Obj>> {
return Pager<Int, Obj>(config = PagingConfig(pageSize = 1),
pagingSourceFactory = { ObjPagingSource(videoApi = videoApi) })
.flow
.map { pagingData ->
pagingData.insertHeaderItem(
TerminalSeparatorType.FULLY_COMPLETE,
Obj("name1")
)
I try this construction
.map { pagingData ->
getList().map {
pagingData.insertHeaderItem(TerminalSeparatorType.FULLY_COMPLETE,
it)
}
}
but it's not working
To insert a list of header items, you can use the fold function from the Kotlin standard library. It allows you to iterate over the list and apply an operation for each item, using the result of the previous operation as the input for the next one.
fun fetchObjs(): Flow<PagingData<Obj>> {
return Pager<Int, Obj>(config = PagingConfig(pageSize = 1),
pagingSourceFactory = { ObjPagingSource(videoApi = videoApi) })
.flow
.map { pagingData ->
val headerList = listOf(Obj("name1"), Obj("name2"), Obj("name3"))
headerList.fold(pagingData) { acc, item ->
acc.insertHeaderItem(TerminalSeparatorType.FULLY_COMPLETE, item)
}
}
}