Merge pull request #1475 from Tilizar/support-room-upsert

Support room @Upsert
This commit is contained in:
Prathamesh More 2023-04-02 10:04:53 +05:30 committed by GitHub
commit 3d766d70f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 75 deletions

View file

@ -122,23 +122,14 @@ 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)
}
}
val songs = repository.checkSongExistInPlayCount(MusicPlayerRemote.currentSong.id)
if (songs.isNotEmpty()) {
repository.updateSongInPlayCount(songs.first().apply {
playCount += 1
})
} else {
repository.insertSongInPlayCount(MusicPlayerRemote.currentSong.toPlayCount())
if (!PreferenceUtil.pauseHistory) {
repository.upsertSongInHistory(MusicPlayerRemote.currentSong)
}
val song = repository.findSongExistInPlayCount(MusicPlayerRemote.currentSong.id)
?.apply { playCount += 1 }
?: MusicPlayerRemote.currentSong.toPlayCount()
repository.upsertSongInPlayCount(song)
}
}

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

@ -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

@ -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,18 +78,15 @@ 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>
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>
@ -269,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))
@ -285,11 +276,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 +289,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

@ -30,16 +30,13 @@ 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 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
@ -133,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()
@ -155,11 +146,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 +160,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()