|
4 | 4 | */ |
5 | 5 | package ch.srgssr.pillarbox.demo.ui.player.controls |
6 | 6 |
|
| 7 | +import androidx.compose.animation.AnimatedVisibility |
| 8 | +import androidx.compose.foundation.background |
7 | 9 | import androidx.compose.foundation.layout.Row |
| 10 | +import androidx.compose.foundation.layout.Spacer |
8 | 11 | import androidx.compose.material.icons.Icons |
9 | 12 | import androidx.compose.material.icons.filled.Fullscreen |
10 | 13 | import androidx.compose.material.icons.filled.FullscreenExit |
11 | 14 | import androidx.compose.material.icons.filled.PictureInPicture |
| 15 | +import androidx.compose.material.icons.filled.Repeat |
| 16 | +import androidx.compose.material.icons.filled.RepeatOn |
| 17 | +import androidx.compose.material.icons.filled.RepeatOneOn |
12 | 18 | import androidx.compose.material.icons.filled.Settings |
| 19 | +import androidx.compose.material.icons.filled.Shuffle |
| 20 | +import androidx.compose.material.icons.filled.ShuffleOn |
13 | 21 | import androidx.compose.material3.Icon |
14 | 22 | import androidx.compose.material3.IconButton |
| 23 | +import androidx.compose.material3.IconToggleButton |
| 24 | +import androidx.compose.material3.LocalContentColor |
| 25 | +import androidx.compose.material3.Surface |
15 | 26 | import androidx.compose.runtime.Composable |
| 27 | +import androidx.compose.runtime.CompositionLocalProvider |
| 28 | +import androidx.compose.runtime.getValue |
| 29 | +import androidx.compose.runtime.mutableIntStateOf |
| 30 | +import androidx.compose.runtime.mutableStateOf |
| 31 | +import androidx.compose.runtime.remember |
| 32 | +import androidx.compose.runtime.setValue |
16 | 33 | import androidx.compose.ui.Modifier |
17 | 34 | import androidx.compose.ui.graphics.Color |
| 35 | +import androidx.compose.ui.graphics.vector.ImageVector |
18 | 36 | import androidx.compose.ui.res.stringResource |
19 | | -import ch.srgssr.pillarbox.demo.shared.R |
| 37 | +import androidx.compose.ui.tooling.preview.Preview |
| 38 | +import androidx.media3.common.Player |
| 39 | +import ch.srgssr.pillarbox.demo.R |
| 40 | +import ch.srgssr.pillarbox.demo.ui.theme.PillarboxTheme |
| 41 | +import ch.srgssr.pillarbox.demo.shared.R as sharedR |
20 | 42 |
|
21 | 43 | /** |
22 | 44 | * Player bottom toolbar that contains Picture in Picture and fullscreen buttons. |
23 | 45 | * |
24 | | - * @param fullScreenEnabled if fullscreen is enabled |
25 | | - * @param modifier The modifier to be applied to the layout. |
26 | | - * @param fullScreenClicked action when fullscreen button is clicked |
27 | | - * @param pictureInPictureClicked action when picture in picture is clicked |
28 | | - * @param optionClicked action when settings is clicked |
| 46 | + * @param modifier The [Modifier] to apply to the layout. |
| 47 | + * @param shuffleEnabled Whether the shuffle mode is enabled. |
| 48 | + * @param onShuffleClick The action to perform when the shuffle button is clicked. `null` to hide the button. |
| 49 | + * @param repeatMode The repeat mode. |
| 50 | + * @param onRepeatClick The action to perform when the repeat button is clicked. `null` to hide the button. |
| 51 | + * @param pictureInPictureEnabled Whether picture in picture is enabled. |
| 52 | + * @param onPictureInPictureClick The action to perform when the picture in picture button is clicked. `null` to hide the button. |
| 53 | + * @param fullScreenEnabled Whether fullscreen is enabled. |
| 54 | + * @param onFullscreenClick The action to perform when the fullscreen button is clicked. `null` to hide the button. |
| 55 | + * @param onSettingsClick The action to perform when the settings button is clicked. `null` to hide the button. |
29 | 56 | */ |
30 | 57 | @Composable |
31 | 58 | fun PlayerBottomToolbar( |
32 | | - fullScreenEnabled: Boolean, |
33 | 59 | modifier: Modifier = Modifier, |
34 | | - fullScreenClicked: () -> Unit, |
35 | | - pictureInPictureClicked: (() -> Unit)?, |
36 | | - optionClicked: () -> Unit, |
| 60 | + shuffleEnabled: Boolean, |
| 61 | + onShuffleClick: (() -> Unit)?, |
| 62 | + repeatMode: @Player.RepeatMode Int, |
| 63 | + onRepeatClick: (() -> Unit)?, |
| 64 | + pictureInPictureEnabled: Boolean, |
| 65 | + onPictureInPictureClick: (() -> Unit)?, |
| 66 | + fullScreenEnabled: Boolean, |
| 67 | + onFullscreenClick: (() -> Unit)?, |
| 68 | + onSettingsClick: () -> Unit, |
37 | 69 | ) { |
38 | 70 | Row(modifier = modifier) { |
39 | | - pictureInPictureClicked?.let { |
40 | | - IconButton(onClick = it) { |
41 | | - Icon( |
42 | | - tint = Color.White, |
43 | | - imageVector = Icons.Default.PictureInPicture, |
44 | | - contentDescription = "Picture in picture" |
45 | | - ) |
46 | | - } |
47 | | - } |
| 71 | + CompositionLocalProvider(LocalContentColor provides Color.White) { |
| 72 | + ToggleableIconButton( |
| 73 | + checked = shuffleEnabled, |
| 74 | + icon = if (shuffleEnabled) Icons.Default.ShuffleOn else Icons.Default.Shuffle, |
| 75 | + contentDestination = stringResource(R.string.shuffle), |
| 76 | + onCheckedChange = onShuffleClick, |
| 77 | + ) |
48 | 78 |
|
49 | | - IconButton(onClick = fullScreenClicked) { |
50 | | - if (fullScreenEnabled) { |
51 | | - Icon( |
52 | | - tint = Color.White, |
53 | | - imageVector = Icons.Default.FullscreenExit, |
54 | | - contentDescription = "Exit fullscreen" |
55 | | - ) |
56 | | - } else { |
| 79 | + ToggleableIconButton( |
| 80 | + checked = repeatMode != Player.REPEAT_MODE_OFF, |
| 81 | + icon = when (repeatMode) { |
| 82 | + Player.REPEAT_MODE_OFF -> Icons.Default.Repeat |
| 83 | + Player.REPEAT_MODE_ONE -> Icons.Default.RepeatOneOn |
| 84 | + Player.REPEAT_MODE_ALL -> Icons.Default.RepeatOn |
| 85 | + else -> error("Unrecognized repeat mode $repeatMode") |
| 86 | + }, |
| 87 | + contentDestination = stringResource(R.string.repeat_mode), |
| 88 | + onCheckedChange = onRepeatClick, |
| 89 | + ) |
| 90 | + |
| 91 | + Spacer(modifier = Modifier.weight(1f)) |
| 92 | + |
| 93 | + ToggleableIconButton( |
| 94 | + checked = pictureInPictureEnabled, |
| 95 | + icon = Icons.Default.PictureInPicture, |
| 96 | + contentDestination = stringResource(R.string.picture_in_picture), |
| 97 | + onCheckedChange = onPictureInPictureClick, |
| 98 | + ) |
| 99 | + |
| 100 | + ToggleableIconButton( |
| 101 | + checked = fullScreenEnabled, |
| 102 | + icon = if (fullScreenEnabled) Icons.Default.FullscreenExit else Icons.Default.Fullscreen, |
| 103 | + contentDestination = stringResource(R.string.fullscreen), |
| 104 | + onCheckedChange = onFullscreenClick, |
| 105 | + ) |
| 106 | + |
| 107 | + IconButton(onClick = onSettingsClick) { |
57 | 108 | Icon( |
58 | | - tint = Color.White, |
59 | | - imageVector = Icons.Default.Fullscreen, |
60 | | - contentDescription = "Enter fullscreen" |
| 109 | + imageVector = Icons.Default.Settings, |
| 110 | + contentDescription = stringResource(sharedR.string.settings), |
61 | 111 | ) |
62 | 112 | } |
63 | 113 | } |
| 114 | + } |
| 115 | +} |
64 | 116 |
|
65 | | - IconButton(onClick = optionClicked) { |
| 117 | +@Composable |
| 118 | +private fun ToggleableIconButton( |
| 119 | + checked: Boolean, |
| 120 | + icon: ImageVector, |
| 121 | + contentDestination: String, |
| 122 | + onCheckedChange: (() -> Unit)?, |
| 123 | +) { |
| 124 | + AnimatedVisibility(visible = onCheckedChange != null) { |
| 125 | + IconToggleButton( |
| 126 | + checked = checked, |
| 127 | + onCheckedChange = { onCheckedChange?.invoke() }, |
| 128 | + ) { |
66 | 129 | Icon( |
67 | | - tint = Color.White, |
68 | | - imageVector = Icons.Default.Settings, |
69 | | - contentDescription = stringResource(R.string.settings) |
| 130 | + imageVector = icon, |
| 131 | + contentDescription = contentDestination, |
| 132 | + ) |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +@Preview |
| 138 | +@Composable |
| 139 | +private fun PlayerBottomToolbarPreview() { |
| 140 | + var shuffleEnabled by remember { mutableStateOf(false) } |
| 141 | + var repeatMode by remember { mutableIntStateOf(Player.REPEAT_MODE_OFF) } |
| 142 | + var pictureInPictureEnabled by remember { mutableStateOf(false) } |
| 143 | + var fullscreenEnabled by remember { mutableStateOf(false) } |
| 144 | + |
| 145 | + PillarboxTheme { |
| 146 | + Surface { |
| 147 | + PlayerBottomToolbar( |
| 148 | + modifier = Modifier.background(Color.Black), |
| 149 | + shuffleEnabled = shuffleEnabled, |
| 150 | + onShuffleClick = { shuffleEnabled = !shuffleEnabled }, |
| 151 | + repeatMode = repeatMode, |
| 152 | + onRepeatClick = { |
| 153 | + repeatMode = when (repeatMode) { |
| 154 | + Player.REPEAT_MODE_OFF -> Player.REPEAT_MODE_ONE |
| 155 | + Player.REPEAT_MODE_ONE -> Player.REPEAT_MODE_ALL |
| 156 | + Player.REPEAT_MODE_ALL -> Player.REPEAT_MODE_OFF |
| 157 | + else -> error("Unrecognized repeat mode $repeatMode") |
| 158 | + } |
| 159 | + }, |
| 160 | + pictureInPictureEnabled = pictureInPictureEnabled, |
| 161 | + onPictureInPictureClick = { pictureInPictureEnabled = !pictureInPictureEnabled }, |
| 162 | + fullScreenEnabled = fullscreenEnabled, |
| 163 | + onFullscreenClick = { fullscreenEnabled = !fullscreenEnabled }, |
| 164 | + onSettingsClick = {}, |
70 | 165 | ) |
71 | 166 | } |
72 | 167 | } |
|
0 commit comments