diff --git a/android/app/src/main/java/eu/lixko/ts3j/client/android/ui/ChatPane.kt b/android/app/src/main/java/eu/lixko/ts3j/client/android/ui/ChatPane.kt index c94b066..ffd0ecd 100644 --- a/android/app/src/main/java/eu/lixko/ts3j/client/android/ui/ChatPane.kt +++ b/android/app/src/main/java/eu/lixko/ts3j/client/android/ui/ChatPane.kt @@ -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