Add Upsert to HistoryDao

This commit is contained in:
Andrei M 2023-04-01 23:35:45 +03:00
parent 025e908c22
commit e27b7b07fe
5 changed files with 11 additions and 38 deletions

View file

@ -122,14 +122,8 @@ abstract class AbsMusicServiceActivity : AbsBaseActivity(), IMusicServiceEventLi
listener.onPlayingMetaChanged() listener.onPlayingMetaChanged()
} }
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
val entity = repository.songPresentInHistory(MusicPlayerRemote.currentSong) if (!PreferenceUtil.pauseHistory) {
if (entity != null) { repository.upsertSongInHistory(MusicPlayerRemote.currentSong)
repository.updateHistorySong(MusicPlayerRemote.currentSong)
} else {
// Check whether pause history option is ON or OFF
if (!PreferenceUtil.pauseHistory) {
repository.addSongToHistory(MusicPlayerRemote.currentSong)
}
} }
val song = repository.findSongExistInPlayCount(MusicPlayerRemote.currentSong.id) val song = repository.findSongExistInPlayCount(MusicPlayerRemote.currentSong.id)
?.apply { playCount += 1 } ?.apply { playCount += 1 }

View file

@ -23,16 +23,11 @@ interface HistoryDao {
private const val HISTORY_LIMIT = 100 private const val HISTORY_LIMIT = 100
} }
@Insert(onConflict = OnConflictStrategy.REPLACE) @Upsert
suspend fun insertSongInHistory(historyEntity: HistoryEntity) suspend fun upsertSongInHistory(historyEntity: HistoryEntity)
@Query("DELETE FROM HistoryEntity WHERE id= :songId") @Query("DELETE FROM HistoryEntity WHERE id= :songId")
fun deleteSongInHistory(songId: Long) fun deleteSongInHistory(songId: Long)
@Query("SELECT * FROM HistoryEntity WHERE id = :songId LIMIT 1")
suspend fun isSongPresentInHistory(songId: Long): HistoryEntity?
@Update
suspend fun updateHistorySong(historyEntity: HistoryEntity)
@Query("SELECT * FROM HistoryEntity ORDER BY time_played DESC LIMIT $HISTORY_LIMIT") @Query("SELECT * FROM HistoryEntity ORDER BY time_played DESC LIMIT $HISTORY_LIMIT")
fun historySongs(): List<HistoryEntity> fun historySongs(): List<HistoryEntity>

View file

@ -311,7 +311,7 @@ class LibraryViewModel(
if (previousSongHistory.isNotEmpty()) { if (previousSongHistory.isNotEmpty()) {
val history = ArrayList<Song>() val history = ArrayList<Song>()
for (song in previousSongHistory) { for (song in previousSongHistory) {
repository.addSongToHistory(song.toSong()) repository.upsertSongInHistory(song.toSong())
history.add(song.toSong()) history.add(song.toSong())
} }
songHistory.postValue(history) songHistory.postValue(history)

View file

@ -78,9 +78,7 @@ interface Repository {
suspend fun deletePlaylistSongs(playlists: List<PlaylistEntity>) suspend fun deletePlaylistSongs(playlists: List<PlaylistEntity>)
suspend fun favoritePlaylist(): PlaylistEntity suspend fun favoritePlaylist(): PlaylistEntity
suspend fun isFavoriteSong(songEntity: SongEntity): List<SongEntity> suspend fun isFavoriteSong(songEntity: SongEntity): List<SongEntity>
suspend fun addSongToHistory(currentSong: Song) suspend fun upsertSongInHistory(currentSong: Song)
suspend fun songPresentInHistory(currentSong: Song): HistoryEntity?
suspend fun updateHistorySong(currentSong: Song)
suspend fun favoritePlaylistSongs(): List<SongEntity> suspend fun favoritePlaylistSongs(): List<SongEntity>
suspend fun recentSongs(): List<Song> suspend fun recentSongs(): List<Song>
suspend fun topPlayedSongs(): List<Song> suspend fun topPlayedSongs(): List<Song>
@ -268,14 +266,8 @@ class RealRepository(
override suspend fun isFavoriteSong(songEntity: SongEntity): List<SongEntity> = override suspend fun isFavoriteSong(songEntity: SongEntity): List<SongEntity> =
roomRepository.isFavoriteSong(songEntity) roomRepository.isFavoriteSong(songEntity)
override suspend fun addSongToHistory(currentSong: Song) = override suspend fun upsertSongInHistory(currentSong: Song) =
roomRepository.addSongToHistory(currentSong) roomRepository.upsertSongInHistory(currentSong)
override suspend fun songPresentInHistory(currentSong: Song): HistoryEntity? =
roomRepository.songPresentInHistory(currentSong)
override suspend fun updateHistorySong(currentSong: Song) =
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))

View file

@ -30,9 +30,7 @@ interface RoomRepository {
suspend fun favoritePlaylist(favorite: String): PlaylistEntity suspend fun favoritePlaylist(favorite: String): PlaylistEntity
suspend fun isFavoriteSong(songEntity: SongEntity): List<SongEntity> suspend fun isFavoriteSong(songEntity: SongEntity): List<SongEntity>
suspend fun removeSongFromPlaylist(songEntity: SongEntity) suspend fun removeSongFromPlaylist(songEntity: SongEntity)
suspend fun addSongToHistory(currentSong: Song) suspend fun upsertSongInHistory(currentSong: Song)
suspend fun songPresentInHistory(song: Song): HistoryEntity?
suspend fun updateHistorySong(song: Song)
suspend fun favoritePlaylistSongs(favorite: String): List<SongEntity> suspend fun favoritePlaylistSongs(favorite: String): List<SongEntity>
suspend fun upsertSongInPlayCount(playCountEntity: PlayCountEntity) suspend fun upsertSongInPlayCount(playCountEntity: PlayCountEntity)
suspend fun deleteSongInPlayCount(playCountEntity: PlayCountEntity) suspend fun deleteSongInPlayCount(playCountEntity: PlayCountEntity)
@ -132,14 +130,8 @@ class RealRoomRepository(
override suspend fun removeSongFromPlaylist(songEntity: SongEntity) = override suspend fun removeSongFromPlaylist(songEntity: SongEntity) =
playlistDao.deleteSongFromPlaylist(songEntity.playlistCreatorId, songEntity.id) playlistDao.deleteSongFromPlaylist(songEntity.playlistCreatorId, songEntity.id)
override suspend fun addSongToHistory(currentSong: Song) = override suspend fun upsertSongInHistory(currentSong: Song) =
historyDao.insertSongInHistory(currentSong.toHistoryEntity(System.currentTimeMillis())) historyDao.upsertSongInHistory(currentSong.toHistoryEntity(System.currentTimeMillis()))
override suspend fun songPresentInHistory(song: Song): HistoryEntity? =
historyDao.isSongPresentInHistory(song.id)
override suspend fun updateHistorySong(song: Song) =
historyDao.updateHistorySong(song.toHistoryEntity(System.currentTimeMillis()))
override fun observableHistorySongs(): LiveData<List<HistoryEntity>> = override fun observableHistorySongs(): LiveData<List<HistoryEntity>> =
historyDao.observableHistorySongs() historyDao.observableHistorySongs()