Fix home header not updating & row movement (#627)

## Description
Removes an incorrect optimization for showing the home page header

Separates logic for focusing on rows on the home page

### Related issues
Fixes #624 
Fixes #620
This commit is contained in:
Ray 2026-01-03 18:09:40 -05:00 committed by GitHub
parent 36d26a5ead
commit 9345d0a698
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 12 deletions

View file

@ -78,12 +78,13 @@ inline fun <T> List<T>.indexOfFirstOrNull(predicate: (T) -> Boolean): Int? {
/** /**
* Try to call [FocusRequester.requestFocus], but catch & log the exception if something is not configured properly * Try to call [FocusRequester.requestFocus], but catch & log the exception if something is not configured properly
*/ */
fun FocusRequester.tryRequestFocus(): Boolean = fun FocusRequester.tryRequestFocus(tag: String? = null): Boolean =
try { try {
requestFocus() requestFocus()
tag?.let { Timber.v("Request focus tag=%s", tag) }
true true
} catch (ex: IllegalStateException) { } catch (ex: IllegalStateException) {
Timber.w(ex, "Failed to request focus") Timber.w(ex, "Failed to request focus, tag=%s", tag)
false false
} }

View file

@ -226,28 +226,34 @@ fun HomePageContent(
mutableStateOf(RowColumn(firstRow, 0)) mutableStateOf(RowColumn(firstRow, 0))
} }
val focusedItem = val focusedItem =
remember(position) { position.let {
position.let { (homeRows.getOrNull(it.row) as? HomeRowLoadingState.Success)?.items?.getOrNull(it.column)
(homeRows.getOrNull(it.row) as? HomeRowLoadingState.Success)?.items?.getOrNull(it.column)
}
} }
val listState = rememberLazyListState() val listState = rememberLazyListState()
val rowFocusRequesters = remember(homeRows.size) { List(homeRows.size) { FocusRequester() } } val rowFocusRequesters = remember(homeRows.size) { List(homeRows.size) { FocusRequester() } }
var focused by rememberSaveable { mutableStateOf(false) } var firstFocused by rememberSaveable { mutableStateOf(false) }
LaunchedEffect(homeRows) { LaunchedEffect(homeRows) {
if (!focused) { if (!firstFocused) {
// Waiting for the first home row to load, then focus on it
homeRows homeRows
.indexOfFirst { it is HomeRowLoadingState.Success && it.items.isNotEmpty() } .indexOfFirst { it is HomeRowLoadingState.Success && it.items.isNotEmpty() }
.takeIf { it >= 0 } .takeIf { it >= 0 }
?.let { ?.let {
rowFocusRequesters[it].tryRequestFocus() rowFocusRequesters[it].tryRequestFocus()
delay(50) delay(50)
listState.animateScrollToItem(position.row) listState.scrollToItem(it)
focused = true firstFocused = true
} }
} else { }
rowFocusRequesters.getOrNull(position.row)?.tryRequestFocus() }
LaunchedEffect(Unit) {
if (firstFocused) {
// After the first home row was loaded & focused, page recompositions should focus on the positioned row
val index = position.row
rowFocusRequesters.getOrNull(index)?.tryRequestFocus()
delay(50)
listState.scrollToItem(index)
} }
} }
LaunchedEffect(position) { LaunchedEffect(position) {