Android: keep chats scrolled to the newest line

A chat follows its end until the user scrolls away from it: a restored log,
new lines and content that grows later all keep the newest line fully in
view, a tall last message included.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-10 14:55:17 +00:00
co-authored by Claude Opus 5.5
parent 78fc45f9b2
commit a713b1e818
@@ -28,11 +28,13 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.runtime.key
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.snapshotFlow
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.snapshots.SnapshotStateMap
@@ -53,6 +55,8 @@ import eu.lixko.ts3j.client.android.ChatMatch
import eu.lixko.ts3j.client.android.Chats
import eu.lixko.ts3j.client.android.Conversation
import eu.lixko.ts3j.client.text.TextQuery
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
@Composable
@@ -159,10 +163,16 @@ private fun Lines(
) {
val listState = rememberLazyListState(initialFirstVisibleItemIndex = (lines.size - 1).coerceAtLeast(0))
val link by rememberUpdatedState(onLink)
LaunchedEffect(lines.lastOrNull()?.id) {
val info = listState.layoutInfo
val atEnd = info.visibleItemsInfo.lastOrNull()?.index?.let { it >= info.totalItemsCount - 2 } ?: true
if (atEnd && lines.isNotEmpty()) listState.scrollToItem(lines.size - 1)
// Following the end until the user scrolls away from it, so a restored log, new lines
// and pictures that load late all keep the newest line in view.
var follow by remember { mutableStateOf(true) }
LaunchedEffect(listState) {
snapshotFlow { listState.isScrollInProgress }.drop(1).filter { !it }
.collect { follow = !listState.canScrollForward }
}
LaunchedEffect(listState) {
snapshotFlow { follow && listState.canScrollForward && !listState.isScrollInProgress }.filter { it }
.collect { listState.scrollToItem(listState.layoutInfo.totalItemsCount - 1, Int.MAX_VALUE) }
}
LaunchedEffect(reveal, lines) {
val id = reveal ?: return@LaunchedEffect