Skip to content

Commit

Permalink
Fix focus detection in cards
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsvanvelzen committed Sep 17, 2023
1 parent c16fd9d commit b39ff24
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.foundation.basicMarquee
import androidx.compose.foundation.clickable
import androidx.compose.foundation.focusable
import androidx.compose.foundation.indication
import androidx.compose.foundation.interaction.FocusInteraction
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand All @@ -17,14 +18,11 @@ import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.style.TextOverflow
Expand All @@ -45,8 +43,8 @@ fun AppCard(
) {
val context = LocalContext.current
val image = remember { app.createDrawable(context) }
var focused by remember { mutableStateOf(false) }
val interactionSource = remember { MutableInteractionSource() }
val focused = interactionSource.interactions.collectAsState(initial = null).value is FocusInteraction.Focus

val launchIntentUri = app.launchIntentUriLeanback ?: app.launchIntentUriDefault

Expand All @@ -55,7 +53,6 @@ fun AppCard(
.width(160.dp)
.focusable(true, interactionSource)
.indication(interactionSource, FocusScaleIndication(1.125f))
.onFocusChanged { focused = it.hasFocus }
.clickable(enabled = launchIntentUri != null) {
if (launchIntentUri != null) context.startActivity(
Intent.parseUri(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.foundation.basicMarquee
import androidx.compose.foundation.clickable
import androidx.compose.foundation.focusable
import androidx.compose.foundation.indication
import androidx.compose.foundation.interaction.FocusInteraction
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand All @@ -18,14 +19,11 @@ import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.style.TextOverflow
Expand All @@ -44,15 +42,14 @@ fun ChannelProgramCard(
modifier: Modifier = Modifier,
) {
val context = LocalContext.current
var focused by remember { mutableStateOf(false) }
val interactionSource = remember { MutableInteractionSource() }
val focused = interactionSource.interactions.collectAsState(initial = null).value is FocusInteraction.Focus

Column(
modifier = modifier
.width(90.dp * (program.posterArtAspectRatio?.floatValue ?: 1f))
.focusable(true, interactionSource)
.indication(interactionSource, FocusScaleIndication(1.125f))
.onFocusChanged { focused = it.hasFocus }
.clickable(enabled = program.intentUri != null) {
if (program.intentUri != null) context.startActivity(
Intent.parseUri(
Expand Down

0 comments on commit b39ff24

Please sign in to comment.