Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 1 | /* |
Vineet Kumar | 507211d | 2023-01-04 19:16:38 +0530 | [diff] [blame] | 2 | * Copyright 2023 The Android Open Source Project |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Vineet Kumar | 507211d | 2023-01-04 19:16:38 +0530 | [diff] [blame] | 17 | package androidx.tv.material3 |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 18 | |
Aditya Arora | 84c138c | 2023-01-05 11:16:55 +0530 | [diff] [blame] | 19 | import android.view.KeyEvent.KEYCODE_BACK |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 20 | import android.view.KeyEvent.KEYCODE_DPAD_LEFT |
| 21 | import android.view.KeyEvent.KEYCODE_DPAD_RIGHT |
Vineet Kumar | 5e59458 | 2022-08-19 15:52:28 +0530 | [diff] [blame] | 22 | import androidx.compose.animation.AnimatedContent |
Aditya Arora | 7938e90c | 2022-11-17 13:32:20 +0530 | [diff] [blame] | 23 | import androidx.compose.animation.AnimatedVisibilityScope |
Vighnesh Raut | 431494a | 2023-01-19 20:03:22 +0530 | [diff] [blame] | 24 | import androidx.compose.animation.ContentTransform |
Vineet Kumar | 5e59458 | 2022-08-19 15:52:28 +0530 | [diff] [blame] | 25 | import androidx.compose.animation.ExperimentalAnimationApi |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 26 | import androidx.compose.animation.core.tween |
| 27 | import androidx.compose.animation.fadeIn |
| 28 | import androidx.compose.animation.fadeOut |
Doris Liu | 4b3a831 | 2023-03-28 18:11:25 -0700 | [diff] [blame^] | 29 | import androidx.compose.animation.togetherWith |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 30 | import androidx.compose.foundation.background |
| 31 | import androidx.compose.foundation.focusable |
| 32 | import androidx.compose.foundation.layout.Arrangement |
| 33 | import androidx.compose.foundation.layout.Box |
| 34 | import androidx.compose.foundation.layout.BoxScope |
| 35 | import androidx.compose.foundation.layout.Row |
| 36 | import androidx.compose.foundation.layout.padding |
| 37 | import androidx.compose.foundation.layout.size |
| 38 | import androidx.compose.foundation.shape.CircleShape |
| 39 | import androidx.compose.runtime.Composable |
| 40 | import androidx.compose.runtime.LaunchedEffect |
| 41 | import androidx.compose.runtime.Stable |
| 42 | import androidx.compose.runtime.getValue |
| 43 | import androidx.compose.runtime.mutableStateOf |
| 44 | import androidx.compose.runtime.remember |
vinekumar | b261247 | 2023-01-27 22:31:37 +0530 | [diff] [blame] | 45 | import androidx.compose.runtime.rememberUpdatedState |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 46 | import androidx.compose.runtime.setValue |
| 47 | import androidx.compose.runtime.snapshotFlow |
| 48 | import androidx.compose.ui.Alignment |
| 49 | import androidx.compose.ui.ExperimentalComposeUiApi |
| 50 | import androidx.compose.ui.Modifier |
| 51 | import androidx.compose.ui.focus.FocusDirection |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 52 | import androidx.compose.ui.focus.FocusManager |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 53 | import androidx.compose.ui.focus.FocusRequester |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 54 | import androidx.compose.ui.focus.FocusState |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 55 | import androidx.compose.ui.focus.focusRequester |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 56 | import androidx.compose.ui.focus.onFocusChanged |
| 57 | import androidx.compose.ui.graphics.Color |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 58 | import androidx.compose.ui.input.key.KeyEventType.Companion.KeyUp |
Aditya Arora | 84c138c | 2023-01-05 11:16:55 +0530 | [diff] [blame] | 59 | import androidx.compose.ui.input.key.key |
| 60 | import androidx.compose.ui.input.key.nativeKeyCode |
| 61 | import androidx.compose.ui.input.key.onKeyEvent |
| 62 | import androidx.compose.ui.input.key.type |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 63 | import androidx.compose.ui.platform.LocalFocusManager |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 64 | import androidx.compose.ui.platform.LocalLayoutDirection |
Vighnesh Raut | 268af2a | 2022-12-13 16:52:41 +0530 | [diff] [blame] | 65 | import androidx.compose.ui.unit.Dp |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 66 | import androidx.compose.ui.unit.LayoutDirection |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 67 | import androidx.compose.ui.unit.dp |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 68 | import java.lang.Math.floorMod |
| 69 | import kotlinx.coroutines.delay |
| 70 | import kotlinx.coroutines.flow.first |
| 71 | import kotlinx.coroutines.yield |
| 72 | |
| 73 | /** |
| 74 | * Composes a hero card rotator to highlight a piece of content. |
| 75 | * |
Vighnesh Raut | 268af2a | 2022-12-13 16:52:41 +0530 | [diff] [blame] | 76 | * Examples: |
| 77 | * @sample androidx.tv.samples.SimpleCarousel |
| 78 | * @sample androidx.tv.samples.CarouselIndicatorWithRectangleShape |
| 79 | * |
Vineet Kumar | 507211d | 2023-01-04 19:16:38 +0530 | [diff] [blame] | 80 | * @param modifier Modifier applied to the Carousel. |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 81 | * @param itemCount total number of items present in the carousel. |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 82 | * @param carouselState state associated with this carousel. |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 83 | * @param autoScrollDurationMillis duration for which item should be visible before moving to |
| 84 | * the next item. |
| 85 | * @param contentTransformStartToEnd animation transform applied when we are moving from start to |
| 86 | * end in the carousel while scrolling to the next item |
| 87 | * @param contentTransformEndToStart animation transform applied when we are moving from end to |
| 88 | * start in the carousel while scrolling to the next item |
| 89 | * @param carouselIndicator indicator showing the position of the current item among all items. |
| 90 | * @param content defines the items for a given index. |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 91 | */ |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 92 | @Suppress("IllegalExperimentalApiUsage") |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 93 | @OptIn(ExperimentalComposeUiApi::class) |
Vineet Kumar | 507211d | 2023-01-04 19:16:38 +0530 | [diff] [blame] | 94 | @ExperimentalTvMaterial3Api |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 95 | @Composable |
| 96 | fun Carousel( |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 97 | itemCount: Int, |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 98 | modifier: Modifier = Modifier, |
| 99 | carouselState: CarouselState = remember { CarouselState() }, |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 100 | autoScrollDurationMillis: Long = CarouselDefaults.TimeToDisplayItemMillis, |
| 101 | contentTransformStartToEnd: ContentTransform = CarouselDefaults.contentTransform, |
| 102 | contentTransformEndToStart: ContentTransform = CarouselDefaults.contentTransform, |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 103 | carouselIndicator: |
| 104 | @Composable BoxScope.() -> Unit = { |
Vighnesh Raut | 268af2a | 2022-12-13 16:52:41 +0530 | [diff] [blame] | 105 | CarouselDefaults.IndicatorRow( |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 106 | itemCount = itemCount, |
| 107 | activeItemIndex = carouselState.activeItemIndex, |
Vineet Kumar | 5e59458 | 2022-08-19 15:52:28 +0530 | [diff] [blame] | 108 | modifier = Modifier |
| 109 | .align(Alignment.BottomEnd) |
| 110 | .padding(16.dp), |
Vighnesh Raut | 268af2a | 2022-12-13 16:52:41 +0530 | [diff] [blame] | 111 | ) |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 112 | }, |
Vighnesh Raut | 431494a | 2023-01-19 20:03:22 +0530 | [diff] [blame] | 113 | content: @Composable CarouselScope.(index: Int) -> Unit |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 114 | ) { |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 115 | CarouselStateUpdater(carouselState, itemCount) |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 116 | var focusState: FocusState? by remember { mutableStateOf(null) } |
| 117 | val focusManager = LocalFocusManager.current |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 118 | val isLtr = LocalLayoutDirection.current == LayoutDirection.Ltr |
Aditya Arora | 7938e90c | 2022-11-17 13:32:20 +0530 | [diff] [blame] | 119 | val carouselOuterBoxFocusRequester = remember { FocusRequester() } |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 120 | var isAutoScrollActive by remember { mutableStateOf(false) } |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 121 | |
| 122 | AutoScrollSideEffect( |
Vighnesh Raut | 431494a | 2023-01-19 20:03:22 +0530 | [diff] [blame] | 123 | autoScrollDurationMillis = autoScrollDurationMillis, |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 124 | itemCount = itemCount, |
Vineet Kumar | 01abb02 | 2023-01-14 14:59:09 +0530 | [diff] [blame] | 125 | carouselState = carouselState, |
| 126 | doAutoScroll = shouldPerformAutoScroll(focusState), |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 127 | onAutoScrollChange = { isAutoScrollActive = it }) |
Vineet Kumar | 11d7e40 | 2022-12-04 13:18:52 +0530 | [diff] [blame] | 128 | |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 129 | Box(modifier = modifier |
Vineet Kumar | 11d7e40 | 2022-12-04 13:18:52 +0530 | [diff] [blame] | 130 | .bringIntoViewIfChildrenAreFocused() |
Aditya Arora | 7938e90c | 2022-11-17 13:32:20 +0530 | [diff] [blame] | 131 | .focusRequester(carouselOuterBoxFocusRequester) |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 132 | .onFocusChanged { |
| 133 | focusState = it |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 134 | |
| 135 | // When the carousel gains focus for the first time |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 136 | if (it.isFocused && isAutoScrollActive) { |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 137 | focusManager.moveFocus(FocusDirection.Enter) |
| 138 | } |
| 139 | } |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 140 | .handleKeyEvents( |
| 141 | carouselState = carouselState, |
| 142 | outerBoxFocusRequester = carouselOuterBoxFocusRequester, |
| 143 | focusManager = focusManager, |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 144 | itemCount = itemCount, |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 145 | isLtr = isLtr, |
| 146 | ) |
| 147 | .focusable() |
| 148 | ) { |
Vineet Kumar | 5e59458 | 2022-08-19 15:52:28 +0530 | [diff] [blame] | 149 | AnimatedContent( |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 150 | targetState = carouselState.activeItemIndex, |
Vighnesh Raut | 431494a | 2023-01-19 20:03:22 +0530 | [diff] [blame] | 151 | transitionSpec = { |
| 152 | if (carouselState.isMovingBackward) { |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 153 | contentTransformEndToStart |
Vighnesh Raut | 431494a | 2023-01-19 20:03:22 +0530 | [diff] [blame] | 154 | } else { |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 155 | contentTransformStartToEnd |
Vighnesh Raut | 431494a | 2023-01-19 20:03:22 +0530 | [diff] [blame] | 156 | } |
| 157 | } |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 158 | ) { activeItemIndex -> |
Aditya Arora | 7938e90c | 2022-11-17 13:32:20 +0530 | [diff] [blame] | 159 | LaunchedEffect(Unit) { |
| 160 | this@AnimatedContent.onAnimationCompletion { |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 161 | // Outer box is focused |
| 162 | if (!isAutoScrollActive && focusState?.isFocused == true) { |
Aditya Arora | 7938e90c | 2022-11-17 13:32:20 +0530 | [diff] [blame] | 163 | carouselOuterBoxFocusRequester.requestFocus() |
| 164 | focusManager.moveFocus(FocusDirection.Enter) |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 165 | } |
Aditya Arora | 7938e90c | 2022-11-17 13:32:20 +0530 | [diff] [blame] | 166 | } |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 167 | } |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 168 | // it is possible for the itemCount to have changed during the transition. |
| 169 | // This can cause the itemIndex to be greater than or equal to itemCount and cause |
| 170 | // IndexOutOfBoundsException. Guarding against this by checking against itemCount |
vinekumar | b261247 | 2023-01-27 22:31:37 +0530 | [diff] [blame] | 171 | // before invoking. |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 172 | if (itemCount > 0) { |
Vighnesh Raut | 431494a | 2023-01-19 20:03:22 +0530 | [diff] [blame] | 173 | CarouselScope(carouselState = carouselState) |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 174 | .content(if (activeItemIndex < itemCount) activeItemIndex else 0) |
vinekumar | b261247 | 2023-01-27 22:31:37 +0530 | [diff] [blame] | 175 | } |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 176 | } |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 177 | this.carouselIndicator() |
| 178 | } |
| 179 | } |
| 180 | |
Vineet Kumar | 01abb02 | 2023-01-14 14:59:09 +0530 | [diff] [blame] | 181 | @Composable |
| 182 | private fun shouldPerformAutoScroll(focusState: FocusState?): Boolean { |
| 183 | val carouselIsFocused = focusState?.isFocused ?: false |
| 184 | val carouselHasFocus = focusState?.hasFocus ?: false |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 185 | return !(carouselIsFocused || carouselHasFocus) |
Vineet Kumar | 01abb02 | 2023-01-14 14:59:09 +0530 | [diff] [blame] | 186 | } |
| 187 | |
Aditya Arora | 7938e90c | 2022-11-17 13:32:20 +0530 | [diff] [blame] | 188 | @Suppress("IllegalExperimentalApiUsage") |
| 189 | @OptIn(ExperimentalAnimationApi::class) |
| 190 | private suspend fun AnimatedVisibilityScope.onAnimationCompletion(action: suspend () -> Unit) { |
| 191 | snapshotFlow { transition.currentState == transition.targetState }.first { it } |
| 192 | action.invoke() |
| 193 | } |
| 194 | |
Vineet Kumar | 507211d | 2023-01-04 19:16:38 +0530 | [diff] [blame] | 195 | @OptIn(ExperimentalTvMaterial3Api::class) |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 196 | @Composable |
| 197 | private fun AutoScrollSideEffect( |
Vighnesh Raut | 431494a | 2023-01-19 20:03:22 +0530 | [diff] [blame] | 198 | autoScrollDurationMillis: Long, |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 199 | itemCount: Int, |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 200 | carouselState: CarouselState, |
Vineet Kumar | 01abb02 | 2023-01-14 14:59:09 +0530 | [diff] [blame] | 201 | doAutoScroll: Boolean, |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 202 | onAutoScrollChange: (isAutoScrollActive: Boolean) -> Unit = {}, |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 203 | ) { |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 204 | // Needed to ensure that the code within LaunchedEffect receives updates to the itemCount. |
| 205 | val updatedItemCount by rememberUpdatedState(newValue = itemCount) |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 206 | if (doAutoScroll) { |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 207 | LaunchedEffect(carouselState) { |
| 208 | while (true) { |
| 209 | yield() |
Vighnesh Raut | 431494a | 2023-01-19 20:03:22 +0530 | [diff] [blame] | 210 | delay(autoScrollDurationMillis) |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 211 | if (carouselState.activePauseHandlesCount > 0) { |
| 212 | snapshotFlow { carouselState.activePauseHandlesCount } |
| 213 | .first { pauseHandleCount -> pauseHandleCount == 0 } |
| 214 | } |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 215 | carouselState.moveToNextItem(updatedItemCount) |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | } |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 219 | onAutoScrollChange(doAutoScroll) |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Aditya Arora | 7938e90c | 2022-11-17 13:32:20 +0530 | [diff] [blame] | 222 | @Suppress("IllegalExperimentalApiUsage") |
Vineet Kumar | 507211d | 2023-01-04 19:16:38 +0530 | [diff] [blame] | 223 | @OptIn(ExperimentalTvMaterial3Api::class, ExperimentalComposeUiApi::class) |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 224 | private fun Modifier.handleKeyEvents( |
Aditya Arora | 7938e90c | 2022-11-17 13:32:20 +0530 | [diff] [blame] | 225 | carouselState: CarouselState, |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 226 | outerBoxFocusRequester: FocusRequester, |
| 227 | focusManager: FocusManager, |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 228 | itemCount: Int, |
Aditya Arora | 7938e90c | 2022-11-17 13:32:20 +0530 | [diff] [blame] | 229 | isLtr: Boolean |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 230 | ): Modifier = onKeyEvent { |
| 231 | // Ignore KeyUp action type |
| 232 | if (it.type == KeyUp) { |
| 233 | return@onKeyEvent KeyEventPropagation.ContinuePropagation |
| 234 | } |
Aditya Arora | 7938e90c | 2022-11-17 13:32:20 +0530 | [diff] [blame] | 235 | |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 236 | val showPreviousItemAndGetKeyEventPropagation = { |
| 237 | if (carouselState.isFirstItem()) { |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 238 | KeyEventPropagation.ContinuePropagation |
| 239 | } else { |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 240 | carouselState.moveToPreviousItem(itemCount) |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 241 | outerBoxFocusRequester.requestFocus() |
| 242 | KeyEventPropagation.StopPropagation |
Aditya Arora | 7938e90c | 2022-11-17 13:32:20 +0530 | [diff] [blame] | 243 | } |
| 244 | } |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 245 | val showNextItemAndGetKeyEventPropagation = { |
| 246 | if (carouselState.isLastItem(itemCount)) { |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 247 | KeyEventPropagation.ContinuePropagation |
| 248 | } else { |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 249 | carouselState.moveToNextItem(itemCount) |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 250 | outerBoxFocusRequester.requestFocus() |
| 251 | KeyEventPropagation.StopPropagation |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | when (it.key.nativeKeyCode) { |
| 256 | KEYCODE_BACK -> { |
| 257 | focusManager.moveFocus(FocusDirection.Exit) |
| 258 | KeyEventPropagation.ContinuePropagation |
| 259 | } |
| 260 | |
Aditya Arora | b2cf37e | 2023-01-17 15:49:42 +0530 | [diff] [blame] | 261 | KEYCODE_DPAD_LEFT -> { |
| 262 | // Ignore long press key event for manual scrolling |
| 263 | if (it.nativeKeyEvent.repeatCount > 0) { |
| 264 | return@onKeyEvent KeyEventPropagation.StopPropagation |
| 265 | } |
| 266 | |
| 267 | if (isLtr) { |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 268 | showPreviousItemAndGetKeyEventPropagation() |
Aditya Arora | b2cf37e | 2023-01-17 15:49:42 +0530 | [diff] [blame] | 269 | } else { |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 270 | showNextItemAndGetKeyEventPropagation() |
Aditya Arora | b2cf37e | 2023-01-17 15:49:42 +0530 | [diff] [blame] | 271 | } |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 272 | } |
| 273 | |
Aditya Arora | b2cf37e | 2023-01-17 15:49:42 +0530 | [diff] [blame] | 274 | KEYCODE_DPAD_RIGHT -> { |
| 275 | // Ignore long press key event for manual scrolling |
| 276 | if (it.nativeKeyEvent.repeatCount > 0) { |
| 277 | return@onKeyEvent KeyEventPropagation.StopPropagation |
| 278 | } |
| 279 | |
| 280 | if (isLtr) { |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 281 | showNextItemAndGetKeyEventPropagation() |
Aditya Arora | b2cf37e | 2023-01-17 15:49:42 +0530 | [diff] [blame] | 282 | } else { |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 283 | showPreviousItemAndGetKeyEventPropagation() |
Aditya Arora | b2cf37e | 2023-01-17 15:49:42 +0530 | [diff] [blame] | 284 | } |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | else -> KeyEventPropagation.ContinuePropagation |
| 288 | } |
| 289 | } |
Aditya Arora | 7938e90c | 2022-11-17 13:32:20 +0530 | [diff] [blame] | 290 | |
Vineet Kumar | 507211d | 2023-01-04 19:16:38 +0530 | [diff] [blame] | 291 | @OptIn(ExperimentalTvMaterial3Api::class) |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 292 | @Composable |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 293 | private fun CarouselStateUpdater(carouselState: CarouselState, itemCount: Int) { |
| 294 | LaunchedEffect(carouselState, itemCount) { |
| 295 | if (itemCount != 0) { |
| 296 | carouselState.activeItemIndex = floorMod(carouselState.activeItemIndex, itemCount) |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /** |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 302 | * State of the Carousel which allows the user to specify the first item that is shown when the |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 303 | * Carousel is instantiated in the constructor. |
| 304 | * |
| 305 | * It also provides the user with support to pause and resume the auto-scroll behaviour of the |
| 306 | * Carousel. |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 307 | * @param initialActiveItemIndex the index of the first active item |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 308 | */ |
| 309 | @Stable |
Vineet Kumar | 507211d | 2023-01-04 19:16:38 +0530 | [diff] [blame] | 310 | @ExperimentalTvMaterial3Api |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 311 | class CarouselState(initialActiveItemIndex: Int = 0) { |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 312 | internal var activePauseHandlesCount by mutableStateOf(0) |
| 313 | |
| 314 | /** |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 315 | * The index of the item that is currently displayed by the carousel |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 316 | */ |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 317 | var activeItemIndex by mutableStateOf(initialActiveItemIndex) |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 318 | internal set |
| 319 | |
| 320 | /** |
Vighnesh Raut | 431494a | 2023-01-19 20:03:22 +0530 | [diff] [blame] | 321 | * Tracks whether we are scrolling backward in the Carousel. By default, we are moving forward |
| 322 | * because of auto-scroll |
| 323 | */ |
| 324 | internal var isMovingBackward = false |
| 325 | private set |
| 326 | |
| 327 | /** |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 328 | * Pauses the auto-scrolling behaviour of Carousel. |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 329 | * The pause request is ignored if [itemIndex] is not the current item that is visible. |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 330 | * Returns a [ScrollPauseHandle] that can be used to resume |
| 331 | */ |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 332 | fun pauseAutoScroll(itemIndex: Int): ScrollPauseHandle { |
| 333 | if (this.activeItemIndex != itemIndex) { |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 334 | return NoOpScrollPauseHandle |
| 335 | } |
| 336 | return ScrollPauseHandleImpl(this) |
| 337 | } |
| 338 | |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 339 | internal fun isFirstItem() = activeItemIndex == 0 |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 340 | |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 341 | internal fun isLastItem(itemCount: Int) = activeItemIndex == itemCount - 1 |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 342 | |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 343 | internal fun moveToPreviousItem(itemCount: Int) { |
| 344 | // No items available for carousel |
| 345 | if (itemCount == 0) return |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 346 | |
Vighnesh Raut | 431494a | 2023-01-19 20:03:22 +0530 | [diff] [blame] | 347 | isMovingBackward = true |
| 348 | |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 349 | // Go to previous item |
| 350 | activeItemIndex = floorMod(activeItemIndex - 1, itemCount) |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 351 | } |
| 352 | |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 353 | internal fun moveToNextItem(itemCount: Int) { |
| 354 | // No items available for carousel |
| 355 | if (itemCount == 0) return |
Vighnesh Raut | 2a62346 | 2022-09-14 10:35:36 +0530 | [diff] [blame] | 356 | |
Vighnesh Raut | 431494a | 2023-01-19 20:03:22 +0530 | [diff] [blame] | 357 | isMovingBackward = false |
| 358 | |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 359 | // Go to next item |
| 360 | activeItemIndex = floorMod(activeItemIndex + 1, itemCount) |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Vineet Kumar | 507211d | 2023-01-04 19:16:38 +0530 | [diff] [blame] | 364 | @ExperimentalTvMaterial3Api |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 365 | /** |
| 366 | * Handle returned by [CarouselState.pauseAutoScroll] that can be used to resume auto-scroll. |
| 367 | */ |
| 368 | sealed interface ScrollPauseHandle { |
| 369 | /** |
| 370 | * Resumes the auto-scroll behaviour if there are no other active [ScrollPauseHandle]s. |
| 371 | */ |
| 372 | fun resumeAutoScroll() |
| 373 | } |
| 374 | |
Vineet Kumar | 507211d | 2023-01-04 19:16:38 +0530 | [diff] [blame] | 375 | @OptIn(ExperimentalTvMaterial3Api::class) |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 376 | internal object NoOpScrollPauseHandle : ScrollPauseHandle { |
| 377 | /** |
| 378 | * Resumes the auto-scroll behaviour if there are no other active [ScrollPauseHandle]s. |
| 379 | */ |
| 380 | override fun resumeAutoScroll() {} |
| 381 | } |
| 382 | |
Vineet Kumar | 507211d | 2023-01-04 19:16:38 +0530 | [diff] [blame] | 383 | @OptIn(ExperimentalTvMaterial3Api::class) |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 384 | internal class ScrollPauseHandleImpl(private val carouselState: CarouselState) : ScrollPauseHandle { |
| 385 | private var active by mutableStateOf(true) |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 386 | |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 387 | init { |
| 388 | carouselState.activePauseHandlesCount += 1 |
| 389 | } |
Vighnesh Raut | 799489f | 2023-01-11 19:38:16 +0530 | [diff] [blame] | 390 | |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 391 | /** |
| 392 | * Resumes the auto-scroll behaviour if there are no other active [ScrollPauseHandle]s. |
| 393 | */ |
| 394 | override fun resumeAutoScroll() { |
| 395 | if (active) { |
| 396 | active = false |
| 397 | carouselState.activePauseHandlesCount -= 1 |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
Vineet Kumar | 507211d | 2023-01-04 19:16:38 +0530 | [diff] [blame] | 402 | @ExperimentalTvMaterial3Api |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 403 | object CarouselDefaults { |
| 404 | /** |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 405 | * Default time for which the item is visible to the user. |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 406 | */ |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 407 | const val TimeToDisplayItemMillis: Long = 5000 |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 408 | |
| 409 | /** |
Vighnesh Raut | 431494a | 2023-01-19 20:03:22 +0530 | [diff] [blame] | 410 | * Transition applied when bringing it into view and removing it from the view |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 411 | */ |
Vighnesh Raut | 431494a | 2023-01-19 20:03:22 +0530 | [diff] [blame] | 412 | val contentTransform: ContentTransform |
| 413 | @Composable get() = |
| 414 | fadeIn(animationSpec = tween(100)) |
Doris Liu | 4b3a831 | 2023-03-28 18:11:25 -0700 | [diff] [blame^] | 415 | .togetherWith(fadeOut(animationSpec = tween(100))) |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 416 | |
| 417 | /** |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 418 | * An indicator showing the position of the current active item among the items of the |
Vighnesh Raut | 268af2a | 2022-12-13 16:52:41 +0530 | [diff] [blame] | 419 | * carousel. |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 420 | * |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 421 | * @param itemCount total number of items in the carousel |
| 422 | * @param activeItemIndex the current active item index |
Vighnesh Raut | 268af2a | 2022-12-13 16:52:41 +0530 | [diff] [blame] | 423 | * @param modifier Modifier applied to the indicators' container |
| 424 | * @param spacing spacing between the indicator dots |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 425 | * @param indicator indicator dot representing each item in the carousel |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 426 | */ |
Vineet Kumar | 507211d | 2023-01-04 19:16:38 +0530 | [diff] [blame] | 427 | @ExperimentalTvMaterial3Api |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 428 | @Composable |
Vighnesh Raut | 268af2a | 2022-12-13 16:52:41 +0530 | [diff] [blame] | 429 | fun IndicatorRow( |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 430 | itemCount: Int, |
| 431 | activeItemIndex: Int, |
Vighnesh Raut | 268af2a | 2022-12-13 16:52:41 +0530 | [diff] [blame] | 432 | modifier: Modifier = Modifier, |
| 433 | spacing: Dp = 8.dp, |
| 434 | indicator: @Composable (isActive: Boolean) -> Unit = { isActive -> |
| 435 | val activeColor = Color.White |
Vighnesh Raut | 431494a | 2023-01-19 20:03:22 +0530 | [diff] [blame] | 436 | val inactiveColor = activeColor.copy(alpha = 0.3f) |
Vighnesh Raut | 268af2a | 2022-12-13 16:52:41 +0530 | [diff] [blame] | 437 | Box( |
| 438 | modifier = Modifier |
| 439 | .size(8.dp) |
| 440 | .background( |
| 441 | color = if (isActive) activeColor else inactiveColor, |
| 442 | shape = CircleShape, |
| 443 | ), |
| 444 | ) |
| 445 | } |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 446 | ) { |
Vighnesh Raut | 268af2a | 2022-12-13 16:52:41 +0530 | [diff] [blame] | 447 | Row( |
| 448 | horizontalArrangement = Arrangement.spacedBy(spacing), |
| 449 | verticalAlignment = Alignment.CenterVertically, |
| 450 | modifier = modifier, |
| 451 | ) { |
Vighnesh Raut | 2f9f7a0 | 2023-03-17 18:34:20 +0530 | [diff] [blame] | 452 | repeat(itemCount) { |
| 453 | val isActive = it == activeItemIndex |
Vighnesh Raut | 268af2a | 2022-12-13 16:52:41 +0530 | [diff] [blame] | 454 | indicator(isActive = isActive) |
Vineet Kumar | efa6350 | 2022-08-30 10:00:54 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | } |
| 458 | } |