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()
}
lifecycleScope.launch(Dispatchers.IO) {
val entity = repository.songPresentInHistory(MusicPlayerRemote.currentSong)
if (entity != null) {
repository.updateHistorySong(MusicPlayerRemote.currentSong)
} else {
// Check whether pause history option is ON or OFF
if (!PreferenceUtil.pauseHistory) {
repository.addSongToHistory(MusicPlayerRemote.currentSong)
}
if (!PreferenceUtil.pauseHistory) {
repository.upsertSongInHistory(MusicPlayerRemote.currentSong)
}
val song = repository.findSongExistInPlayCount(MusicPlayerRemote.currentSong.id)
?.apply { playCount += 1 }

View file

@ -23,16 +23,11 @@ interface HistoryDao {
private const val HISTORY_LIMIT = 100
}
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertSongInHistory(historyEntity: HistoryEntity)
@Upsert
suspend fun upsertSongInHistory(historyEntity: HistoryEntity)
@Query("DELETE FROM HistoryEntity WHERE id= :songId")
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")
fun historySongs(): List<HistoryEntity>

View file

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

View file

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

View file

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