|
| 1 | +package video.typetype.tv.ui.subscriptions |
| 2 | + |
| 3 | +import androidx.compose.animation.Crossfade |
| 4 | +import androidx.compose.animation.core.tween |
| 5 | +import androidx.compose.foundation.layout.Arrangement |
| 6 | +import androidx.compose.foundation.layout.Box |
| 7 | +import androidx.compose.foundation.layout.Row |
| 8 | +import androidx.compose.foundation.layout.fillMaxHeight |
| 9 | +import androidx.compose.foundation.layout.fillMaxSize |
| 10 | +import androidx.compose.foundation.layout.padding |
| 11 | +import androidx.compose.runtime.Composable |
| 12 | +import androidx.compose.runtime.LaunchedEffect |
| 13 | +import androidx.compose.runtime.getValue |
| 14 | +import androidx.compose.runtime.mutableIntStateOf |
| 15 | +import androidx.compose.runtime.mutableStateOf |
| 16 | +import androidx.compose.runtime.remember |
| 17 | +import androidx.compose.runtime.setValue |
| 18 | +import androidx.compose.ui.Modifier |
| 19 | +import androidx.compose.ui.focus.FocusRequester |
| 20 | +import androidx.compose.ui.res.stringResource |
| 21 | +import androidx.compose.ui.unit.dp |
| 22 | +import video.typetype.tv.data.ChannelSummary |
| 23 | +import video.typetype.tv.data.Video |
| 24 | +import video.typetype.tv.resources.R |
| 25 | +import video.typetype.tv.theme.Dimens |
| 26 | +import video.typetype.tv.ui.components.InkBackground |
| 27 | +import video.typetype.tv.ui.components.RailEntry |
| 28 | +import video.typetype.tv.ui.components.SectionRail |
| 29 | +import video.typetype.tv.ui.util.requestFocusWhenReady |
| 30 | +import video.typetype.tv.ui.util.trapFocusDown |
| 31 | + |
| 32 | +@Composable |
| 33 | +fun SubscriptionsScreen( |
| 34 | + session: SubscriptionsSession, |
| 35 | + onOpen: (Video) -> Unit, |
| 36 | + onOpenChannel: (ChannelSummary) -> Unit, |
| 37 | + modifier: Modifier = Modifier, |
| 38 | +) { |
| 39 | + val selectedRequester = remember { FocusRequester() } |
| 40 | + val restoreId = remember { session.restoreId } |
| 41 | + var pendingRestore by remember { mutableStateOf(restoreId != null) } |
| 42 | + var attempt by remember { mutableIntStateOf(0) } |
| 43 | + val feed = session.feed |
| 44 | + val channels = session.channels |
| 45 | + LaunchedEffect(Unit) { |
| 46 | + if (restoreId == null) selectedRequester.requestFocusWhenReady() |
| 47 | + } |
| 48 | + LaunchedEffect(session.selected, feed, channels, attempt) { |
| 49 | + when (session.selected) { |
| 50 | + SubscriptionsSection.Videos -> feed.load() |
| 51 | + SubscriptionsSection.Channels -> channels.load() |
| 52 | + } |
| 53 | + } |
| 54 | + val restore = restoreId?.takeIf { pendingRestore } |
| 55 | + val onRestored = { pendingRestore = false } |
| 56 | + Box(modifier.fillMaxSize()) { |
| 57 | + InkBackground() |
| 58 | + Row( |
| 59 | + modifier = Modifier |
| 60 | + .fillMaxSize() |
| 61 | + .padding(start = Dimens.SafeStart, end = Dimens.SafeEnd, top = Dimens.TabContentTop), |
| 62 | + horizontalArrangement = Arrangement.spacedBy(36.dp), |
| 63 | + ) { |
| 64 | + SectionRail( |
| 65 | + title = stringResource(R.string.subscriptions_title), |
| 66 | + entries = SubscriptionsSection.entries.map { RailEntry(it.name, stringResource(it.labelRes), it.icon) }, |
| 67 | + selectedId = session.selected.name, |
| 68 | + onSelect = { session.selected = SubscriptionsSection.valueOf(it) }, |
| 69 | + selectedRequester = selectedRequester, |
| 70 | + ) |
| 71 | + Crossfade( |
| 72 | + targetState = session.selected, |
| 73 | + animationSpec = tween(240), |
| 74 | + modifier = Modifier.weight(1f).fillMaxHeight().trapFocusDown(), |
| 75 | + label = "subscriptions-content", |
| 76 | + ) { section -> |
| 77 | + when (section) { |
| 78 | + SubscriptionsSection.Videos -> FeedPane( |
| 79 | + feed = feed, |
| 80 | + onOpen = { |
| 81 | + session.restoreId = it.id |
| 82 | + onOpen(it) |
| 83 | + }, |
| 84 | + onRetry = { attempt++ }, |
| 85 | + restoreId = restore, |
| 86 | + fallback = selectedRequester, |
| 87 | + onRestored = onRestored, |
| 88 | + ) |
| 89 | + SubscriptionsSection.Channels -> ChannelsPane( |
| 90 | + channels = channels, |
| 91 | + onOpen = { |
| 92 | + session.restoreId = it.url |
| 93 | + onOpenChannel(it) |
| 94 | + }, |
| 95 | + onRetry = { attempt++ }, |
| 96 | + restoreId = restore, |
| 97 | + fallback = selectedRequester, |
| 98 | + onRestored = onRestored, |
| 99 | + ) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments