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:
@@ -28,11 +28,13 @@ import androidx.compose.material3.Text
|
|||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.runtime.key
|
import androidx.compose.runtime.key
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.rememberUpdatedState
|
import androidx.compose.runtime.rememberUpdatedState
|
||||||
import androidx.compose.runtime.snapshotFlow
|
import androidx.compose.runtime.snapshotFlow
|
||||||
import androidx.compose.runtime.mutableStateMapOf
|
import androidx.compose.runtime.mutableStateMapOf
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.saveable.Saver
|
import androidx.compose.runtime.saveable.Saver
|
||||||
import androidx.compose.runtime.saveable.rememberSaveable
|
import androidx.compose.runtime.saveable.rememberSaveable
|
||||||
import androidx.compose.runtime.snapshots.SnapshotStateMap
|
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.Chats
|
||||||
import eu.lixko.ts3j.client.android.Conversation
|
import eu.lixko.ts3j.client.android.Conversation
|
||||||
import eu.lixko.ts3j.client.text.TextQuery
|
import eu.lixko.ts3j.client.text.TextQuery
|
||||||
|
import kotlinx.coroutines.flow.drop
|
||||||
|
import kotlinx.coroutines.flow.filter
|
||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.flow.first
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@@ -159,10 +163,16 @@ private fun Lines(
|
|||||||
) {
|
) {
|
||||||
val listState = rememberLazyListState(initialFirstVisibleItemIndex = (lines.size - 1).coerceAtLeast(0))
|
val listState = rememberLazyListState(initialFirstVisibleItemIndex = (lines.size - 1).coerceAtLeast(0))
|
||||||
val link by rememberUpdatedState(onLink)
|
val link by rememberUpdatedState(onLink)
|
||||||
LaunchedEffect(lines.lastOrNull()?.id) {
|
// Following the end until the user scrolls away from it, so a restored log, new lines
|
||||||
val info = listState.layoutInfo
|
// and pictures that load late all keep the newest line in view.
|
||||||
val atEnd = info.visibleItemsInfo.lastOrNull()?.index?.let { it >= info.totalItemsCount - 2 } ?: true
|
var follow by remember { mutableStateOf(true) }
|
||||||
if (atEnd && lines.isNotEmpty()) listState.scrollToItem(lines.size - 1)
|
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) {
|
LaunchedEffect(reveal, lines) {
|
||||||
val id = reveal ?: return@LaunchedEffect
|
val id = reveal ?: return@LaunchedEffect
|
||||||
|
|||||||
Reference in New Issue
Block a user