Skip to content

Instantly share code, notes, and snippets.

@Skyyo
Last active August 29, 2022 10:40
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 Skyyo/4d6dbdec1a78ed17912c29f8165b66fc to your computer and use it in GitHub Desktop.
Save Skyyo/4d6dbdec1a78ed17912c29f8165b66fc to your computer and use it in GitHub Desktop.
@HiltViewModel
class VideosViewModel @Inject constructor() : ViewModel() {
val videos = MutableStateFlow<List<VideoItem>>(listOf())
val currentlyPlayingIndex = MutableStateFlow<Int?>(null)
init {
populateVideosWithTestData()
}
fun onPlayVideoClick(playbackPosition: Long, videoIndex: Int) {
when (currentlyPlayingIndex.value) {
null -> currentlyPlayingIndex.value = videoIndex
videoIndex -> {
currentlyPlayingIndex.value = 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.value = videoIndex
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment