️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)
@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")
suspend fun playlists(): List<PlaylistEntity>
@ -57,4 +57,10 @@ interface PlaylistDao {
@Query("SELECT * FROM HistoryEntity ORDER BY time_played DESC")
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.model.Album
import code.name.monkey.retromusic.model.Artist
import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.repository.RealRepository
import kotlinx.android.synthetic.main.fragment_playlist_detail.*
import kotlinx.coroutines.Dispatchers.IO
@ -74,19 +73,20 @@ class DetailListFragment : AbsMainActivityFragment(R.layout.fragment_playlist_de
private fun loadFavorite() {
toolbar.setTitle(R.string.favorites)
lifecycleScope.launch(IO) {
val songs = repository.favoritePlaylistHome()
withContext(Main) {
recyclerView.apply {
adapter = SongAdapter(
requireActivity(),
songs.arrayList as MutableList<Song>,
R.layout.item_list, null
)
layoutManager = linearLayoutManager()
}
}
val songAdapter = SongAdapter(
requireActivity(),
mutableListOf(),
R.layout.item_list, null
)
recyclerView.apply {
adapter = songAdapter
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) {

View file

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

View file

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

View file

@ -23,7 +23,9 @@ interface RoomPlaylistRepository {
suspend fun addSongToHistory(currentSong: Song)
suspend fun songPresentInHistory(song: Song): HistoryEntity?
suspend fun updateHistorySong(song: Song)
suspend fun favoritePlaylistSongs(favorite: String): List<SongEntity>
fun historySongs(): LiveData<List<HistoryEntity>>
fun favoritePlaylistLiveData(favorite: String): LiveData<List<SongEntity>>
}
class RealRoomRepository(
@ -99,4 +101,14 @@ class RealRoomRepository(
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
)
}