Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@HiltViewModel
class VideosViewModel @Inject constructor() : ViewModel() {
val videos = MutableLiveData<List<VideoItem>>()
val currentlyPlayingIndex = MutableLiveData<Int?>()
init {
populateVideosWithTestData()
}
fun onPlayVideoClick(playbackPosition: Long, videoIndex: Int) {
when (currentlyPlayingIndex.value) {
null -> currentlyPlayingIndex.postValue(videoIndex)
videoIndex -> {
currentlyPlayingIndex.postValue(null)
videos.value = videos.value!!.toMutableList().also { list ->
list[videoIndex] = list[videoIndex].copy(lastPlayedPosition = playbackPosition)
}
}
else -> {
videos.value = videos.value!!.toMutableList().also { list ->
list[currentlyPlayingIndex.value!!] = list[currentlyPlayingIndex.value!!].copy(lastPlayedPosition = playbackPosition)
}
currentlyPlayingIndex.postValue(videoIndex)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment