🔨 Fix smart playlists

This commit is contained in:
Hemanth S 2020-08-21 20:29:14 +05:30
parent 2769db3f17
commit 859d10d907
5 changed files with 59 additions and 17 deletions

View file

@ -53,7 +53,6 @@ object Constants {
) )
const val NUMBER_OF_TOP_TRACKS = 99 const val NUMBER_OF_TOP_TRACKS = 99
} }
const val HISTORY = 8
const val EXTRA_GENRE = "extra_genre" const val EXTRA_GENRE = "extra_genre"
const val EXTRA_PLAYLIST = "extra_playlist" const val EXTRA_PLAYLIST = "extra_playlist"
const val EXTRA_PLAYLIST_ID = "extra_playlist_id" const val EXTRA_PLAYLIST_ID = "extra_playlist_id"

View file

@ -23,3 +23,6 @@ const val SUGGESTIONS = 5
const val FAVOURITES = 4 const val FAVOURITES = 4
const val GENRES = 6 const val GENRES = 6
const val PLAYLISTS = 7 const val PLAYLISTS = 7
const val HISTORY_PLAYLIST = 8
const val LAST_ADDED_PLAYLIST = 9
const val TOP_PLAYED_PLAYLIST = 10

View file

@ -49,7 +49,43 @@ class DetailListFragment : AbsMainActivityFragment(R.layout.fragment_playlist_de
loadAlbums(R.string.recent_albums, RECENT_ALBUMS) loadAlbums(R.string.recent_albums, RECENT_ALBUMS)
} }
FAVOURITES -> loadFavorite() FAVOURITES -> loadFavorite()
HISTORY -> loadHistory() HISTORY_PLAYLIST -> loadHistory()
LAST_ADDED_PLAYLIST -> lastAddedSongs()
TOP_PLAYED_PLAYLIST -> topPlayed()
}
}
private fun lastAddedSongs() {
toolbar.setTitle(R.string.last_added)
val songAdapter = SongAdapter(
requireActivity(),
mutableListOf(),
R.layout.item_list, null
)
recyclerView.apply {
adapter = songAdapter
layoutManager = linearLayoutManager()
}
lifecycleScope.launch(IO) {
val songs = repository.recentSongs()
withContext(Main) { songAdapter.swapDataSet(songs) }
}
}
private fun topPlayed() {
toolbar.setTitle(R.string.my_top_tracks)
val songAdapter = SongAdapter(
requireActivity(),
mutableListOf(),
R.layout.item_list, null
)
recyclerView.apply {
adapter = songAdapter
layoutManager = linearLayoutManager()
}
lifecycleScope.launch(IO) {
val songs = repository.recentSongs()
withContext(Main) { songAdapter.swapDataSet(songs) }
} }
} }

View file

@ -22,9 +22,10 @@ import androidx.core.os.bundleOf
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import code.name.monkey.retromusic.EXTRA_PLAYLIST import code.name.monkey.retromusic.HISTORY_PLAYLIST
import code.name.monkey.retromusic.HISTORY import code.name.monkey.retromusic.LAST_ADDED_PLAYLIST
import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.TOP_PLAYED_PLAYLIST
import code.name.monkey.retromusic.adapter.HomeAdapter import code.name.monkey.retromusic.adapter.HomeAdapter
import code.name.monkey.retromusic.extensions.findActivityNavController import code.name.monkey.retromusic.extensions.findActivityNavController
import code.name.monkey.retromusic.fragments.LibraryViewModel import code.name.monkey.retromusic.fragments.LibraryViewModel
@ -32,8 +33,6 @@ import code.name.monkey.retromusic.fragments.base.AbsMainActivityFragment
import code.name.monkey.retromusic.glide.ProfileBannerGlideRequest import code.name.monkey.retromusic.glide.ProfileBannerGlideRequest
import code.name.monkey.retromusic.glide.UserProfileGlideRequest import code.name.monkey.retromusic.glide.UserProfileGlideRequest
import code.name.monkey.retromusic.helper.MusicPlayerRemote import code.name.monkey.retromusic.helper.MusicPlayerRemote
import code.name.monkey.retromusic.model.smartplaylist.LastAddedPlaylist
import code.name.monkey.retromusic.model.smartplaylist.TopTracksPlaylist
import code.name.monkey.retromusic.repository.Repository import code.name.monkey.retromusic.repository.Repository
import code.name.monkey.retromusic.util.NavigationUtil import code.name.monkey.retromusic.util.NavigationUtil
import code.name.monkey.retromusic.util.PreferenceUtil import code.name.monkey.retromusic.util.PreferenceUtil
@ -73,15 +72,15 @@ class HomeFragment :
lastAdded.setOnClickListener { lastAdded.setOnClickListener {
findActivityNavController(R.id.fragment_container).navigate( findActivityNavController(R.id.fragment_container).navigate(
R.id.playlistDetailsFragment, R.id.detailListFragment,
bundleOf(EXTRA_PLAYLIST to LastAddedPlaylist()) bundleOf("type" to LAST_ADDED_PLAYLIST)
) )
} }
topPlayed.setOnClickListener { topPlayed.setOnClickListener {
findActivityNavController(R.id.fragment_container).navigate( findActivityNavController(R.id.fragment_container).navigate(
R.id.playlistDetailsFragment, R.id.detailListFragment,
bundleOf(EXTRA_PLAYLIST to TopTracksPlaylist()) bundleOf("type" to TOP_PLAYED_PLAYLIST)
) )
} }
@ -97,7 +96,7 @@ class HomeFragment :
history.setOnClickListener { history.setOnClickListener {
findActivityNavController(R.id.fragment_container).navigate( findActivityNavController(R.id.fragment_container).navigate(
R.id.detailListFragment, R.id.detailListFragment,
bundleOf("type" to HISTORY) bundleOf("type" to HISTORY_PLAYLIST)
) )
} }

View file

@ -84,6 +84,8 @@ interface Repository {
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> suspend fun favoritePlaylistSongs(): List<SongEntity>
suspend fun recentSongs(): List<Song>
suspend fun topPlayedSongs(): List<Song>
fun historySong(): LiveData<List<HistoryEntity>> fun historySong(): LiveData<List<HistoryEntity>>
fun favorites(): LiveData<List<SongEntity>> fun favorites(): LiveData<List<SongEntity>>
} }
@ -98,7 +100,7 @@ class RealRepository(
private val lastAddedRepository: LastAddedRepository, private val lastAddedRepository: LastAddedRepository,
private val playlistRepository: PlaylistRepository, private val playlistRepository: PlaylistRepository,
private val searchRepository: RealSearchRepository, private val searchRepository: RealSearchRepository,
private val playedTracksRepository: TopPlayedRepository, private val topPlayedRepository: TopPlayedRepository,
private val roomRepository: RoomPlaylistRepository private val roomRepository: RoomPlaylistRepository
) : Repository { ) : Repository {
@ -116,9 +118,9 @@ class RealRepository(
override suspend fun recentAlbums(): List<Album> = lastAddedRepository.recentAlbums() override suspend fun recentAlbums(): List<Album> = lastAddedRepository.recentAlbums()
override suspend fun topArtists(): List<Artist> = playedTracksRepository.topArtists() override suspend fun topArtists(): List<Artist> = topPlayedRepository.topArtists()
override suspend fun topAlbums(): List<Album> = playedTracksRepository.topAlbums() override suspend fun topAlbums(): List<Album> = topPlayedRepository.topAlbums()
override suspend fun allPlaylists(): List<Playlist> = playlistRepository.playlists() override suspend fun allPlaylists(): List<Playlist> = playlistRepository.playlists()
@ -254,10 +256,13 @@ 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> = override suspend fun favoritePlaylistSongs(): List<SongEntity> =
roomRepository.favoritePlaylistSongs(context.getString(R.string.favorites)) roomRepository.favoritePlaylistSongs(context.getString(R.string.favorites))
override suspend fun recentSongs(): List<Song> = lastAddedRepository.recentSongs()
override suspend fun topPlayedSongs(): List<Song> = topPlayedRepository.topTracks()
override fun historySong(): LiveData<List<HistoryEntity>> = override fun historySong(): LiveData<List<HistoryEntity>> =
roomRepository.historySongs() roomRepository.historySongs()
@ -293,12 +298,12 @@ class RealRepository(
} }
override suspend fun topAlbumsHome(): Home { override suspend fun topAlbumsHome(): Home {
val albums = playedTracksRepository.topAlbums().take(5) val albums = topPlayedRepository.topAlbums().take(5)
return Home(albums, TOP_ALBUMS, R.string.top_albums) return Home(albums, TOP_ALBUMS, R.string.top_albums)
} }
override suspend fun topArtistsHome(): Home { override suspend fun topArtistsHome(): Home {
val artists = playedTracksRepository.topArtists().take(5) val artists = topPlayedRepository.topArtists().take(5)
return Home(artists, TOP_ARTISTS, R.string.top_artists) return Home(artists, TOP_ARTISTS, R.string.top_artists)
} }