Fixed favorite toggle in DriveModeActivity
This commit is contained in:
parent
144fcf29bd
commit
421dc817a2
10 changed files with 125 additions and 95 deletions
|
@ -233,6 +233,7 @@ class LibraryViewModel(
|
|||
suspend fun artistById(id: Long) = repository.artistById(id)
|
||||
suspend fun favoritePlaylist() = repository.favoritePlaylist()
|
||||
suspend fun isFavoriteSong(song: SongEntity) = repository.isFavoriteSong(song)
|
||||
suspend fun isSongFavorite(songId: Long) = repository.isSongFavorite(songId)
|
||||
suspend fun insertSongs(songs: List<SongEntity>) = repository.insertSongs(songs)
|
||||
suspend fun removeSongFromPlaylist(songEntity: SongEntity) =
|
||||
repository.removeSongFromPlaylist(songEntity)
|
||||
|
|
|
@ -38,7 +38,6 @@ import androidx.core.os.bundleOf
|
|||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.navigation.findNavController
|
||||
import androidx.navigation.navOptions
|
||||
import androidx.transition.Fade
|
||||
import androidx.viewpager.widget.ViewPager
|
||||
import code.name.monkey.retromusic.EXTRA_ALBUM_ID
|
||||
import code.name.monkey.retromusic.EXTRA_ARTIST_ID
|
||||
|
@ -232,7 +231,7 @@ abstract class AbsPlayerFragment(@LayoutRes layout: Int) : AbsMainActivityFragme
|
|||
val playlist: PlaylistEntity = libraryViewModel.favoritePlaylist()
|
||||
if (playlist != null) {
|
||||
val songEntity = song.toSongEntity(playlist.playListId)
|
||||
val isFavorite = libraryViewModel.isFavoriteSong(songEntity).isNotEmpty()
|
||||
val isFavorite = libraryViewModel.isSongFavorite(song.id)
|
||||
if (isFavorite) {
|
||||
libraryViewModel.removeSongFromPlaylist(songEntity)
|
||||
} else {
|
||||
|
@ -246,32 +245,28 @@ abstract class AbsPlayerFragment(@LayoutRes layout: Int) : AbsMainActivityFragme
|
|||
|
||||
fun updateIsFavorite(animate: Boolean = false) {
|
||||
lifecycleScope.launch(IO) {
|
||||
val playlist: PlaylistEntity = libraryViewModel.favoritePlaylist()
|
||||
if (playlist != null) {
|
||||
val song: SongEntity =
|
||||
MusicPlayerRemote.currentSong.toSongEntity(playlist.playListId)
|
||||
val isFavorite: Boolean = libraryViewModel.isFavoriteSong(song).isNotEmpty()
|
||||
withContext(Main) {
|
||||
val icon = if (animate) {
|
||||
if (isFavorite) R.drawable.avd_favorite else R.drawable.avd_unfavorite
|
||||
} else {
|
||||
if (isFavorite) R.drawable.ic_favorite else R.drawable.ic_favorite_border
|
||||
}
|
||||
val drawable: Drawable = RetroUtil.getTintedVectorDrawable(
|
||||
requireContext(),
|
||||
icon,
|
||||
toolbarIconColor()
|
||||
)
|
||||
if (playerToolbar() != null) {
|
||||
playerToolbar()?.menu?.findItem(R.id.action_toggle_favorite)?.apply {
|
||||
setIcon(drawable)
|
||||
title =
|
||||
if (isFavorite) getString(R.string.action_remove_from_favorites)
|
||||
else getString(R.string.action_add_to_favorites)
|
||||
getIcon().also {
|
||||
if (it is AnimatedVectorDrawable) {
|
||||
it.start()
|
||||
}
|
||||
val isFavorite: Boolean =
|
||||
libraryViewModel.isSongFavorite(MusicPlayerRemote.currentSong.id)
|
||||
withContext(Main) {
|
||||
val icon = if (animate) {
|
||||
if (isFavorite) R.drawable.avd_favorite else R.drawable.avd_unfavorite
|
||||
} else {
|
||||
if (isFavorite) R.drawable.ic_favorite else R.drawable.ic_favorite_border
|
||||
}
|
||||
val drawable: Drawable = RetroUtil.getTintedVectorDrawable(
|
||||
requireContext(),
|
||||
icon,
|
||||
toolbarIconColor()
|
||||
)
|
||||
if (playerToolbar() != null) {
|
||||
playerToolbar()?.menu?.findItem(R.id.action_toggle_favorite)?.apply {
|
||||
setIcon(drawable)
|
||||
title =
|
||||
if (isFavorite) getString(R.string.action_remove_from_favorites)
|
||||
else getString(R.string.action_add_to_favorites)
|
||||
getIcon().also {
|
||||
if (it is AnimatedVectorDrawable) {
|
||||
it.start()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -317,28 +317,24 @@ class FullPlaybackControlsFragment :
|
|||
|
||||
fun updateIsFavorite(animate: Boolean = false) {
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
val playlist: PlaylistEntity = libraryViewModel.favoritePlaylist()
|
||||
if (playlist != null) {
|
||||
val song: SongEntity =
|
||||
MusicPlayerRemote.currentSong.toSongEntity(playlist.playListId)
|
||||
val isFavorite: Boolean = libraryViewModel.isFavoriteSong(song).isNotEmpty()
|
||||
withContext(Dispatchers.Main) {
|
||||
val icon = if (animate) {
|
||||
if (isFavorite) R.drawable.avd_favorite else R.drawable.avd_unfavorite
|
||||
} else {
|
||||
if (isFavorite) R.drawable.ic_favorite else R.drawable.ic_favorite_border
|
||||
}
|
||||
val drawable: Drawable = RetroUtil.getTintedVectorDrawable(
|
||||
requireContext(),
|
||||
icon,
|
||||
Color.WHITE
|
||||
)
|
||||
binding.songFavourite.apply {
|
||||
setImageDrawable(drawable)
|
||||
getDrawable().also {
|
||||
if (it is AnimatedVectorDrawable) {
|
||||
it.start()
|
||||
}
|
||||
val isFavorite: Boolean =
|
||||
libraryViewModel.isSongFavorite(MusicPlayerRemote.currentSong.id)
|
||||
withContext(Dispatchers.Main) {
|
||||
val icon = if (animate) {
|
||||
if (isFavorite) R.drawable.avd_favorite else R.drawable.avd_unfavorite
|
||||
} else {
|
||||
if (isFavorite) R.drawable.ic_favorite else R.drawable.ic_favorite_border
|
||||
}
|
||||
val drawable: Drawable = RetroUtil.getTintedVectorDrawable(
|
||||
requireContext(),
|
||||
icon,
|
||||
Color.WHITE
|
||||
)
|
||||
binding.songFavourite.apply {
|
||||
setImageDrawable(drawable)
|
||||
getDrawable().also {
|
||||
if (it is AnimatedVectorDrawable) {
|
||||
it.start()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -281,23 +281,19 @@ class GradientPlayerFragment : AbsPlayerFragment(R.layout.fragment_gradient_play
|
|||
|
||||
private fun updateIsFavoriteIcon(animate: Boolean = false) {
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
val playlist: PlaylistEntity = libraryViewModel.favoritePlaylist()
|
||||
if (playlist != null) {
|
||||
val song: SongEntity =
|
||||
MusicPlayerRemote.currentSong.toSongEntity(playlist.playListId)
|
||||
val isFavorite: Boolean = libraryViewModel.isFavoriteSong(song).isNotEmpty()
|
||||
withContext(Dispatchers.Main) {
|
||||
val icon = if (animate) {
|
||||
if (isFavorite) R.drawable.avd_favorite else R.drawable.avd_unfavorite
|
||||
} else {
|
||||
if (isFavorite) R.drawable.ic_favorite else R.drawable.ic_favorite_border
|
||||
}
|
||||
binding.playbackControlsFragment.songFavourite.apply {
|
||||
setImageResource(icon)
|
||||
drawable.also {
|
||||
if (it is AnimatedVectorDrawable) {
|
||||
it.start()
|
||||
}
|
||||
val isFavorite: Boolean =
|
||||
libraryViewModel.isSongFavorite(MusicPlayerRemote.currentSong.id)
|
||||
withContext(Dispatchers.Main) {
|
||||
val icon = if (animate) {
|
||||
if (isFavorite) R.drawable.avd_favorite else R.drawable.avd_unfavorite
|
||||
} else {
|
||||
if (isFavorite) R.drawable.ic_favorite else R.drawable.ic_favorite_border
|
||||
}
|
||||
binding.playbackControlsFragment.songFavourite.apply {
|
||||
setImageResource(icon)
|
||||
drawable.also {
|
||||
if (it is AnimatedVectorDrawable) {
|
||||
it.start()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ class PlaylistsFragment :
|
|||
super.onViewCreated(view, savedInstanceState)
|
||||
libraryViewModel.getPlaylists().observe(viewLifecycleOwner, {
|
||||
if (it.isNotEmpty())
|
||||
adapter?.swapDataSet(it.filter { playlistWithSongs-> playlistWithSongs.songs.isNotEmpty() })
|
||||
adapter?.swapDataSet(it)
|
||||
else
|
||||
adapter?.swapDataSet(listOf())
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue