mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 06:10:31 +05:30
Merge pull request #5637 from Bnyro/master
refactor: simplify NowPlayingNotification.kt
This commit is contained in:
commit
e451685776
@ -14,6 +14,7 @@ fun Player.togglePlayPauseState() {
|
||||
}
|
||||
|
||||
!isPlaying -> play()
|
||||
|
||||
else -> pause()
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ import com.github.libretube.constants.IntentData
|
||||
import com.github.libretube.enums.NotificationId
|
||||
import com.github.libretube.extensions.seekBy
|
||||
import com.github.libretube.extensions.toMediaMetadataCompat
|
||||
import com.github.libretube.extensions.togglePlayPauseState
|
||||
import com.github.libretube.helpers.BackgroundHelper
|
||||
import com.github.libretube.helpers.ImageHelper
|
||||
import com.github.libretube.helpers.PlayerHelper
|
||||
@ -66,6 +67,7 @@ class NowPlayingNotification(
|
||||
|
||||
private fun loadCurrentLargeIcon() {
|
||||
if (DataSaverMode.isEnabled(context)) return
|
||||
|
||||
if (notificationBitmap == null) {
|
||||
enqueueThumbnailRequest {
|
||||
createOrUpdateNotification()
|
||||
@ -84,12 +86,14 @@ class NowPlayingNotification(
|
||||
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
|
||||
}
|
||||
}
|
||||
|
||||
return PendingIntentCompat
|
||||
.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT, false)
|
||||
}
|
||||
|
||||
private fun createIntent(action: String): PendingIntent? {
|
||||
val intent = Intent(action).setPackage(context.packageName)
|
||||
|
||||
return PendingIntentCompat
|
||||
.getBroadcast(context, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT, false)
|
||||
}
|
||||
@ -245,15 +249,13 @@ class NowPlayingNotification(
|
||||
private fun updateSessionPlaybackState(isPlaying: Boolean? = null, isLoading: Boolean? = null) {
|
||||
val loading = isLoading == true || (isPlaying == false && player.isLoading)
|
||||
|
||||
val newPlaybackState = if (loading) {
|
||||
createPlaybackState(PlaybackStateCompat.STATE_BUFFERING)
|
||||
} else if (isPlaying ?: player.isPlaying) {
|
||||
createPlaybackState(PlaybackStateCompat.STATE_PLAYING)
|
||||
} else {
|
||||
createPlaybackState(PlaybackStateCompat.STATE_PAUSED)
|
||||
val newPlaybackState = when {
|
||||
loading -> PlaybackStateCompat.STATE_BUFFERING
|
||||
isPlaying ?: player.isPlaying -> PlaybackStateCompat.STATE_PLAYING
|
||||
else -> PlaybackStateCompat.STATE_PAUSED
|
||||
}
|
||||
|
||||
mediaSession.setPlaybackState(newPlaybackState)
|
||||
mediaSession.setPlaybackState(createPlaybackState(newPlaybackState))
|
||||
}
|
||||
|
||||
private fun createPlaybackState(@PlaybackStateCompat.State state: Int): PlaybackStateCompat {
|
||||
@ -277,19 +279,15 @@ class NowPlayingNotification(
|
||||
private fun handlePlayerAction(action: String) {
|
||||
when (action) {
|
||||
NEXT -> {
|
||||
if (PlayingQueue.hasNext()) {
|
||||
PlayingQueue.onQueueItemSelected(
|
||||
PlayingQueue.currentIndex() + 1
|
||||
)
|
||||
}
|
||||
if (!PlayingQueue.hasNext()) return
|
||||
|
||||
PlayingQueue.onQueueItemSelected(PlayingQueue.currentIndex() + 1)
|
||||
}
|
||||
|
||||
PREV -> {
|
||||
if (PlayingQueue.hasPrev()) {
|
||||
PlayingQueue.onQueueItemSelected(
|
||||
PlayingQueue.currentIndex() - 1
|
||||
)
|
||||
}
|
||||
if (!PlayingQueue.hasPrev()) return
|
||||
|
||||
PlayingQueue.onQueueItemSelected(PlayingQueue.currentIndex() - 1)
|
||||
}
|
||||
|
||||
REWIND -> {
|
||||
@ -301,10 +299,7 @@ class NowPlayingNotification(
|
||||
}
|
||||
|
||||
PLAY_PAUSE -> {
|
||||
if (player.playerError != null) player.prepare()
|
||||
if (player.isPlaying) player.pause()
|
||||
else if (player.playbackState == Player.STATE_ENDED) player.seekTo(0)
|
||||
else player.play()
|
||||
player.togglePlayPauseState()
|
||||
}
|
||||
|
||||
STOP -> {
|
||||
|
Loading…
Reference in New Issue
Block a user