Skip to content

Commit

Permalink
Improve back press on search field
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 committed Dec 13, 2023
1 parent 5695b22 commit 5889173
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ fun TVDemoTopBar(
) {
val focusManager = LocalFocusManager.current
val focusedTabIndex by rememberSaveable(currentNavDestination) {
val destinationHierarchy = currentNavDestination?.hierarchy.orEmpty()

mutableIntStateOf(
destinations.indexOfFirst { dest ->
currentNavDestination?.hierarchy?.any {
it.route == dest.route
} == true
destinationHierarchy.any { it.route == dest.route }
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusDirection
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.tooling.preview.Preview
Expand Down Expand Up @@ -115,13 +122,24 @@ private fun SearchRow(
onQueryChange: (query: String) -> Unit,
onBuChange: (bu: Bu) -> Unit
) {
val focusManager = LocalFocusManager.current

Column(
modifier = modifier,
verticalArrangement = Arrangement.spacedBy(MaterialTheme.paddings.baseline)
) {
SearchInput(
query = query,
modifier = Modifier.fillMaxWidth(),
modifier = Modifier
.fillMaxWidth()
.onPreviewKeyEvent {
if (it.key == Key.Back && it.type == KeyEventType.KeyDown) {
focusManager.moveFocus(FocusDirection.Up)
true
} else {
false
}
},
onQueryChange = onQueryChange
)

Expand Down

0 comments on commit 5889173

Please sign in to comment.