Merge pull request #1475 from Tilizar/support-room-upsert
Support room @Upsert
This commit is contained in:
commit
3d766d70f3
6 changed files with 32 additions and 75 deletions
|
@ -122,23 +122,14 @@ 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 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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>
|
||||||
|
|
|
@ -18,24 +18,19 @@ import androidx.room.*
|
||||||
|
|
||||||
@Dao
|
@Dao
|
||||||
interface PlayCountDao {
|
interface PlayCountDao {
|
||||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
|
||||||
fun insertSongInPlayCount(playCountEntity: PlayCountEntity)
|
|
||||||
|
|
||||||
@Update
|
@Upsert
|
||||||
fun updateSongInPlayCount(playCountEntity: PlayCountEntity)
|
fun upsertSongInPlayCount(playCountEntity: PlayCountEntity)
|
||||||
|
|
||||||
@Delete
|
@Delete
|
||||||
fun deleteSongInPlayCount(playCountEntity: PlayCountEntity)
|
fun deleteSongInPlayCount(playCountEntity: PlayCountEntity)
|
||||||
|
|
||||||
@Query("SELECT * FROM PlayCountEntity WHERE id =:songId")
|
@Query("SELECT * FROM PlayCountEntity WHERE id =:songId LIMIT 1")
|
||||||
fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity>
|
fun findSongExistInPlayCount(songId: Long): PlayCountEntity?
|
||||||
|
|
||||||
@Query("SELECT * FROM PlayCountEntity ORDER BY play_count DESC")
|
@Query("SELECT * FROM PlayCountEntity ORDER BY play_count DESC")
|
||||||
fun playCountSongs(): List<PlayCountEntity>
|
fun playCountSongs(): List<PlayCountEntity>
|
||||||
|
|
||||||
@Query("DELETE FROM SongEntity WHERE id =:songId")
|
@Query("DELETE FROM SongEntity WHERE id =:songId")
|
||||||
fun deleteSong(songId: Long)
|
fun deleteSong(songId: Long)
|
||||||
|
|
||||||
@Query("UPDATE PlayCountEntity SET play_count = play_count + 1 WHERE id = :id")
|
|
||||||
fun updateQuantity(id: Long)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -78,18 +78,15 @@ 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>
|
||||||
suspend fun insertSongInPlayCount(playCountEntity: PlayCountEntity)
|
suspend fun upsertSongInPlayCount(playCountEntity: PlayCountEntity)
|
||||||
suspend fun updateSongInPlayCount(playCountEntity: PlayCountEntity)
|
|
||||||
suspend fun deleteSongInPlayCount(playCountEntity: PlayCountEntity)
|
suspend fun deleteSongInPlayCount(playCountEntity: PlayCountEntity)
|
||||||
suspend fun deleteSongInHistory(songId: Long)
|
suspend fun deleteSongInHistory(songId: Long)
|
||||||
suspend fun clearSongHistory()
|
suspend fun clearSongHistory()
|
||||||
suspend fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity>
|
suspend fun findSongExistInPlayCount(songId: Long): PlayCountEntity?
|
||||||
suspend fun playCountSongs(): List<PlayCountEntity>
|
suspend fun playCountSongs(): List<PlayCountEntity>
|
||||||
suspend fun deleteSongs(songs: List<Song>)
|
suspend fun deleteSongs(songs: List<Song>)
|
||||||
suspend fun contributor(): List<Contributor>
|
suspend fun contributor(): List<Contributor>
|
||||||
|
@ -269,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))
|
||||||
|
@ -285,11 +276,8 @@ class RealRepository(
|
||||||
|
|
||||||
override suspend fun topPlayedSongs(): List<Song> = topPlayedRepository.topTracks()
|
override suspend fun topPlayedSongs(): List<Song> = topPlayedRepository.topTracks()
|
||||||
|
|
||||||
override suspend fun insertSongInPlayCount(playCountEntity: PlayCountEntity) =
|
override suspend fun upsertSongInPlayCount(playCountEntity: PlayCountEntity) =
|
||||||
roomRepository.insertSongInPlayCount(playCountEntity)
|
roomRepository.upsertSongInPlayCount(playCountEntity)
|
||||||
|
|
||||||
override suspend fun updateSongInPlayCount(playCountEntity: PlayCountEntity) =
|
|
||||||
roomRepository.updateSongInPlayCount(playCountEntity)
|
|
||||||
|
|
||||||
override suspend fun deleteSongInPlayCount(playCountEntity: PlayCountEntity) =
|
override suspend fun deleteSongInPlayCount(playCountEntity: PlayCountEntity) =
|
||||||
roomRepository.deleteSongInPlayCount(playCountEntity)
|
roomRepository.deleteSongInPlayCount(playCountEntity)
|
||||||
|
@ -301,8 +289,8 @@ class RealRepository(
|
||||||
roomRepository.clearSongHistory()
|
roomRepository.clearSongHistory()
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity> =
|
override suspend fun findSongExistInPlayCount(songId: Long): PlayCountEntity? =
|
||||||
roomRepository.checkSongExistInPlayCount(songId)
|
roomRepository.findSongExistInPlayCount(songId)
|
||||||
|
|
||||||
override suspend fun playCountSongs(): List<PlayCountEntity> =
|
override suspend fun playCountSongs(): List<PlayCountEntity> =
|
||||||
roomRepository.playCountSongs()
|
roomRepository.playCountSongs()
|
||||||
|
|
|
@ -30,16 +30,13 @@ 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 insertSongInPlayCount(playCountEntity: PlayCountEntity)
|
suspend fun upsertSongInPlayCount(playCountEntity: PlayCountEntity)
|
||||||
suspend fun updateSongInPlayCount(playCountEntity: PlayCountEntity)
|
|
||||||
suspend fun deleteSongInPlayCount(playCountEntity: PlayCountEntity)
|
suspend fun deleteSongInPlayCount(playCountEntity: PlayCountEntity)
|
||||||
suspend fun deleteSongInHistory(songId: Long)
|
suspend fun deleteSongInHistory(songId: Long)
|
||||||
suspend fun clearSongHistory()
|
suspend fun clearSongHistory()
|
||||||
suspend fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity>
|
suspend fun findSongExistInPlayCount(songId: Long): PlayCountEntity?
|
||||||
suspend fun playCountSongs(): List<PlayCountEntity>
|
suspend fun playCountSongs(): List<PlayCountEntity>
|
||||||
suspend fun deleteSongs(songs: List<Song>)
|
suspend fun deleteSongs(songs: List<Song>)
|
||||||
suspend fun isSongFavorite(context: Context, songId: Long): Boolean
|
suspend fun isSongFavorite(context: Context, songId: Long): Boolean
|
||||||
|
@ -133,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()
|
||||||
|
@ -155,11 +146,8 @@ class RealRoomRepository(
|
||||||
playlistDao.playlist(favorite).first().playListId
|
playlistDao.playlist(favorite).first().playListId
|
||||||
) else emptyList()
|
) else emptyList()
|
||||||
|
|
||||||
override suspend fun insertSongInPlayCount(playCountEntity: PlayCountEntity) =
|
override suspend fun upsertSongInPlayCount(playCountEntity: PlayCountEntity) =
|
||||||
playCountDao.insertSongInPlayCount(playCountEntity)
|
playCountDao.upsertSongInPlayCount(playCountEntity)
|
||||||
|
|
||||||
override suspend fun updateSongInPlayCount(playCountEntity: PlayCountEntity) =
|
|
||||||
playCountDao.updateSongInPlayCount(playCountEntity)
|
|
||||||
|
|
||||||
override suspend fun deleteSongInPlayCount(playCountEntity: PlayCountEntity) =
|
override suspend fun deleteSongInPlayCount(playCountEntity: PlayCountEntity) =
|
||||||
playCountDao.deleteSongInPlayCount(playCountEntity)
|
playCountDao.deleteSongInPlayCount(playCountEntity)
|
||||||
|
@ -172,8 +160,8 @@ class RealRoomRepository(
|
||||||
historyDao.clearHistory()
|
historyDao.clearHistory()
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity> =
|
override suspend fun findSongExistInPlayCount(songId: Long): PlayCountEntity? =
|
||||||
playCountDao.checkSongExistInPlayCount(songId)
|
playCountDao.findSongExistInPlayCount(songId)
|
||||||
|
|
||||||
override suspend fun playCountSongs(): List<PlayCountEntity> =
|
override suspend fun playCountSongs(): List<PlayCountEntity> =
|
||||||
playCountDao.playCountSongs()
|
playCountDao.playCountSongs()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue