-
-
Save DivS-15/f7ae44bc8da30387c1029c04d7b6e91e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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