File tree Expand file tree Collapse file tree
ui/components/src/main/kotlin/com/getcode/ui/components/chat Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -266,9 +266,26 @@ private fun HandleStartAtUnread(
266266 .indexOfFirst { it is ChatItem .UnreadSeparator }
267267
268268 if (separatorIndex > 0 && ! hasSetAtUnread) {
269- onHandled()
269+ val previousItemIndex = separatorIndex - 1
270+
271+ // First scroll to bring the item into view
272+ listState.scrollToItem(previousItemIndex)
273+
274+ // Dynamically calculate the correct offset
275+ val itemInfo = listState.layoutInfo.visibleItemsInfo
276+ .find { it.index == previousItemIndex }
277+
278+ itemInfo?.let {
279+ val viewportEnd = listState.layoutInfo.viewportEndOffset
280+ val offsetFromEnd = viewportEnd - (it.offset + it.size)
281+
282+ // Scroll only if the item isn't sufficiently visible
283+ if (offsetFromEnd > 0 ) {
284+ listState.scrollToItem(previousItemIndex, scrollOffset = it.offset)
285+ }
286+ }
270287 hasScrolledToUnread = true
271- listState.scrollToItem(separatorIndex - 1 )
288+ onHandled( )
272289 } else {
273290 onHandled()
274291 }
You can’t perform that action at this time.
0 commit comments