Last active
August 29, 2022 10:40
-
-
Save Skyyo/4d6dbdec1a78ed17912c29f8165b66fc 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 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