Skip to content

Commit f0431e2

Browse files
committed
chore(ui/message): attempt to anchor last read and unread separator towards top of viewport
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 2989318 commit f0431e2

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

  • ui/components/src/main/kotlin/com/getcode/ui/components/chat

ui/components/src/main/kotlin/com/getcode/ui/components/chat/MessageList.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)