Merge pull request #1278 from 66Omar/1216-add-option-to-clear-history

1216 add option to clear history
This commit is contained in:
Prathamesh More 2022-03-04 21:32:19 +05:30 committed by GitHub
commit 0e9392b6c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 75 additions and 8 deletions

View file

@ -39,4 +39,7 @@ interface HistoryDao {
@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 observableHistorySongs(): LiveData<List<HistoryEntity>> fun observableHistorySongs(): LiveData<List<HistoryEntity>>
@Query("DELETE FROM HistoryEntity")
suspend fun clearHistory()
} }

View file

@ -49,6 +49,7 @@ class LibraryViewModel(
private val genres = MutableLiveData<List<Genre>>() private val genres = MutableLiveData<List<Genre>>()
private val searchResults = MutableLiveData<List<Any>>() private val searchResults = MutableLiveData<List<Any>>()
private val fabMargin = MutableLiveData(0) private val fabMargin = MutableLiveData(0)
private val songHistory = MutableLiveData<List<Song>>()
val paletteColor: LiveData<Int> = _paletteColor val paletteColor: LiveData<Int> = _paletteColor
init { init {
@ -320,20 +321,30 @@ class LibraryViewModel(
emit(repository.contributor()) emit(repository.contributor())
} }
fun observableHistorySongs(): LiveData<List<Song>> = liveData { fun observableHistorySongs(): LiveData<List<Song>> {
val songs = repository.historySong().map { val songs = repository.historySong().map {
it.toSong() it.toSong()
} }
emit(songs) songHistory.value = songs
// Cleaning up deleted or moved songs // Cleaning up deleted or moved songs
songs.forEach { song -> viewModelScope.launch {
if (!File(song.data).exists() || song.id == -1L) { songs.forEach { song ->
repository.deleteSongInHistory(song.id) if (!File(song.data).exists() || song.id == -1L) {
repository.deleteSongInHistory(song.id)
}
} }
} }
emit(repository.historySong().map { songHistory.value = repository.historySong().map {
it.toSong() it.toSong()
}) }
return songHistory
}
fun clearHistory() {
viewModelScope.launch(IO) {
repository.clearSongHistory()
}
songHistory.value = emptyList()
} }
fun favorites() = repository.favorites() fun favorites() = repository.favorites()

View file

@ -15,6 +15,9 @@
package code.name.monkey.retromusic.fragments.other package code.name.monkey.retromusic.fragments.other
import android.os.Bundle import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View import android.view.View
import androidx.activity.addCallback import androidx.activity.addCallback
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
@ -55,6 +58,12 @@ class DetailListFragment : AbsMainActivityFragment(R.layout.fragment_playlist_de
private val args by navArgs<DetailListFragmentArgs>() private val args by navArgs<DetailListFragmentArgs>()
private var _binding: FragmentPlaylistDetailBinding? = null private var _binding: FragmentPlaylistDetailBinding? = null
private val binding get() = _binding!! private val binding get() = _binding!!
private var showClearHistoryOption = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
@ -81,7 +90,10 @@ class DetailListFragment : AbsMainActivityFragment(R.layout.fragment_playlist_de
TOP_ALBUMS -> loadAlbums(R.string.top_albums, TOP_ALBUMS) TOP_ALBUMS -> loadAlbums(R.string.top_albums, TOP_ALBUMS)
RECENT_ALBUMS -> loadAlbums(R.string.recent_albums, RECENT_ALBUMS) RECENT_ALBUMS -> loadAlbums(R.string.recent_albums, RECENT_ALBUMS)
FAVOURITES -> loadFavorite() FAVOURITES -> loadFavorite()
HISTORY_PLAYLIST -> loadHistory() HISTORY_PLAYLIST -> {
loadHistory()
showClearHistoryOption = true // Reference to onCreateOptionsMenu
}
LAST_ADDED_PLAYLIST -> lastAddedSongs() LAST_ADDED_PLAYLIST -> lastAddedSongs()
TOP_PLAYED_PLAYLIST -> topPlayed() TOP_PLAYED_PLAYLIST -> topPlayed()
} }
@ -281,4 +293,24 @@ class DetailListFragment : AbsMainActivityFragment(R.layout.fragment_playlist_de
} }
return cab as AttachedCab return cab as AttachedCab
} }
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.menu_clear_history, menu)
if (showClearHistoryOption) {
menu.findItem(R.id.action_clear_history).isVisible = true // Show Clear History option
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_clear_history -> libraryViewModel.clearHistory()
/*
TODO("Show a snackbar showing that history has been successfully
cleared and that will have an undo button")
*/
}
return false
}
} }

View file

@ -100,6 +100,7 @@ interface Repository {
suspend fun updateSongInPlayCount(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 checkSongExistInPlayCount(songId: Long): List<PlayCountEntity> suspend fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity>
suspend fun playCountSongs(): List<PlayCountEntity> suspend fun playCountSongs(): List<PlayCountEntity>
suspend fun blackListPaths(): List<BlackListStoreEntity> suspend fun blackListPaths(): List<BlackListStoreEntity>
@ -330,6 +331,10 @@ class RealRepository(
override suspend fun deleteSongInHistory(songId: Long) = override suspend fun deleteSongInHistory(songId: Long) =
roomRepository.deleteSongInHistory(songId) roomRepository.deleteSongInHistory(songId)
override suspend fun clearSongHistory() {
roomRepository.clearSongHistory()
}
override suspend fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity> = override suspend fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity> =
roomRepository.checkSongExistInPlayCount(songId) roomRepository.checkSongExistInPlayCount(songId)

View file

@ -39,6 +39,7 @@ interface RoomRepository {
suspend fun updateSongInPlayCount(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 checkSongExistInPlayCount(songId: Long): List<PlayCountEntity> suspend fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity>
suspend fun playCountSongs(): List<PlayCountEntity> suspend fun playCountSongs(): List<PlayCountEntity>
suspend fun insertBlacklistPath(blackListStoreEntities: List<BlackListStoreEntity>) suspend fun insertBlacklistPath(blackListStoreEntities: List<BlackListStoreEntity>)
@ -170,6 +171,10 @@ class RealRoomRepository(
historyDao.deleteSongInHistory(songId) historyDao.deleteSongInHistory(songId)
} }
override suspend fun clearSongHistory() {
historyDao.clearHistory()
}
override suspend fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity> = override suspend fun checkSongExistInPlayCount(songId: Long): List<PlayCountEntity> =
playCountDao.checkSongExistInPlayCount(songId) playCountDao.checkSongExistInPlayCount(songId)

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_clear_history"
android:title="@string/clear_history"
app:showAsAction="never"
android:visible="false"
/>
</menu>

View file

@ -206,6 +206,7 @@
<string name="help_summary">Need more help?</string> <string name="help_summary">Need more help?</string>
<string name="hinge">Hinge</string> <string name="hinge">Hinge</string>
<string name="history">History</string> <string name="history">History</string>
<string name="clear_history">Clear History</string>
<string name="home">Home</string> <string name="home">Home</string>
<string name="horizontal_flip">Horizontal flip</string> <string name="horizontal_flip">Horizontal flip</string>
<string name="image">Image</string> <string name="image">Image</string>