My application is something like the mobile version of Youtube, it has a search by keyword, that is, (q = something), for searching I use https://www.googleapis.com/youtube/v3/search?q=UtopiaShow&part=snippet&key={API_KEY} this request, but it does not return in detail about the channels, that is, their number of subscribers and videos as in YouTube. I was told that for this I need to make a request to /channel endpoint and pass the channel Id received from /search endpoint there
interface SearchApiService { @GET("search") suspend fun getSearch( @Query("q") q: String, @Query("pageToken") page: String, @Query("maxResults") max: Int, @Query("type") type: String = "video,channel,playlist", @Query("part") part: String = "snippet", @Query("order") order: String = "relevance", @Query("videoType") videoType: String = "any", ): YoutubeResponseDto<SearchItemsDto.SearchDto> @GET("channels") suspend fun fetchChannel( @Query("id") id: String?, @Query("part") part: String = "snippet,statistics" ): YoutubeResponseDto<SearchItemsDto.ChannelsDto>}
This my api service. in the request parameters /channel endpoint there is an id: String there I need to pass the channelId received from /search endpoint, as I did:
class ChannelsPagingSource(private val apiService: SearchApiService,) : PagingSource<String, SearchItemsDto.ChannelsDto>() {override fun getRefreshKey(state: PagingState<String, SearchItemsDto.ChannelsDto>): String? { return state.anchorPosition?.let { val anchorPage = state.closestPageToPosition(it) anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey }}override suspend fun load(params: LoadParams<String>): LoadResult<String, SearchItemsDto.ChannelsDto> { return try { val nextPageToken = params.key ?: "" val channelIdResponse = apiService.getSearch("", nextPageToken, params.loadSize) val channelId = channelIdResponse.items?.get(0)?.snippet?.channelId val response = apiService.fetchChannel(channelId) Log.i("channelId", channelId.toString()) val data = response.items!! LoadResult.Page( data = data, prevKey = null, nextKey = response.nextPageToken ) } catch (exception: IOException) { LoadResult.Error(exception) } catch (exception: HttpException) { LoadResult.Error(exception) } }}
since they have one Api service, I created the channelIdResponse variable where I turned to the getSearch() request and got the channelId from there and passed it to fetchChannels().
SearchItems is a sealed class inside which there are two models SearchModel and ChannelsModel, this is done in order to make it easier to split the viewType in recyclerview.
The problem is that I do not know how to combine two requests into one RecyclerView so that it distinguishes channels from videos and that there is a channel icon under each video, how to do this, whether you need to process everything in one fragment, if you have any ideas, please help.