Fix toggle notification favourite icon #954
This commit is contained in:
parent
1a5d74d9cf
commit
85bd49f391
2 changed files with 144 additions and 123 deletions
|
@ -27,6 +27,8 @@ import androidx.core.text.HtmlCompat
|
|||
import androidx.media.app.NotificationCompat.MediaStyle
|
||||
import code.name.monkey.retromusic.R
|
||||
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.palette.BitmapPaletteWrapper
|
||||
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.target.SimpleTarget
|
||||
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
|
||||
|
||||
@Synchronized
|
||||
override fun update() {
|
||||
stopped = false
|
||||
|
||||
GlobalScope.launch {
|
||||
val song = service.currentSong
|
||||
val playlist: PlaylistEntity? = MusicUtil.repository.favoritePlaylist()
|
||||
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 =
|
||||
if (isPlaying) R.drawable.ic_pause_white_48dp else R.drawable.ic_play_arrow_white_48dp
|
||||
val favoriteResId =
|
||||
|
@ -174,6 +184,7 @@ class PlayingNotificationImpl : PlayingNotification() {
|
|||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun retrievePlaybackAction(action: String): PendingIntent {
|
||||
val serviceName = ComponentName(service, MusicService::class.java)
|
||||
|
|
|
@ -15,7 +15,9 @@ import android.widget.Toast
|
|||
import androidx.core.content.FileProvider
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
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.toSongEntity
|
||||
import code.name.monkey.retromusic.extensions.getLong
|
||||
import code.name.monkey.retromusic.helper.MusicPlayerRemote.removeFromQueue
|
||||
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.repository.RealPlaylistRepository
|
||||
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.service.MusicService
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import org.jaudiotagger.audio.AudioFileIO
|
||||
import org.jaudiotagger.tag.FieldKey
|
||||
import org.koin.core.KoinComponent
|
||||
|
@ -321,17 +326,22 @@ object MusicUtil : KoinComponent {
|
|||
return playlist.name == context.getString(R.string.favorites)
|
||||
}
|
||||
|
||||
val repository = get<Repository>()
|
||||
fun toggleFavorite(context: Context, song: Song) {
|
||||
if (isFavorite(context, song)) {
|
||||
PlaylistsUtil.removeFromPlaylist(context, song, getFavoritesPlaylist(context).id)
|
||||
GlobalScope.launch {
|
||||
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 {
|
||||
PlaylistsUtil.addToPlaylist(
|
||||
context, song, getOrCreateFavoritesPlaylist(context).id,
|
||||
false
|
||||
)
|
||||
repository.insertSongs(listOf(song.toSongEntity(playlist.playListId)))
|
||||
}
|
||||
}
|
||||
context.sendBroadcast(Intent(MusicService.FAVORITE_STATE_CHANGED))
|
||||
}
|
||||
}
|
||||
|
||||
private fun getFavoritesPlaylist(context: Context): Playlist {
|
||||
return RealPlaylistRepository(context.contentResolver).playlist(context.getString(R.string.favorites))
|
||||
|
|
Loading…
Reference in a new issue