File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
app/src/main/java/com/maxrave/simpmusic/viewModel Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -103,6 +103,17 @@ class PlaylistViewModel(
103103 getFullTracks { tracks ->
104104 newUpdateJob =
105105 launch {
106+ val listSongs = mainRepository.getSongsByListVideoId(tracks.toListVideoId()).firstOrNull() ? : emptyList()
107+ if (state == STATE_DOWNLOADED && listSongs.isNotEmpty()) {
108+ listSongs.filter { it.downloadState != STATE_DOWNLOADED }.let { notDownloaded ->
109+ if (notDownloaded.isNotEmpty()) {
110+ downloadTracks(notDownloaded.map { it.videoId })
111+ updatePlaylistDownloadState(id, STATE_DOWNLOADING )
112+ } else {
113+ updatePlaylistDownloadState(id, STATE_DOWNLOADED )
114+ }
115+ }
116+ }
106117 downloadUtils.downloads.collectLatest { downloads ->
107118 var count = 0
108119 tracks.forEachIndexed { index, track ->
@@ -144,6 +155,18 @@ class PlaylistViewModel(
144155 }
145156 }
146157
158+ private fun downloadTracks (listJob : List <String >) {
159+ viewModelScope.launch {
160+ listJob.forEach { videoId ->
161+ mainRepository.getSongById(videoId).singleOrNull()?.let { song ->
162+ if (song.downloadState != STATE_DOWNLOADED ) {
163+ downloadUtils.downloadTrack(videoId, song.title, song.thumbnails ? : " " )
164+ }
165+ }
166+ }
167+ }
168+ }
169+
147170 private fun resetData () {
148171 _uiState .value = Loading
149172 _playlistEntity .value = null
You can’t perform that action at this time.
0 commit comments