Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Left menu improvement #100

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package com.techlads.composetv.features.home
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.layout.Row
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.tooling.preview.Preview
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
import com.techlads.composetv.features.home.leftmenu.data.MenuData
import com.techlads.composetv.features.home.navigation.NestedHomeNavigation
import com.techlads.composetv.features.home.navigation.drawer.HomeDrawer
import com.techlads.composetv.theme.ComposeTvTheme
Expand All @@ -17,10 +21,20 @@ fun HomeScreenContent(
) {
val navController = rememberAnimatedNavController()

val selectedId = remember {
mutableStateOf(MenuData.menuItems.first().id)
}

LaunchedEffect(key1 = Unit) {
navController.addOnDestinationChangedListener { _, destination, _ ->
selectedId.value = destination.route ?: return@addOnDestinationChangedListener
}
}

Row {
HomeDrawer(content = {
NestedHomeNavigation(navController, onItemFocus, onSongClick)
}) {
}, selectedId = selectedId.value) {
navController.navigate(it.id)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import compose.icons.lineawesomeicons.TvSolid
fun MenuHeader(expanded: Boolean = true) {
val animatedAlpha = animateFloatAsState(
animationSpec = keyframes {
this.delayMillis = 500
this.durationMillis = 500
this.delayMillis = 100
this.durationMillis = 100
},
targetValue = if (expanded) 1f else 0f,
targetValue = if (expanded) 1f else 0f, label = "animatedAlpha",
)
Column(
horizontalAlignment = Alignment.CenterHorizontally,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.tv.material3.ClickableSurfaceDefaults
import androidx.tv.material3.DrawerValue
import androidx.tv.material3.ExperimentalTvMaterial3Api
import androidx.tv.material3.Icon
import androidx.tv.material3.MaterialTheme
import androidx.tv.material3.ModalNavigationDrawer
import androidx.tv.material3.Surface
import androidx.tv.material3.Text
import androidx.tv.material3.rememberDrawerState
import com.techlads.composetv.features.home.leftmenu.MenuHeader
import com.techlads.composetv.features.home.leftmenu.data.MenuData
import com.techlads.composetv.features.home.leftmenu.model.MenuItem
Expand All @@ -34,53 +36,62 @@ val navigationRow: @Composable (
drawerValue: DrawerValue,
menu: MenuItem,
modifier: Modifier,
isSelected: Boolean,
onMenuSelected: ((menuItem: MenuItem) -> Unit)?,
) -> Unit =
{ drawerValue, menu, modifier, onMenuSelected ->
val padding = animateDpAsState(
animationSpec = keyframes {
this.delayMillis = 100
},
targetValue = if (drawerValue == DrawerValue.Open) 4.dp else 0.dp,
)
) -> Unit = { drawerValue, menu, modifier, isSelected, onMenuSelected ->

Surface(
onClick = { onMenuSelected?.invoke(menu) },
modifier =
modifier
.padding(vertical = 4.dp)
.then(if (drawerValue == DrawerValue.Open) modifier.width(170.dp) else modifier),
val padding = animateDpAsState(
animationSpec = keyframes {
this.delayMillis = 100
},
targetValue = if (drawerValue == DrawerValue.Open) 4.dp else 0.dp, label = "",
)

Surface(
onClick = { onMenuSelected?.invoke(menu) },
color = ClickableSurfaceDefaults.color(
color = if (isSelected) MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f) else MaterialTheme.colorScheme.surface,
),
modifier = modifier
.padding(vertical = 4.dp)
.then(if (drawerValue == DrawerValue.Open) modifier.width(170.dp) else modifier)
) {
Row(
Modifier.padding(horizontal = 12.dp, vertical = 8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Row(
Modifier
.padding(horizontal = 12.dp, vertical = 8.dp),
verticalAlignment = Alignment.CenterVertically,
menu.icon?.let {
Icon(
imageVector = it,
contentDescription = menu.text,
modifier = Modifier.size(20.dp),
)
Spacer(modifier = Modifier.padding(horizontal = padding.value))
}
AnimatedVisibility(
visible = drawerValue == DrawerValue.Open,
modifier = Modifier.height(20.dp),
) {
menu.icon?.let {
Icon(
imageVector = it,
contentDescription = menu.text,
modifier = Modifier.size(16.dp),
)
Spacer(modifier = Modifier.padding(horizontal = padding.value))
}
AnimatedVisibility(
visible = drawerValue == DrawerValue.Open,
modifier = Modifier.height(20.dp),
) {
Text(
text = menu.text,
maxLines = 1,
style = MaterialTheme.typography.bodyMedium,
)
}
Text(
text = menu.text,
maxLines = 1,
style = MaterialTheme.typography.bodyMedium,
)
}
}
}
}

@Composable
fun HomeDrawer(content: @Composable () -> Unit, onMenuSelected: ((menuItem: MenuItem) -> Unit)?) {
fun HomeDrawer(
content: @Composable () -> Unit,
selectedId: String = MenuData.menuItems.first().id,
onMenuSelected: ((menuItem: MenuItem) -> Unit)?
) {
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)

ModalNavigationDrawer(
drawerState = drawerState,
drawerContent = { drawer ->
Column(
Modifier
Expand All @@ -92,14 +103,20 @@ fun HomeDrawer(content: @Composable () -> Unit, onMenuSelected: ((menuItem: Menu
Spacer(modifier = Modifier.height(8.dp))
MenuHeader(expanded = drawer == DrawerValue.Open)
Spacer(modifier = Modifier.height(16.dp))

MenuData.menuItems.forEach {
navigationRow(drawer, it, Modifier) {
navigationRow(drawer, it, Modifier, selectedId == it.id) {
onMenuSelected?.invoke(it)
drawerState.setValue(DrawerValue.Closed)
}
}

Spacer(modifier = Modifier.weight(1f))
navigationRow(drawer, MenuData.settingsItem, Modifier) {
navigationRow(
drawer, MenuData.settingsItem, Modifier, MenuData.settingsItem.id == selectedId
) {
onMenuSelected?.invoke(it)
drawerState.setValue(DrawerValue.Closed)
}
}
},
Expand Down