From 025e908c22615ea591b1039cc509a6751b2c96e6 Mon Sep 17 00:00:00 2001 From: Andrei M Date: Sat, 1 Apr 2023 23:18:15 +0300 Subject: [PATCH] Add Upsert to PlayCountDao --- .../activities/base/AbsMusicServiceActivity.kt | 13 +++++-------- .../name/monkey/retromusic/db/PlayCountDao.kt | 13 ++++--------- .../monkey/retromusic/repository/Repository.kt | 16 ++++++---------- .../retromusic/repository/RoomRepository.kt | 16 ++++++---------- 4 files changed, 21 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsMusicServiceActivity.kt b/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsMusicServiceActivity.kt index 86bf86461..1d9489bb8 100644 --- a/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsMusicServiceActivity.kt +++ b/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsMusicServiceActivity.kt @@ -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) } } diff --git a/app/src/main/java/code/name/monkey/retromusic/db/PlayCountDao.kt b/app/src/main/java/code/name/monkey/retromusic/db/PlayCountDao.kt index 420b2d434..087ae4faf 100644 --- a/app/src/main/java/code/name/monkey/retromusic/db/PlayCountDao.kt +++ b/app/src/main/java/code/name/monkey/retromusic/db/PlayCountDao.kt @@ -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 + @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 @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) } diff --git a/app/src/main/java/code/name/monkey/retromusic/repository/Repository.kt b/app/src/main/java/code/name/monkey/retromusic/repository/Repository.kt index 76f1fa53c..d02b900b4 100644 --- a/app/src/main/java/code/name/monkey/retromusic/repository/Repository.kt +++ b/app/src/main/java/code/name/monkey/retromusic/repository/Repository.kt @@ -84,12 +84,11 @@ interface Repository { suspend fun favoritePlaylistSongs(): List suspend fun recentSongs(): List suspend fun topPlayedSongs(): List - 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 + suspend fun findSongExistInPlayCount(songId: Long): PlayCountEntity? suspend fun playCountSongs(): List suspend fun deleteSongs(songs: List) suspend fun contributor(): List @@ -285,11 +284,8 @@ class RealRepository( override suspend fun topPlayedSongs(): List = 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 = - roomRepository.checkSongExistInPlayCount(songId) + override suspend fun findSongExistInPlayCount(songId: Long): PlayCountEntity? = + roomRepository.findSongExistInPlayCount(songId) override suspend fun playCountSongs(): List = roomRepository.playCountSongs() diff --git a/app/src/main/java/code/name/monkey/retromusic/repository/RoomRepository.kt b/app/src/main/java/code/name/monkey/retromusic/repository/RoomRepository.kt index a5231ccae..04d1da4ba 100644 --- a/app/src/main/java/code/name/monkey/retromusic/repository/RoomRepository.kt +++ b/app/src/main/java/code/name/monkey/retromusic/repository/RoomRepository.kt @@ -34,12 +34,11 @@ interface RoomRepository { suspend fun songPresentInHistory(song: Song): HistoryEntity? suspend fun updateHistorySong(song: Song) suspend fun favoritePlaylistSongs(favorite: String): List - 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 + suspend fun findSongExistInPlayCount(songId: Long): PlayCountEntity? suspend fun playCountSongs(): List suspend fun deleteSongs(songs: List) 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 = - playCountDao.checkSongExistInPlayCount(songId) + override suspend fun findSongExistInPlayCount(songId: Long): PlayCountEntity? = + playCountDao.findSongExistInPlayCount(songId) override suspend fun playCountSongs(): List = playCountDao.playCountSongs()