Fix toggle notification favourite icon #954

main
Hemanth S 2020-10-17 14:47:57 +05:30
parent 1a5d74d9cf
commit 85bd49f391
2 changed files with 144 additions and 123 deletions

View File

@ -27,6 +27,8 @@ import androidx.core.text.HtmlCompat
import androidx.media.app.NotificationCompat.MediaStyle import androidx.media.app.NotificationCompat.MediaStyle
import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.activities.MainActivity import code.name.monkey.retromusic.activities.MainActivity
import code.name.monkey.retromusic.db.PlaylistEntity
import code.name.monkey.retromusic.db.toSongEntity
import code.name.monkey.retromusic.glide.SongGlideRequest import code.name.monkey.retromusic.glide.SongGlideRequest
import code.name.monkey.retromusic.glide.palette.BitmapPaletteWrapper import code.name.monkey.retromusic.glide.palette.BitmapPaletteWrapper
import code.name.monkey.retromusic.service.MusicService import code.name.monkey.retromusic.service.MusicService
@ -38,17 +40,25 @@ import com.bumptech.glide.Glide
import com.bumptech.glide.request.animation.GlideAnimation import com.bumptech.glide.request.animation.GlideAnimation
import com.bumptech.glide.request.target.SimpleTarget import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.target.Target import com.bumptech.glide.request.target.Target
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.koin.core.KoinComponent
class PlayingNotificationImpl : PlayingNotification() { class PlayingNotificationImpl : PlayingNotification(), KoinComponent {
private var target: Target<BitmapPaletteWrapper>? = null private var target: Target<BitmapPaletteWrapper>? = null
@Synchronized @Synchronized
override fun update() { override fun update() {
stopped = false stopped = false
GlobalScope.launch {
val song = service.currentSong val song = service.currentSong
val playlist: PlaylistEntity? = MusicUtil.repository.favoritePlaylist()
val isPlaying = service.isPlaying val isPlaying = service.isPlaying
val isFavorite = MusicUtil.isFavorite(service, song) val isFavorite = if (playlist != null) {
val songEntity = song.toSongEntity(playlist.playListId)
MusicUtil.repository.isFavoriteSong(songEntity).isNotEmpty()
} else false
val playButtonResId = val playButtonResId =
if (isPlaying) R.drawable.ic_pause_white_48dp else R.drawable.ic_play_arrow_white_48dp if (isPlaying) R.drawable.ic_pause_white_48dp else R.drawable.ic_play_arrow_white_48dp
val favoriteResId = val favoriteResId =
@ -174,6 +184,7 @@ class PlayingNotificationImpl : PlayingNotification() {
}) })
} }
} }
}
private fun retrievePlaybackAction(action: String): PendingIntent { private fun retrievePlaybackAction(action: String): PendingIntent {
val serviceName = ComponentName(service, MusicService::class.java) val serviceName = ComponentName(service, MusicService::class.java)

View File

@ -15,7 +15,9 @@ import android.widget.Toast
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.db.PlaylistEntity
import code.name.monkey.retromusic.db.SongEntity import code.name.monkey.retromusic.db.SongEntity
import code.name.monkey.retromusic.db.toSongEntity
import code.name.monkey.retromusic.extensions.getLong import code.name.monkey.retromusic.extensions.getLong
import code.name.monkey.retromusic.helper.MusicPlayerRemote.removeFromQueue import code.name.monkey.retromusic.helper.MusicPlayerRemote.removeFromQueue
import code.name.monkey.retromusic.model.Artist import code.name.monkey.retromusic.model.Artist
@ -24,8 +26,11 @@ import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.model.lyrics.AbsSynchronizedLyrics import code.name.monkey.retromusic.model.lyrics.AbsSynchronizedLyrics
import code.name.monkey.retromusic.repository.RealPlaylistRepository import code.name.monkey.retromusic.repository.RealPlaylistRepository
import code.name.monkey.retromusic.repository.RealSongRepository import code.name.monkey.retromusic.repository.RealSongRepository
import code.name.monkey.retromusic.repository.Repository
import code.name.monkey.retromusic.repository.SongRepository import code.name.monkey.retromusic.repository.SongRepository
import code.name.monkey.retromusic.service.MusicService import code.name.monkey.retromusic.service.MusicService
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.jaudiotagger.audio.AudioFileIO import org.jaudiotagger.audio.AudioFileIO
import org.jaudiotagger.tag.FieldKey import org.jaudiotagger.tag.FieldKey
import org.koin.core.KoinComponent import org.koin.core.KoinComponent
@ -321,17 +326,22 @@ object MusicUtil : KoinComponent {
return playlist.name == context.getString(R.string.favorites) return playlist.name == context.getString(R.string.favorites)
} }
val repository = get<Repository>()
fun toggleFavorite(context: Context, song: Song) { fun toggleFavorite(context: Context, song: Song) {
if (isFavorite(context, song)) { GlobalScope.launch {
PlaylistsUtil.removeFromPlaylist(context, song, getFavoritesPlaylist(context).id) val playlist: PlaylistEntity? = repository.favoritePlaylist()
if (playlist != null) {
val songEntity = song.toSongEntity(playlist.playListId)
val isFavorite = repository.isFavoriteSong(songEntity).isNotEmpty()
if (isFavorite) {
repository.removeSongFromPlaylist(songEntity)
} else { } else {
PlaylistsUtil.addToPlaylist( repository.insertSongs(listOf(song.toSongEntity(playlist.playListId)))
context, song, getOrCreateFavoritesPlaylist(context).id, }
false
)
} }
context.sendBroadcast(Intent(MusicService.FAVORITE_STATE_CHANGED)) context.sendBroadcast(Intent(MusicService.FAVORITE_STATE_CHANGED))
} }
}
private fun getFavoritesPlaylist(context: Context): Playlist { private fun getFavoritesPlaylist(context: Context): Playlist {
return RealPlaylistRepository(context.contentResolver).playlist(context.getString(R.string.favorites)) return RealPlaylistRepository(context.contentResolver).playlist(context.getString(R.string.favorites))