|
| 1 | +package video.typetype.tv.ui.home |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.Arrangement |
| 4 | +import androidx.compose.foundation.layout.Row |
| 5 | +import androidx.compose.foundation.layout.padding |
| 6 | +import androidx.compose.runtime.Composable |
| 7 | +import androidx.compose.runtime.LaunchedEffect |
| 8 | +import androidx.compose.runtime.getValue |
| 9 | +import androidx.compose.runtime.mutableIntStateOf |
| 10 | +import androidx.compose.runtime.remember |
| 11 | +import androidx.compose.runtime.setValue |
| 12 | +import androidx.compose.ui.Alignment |
| 13 | +import androidx.compose.ui.Modifier |
| 14 | +import androidx.compose.ui.res.stringResource |
| 15 | +import androidx.compose.ui.semantics.LiveRegionMode |
| 16 | +import androidx.compose.ui.semantics.liveRegion |
| 17 | +import androidx.compose.ui.semantics.semantics |
| 18 | +import androidx.compose.ui.unit.dp |
| 19 | +import kotlinx.coroutines.delay |
| 20 | +import video.typetype.tv.data.ContentSource |
| 21 | +import video.typetype.tv.resources.R |
| 22 | +import video.typetype.tv.theme.Dimens |
| 23 | +import video.typetype.tv.theme.Type |
| 24 | +import video.typetype.tv.ui.components.FadeVisibility |
| 25 | +import video.typetype.tv.ui.components.ServiceToggle |
| 26 | +import video.typetype.tv.ui.components.TextLabel |
| 27 | + |
| 28 | +private const val REFUSED_HINT_MILLIS = 2_500L |
| 29 | + |
| 30 | +// The services the home shows, above the hero: OK on one shows or leaves it out. Leaving out the last one is |
| 31 | +// refused with a short hint, since a home without a service would be empty. |
| 32 | +@Composable |
| 33 | +fun HomeServicesRow( |
| 34 | + chosen: Set<ContentSource>, |
| 35 | + onToggle: (ContentSource) -> Boolean, |
| 36 | + modifier: Modifier = Modifier, |
| 37 | +) { |
| 38 | + var refused by remember { mutableIntStateOf(0) } |
| 39 | + LaunchedEffect(refused) { |
| 40 | + if (refused == 0) return@LaunchedEffect |
| 41 | + delay(REFUSED_HINT_MILLIS) |
| 42 | + refused = 0 |
| 43 | + } |
| 44 | + Row( |
| 45 | + modifier = modifier.padding(start = Dimens.SafeStart, top = Dimens.TopNavHeight + Dimens.SafeTop + 16.dp), |
| 46 | + horizontalArrangement = Arrangement.spacedBy(10.dp), |
| 47 | + verticalAlignment = Alignment.CenterVertically, |
| 48 | + ) { |
| 49 | + ContentSource.extractors.forEach { source -> |
| 50 | + ServiceToggle(source, on = source in chosen, onToggle = { if (!onToggle(source)) refused++ }) |
| 51 | + } |
| 52 | + FadeVisibility(refused > 0, Modifier.padding(start = 6.dp)) { |
| 53 | + TextLabel( |
| 54 | + stringResource(R.string.home_services_keep_one), |
| 55 | + Type.Meta, |
| 56 | + modifier = Modifier.semantics { liveRegion = LiveRegionMode.Polite }, |
| 57 | + ) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments