Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify NowPlayingNotification.kt #5637

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fun Player.togglePlayPauseState() {
}

!isPlaying -> play()

else -> pause()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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
Expand Down Expand Up @@ -66,6 +67,7 @@

private fun loadCurrentLargeIcon() {
if (DataSaverMode.isEnabled(context)) return

if (notificationBitmap == null) {
enqueueThumbnailRequest {
createOrUpdateNotification()
Expand All @@ -84,12 +86,14 @@
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)
}
Expand Down Expand Up @@ -245,15 +249,13 @@
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 {

Check failure on line 252 in app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 A multiline expression should start on a new line Raw Output: app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt:252:32: error: A multiline expression should start on a new line (standard:multiline-expression-wrapping)
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 {
Expand All @@ -277,19 +279,15 @@
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 -> {
Expand All @@ -301,10 +299,7 @@
}

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 -> {
Expand Down
Loading