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

Add support for track shuffling #327

Merged
merged 1 commit into from
Oct 3, 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 @@ -32,6 +32,7 @@ fun MusicPlayerScreen(
PlaybackUiEvent.Previous -> previous()
PlaybackUiEvent.Repeat -> repeat()
is PlaybackUiEvent.Seek -> seekTo(event.position)
PlaybackUiEvent.ToggleShuffle -> toggleShuffle()
}
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package io.newm.feature.musicplayer

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.Spring.StiffnessLow
import androidx.compose.animation.core.spring
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand Down Expand Up @@ -140,7 +139,8 @@ internal fun MusicPlayerViewer(
Row(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 16.dp)
.padding(vertical = 16.dp),
horizontalArrangement = Arrangement.SpaceBetween
) {
IconButton(onClick = onNavigateUp) {
Icon(
Expand All @@ -149,6 +149,11 @@ internal fun MusicPlayerViewer(
tint = White
)
}

ShareButton(
songTitle = playbackStatus.track?.title,
songArtist = playbackStatus.track?.artist
)
}
Spacer(modifier = Modifier.weight(1f))
Text(
Expand Down Expand Up @@ -256,15 +261,29 @@ fun PlaybackControlPanel(
modifier = Modifier.padding(horizontal = 12.dp),
onClick = { onEvent(PlaybackUiEvent.Next) })
Spacer(modifier = Modifier.weight(1f))
ShareButton(
songTitle = playbackStatus.track?.title,
songArtist = playbackStatus.track?.artist
ShuffleButton(
shuffleMode = playbackStatus.shuffleMode,
onClick = { onEvent(PlaybackUiEvent.ToggleShuffle) }
)
}
}
}
}

@Composable
fun ShuffleButton(
onClick: () -> Unit,
shuffleMode: Boolean
) {
IconButton(onClick = onClick) {
Icon(
painter = painterResource(R.drawable.ic_music_player_shuffle),
contentDescription = "Shuffle",
tint = if (shuffleMode) DarkViolet else White
)
}
}

@Composable
private fun PlayOrPauseButton(
playbackStatus: PlaybackStatus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ data class PlaybackStatus(
val track: Track?,
val position: Long,
val duration: Long,
val repeatMode: PlaybackRepeatMode
val repeatMode: PlaybackRepeatMode,
val shuffleMode: Boolean
) {
companion object {
val EMPTY: PlaybackStatus = PlaybackStatus(
state = PlaybackState.STOPPED,
position = 0,
duration = 0,
track = null,
repeatMode = PlaybackRepeatMode.REPEAT_OFF
repeatMode = PlaybackRepeatMode.REPEAT_OFF,
shuffleMode = false,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface MusicPlayer {
fun seekTo(index: Int, position: Long)
fun repeat()
fun setPlaylist(playlist: Playlist, initialTrackIndex: Int)
fun toggleShuffle()
}

class MusicPlayerImpl(
Expand Down Expand Up @@ -85,7 +86,8 @@ class MusicPlayerImpl(
position = player.currentPosition,
duration = player.duration,
track = player.currentMediaItem?.toTrack(),
repeatMode = repeatMode
repeatMode = repeatMode,
shuffleMode = player.shuffleModeEnabled
)
}
}
Expand Down Expand Up @@ -162,6 +164,12 @@ class MusicPlayerImpl(
})
player.seekTo(initialTrackIndex, 0)
}

override fun toggleShuffle() {
eventLogger.logClickEvent(AppScreens.MusicPlayerScreen.TOGGLE_SHUFFLE_BUTTON)
Log.d("MusicPlayer", "Toggle shuffle")
player.shuffleModeEnabled = !player.shuffleModeEnabled
}
}

private fun MediaItem.toTrack(): Track {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ sealed interface PlaybackUiEvent {
data class Seek(val position: Long): PlaybackUiEvent

data object Repeat : PlaybackUiEvent
data object ToggleShuffle : PlaybackUiEvent
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ object AppScreens {
const val STOP_BUTTON: String = "Stop"
const val REPEAT_BUTTON: String = "Repeat"
const val SEEK_ACTION: String = "Seek Action"

const val TOGGLE_SHUFFLE_BUTTON: String = "Toggle Shuffle"
}

object NFTLibraryEmptyWalletScreen : ScreenEvents {
Expand Down
Loading