Skip to content

Commit

Permalink
Update "Examples" as well
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 committed Nov 29, 2023
1 parent bc2fe6f commit a6b468f
Showing 1 changed file with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
Expand All @@ -38,6 +39,7 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
Expand All @@ -46,6 +48,7 @@ import androidx.navigation.navArgument
import androidx.tv.foundation.lazy.grid.TvGridCells
import androidx.tv.foundation.lazy.grid.TvLazyVerticalGrid
import androidx.tv.foundation.lazy.grid.itemsIndexed
import androidx.tv.foundation.lazy.grid.rememberTvLazyGridState
import androidx.tv.material3.Card
import androidx.tv.material3.ExperimentalTvMaterial3Api
import androidx.tv.material3.MaterialTheme
Expand All @@ -55,6 +58,7 @@ import ch.srgssr.pillarbox.demo.shared.data.Playlist
import ch.srgssr.pillarbox.demo.shared.ui.NavigationRoutes
import ch.srgssr.pillarbox.demo.tv.ui.theme.PillarboxTheme
import ch.srgssr.pillarbox.demo.tv.ui.theme.paddings
import kotlinx.coroutines.launch

/**
* Examples home
Expand All @@ -81,6 +85,7 @@ fun ExamplesHome(
ExamplesSection(
modifier = modifier,
items = playlists,
navController = navController,
onItemClick = { index, _ ->
navController.navigate("${NavigationRoutes.homeSample}/$index")
}
Expand Down Expand Up @@ -114,6 +119,7 @@ fun ExamplesHome(
ExamplesSection(
title = playlist.title,
items = playlist.items,
navController = navController,
onItemClick = { _, item ->
onItemSelected(item)
}
Expand Down Expand Up @@ -154,6 +160,7 @@ private fun <T> ExamplesSection(
modifier: Modifier = Modifier,
title: String? = null,
items: List<T>,
navController: NavHostController,
onItemClick: (index: Int, item: T) -> Unit,
content: @Composable (item: T) -> Unit
) {
Expand All @@ -166,15 +173,7 @@ private fun <T> ExamplesSection(
}

Column(
modifier = modifier
.onPreviewKeyEvent {
if (it.key == Key.DirectionUp && it.type == KeyEventType.KeyDown && isOnFirstRow) {
focusedIndex = -1
focusManager.moveFocus(FocusDirection.Up)
} else {
false
}
},
modifier = modifier,
verticalArrangement = Arrangement.spacedBy(MaterialTheme.paddings.baseline)
) {
if (title != null) {
Expand All @@ -184,9 +183,37 @@ private fun <T> ExamplesSection(
)
}

val coroutineScope = rememberCoroutineScope()
val scrollState = rememberTvLazyGridState()

TvLazyVerticalGrid(
columns = TvGridCells.Fixed(columnCount),
modifier = Modifier.focusRestorer(),
modifier = Modifier
.focusRestorer()
.onPreviewKeyEvent {
if (it.key == Key.DirectionUp && it.type == KeyEventType.KeyDown && isOnFirstRow) {
focusedIndex = -1
focusManager.moveFocus(FocusDirection.Up)
} else if (it.key == Key.Back && it.type == KeyEventType.KeyDown) {
if (!isOnFirstRow) {
focusedIndex = 0

coroutineScope.launch {
scrollState.animateScrollToItem(focusedIndex)
}

true
} else if (navController.previousBackStackEntry == null) {
focusedIndex = -1
focusManager.moveFocus(FocusDirection.Up)
} else {
false
}
} else {
false
}
},
state = scrollState,
contentPadding = PaddingValues(vertical = MaterialTheme.paddings.baseline),
verticalArrangement = Arrangement.spacedBy(MaterialTheme.paddings.baseline),
horizontalArrangement = Arrangement.spacedBy(MaterialTheme.paddings.baseline)
Expand Down

0 comments on commit a6b468f

Please sign in to comment.