️Favorite playlist added

This commit is contained in:
Hemanth S 2020-08-21 20:17:25 +05:30
parent b22b3a627f
commit 2769db3f17
5 changed files with 47 additions and 21 deletions

View file

@ -12,7 +12,7 @@ interface PlaylistDao {
suspend fun renamePlaylistEntity(playlistId: Int, name: String) suspend fun renamePlaylistEntity(playlistId: Int, name: String)
@Query("SELECT * FROM PlaylistEntity WHERE playlist_name = :name") @Query("SELECT * FROM PlaylistEntity WHERE playlist_name = :name")
suspend fun checkPlaylistExists(name: String): List<PlaylistEntity> fun checkPlaylistExists(name: String): List<PlaylistEntity>
@Query("SELECT * FROM PlaylistEntity") @Query("SELECT * FROM PlaylistEntity")
suspend fun playlists(): List<PlaylistEntity> suspend fun playlists(): List<PlaylistEntity>
@ -57,4 +57,10 @@ interface PlaylistDao {
@Query("SELECT * FROM HistoryEntity ORDER BY time_played DESC") @Query("SELECT * FROM HistoryEntity ORDER BY time_played DESC")
fun historySongs(): LiveData<List<HistoryEntity>> fun historySongs(): LiveData<List<HistoryEntity>>
@Query("SELECT * FROM SongEntity WHERE playlist_creator_id= :playlistId")
fun favoritesSongsLiveData(playlistId: Int): LiveData<List<SongEntity>>
@Query("SELECT * FROM SongEntity WHERE playlist_creator_id= :playlistId")
fun favoritesSongs(playlistId: Int): List<SongEntity>
} }

View file

@ -17,7 +17,6 @@ import code.name.monkey.retromusic.fragments.artists.ArtistClickListener
import code.name.monkey.retromusic.fragments.base.AbsMainActivityFragment import code.name.monkey.retromusic.fragments.base.AbsMainActivityFragment
import code.name.monkey.retromusic.model.Album import code.name.monkey.retromusic.model.Album
import code.name.monkey.retromusic.model.Artist import code.name.monkey.retromusic.model.Artist
import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.repository.RealRepository import code.name.monkey.retromusic.repository.RealRepository
import kotlinx.android.synthetic.main.fragment_playlist_detail.* import kotlinx.android.synthetic.main.fragment_playlist_detail.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
@ -74,19 +73,20 @@ class DetailListFragment : AbsMainActivityFragment(R.layout.fragment_playlist_de
private fun loadFavorite() { private fun loadFavorite() {
toolbar.setTitle(R.string.favorites) toolbar.setTitle(R.string.favorites)
lifecycleScope.launch(IO) { val songAdapter = SongAdapter(
val songs = repository.favoritePlaylistHome() requireActivity(),
withContext(Main) { mutableListOf(),
recyclerView.apply { R.layout.item_list, null
adapter = SongAdapter( )
requireActivity(), recyclerView.apply {
songs.arrayList as MutableList<Song>, adapter = songAdapter
R.layout.item_list, null layoutManager = linearLayoutManager()
)
layoutManager = linearLayoutManager()
}
}
} }
repository.favorites().observe(viewLifecycleOwner, Observer {
println(it.size)
val songs = it.map { songEntity -> songEntity.toSong() }
songAdapter.swapDataSet(songs)
})
} }
private fun loadArtists(title: Int, type: Int) { private fun loadArtists(title: Int, type: Int) {

View file

@ -164,7 +164,8 @@ abstract class AbsPlayerFragment(@LayoutRes layout: Int) : AbsMainActivityFragme
lifecycleScope.launch(IO) { lifecycleScope.launch(IO) {
val playlist: PlaylistEntity = repository.favoritePlaylist().first() val playlist: PlaylistEntity = repository.favoritePlaylist().first()
val songEntity = song.toSongEntity(playlist.playListId) val songEntity = song.toSongEntity(playlist.playListId)
if (repository.isFavoriteSong(songEntity).isNotEmpty()) { val isFavorite = repository.isFavoriteSong(songEntity).isNotEmpty()
if (isFavorite) {
repository.removeSongFromPlaylist(songEntity) repository.removeSongFromPlaylist(songEntity)
} else { } else {
repository.insertSongs(listOf(song.toSongEntity(playlist.playListId))) repository.insertSongs(listOf(song.toSongEntity(playlist.playListId)))

View file

@ -83,7 +83,9 @@ interface Repository {
suspend fun addSongToHistory(currentSong: Song) suspend fun addSongToHistory(currentSong: Song)
suspend fun songPresentInHistory(currentSong: Song): HistoryEntity? suspend fun songPresentInHistory(currentSong: Song): HistoryEntity?
suspend fun updateHistorySong(currentSong: Song) suspend fun updateHistorySong(currentSong: Song)
suspend fun favoritePlaylistSongs(): List<SongEntity>
fun historySong(): LiveData<List<HistoryEntity>> fun historySong(): LiveData<List<HistoryEntity>>
fun favorites(): LiveData<List<SongEntity>>
} }
class RealRepository( class RealRepository(
@ -252,9 +254,16 @@ class RealRepository(
override suspend fun updateHistorySong(currentSong: Song) = override suspend fun updateHistorySong(currentSong: Song) =
roomRepository.updateHistorySong(currentSong) roomRepository.updateHistorySong(currentSong)
override suspend fun favoritePlaylistSongs(): List<SongEntity> =
roomRepository.favoritePlaylistSongs(context.getString(R.string.favorites))
override fun historySong(): LiveData<List<HistoryEntity>> = override fun historySong(): LiveData<List<HistoryEntity>> =
roomRepository.historySongs() roomRepository.historySongs()
override fun favorites(): LiveData<List<SongEntity>> =
roomRepository.favoritePlaylistLiveData(context.getString(R.string.favorites))
override suspend fun suggestionsHome(): Home { override suspend fun suggestionsHome(): Home {
val songs = val songs =
NotPlayedPlaylist().songs().shuffled().takeIf { NotPlayedPlaylist().songs().shuffled().takeIf {
@ -294,12 +303,10 @@ class RealRepository(
} }
override suspend fun favoritePlaylistHome(): Home { override suspend fun favoritePlaylistHome(): Home {
val playlists = val songs = favoritePlaylistSongs().map {
playlistRepository.favoritePlaylist(context.getString(R.string.favorites)).take(5) it.toSong()
val songs = if (playlists.isNotEmpty()) }
PlaylistSongsLoader.getPlaylistSongList(context, playlists[0]) println(songs.size)
else emptyList()
return Home(songs, FAVOURITES, R.string.favorites) return Home(songs, FAVOURITES, R.string.favorites)
} }

View file

@ -23,7 +23,9 @@ interface RoomPlaylistRepository {
suspend fun addSongToHistory(currentSong: Song) suspend fun addSongToHistory(currentSong: Song)
suspend fun songPresentInHistory(song: Song): HistoryEntity? suspend fun songPresentInHistory(song: Song): HistoryEntity?
suspend fun updateHistorySong(song: Song) suspend fun updateHistorySong(song: Song)
suspend fun favoritePlaylistSongs(favorite: String): List<SongEntity>
fun historySongs(): LiveData<List<HistoryEntity>> fun historySongs(): LiveData<List<HistoryEntity>>
fun favoritePlaylistLiveData(favorite: String): LiveData<List<SongEntity>>
} }
class RealRoomRepository( class RealRoomRepository(
@ -99,4 +101,14 @@ class RealRoomRepository(
return playlistDao.historySongs() return playlistDao.historySongs()
} }
override fun favoritePlaylistLiveData(favorite: String): LiveData<List<SongEntity>> =
playlistDao.favoritesSongsLiveData(
playlistDao.checkPlaylistExists(favorite).first().playListId
)
override suspend fun favoritePlaylistSongs(favorite: String): List<SongEntity> =
playlistDao.favoritesSongs(
playlistDao.checkPlaylistExists(favorite).first().playListId
)
} }