Add Upsert to PlayCountDao

This commit is contained in:
Andrei M 2023-04-01 23:18:15 +03:00
parent 8024700b7d
commit 025e908c22
4 changed files with 21 additions and 37 deletions

View file

@ -131,14 +131,11 @@ abstract class AbsMusicServiceActivity : AbsBaseActivity(), IMusicServiceEventLi
repository.addSongToHistory(MusicPlayerRemote.currentSong)
}
}
val songs = repository.checkSongExistInPlayCount(MusicPlayerRemote.currentSong.id)
if (songs.isNotEmpty()) {
repository.updateSongInPlayCount(songs.first().apply {
playCount += 1
})
} else {
repository.insertSongInPlayCount(MusicPlayerRemote.currentSong.toPlayCount())
}
val song = repository.findSongExistInPlayCount(MusicPlayerRemote.currentSong.id)
?.apply { playCount += 1 }
?: MusicPlayerRemote.currentSong.toPlayCount()
repository.upsertSongInPlayCount(song)
}
}

View file

@ -18,24 +18,19 @@ import androidx.room.*
@Dao
interface PlayCountDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertSongInPlayCount(playCountEntity: PlayCountEntity)
@Update
fun updateSongInPlayCount(playCountEntity: PlayCountEntity)
@Upsert
fun upsertSongInPlayCount(playCountEntity: PlayCountEntity)
@Delete
fun deleteSongInPlayCount(playCountEntity: PlayCountEntity)
@Query("SELECT * FROM PlayCountEntity WHERE id =:songId")
fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity>
@Query("SELECT * FROM PlayCountEntity WHERE id =:songId LIMIT 1")
fun findSongExistInPlayCount(songId: Long): PlayCountEntity?
@Query("SELECT * FROM PlayCountEntity ORDER BY play_count DESC")
fun playCountSongs(): List<PlayCountEntity>
@Query("DELETE FROM SongEntity WHERE id =:songId")
fun deleteSong(songId: Long)
@Query("UPDATE PlayCountEntity SET play_count = play_count + 1 WHERE id = :id")
fun updateQuantity(id: Long)
}

View file

@ -84,12 +84,11 @@ interface Repository {
suspend fun favoritePlaylistSongs(): List<SongEntity>
suspend fun recentSongs(): List<Song>
suspend fun topPlayedSongs(): List<Song>
suspend fun insertSongInPlayCount(playCountEntity: PlayCountEntity)
suspend fun updateSongInPlayCount(playCountEntity: PlayCountEntity)
suspend fun upsertSongInPlayCount(playCountEntity: PlayCountEntity)
suspend fun deleteSongInPlayCount(playCountEntity: PlayCountEntity)
suspend fun deleteSongInHistory(songId: Long)
suspend fun clearSongHistory()
suspend fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity>
suspend fun findSongExistInPlayCount(songId: Long): PlayCountEntity?
suspend fun playCountSongs(): List<PlayCountEntity>
suspend fun deleteSongs(songs: List<Song>)
suspend fun contributor(): List<Contributor>
@ -285,11 +284,8 @@ class RealRepository(
override suspend fun topPlayedSongs(): List<Song> = topPlayedRepository.topTracks()
override suspend fun insertSongInPlayCount(playCountEntity: PlayCountEntity) =
roomRepository.insertSongInPlayCount(playCountEntity)
override suspend fun updateSongInPlayCount(playCountEntity: PlayCountEntity) =
roomRepository.updateSongInPlayCount(playCountEntity)
override suspend fun upsertSongInPlayCount(playCountEntity: PlayCountEntity) =
roomRepository.upsertSongInPlayCount(playCountEntity)
override suspend fun deleteSongInPlayCount(playCountEntity: PlayCountEntity) =
roomRepository.deleteSongInPlayCount(playCountEntity)
@ -301,8 +297,8 @@ class RealRepository(
roomRepository.clearSongHistory()
}
override suspend fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity> =
roomRepository.checkSongExistInPlayCount(songId)
override suspend fun findSongExistInPlayCount(songId: Long): PlayCountEntity? =
roomRepository.findSongExistInPlayCount(songId)
override suspend fun playCountSongs(): List<PlayCountEntity> =
roomRepository.playCountSongs()

View file

@ -34,12 +34,11 @@ interface RoomRepository {
suspend fun songPresentInHistory(song: Song): HistoryEntity?
suspend fun updateHistorySong(song: Song)
suspend fun favoritePlaylistSongs(favorite: String): List<SongEntity>
suspend fun insertSongInPlayCount(playCountEntity: PlayCountEntity)
suspend fun updateSongInPlayCount(playCountEntity: PlayCountEntity)
suspend fun upsertSongInPlayCount(playCountEntity: PlayCountEntity)
suspend fun deleteSongInPlayCount(playCountEntity: PlayCountEntity)
suspend fun deleteSongInHistory(songId: Long)
suspend fun clearSongHistory()
suspend fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity>
suspend fun findSongExistInPlayCount(songId: Long): PlayCountEntity?
suspend fun playCountSongs(): List<PlayCountEntity>
suspend fun deleteSongs(songs: List<Song>)
suspend fun isSongFavorite(context: Context, songId: Long): Boolean
@ -155,11 +154,8 @@ class RealRoomRepository(
playlistDao.playlist(favorite).first().playListId
) else emptyList()
override suspend fun insertSongInPlayCount(playCountEntity: PlayCountEntity) =
playCountDao.insertSongInPlayCount(playCountEntity)
override suspend fun updateSongInPlayCount(playCountEntity: PlayCountEntity) =
playCountDao.updateSongInPlayCount(playCountEntity)
override suspend fun upsertSongInPlayCount(playCountEntity: PlayCountEntity) =
playCountDao.upsertSongInPlayCount(playCountEntity)
override suspend fun deleteSongInPlayCount(playCountEntity: PlayCountEntity) =
playCountDao.deleteSongInPlayCount(playCountEntity)
@ -172,8 +168,8 @@ class RealRoomRepository(
historyDao.clearHistory()
}
override suspend fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity> =
playCountDao.checkSongExistInPlayCount(songId)
override suspend fun findSongExistInPlayCount(songId: Long): PlayCountEntity? =
playCountDao.findSongExistInPlayCount(songId)
override suspend fun playCountSongs(): List<PlayCountEntity> =
playCountDao.playCountSongs()