Hi my question is how to transform data from remote to tree structure or any structure , so how we do transormation while fetching data, because when we call collectAsLazyPagingItems() we cant do modification or such thing so how to do it properly
Thanks
This method
val pagingData = mainViewModel.youtubeApi().collectAsLazyPagingItems()
we have to put over data directly into LAzyColumn
so where to change to transform data.
class RemotePagingSource constructor(private val listSection : ListSection<Video>,
private val youtubeClient: YoutubeClient
) : PagingSource<String, Video>() {
override fun getRefreshKey(state: PagingState<String, Video>): String? {
Log.i("RemoteSourceData" , "PagingSource ${state.anchorPosition}")
return state.anchorPosition?.let { anchorPosition ->
state.closestPageToPosition(anchorPosition)?.nextKey }
}
val coroutineScope = CoroutineScope(Dispatchers.IO)
override suspend fun load(params: LoadParams<String>): LoadResult<String, Video> {
return try {
val previousKey = if(params is LoadParams.Prepend) params.key else null
val nextKey: String = if(params is Append) params.key else ""
val pageSize = params.loadSize
val youtube = coroutineScope.async {
// Log.i("RemotePagingSource" , "Thread ${Thread.currentThread().name}")
youtubeClient.youtubeApi(nextKey , pageSize)
}.await()
youtube.items.forEach {
listSection.add(it) }
Log.i("RemotePagingSource" , "Source ${youtube.nextPageToken}")
LoadResult.Page(
data = youtube.items ,
prevKey = previousKey ,
nextKey = if(youtube.nextPageToken!=null) youtube.nextPageToken else null )
} catch (e: IOException) {
LoadResult.Error(e)
} catch (e: HttpException) {
LoadResult.Error(e)
}
}
}
and this Pager Code as well
@OptIn(ExperimentalPagingApi::class)
fun getYoutubePost() = Pager (
config = PagingConfig(7 , maxSize = 200)){
RemotePagingSource( sectionList,youtubeClient)
}.flow