Cleaning up moved or deleted songs from Smart playlists i.e. History, Top played

main
Prathamesh More 2021-11-29 19:31:41 +05:30
parent 2dcbe0801b
commit dcc04c6ea4
4 changed files with 37 additions and 1 deletions

View File

@ -26,6 +26,8 @@ interface HistoryDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertSongInHistory(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?

View File

@ -29,6 +29,7 @@ import code.name.monkey.retromusic.util.DensityUtil
import code.name.monkey.retromusic.util.PreferenceUtil
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
import java.io.File
class LibraryViewModel(
private val repository: RealRepository
@ -272,6 +273,16 @@ class LibraryViewModel(
}
fun playCountSongs(): LiveData<List<Song>> = liveData {
val songs = repository.playCountSongs().map {
it.toSong()
}
emit(songs)
// Cleaning up deleted or moved songs
songs.forEach { song ->
if (!File(song.data).exists() || song.id == -1L) {
repository.deleteSongInPlayCount(song.toPlayCount())
}
}
emit(repository.playCountSongs().map {
it.toSong()
})
@ -303,7 +314,21 @@ class LibraryViewModel(
emit(repository.contributor())
}
fun observableHistorySongs() = repository.observableHistorySongs()
fun observableHistorySongs(): LiveData<List<Song>> = liveData {
val songs = repository.historySong().map {
it.toSong()
}
emit(songs)
// Cleaning up deleted or moved songs
songs.forEach { song ->
if (!File(song.data).exists() || song.id == -1L) {
repository.deleteSongInHistory(song.id)
}
}
emit(repository.historySong().map {
it.toSong()
})
}
fun favorites() = repository.favorites()

View File

@ -97,6 +97,7 @@ interface Repository {
suspend fun insertSongInPlayCount(playCountEntity: PlayCountEntity)
suspend fun updateSongInPlayCount(playCountEntity: PlayCountEntity)
suspend fun deleteSongInPlayCount(playCountEntity: PlayCountEntity)
suspend fun deleteSongInHistory(songId: Long)
suspend fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity>
suspend fun playCountSongs(): List<PlayCountEntity>
suspend fun blackListPaths(): List<BlackListStoreEntity>
@ -323,6 +324,9 @@ class RealRepository(
override suspend fun deleteSongInPlayCount(playCountEntity: PlayCountEntity) =
roomRepository.deleteSongInPlayCount(playCountEntity)
override suspend fun deleteSongInHistory(songId: Long) =
roomRepository.deleteSongInHistory(songId)
override suspend fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity> =
roomRepository.checkSongExistInPlayCount(songId)

View File

@ -36,6 +36,7 @@ interface RoomRepository {
suspend fun insertSongInPlayCount(playCountEntity: PlayCountEntity)
suspend fun updateSongInPlayCount(playCountEntity: PlayCountEntity)
suspend fun deleteSongInPlayCount(playCountEntity: PlayCountEntity)
suspend fun deleteSongInHistory(songId: Long)
suspend fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity>
suspend fun playCountSongs(): List<PlayCountEntity>
suspend fun insertBlacklistPath(blackListStoreEntities: List<BlackListStoreEntity>)
@ -161,6 +162,10 @@ class RealRoomRepository(
override suspend fun deleteSongInPlayCount(playCountEntity: PlayCountEntity) =
playCountDao.deleteSongInPlayCount(playCountEntity)
override suspend fun deleteSongInHistory(songId: Long) {
historyDao.deleteSongInHistory(songId)
}
override suspend fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity> =
playCountDao.checkSongExistInPlayCount(songId)