Skip to content

Commit a3bfa04

Browse files
refactor(widget): reuse controller.setRating for favorite toggle
1 parent 0f1c47b commit a3bfa04

1 file changed

Lines changed: 3 additions & 78 deletions

File tree

app/src/main/java/org/akanework/gramophone/logic/GramophonePlaybackService.kt

Lines changed: 3 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -810,84 +810,9 @@ class GramophonePlaybackService : MediaLibraryService(), MediaSessionService.Lis
810810
}
811811

812812
fun toggleCurrentItemFavorite() {
813-
val item = endedWorkaroundPlayer?.currentMediaItem ?: return
814-
val currentIsHeart = (item.mediaMetadata.userRating as? HeartRating)?.isHeart == true
815-
val newIsHeart = !currentIsHeart
816-
lifecycleScope.launch(Dispatchers.Default) {
817-
val song = Entry.ofMediaItem(item) ?: return@launch
818-
updateFavoritePlaylist(song, newIsHeart)
819-
}
820-
}
821-
822-
private suspend fun updateFavoritePlaylist(song: Entry, isHeart: Boolean): Boolean {
823-
val uriIn = gramophoneApplication.reader.playlistListFlow.map { it.find { p -> p is Favorite } }.first()?.id?.let {
824-
ContentUris.withAppendedId(@Suppress("deprecation") MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, it)
825-
}
826-
val token = if (uriIn != null) {
827-
MediaStoreCompat.needRequestBytesWrite(this@GramophonePlaybackService, uriIn)
828-
} else {
829-
MediaStoreCompat.needRequestCreate(
830-
this@GramophonePlaybackService,
831-
ItemManipulator.getDefaultPlaylistFile(ItemManipulator.FAVORITES).path
832-
)
833-
}
834-
if (token != null) {
835-
notifyFavoriteError(song, isHeart)
836-
return false
837-
}
838-
return try {
839-
val uri = uriIn ?: ItemManipulator.createPlaylist(
840-
this@GramophonePlaybackService,
841-
ItemManipulator.getDefaultPlaylistFile(ItemManipulator.FAVORITES)
842-
)
843-
val readback = if (uriIn != null) {
844-
ItemManipulator.readbackPlaylist(this@GramophonePlaybackService, uri)
845-
} else {
846-
PlaylistSerializer.Playlist.create()
847-
}
848-
val newSongs = if (isHeart) {
849-
readback.entries + song
850-
} else {
851-
readback.entries.filter { !song.fuzzyEquals(it) }
852-
}
853-
ItemManipulator.setPlaylistContent(this@GramophonePlaybackService, uri, readback.copy(entries = newSongs), uriIn == null)
854-
true
855-
} catch (e: Exception) {
856-
Log.e(TAG, "failed to update favorite for ${song.title}", e)
857-
notifyFavoriteError(song, isHeart)
858-
false
859-
}
860-
}
861-
862-
private fun notifyFavoriteError(song: Entry, isHeart: Boolean) {
863-
if (!supportsNotificationPermission() || hasNotificationPermission()) {
864-
@SuppressLint("MissingPermission")
865-
nm.notify(
866-
FAVE_ID,
867-
NotificationCompat.Builder(this, NOTIFY_CHANNEL_ID).apply {
868-
setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
869-
setAutoCancel(true)
870-
setCategory(NotificationCompat.CATEGORY_ERROR)
871-
setSmallIcon(R.drawable.ic_error)
872-
setContentTitle(getString(R.string.favorite_failed_title))
873-
setContentText(getString(R.string.favorite_failed_text))
874-
setContentIntent(
875-
PendingIntent.getActivity(
876-
this@GramophonePlaybackService,
877-
PENDING_INTENT_FAVE_ID,
878-
Intent(this@GramophonePlaybackService, MainActivity::class.java)
879-
.putExtra(MainActivity.FAVORITE_ENTRY, song)
880-
.putExtra(MainActivity.FAVORITE_STATE, isHeart),
881-
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
882-
)
883-
)
884-
setVibrate(longArrayOf(0L, 200L))
885-
setLights(0, 0, 0)
886-
setBadgeIconType(NotificationCompat.BADGE_ICON_NONE)
887-
setSound(null)
888-
}.build()
889-
)
890-
}
813+
val currentMediaItem = controller?.currentMediaItem ?: return
814+
val isHeart = (currentMediaItem.mediaMetadata.userRating as? HeartRating)?.isHeart == true
815+
controller?.setRating(HeartRating(!isHeart))
891816
}
892817

893818
// When destroying, we should release server side player

0 commit comments

Comments
 (0)