This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
846ad1b
commit 5775dfc
Showing
11 changed files
with
191 additions
and
9 deletions.
There are no files selected for viewing
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
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
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
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
39 changes: 39 additions & 0 deletions
39
app/src/main/java/app/suhasdissa/vibeyou/backend/repository/PlaylistRepository.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package app.suhasdissa.vibeyou.backend.repository | ||
|
||
import app.suhasdissa.vibeyou.backend.data.Album | ||
import app.suhasdissa.vibeyou.backend.data.Song | ||
import app.suhasdissa.vibeyou.backend.database.dao.PlaylistDao | ||
import app.suhasdissa.vibeyou.backend.database.dao.SongsDao | ||
import app.suhasdissa.vibeyou.backend.database.entities.PlaylistEntity | ||
import app.suhasdissa.vibeyou.backend.database.entities.PlaylistWithSongs | ||
import app.suhasdissa.vibeyou.backend.database.entities.SongPlaylistMap | ||
import app.suhasdissa.vibeyou.utils.asPlaylistEntity | ||
import app.suhasdissa.vibeyou.utils.asSongEntity | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.withContext | ||
|
||
class PlaylistRepository(private val playlistDao: PlaylistDao, private val songsDao: SongsDao) { | ||
private fun createNew(album: Album) { | ||
playlistDao.addPlaylist( | ||
album.asPlaylistEntity | ||
) | ||
} | ||
|
||
private fun addSongsToPlaylist(playlistId: String, songs: List<Song>) { | ||
songsDao.addSongs(songs.map { it.asSongEntity }) | ||
val songMap = songs.map { SongPlaylistMap(playlistId, it.id) } | ||
playlistDao.addPlaylistMaps(songMap) | ||
} | ||
|
||
suspend fun newPlaylistWithSongs(album: Album, songs: List<Song>) = | ||
withContext(Dispatchers.IO) { | ||
createNew(album) | ||
addSongsToPlaylist(album.id, songs) | ||
} | ||
|
||
fun getPlaylists(): Flow<List<PlaylistEntity>> = playlistDao.getAllPlaylists() | ||
|
||
suspend fun getPlaylist(id: String): PlaylistWithSongs = | ||
withContext(Dispatchers.IO) { return@withContext playlistDao.getPlaylist(id) } | ||
} |
66 changes: 66 additions & 0 deletions
66
app/src/main/java/app/suhasdissa/vibeyou/backend/viewmodel/PlaylistViewModel.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package app.suhasdissa.vibeyou.backend.viewmodel | ||
|
||
import android.util.Log | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.viewModelScope | ||
import androidx.lifecycle.viewmodel.initializer | ||
import androidx.lifecycle.viewmodel.viewModelFactory | ||
import app.suhasdissa.vibeyou.MellowMusicApplication | ||
import app.suhasdissa.vibeyou.backend.data.Album | ||
import app.suhasdissa.vibeyou.backend.data.Song | ||
import app.suhasdissa.vibeyou.backend.repository.PlaylistRepository | ||
import app.suhasdissa.vibeyou.backend.viewmodel.state.AlbumInfoState | ||
import app.suhasdissa.vibeyou.utils.asSong | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.stateIn | ||
import kotlinx.coroutines.launch | ||
|
||
class PlaylistViewModel(private val playlistRepository: PlaylistRepository) : ViewModel() { | ||
var albumInfoState: AlbumInfoState by mutableStateOf(AlbumInfoState.Loading) | ||
private set | ||
|
||
var albums = playlistRepository.getPlaylists().stateIn( | ||
scope = viewModelScope, | ||
started = SharingStarted.WhileSubscribed(5000L), | ||
initialValue = listOf() | ||
) | ||
|
||
fun getPlaylistInfo(playlist: Album) { | ||
viewModelScope.launch { | ||
albumInfoState = AlbumInfoState.Loading | ||
albumInfoState = try { | ||
Log.e("PlaylistViewModel", "Getting info") | ||
val info = playlistRepository.getPlaylist(playlist.id) | ||
AlbumInfoState.Success( | ||
playlist, | ||
info.songs.map { it.asSong } | ||
) | ||
} catch (e: Exception) { | ||
Log.e("Playlist Info", e.toString()) | ||
AlbumInfoState.Error | ||
} | ||
} | ||
} | ||
|
||
fun newPlaylistWithSongs(album: Album, songs: List<Song>) { | ||
viewModelScope.launch { | ||
playlistRepository.newPlaylistWithSongs(album, songs) | ||
} | ||
} | ||
|
||
companion object { | ||
val Factory: ViewModelProvider.Factory = viewModelFactory { | ||
initializer { | ||
val application = | ||
(this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as MellowMusicApplication) | ||
PlaylistViewModel( | ||
application.container.playlistRepository | ||
) | ||
} | ||
} | ||
} | ||
} |
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
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
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
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
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