Merge pull request #5637 from Bnyro/master

refactor: simplify NowPlayingNotification.kt
This commit is contained in:
Bnyro 2024-02-18 17:14:12 +01:00 committed by GitHub
commit e451685776
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 21 deletions

View File

@ -14,6 +14,7 @@ fun Player.togglePlayPauseState() {
} }
!isPlaying -> play() !isPlaying -> play()
else -> pause() else -> pause()
} }
} }

View File

@ -27,6 +27,7 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.enums.NotificationId import com.github.libretube.enums.NotificationId
import com.github.libretube.extensions.seekBy import com.github.libretube.extensions.seekBy
import com.github.libretube.extensions.toMediaMetadataCompat import com.github.libretube.extensions.toMediaMetadataCompat
import com.github.libretube.extensions.togglePlayPauseState
import com.github.libretube.helpers.BackgroundHelper import com.github.libretube.helpers.BackgroundHelper
import com.github.libretube.helpers.ImageHelper import com.github.libretube.helpers.ImageHelper
import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.PlayerHelper
@ -66,6 +67,7 @@ class NowPlayingNotification(
private fun loadCurrentLargeIcon() { private fun loadCurrentLargeIcon() {
if (DataSaverMode.isEnabled(context)) return if (DataSaverMode.isEnabled(context)) return
if (notificationBitmap == null) { if (notificationBitmap == null) {
enqueueThumbnailRequest { enqueueThumbnailRequest {
createOrUpdateNotification() createOrUpdateNotification()
@ -84,12 +86,14 @@ class NowPlayingNotification(
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
} }
} }
return PendingIntentCompat return PendingIntentCompat
.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT, false) .getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT, false)
} }
private fun createIntent(action: String): PendingIntent? { private fun createIntent(action: String): PendingIntent? {
val intent = Intent(action).setPackage(context.packageName) val intent = Intent(action).setPackage(context.packageName)
return PendingIntentCompat return PendingIntentCompat
.getBroadcast(context, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT, false) .getBroadcast(context, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT, false)
} }
@ -245,15 +249,13 @@ class NowPlayingNotification(
private fun updateSessionPlaybackState(isPlaying: Boolean? = null, isLoading: Boolean? = null) { private fun updateSessionPlaybackState(isPlaying: Boolean? = null, isLoading: Boolean? = null) {
val loading = isLoading == true || (isPlaying == false && player.isLoading) val loading = isLoading == true || (isPlaying == false && player.isLoading)
val newPlaybackState = if (loading) { val newPlaybackState = when {
createPlaybackState(PlaybackStateCompat.STATE_BUFFERING) loading -> PlaybackStateCompat.STATE_BUFFERING
} else if (isPlaying ?: player.isPlaying) { isPlaying ?: player.isPlaying -> PlaybackStateCompat.STATE_PLAYING
createPlaybackState(PlaybackStateCompat.STATE_PLAYING) else -> PlaybackStateCompat.STATE_PAUSED
} else {
createPlaybackState(PlaybackStateCompat.STATE_PAUSED)
} }
mediaSession.setPlaybackState(newPlaybackState) mediaSession.setPlaybackState(createPlaybackState(newPlaybackState))
} }
private fun createPlaybackState(@PlaybackStateCompat.State state: Int): PlaybackStateCompat { private fun createPlaybackState(@PlaybackStateCompat.State state: Int): PlaybackStateCompat {
@ -277,19 +279,15 @@ class NowPlayingNotification(
private fun handlePlayerAction(action: String) { private fun handlePlayerAction(action: String) {
when (action) { when (action) {
NEXT -> { NEXT -> {
if (PlayingQueue.hasNext()) { if (!PlayingQueue.hasNext()) return
PlayingQueue.onQueueItemSelected(
PlayingQueue.currentIndex() + 1 PlayingQueue.onQueueItemSelected(PlayingQueue.currentIndex() + 1)
)
}
} }
PREV -> { PREV -> {
if (PlayingQueue.hasPrev()) { if (!PlayingQueue.hasPrev()) return
PlayingQueue.onQueueItemSelected(
PlayingQueue.currentIndex() - 1 PlayingQueue.onQueueItemSelected(PlayingQueue.currentIndex() - 1)
)
}
} }
REWIND -> { REWIND -> {
@ -301,10 +299,7 @@ class NowPlayingNotification(
} }
PLAY_PAUSE -> { PLAY_PAUSE -> {
if (player.playerError != null) player.prepare() player.togglePlayPauseState()
if (player.isPlaying) player.pause()
else if (player.playbackState == Player.STATE_ENDED) player.seekTo(0)
else player.play()
} }
STOP -> { STOP -> {