Skip to content

Commit

Permalink
New navigation setup (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
UmairKhalid786 authored Nov 10, 2024
1 parent 770cd90 commit 3d5e25e
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 203 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
package com.techlads.composetv.features.home

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.tv.material3.MaterialTheme
import androidx.tv.material3.Text
import com.techlads.composetv.features.home.carousel.HomeCarousel
import com.techlads.composetv.features.home.hero.HeroItem
import com.techlads.composetv.utils.handleDPadKeyEvents
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Composable
fun HomeNestedScreen(
navigationBar: (NavigationEvent) -> Unit,
onItemFocus: (parent: Int, child: Int) -> Unit,
onItemClick: (parent: Int, child: Int) -> Unit,
) {
Expand All @@ -19,16 +41,108 @@ fun HomeNestedScreen(
mutableStateOf(FocusPosition(0, 0))
}

val showCarousel = remember {
mutableStateOf(true)
}

val showTopPickDetails = remember {
mutableStateOf(false)
}

val focusRequester = remember { FocusRequester() }

val coroutineScope = rememberCoroutineScope()

Column(Modifier.fillMaxSize()) {
HeroItem()
AnimatedVisibility(showCarousel.value) {
HeroItem(modifier = Modifier.focusRequester(focusRequester))
}
AnimatedVisibility(showTopPickDetails.value) {
TopPickDetails(modifier = Modifier
.padding(start = 52.dp, end = 52.dp, bottom = 32.dp)
.height(200.dp), title = "Top Picks", metadata = "SVT Play • 2021 • 1h 30m")
}
HomeCarousel(
Modifier
.handleDPadKeyEvents(
onUp = {
coroutineScope.launch {
if (focusState.value.first == 0) {
showCarousel.value = true
showTopPickDetails.value = false
navigationBar(NavigationEvent.TopBar)
delay(200)
focusRequester.requestFocus()
}
}~Z
},
)
.onFocusChanged {
if (it.hasFocus) {
// showCarousel.value = true
// showTopPickDetails.value = false
} else {
showCarousel.value = true
showTopPickDetails.value = false
}
}
.weight(1f), onItemFocus = { parent, child ->

focusState.value = FocusPosition(parent, child)
onItemFocus(parent, child)

if (parent == 0) {
showCarousel.value = false
showTopPickDetails.value = true
} else {
showCarousel.value = false
showTopPickDetails.value = false
}

navigationBar(if (parent >= 0) NavigationEvent.None else NavigationEvent.TopBar)

}, onItemClick = onItemClick
)
}
}

@Composable
fun TopPickDetails(
title: String,
metadata: String,
modifier: Modifier = Modifier,
) {
Box(
modifier = modifier,
) {
Column(
modifier = Modifier
.align(Alignment.BottomStart)
.wrapContentSize(),
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
AnimatedContent(title) {
Text(
text = it,
style = MaterialTheme.typography.titleLarge.copy(
fontWeight = FontWeight.Black
),
minLines = 2,
maxLines = 2,
overflow = TextOverflow.Ellipsis,
color = MaterialTheme.colorScheme.onSurface,
)
}
AnimatedContent(metadata) {
Text(
modifier = Modifier.graphicsLayer { alpha = 0.5f },
text = it,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurface,
)
}
}
}
}

typealias FocusPosition = Pair<Int, Int>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ fun HomeScreen(
onSongClick: () -> Unit,
) {
HomeScreenContent(onItemFocus, homeViewModel.usedTopBar,
{ homeViewModel.toggleTopBar() }, onSongClick
{ homeViewModel.updateMenu(it) }, onSongClick
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.tooling.preview.Preview
import androidx.tv.material3.MaterialTheme
Expand All @@ -31,8 +33,8 @@ import kotlinx.coroutines.flow.StateFlow
@Composable
fun HomeScreenContent(
onItemFocus: (parent: Int, child: Int) -> Unit,
usedTopBar: StateFlow<Boolean>,
toggleNavigationBar: () -> Unit,
usedTopBar: StateFlow<NavigationEvent>,
navigationBar: (NavigationEvent) -> Unit,
onSongClick: () -> Unit,
) {
val navController = rememberAnimatedNavController()
Expand Down Expand Up @@ -61,7 +63,22 @@ fun HomeScreenContent(
.fillMaxSize()
.drawWithContent {
drawContent()
drawRect(color = overlayColor)
drawRect(
Brush.horizontalGradient(
listOf(
overlayColor,
overlayColor.copy(alpha = 0.8f),
Color.Transparent
)
)
)
drawRect(
Brush.verticalGradient(
listOf(
Color.Transparent, overlayColor.copy(alpha = 0.5f)
)
)
)
},
bitmap = it,
contentDescription = "Hero item background",
Expand All @@ -71,40 +88,39 @@ fun HomeScreenContent(
}
}

usedTopBar.collectAsState().value.let { selectedTopBar ->
val menu by usedTopBar.collectAsState()

when (selectedTopBar) {
true -> HomeTopBar(content = {
NestedHomeNavigation(usedTopBar,
toggleNavigationBar,
navController,
onItemClick = { parent, child ->
onItemFocus(parent, child)
},
onItemFocus = { _, child ->
backgroundState.load(Storage.movies[child % Storage.movies.size].imageUrl)
},
onSongClick
)
}, selectedId = selectedId.value) {
navController.navigate(it.id)
}

false -> HomeDrawer(content = {
NestedHomeNavigation(usedTopBar,
toggleNavigationBar,
navController,
onItemClick = { parent, child ->
onItemFocus(parent, child)
},
onItemFocus = { _, child ->
backgroundState.load(Storage.movies[child % Storage.movies.size].imageUrl)
},
onSongClick
)
}, selectedId = selectedId.value) {
navController.navigate(it.id)
}
if (menu == NavigationEvent.LeftMenu) {
HomeDrawer(content = {
NestedHomeNavigation(usedTopBar,
navigationBar,
navController,
onItemClick = { parent, child ->
onItemFocus(parent, child)
},
onItemFocus = { _, child ->
backgroundState.load(Storage.movies[child % Storage.movies.size].imageUrl)
},
onSongClick
)
}, selectedId = selectedId.value) {
navController.navigate(it.id)
}
} else {
HomeTopBar(content = {
NestedHomeNavigation(usedTopBar,
navigationBar,
navController,
onItemClick = { parent, child ->
onItemFocus(parent, child)
},
onItemFocus = { _, child ->
backgroundState.load(Storage.movies[child % Storage.movies.size].imageUrl)
},
onSongClick
)
}, selectedId = selectedId.value, minimiseTopBar = menu == NavigationEvent.None) {
navController.navigate(it.id)
}
}
}
Expand All @@ -114,7 +130,9 @@ fun HomeScreenContent(
fun HomeScreenContentPrev() {
ComposeTvTheme {
HomeScreenContent(onItemFocus = { _, _ -> },
usedTopBar = MutableStateFlow(false),
toggleNavigationBar = {}) {}
usedTopBar = MutableStateFlow(NavigationEvent.TopBar),
navigationBar = {

}) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class HomeViewModel @Inject constructor(
private val _movies = MutableStateFlow<List<Movie>>(emptyList())
val movies: StateFlow<List<Movie>> get() = _movies.asStateFlow()

fun fetchPopularMovies() {
private fun fetchPopularMovies() {
viewModelScope.launch {
try {
when (val response = api.getPopularMovies()) {
Expand All @@ -40,8 +40,8 @@ class HomeViewModel @Inject constructor(

val menuItems: StateFlow<List<MenuItem>> = MutableStateFlow(emptyList())
val menuState: StateFlow<Boolean> = MutableStateFlow(false)
private val _usedTopBar: MutableStateFlow<Boolean> = MutableStateFlow(false)
val usedTopBar: StateFlow<Boolean> = _usedTopBar
private val _usedTopBar: MutableStateFlow<NavigationEvent> = MutableStateFlow(NavigationEvent.TopBar)
val usedTopBar: StateFlow<NavigationEvent> = _usedTopBar

init {
menuItems.toMutable().value = MenuData.menuItems
Expand All @@ -56,7 +56,14 @@ class HomeViewModel @Inject constructor(
menuState.toMutable().value = true
}

fun toggleTopBar(){
_usedTopBar.value = !_usedTopBar.value
fun updateMenu(menu: NavigationEvent){
_usedTopBar.value = menu
}
}


sealed class NavigationEvent {
data object None : NavigationEvent()
data object TopBar : NavigationEvent()
data object LeftMenu : NavigationEvent()
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ fun HorizontalCarouselItem(
onItemClick: (parent: Int, child: Int) -> Unit,
) {
Column(Modifier.height(150.dp)) {
Text(text = "Row $parent", modifier = Modifier.padding(horizontal = 24.dp))
Text(text = "Row $parent", modifier = Modifier.padding(horizontal = 52.dp))
PositionFocusedItemInLazyLayout(
parentFraction = 0.1f,
childFraction = 0.1f,
) {
LazyRow(
contentPadding = PaddingValues(
start = 16.dp,
start = 42.dp,
top = 8.dp,
bottom = 8.dp,
end = 100.dp,
Expand Down
Loading

0 comments on commit 3d5e25e

Please sign in to comment.