Code refactor

This commit is contained in:
Hemanth S 2020-09-06 16:17:06 +05:30
parent 5211890607
commit 332c2dc69b
8 changed files with 54 additions and 33 deletions

View file

@ -55,6 +55,9 @@ private val roomModule = module {
factory { factory {
get<RetroDatabase>().playCountDao() get<RetroDatabase>().playCountDao()
} }
factory {
get<RetroDatabase>().historyDao()
}
single { single {
RealRoomRepository(get(), get(), get()) RealRoomRepository(get(), get(), get())

View file

@ -0,0 +1,26 @@
package code.name.monkey.retromusic.db
import androidx.lifecycle.LiveData
import androidx.room.*
@Dao
interface HistoryDao {
companion object {
private const val HISTORY_LIMIT = 100
}
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertSongInHistory(historyEntity: HistoryEntity)
@Query("SELECT * FROM HistoryEntity WHERE id = :songId LIMIT 1")
suspend fun isSongPresentInHistory(songId: Int): HistoryEntity?
@Update
suspend fun updateHistorySong(historyEntity: HistoryEntity)
@Query("SELECT * FROM HistoryEntity ORDER BY time_played DESC LIMIT $HISTORY_LIMIT")
fun historySongs(): List<HistoryEntity>
@Query("SELECT * FROM HistoryEntity ORDER BY time_played DESC LIMIT $HISTORY_LIMIT")
fun observableHistorySongs(): LiveData<List<HistoryEntity>>
}

View file

@ -45,17 +45,6 @@ interface PlaylistDao {
@Delete @Delete
suspend fun deleteSongsInPlaylist(songs: List<SongEntity>) suspend fun deleteSongsInPlaylist(songs: List<SongEntity>)
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertSongInHistory(historyEntity: HistoryEntity)
@Query("SELECT * FROM HistoryEntity WHERE id = :songId LIMIT 1")
suspend fun isSongPresentInHistory(songId: Int): HistoryEntity?
@Update
suspend fun updateHistorySong(historyEntity: HistoryEntity)
@Query("SELECT * FROM HistoryEntity ORDER BY time_played DESC")
fun historySongs(): LiveData<List<HistoryEntity>>
@Query("SELECT * FROM SongEntity WHERE playlist_creator_id= :playlistId") @Query("SELECT * FROM SongEntity WHERE playlist_creator_id= :playlistId")
fun favoritesSongsLiveData(playlistId: Int): LiveData<List<SongEntity>> fun favoritesSongsLiveData(playlistId: Int): LiveData<List<SongEntity>>

View file

@ -12,4 +12,5 @@ abstract class RetroDatabase : RoomDatabase() {
abstract fun playlistDao(): PlaylistDao abstract fun playlistDao(): PlaylistDao
abstract fun blackListStore(): BlackListStoreDao abstract fun blackListStore(): BlackListStoreDao
abstract fun playCountDao(): PlayCountDao abstract fun playCountDao(): PlayCountDao
abstract fun historyDao(): HistoryDao
} }

View file

@ -168,9 +168,7 @@ class AlbumDetailsFragment : AbsMainActivityFragment(R.layout.fragment_album_det
detailsViewModel.getArtist(album.artistId).observe(viewLifecycleOwner, Observer { detailsViewModel.getArtist(album.artistId).observe(viewLifecycleOwner, Observer {
loadArtistImage(it) loadArtistImage(it)
}) })
/*detailsViewModel.getMoreAlbums().observe(viewLifecycleOwner, Observer {
moreAlbums(it)
})*/
detailsViewModel.getAlbumInfo(album).observe(viewLifecycleOwner, Observer { result -> detailsViewModel.getAlbumInfo(album).observe(viewLifecycleOwner, Observer { result ->
when (result) { when (result) {
is Result.Loading -> { is Result.Loading -> {
@ -224,6 +222,9 @@ class AlbumDetailsFragment : AbsMainActivityFragment(R.layout.fragment_album_det
} }
private fun loadArtistImage(artist: Artist) { private fun loadArtistImage(artist: Artist) {
detailsViewModel.getMoreAlbums(artist).observe(viewLifecycleOwner, Observer {
moreAlbums(it)
})
ArtistGlideRequest.Builder.from(Glide.with(requireContext()), artist) ArtistGlideRequest.Builder.from(Glide.with(requireContext()), artist)
.forceDownload(PreferenceUtil.isAllowedToDownloadMetadata()) .forceDownload(PreferenceUtil.isAllowedToDownloadMetadata())
.generatePalette(requireContext()) .generatePalette(requireContext())

View file

@ -18,6 +18,7 @@ import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import code.name.monkey.appthemehelper.ThemeStore import code.name.monkey.appthemehelper.ThemeStore
import code.name.monkey.retromusic.EXTRA_ALBUM_ID
import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.adapter.album.HorizontalAlbumAdapter import code.name.monkey.retromusic.adapter.album.HorizontalAlbumAdapter
import code.name.monkey.retromusic.adapter.song.SimpleSongAdapter import code.name.monkey.retromusic.adapter.song.SimpleSongAdapter
@ -208,7 +209,7 @@ class ArtistDetailsFragment : AbsMainActivityFragment(R.layout.fragment_artist_d
override fun onAlbumClick(albumId: Int, view: View) { override fun onAlbumClick(albumId: Int, view: View) {
findNavController().navigate( findNavController().navigate(
R.id.albumDetailsFragment, R.id.albumDetailsFragment,
bundleOf("extra_album_id" to albumId), bundleOf(EXTRA_ALBUM_ID to albumId),
null, null,
FragmentNavigatorExtras( FragmentNavigatorExtras(
view to getString(R.string.transition_album_art) view to getString(R.string.transition_album_art)

View file

@ -36,11 +36,12 @@ interface Repository {
fun artistsFlow(): Flow<Result<List<Artist>>> fun artistsFlow(): Flow<Result<List<Artist>>>
fun playlistsFlow(): Flow<Result<List<Playlist>>> fun playlistsFlow(): Flow<Result<List<Playlist>>>
fun genresFlow(): Flow<Result<List<Genre>>> fun genresFlow(): Flow<Result<List<Genre>>>
fun historySong(): LiveData<List<HistoryEntity>> fun historySong(): List<HistoryEntity>
fun favorites(): LiveData<List<SongEntity>> fun favorites(): LiveData<List<SongEntity>>
fun observableHistorySongs(): LiveData<List<HistoryEntity>>
fun albumById(albumId: Int): Album
suspend fun fetchAlbums(): List<Album> suspend fun fetchAlbums(): List<Album>
suspend fun albumByIdAsync(albumId: Int): Album suspend fun albumByIdAsync(albumId: Int): Album
fun albumById(albumId: Int): Album
suspend fun allSongs(): List<Song> suspend fun allSongs(): List<Song>
suspend fun fetchArtists(): List<Artist> suspend fun fetchArtists(): List<Artist>
suspend fun albumArtists(): List<Artist> suspend fun albumArtists(): List<Artist>
@ -294,7 +295,10 @@ class RealRepository(
override suspend fun blackListPaths(): List<BlackListStoreEntity> = override suspend fun blackListPaths(): List<BlackListStoreEntity> =
roomRepository.blackListPaths() roomRepository.blackListPaths()
override fun historySong(): LiveData<List<HistoryEntity>> = override fun observableHistorySongs(): LiveData<List<HistoryEntity>> =
roomRepository.observableHistorySongs()
override fun historySong(): List<HistoryEntity> =
roomRepository.historySongs() roomRepository.historySongs()
override fun favorites(): LiveData<List<SongEntity>> = override fun favorites(): LiveData<List<SongEntity>> =

View file

@ -7,9 +7,10 @@ import code.name.monkey.retromusic.model.Song
interface RoomRepository { interface RoomRepository {
fun historySongs(): LiveData<List<HistoryEntity>> fun historySongs(): List<HistoryEntity>
fun favoritePlaylistLiveData(favorite: String): LiveData<List<SongEntity>> fun favoritePlaylistLiveData(favorite: String): LiveData<List<SongEntity>>
fun insertBlacklistPath(blackListStoreEntity: BlackListStoreEntity) fun insertBlacklistPath(blackListStoreEntity: BlackListStoreEntity)
fun observableHistorySongs(): LiveData<List<HistoryEntity>>
suspend fun createPlaylist(playlistEntity: PlaylistEntity): Long suspend fun createPlaylist(playlistEntity: PlaylistEntity): Long
suspend fun checkPlaylistExists(playlistName: String): List<PlaylistEntity> suspend fun checkPlaylistExists(playlistName: String): List<PlaylistEntity>
suspend fun playlists(): List<PlaylistEntity> suspend fun playlists(): List<PlaylistEntity>
@ -42,7 +43,8 @@ interface RoomRepository {
class RealRoomRepository( class RealRoomRepository(
private val playlistDao: PlaylistDao, private val playlistDao: PlaylistDao,
private val blackListStoreDao: BlackListStoreDao, private val blackListStoreDao: BlackListStoreDao,
private val playCountDao: PlayCountDao private val playCountDao: PlayCountDao,
private val historyDao: HistoryDao
) : RoomRepository { ) : RoomRepository {
@WorkerThread @WorkerThread
override suspend fun createPlaylist(playlistEntity: PlaylistEntity): Long = override suspend fun createPlaylist(playlistEntity: PlaylistEntity): Long =
@ -59,12 +61,6 @@ class RealRoomRepository(
override suspend fun playlistWithSongs(): List<PlaylistWithSongs> = override suspend fun playlistWithSongs(): List<PlaylistWithSongs> =
playlistDao.playlistsWithSongs() playlistDao.playlistsWithSongs()
/* val tempList = ArrayList<SongEntity>(songs)
val existingSongs = songs.map {
playlistDao.checkSongExistsWithPlaylistName(it.playlistCreatorName, it.songId)
}.first()
println("Existing ${existingSongs.size}")
tempList.removeAll(existingSongs)*/
@WorkerThread @WorkerThread
override suspend fun insertSongs(songs: List<SongEntity>) = override suspend fun insertSongs(songs: List<SongEntity>) =
playlistDao.insertSongsToPlaylist(songs) playlistDao.insertSongsToPlaylist(songs)
@ -96,7 +92,6 @@ class RealRoomRepository(
} }
} }
override suspend fun isFavoriteSong(songEntity: SongEntity): List<SongEntity> = override suspend fun isFavoriteSong(songEntity: SongEntity): List<SongEntity> =
playlistDao.isSongExistsInPlaylist( playlistDao.isSongExistsInPlaylist(
songEntity.playlistCreatorId, songEntity.playlistCreatorId,
@ -107,17 +102,18 @@ class RealRoomRepository(
playlistDao.removeSongFromPlaylist(songEntity.playlistCreatorId, songEntity.id) playlistDao.removeSongFromPlaylist(songEntity.playlistCreatorId, songEntity.id)
override suspend fun addSongToHistory(currentSong: Song) = override suspend fun addSongToHistory(currentSong: Song) =
playlistDao.insertSongInHistory(currentSong.toHistoryEntity(System.currentTimeMillis())) historyDao.insertSongInHistory(currentSong.toHistoryEntity(System.currentTimeMillis()))
override suspend fun songPresentInHistory(song: Song): HistoryEntity? = override suspend fun songPresentInHistory(song: Song): HistoryEntity? =
playlistDao.isSongPresentInHistory(song.id) historyDao.isSongPresentInHistory(song.id)
override suspend fun updateHistorySong(song: Song) = override suspend fun updateHistorySong(song: Song) =
playlistDao.updateHistorySong(song.toHistoryEntity(System.currentTimeMillis())) historyDao.updateHistorySong(song.toHistoryEntity(System.currentTimeMillis()))
override fun historySongs(): LiveData<List<HistoryEntity>> { override fun observableHistorySongs(): LiveData<List<HistoryEntity>> =
return playlistDao.historySongs() historyDao.observableHistorySongs()
}
override fun historySongs(): List<HistoryEntity> = historyDao.historySongs()
override fun favoritePlaylistLiveData(favorite: String): LiveData<List<SongEntity>> = override fun favoritePlaylistLiveData(favorite: String): LiveData<List<SongEntity>> =
playlistDao.favoritesSongsLiveData( playlistDao.favoritesSongsLiveData(