Skip to content

Instantly share code, notes, and snippets.

@DivS-15
Last active May 22, 2022 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DivS-15/f7ae44bc8da30387c1029c04d7b6e91e to your computer and use it in GitHub Desktop.
Save DivS-15/f7ae44bc8da30387c1029c04d7b6e91e to your computer and use it in GitHub Desktop.
@HiltViewModel
class VideoViewModel @Inject constructor(
private val repository: VideoRepository
) : ViewModel() {
val items: Flow<PagingData<VideoWithChannelModel>> = Pager(
PagingConfig(10, enablePlaceholders = false),
pagingSourceFactory = { repository.videosPagingSource() }
).flow
.map { value: PagingData<Item> -> //map function from kotlinx.coroutines.flow
value.map { // map function in androidx.paging
val channelUrl = onChannelImgRequired(it.snippet.channelId.toString())
VideoWithChannelModel(it, channelUrl)
}
}
.cachedIn(viewModelScope)
private suspend fun onChannelImgRequired(channelId: String): String {
return repository.loadChannelThumbnail(channelId)
}
}
data class VideoWithChannelModel(
val item: Item,
val channelUrl: String
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment