Repository navigation
Fix iOS chat transcript expansion anchoring #7057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6572b2a
d83b81e
0b9cbb0
db96ce5
48a3c46
17122ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -84,6 +84,8 @@ struct ChatTranscriptTableView: UIViewRepresentable { | |
| private var topRequestKey: String? | ||
| private var lastScrollToBottomRequest = 0 | ||
| private var isHandlingLayout = false | ||
| private var isApplyingDataUpdate = false | ||
| private var pendingContentUpdateAnchor: ChatTranscriptTableAnchor? | ||
| private weak var tableView: ChatTranscriptUITableView? | ||
| private var isAtBottom: Binding<Bool> | ||
| #if DEBUG | ||
|
|
@@ -97,13 +99,18 @@ struct ChatTranscriptTableView: UIViewRepresentable { | |
|
|
||
| func attach(_ tableView: ChatTranscriptUITableView) { | ||
| self.tableView = tableView | ||
| tableView.afterLayout = { [weak self, weak tableView] oldBoundsSize, oldContentSize, oldViewport in | ||
| tableView.anchorBeforeLayout = { [weak self, weak tableView] in | ||
| guard let self, let tableView else { return nil } | ||
| return self.firstVisibleAnchor(in: tableView) | ||
| } | ||
| tableView.afterLayout = { [weak self, weak tableView] oldBoundsSize, oldContentSize, oldViewport, oldAnchor in | ||
| guard let self, let tableView else { return } | ||
| self.handleLayoutChange( | ||
| in: tableView, | ||
| oldBoundsSize: oldBoundsSize, | ||
| oldContentSize: oldContentSize, | ||
| oldViewport: oldViewport | ||
| oldViewport: oldViewport, | ||
| oldAnchor: oldAnchor | ||
| ) | ||
| } | ||
| } | ||
|
|
@@ -120,29 +127,34 @@ struct ChatTranscriptTableView: UIViewRepresentable { | |
| || configuration.agentState != agentState | ||
| let shouldScrollToBottom = scrollToBottomRequest != lastScrollToBottomRequest | ||
| lastScrollToBottomRequest = scrollToBottomRequest | ||
| let wasAtBottom = isAtBottom.wrappedValue | ||
| || distanceFromBottom(in: tableView) <= chatTranscriptAtBottomThreshold | ||
| let wasAtBottom = distanceFromBottom(in: tableView) <= chatTranscriptAtBottomThreshold | ||
| let anchor = firstVisibleAnchor(in: tableView) | ||
|
|
||
| guard shouldReload else { | ||
| if shouldScrollToBottom { | ||
| pendingContentUpdateAnchor = nil | ||
| scrollToBottom(in: tableView, animated: true) | ||
| } | ||
| updateBottomState(from: tableView) | ||
| return | ||
| } | ||
|
|
||
| pendingContentUpdateAnchor = nil | ||
| items = nextItems | ||
| expandedIDs = configuration.expandedIDs | ||
| agentState = configuration.agentState | ||
|
|
||
| isApplyingDataUpdate = true | ||
| defer { isApplyingDataUpdate = false } | ||
| tableView.reloadData() | ||
| tableView.layoutIfNeeded() | ||
|
|
||
| if shouldScrollToBottom || wasAtBottom { | ||
| pendingContentUpdateAnchor = nil | ||
| scrollToBottom(in: tableView, animated: false) | ||
| } else if let anchor { | ||
| restore(anchor, in: tableView) | ||
| pendingContentUpdateAnchor = anchor | ||
| } | ||
| #if DEBUG | ||
| applyDebugInitialScrollIfNeeded(in: tableView) | ||
|
|
@@ -179,17 +191,25 @@ struct ChatTranscriptTableView: UIViewRepresentable { | |
| requestOlderHistoryIfNeeded(in: tableView) | ||
| } | ||
|
|
||
| func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { | ||
| pendingContentUpdateAnchor = nil | ||
| } | ||
|
|
||
| private func handleLayoutChange( | ||
| in tableView: ChatTranscriptUITableView, | ||
| oldBoundsSize: CGSize, | ||
| oldContentSize: CGSize, | ||
| oldViewport: MobileScrollViewportSnapshot? | ||
| oldViewport: MobileScrollViewportSnapshot?, | ||
| oldAnchor: ChatTranscriptTableAnchor? | ||
| ) { | ||
| guard !isHandlingLayout else { return } | ||
| let boundsChanged = abs(oldBoundsSize.height - tableView.bounds.height) > 0.5 | ||
| || abs(oldBoundsSize.width - tableView.bounds.width) > 0.5 | ||
| let contentChanged = abs(oldContentSize.height - tableView.contentSize.height) > 0.5 | ||
| guard boundsChanged || contentChanged else { | ||
| if !isApplyingDataUpdate { | ||
| pendingContentUpdateAnchor = nil | ||
| } | ||
| updateBottomState(from: tableView) | ||
| return | ||
| } | ||
|
|
@@ -201,11 +221,20 @@ struct ChatTranscriptTableView: UIViewRepresentable { | |
| updateBottomState(from: tableView) | ||
| return | ||
| } | ||
| if isApplyingDataUpdate { | ||
| updateBottomState(from: tableView) | ||
| return | ||
| } | ||
|
|
||
| if boundsChanged, let oldViewport { | ||
| restoreKeyboardViewport(snapshot: oldViewport, in: tableView) | ||
| } else if isAtBottom.wrappedValue { | ||
| } else if contentChanged, let pendingContentUpdateAnchor { | ||
| restore(pendingContentUpdateAnchor, in: tableView) | ||
| self.pendingContentUpdateAnchor = nil | ||
| } else if oldViewport?.wasAtBottom == true { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When The previous code used |
||
| scrollToBottom(in: tableView, animated: false) | ||
| } else if contentChanged, let oldAnchor { | ||
| restore(oldAnchor, in: tableView) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| updateBottomState(from: tableView) | ||
| } | ||
|
|
@@ -231,12 +260,14 @@ struct ChatTranscriptTableView: UIViewRepresentable { | |
| y: clampedOffsetY(rect.minY + anchor.offsetFromRowTop, in: tableView) | ||
| ) | ||
| tableView.setContentOffset(offset, animated: false) | ||
| (tableView as? ChatTranscriptUITableView)?.recordCurrentViewport() | ||
| } | ||
|
|
||
| private func scrollToBottom(in tableView: UITableView, animated: Bool) { | ||
| tableView.layoutIfNeeded() | ||
| let targetY = maxOffsetY(in: tableView) | ||
| tableView.setContentOffset(CGPoint(x: tableView.contentOffset.x, y: targetY), animated: animated) | ||
| (tableView as? ChatTranscriptUITableView)?.recordCurrentViewport() | ||
| setAtBottom(true) | ||
| } | ||
|
|
||
|
|
@@ -302,6 +333,7 @@ struct ChatTranscriptTableView: UIViewRepresentable { | |
| let maxY = maxOffsetY(in: tableView) | ||
| let targetY = clampedOffsetY(minY + ((maxY - minY) * 0.5), in: tableView) | ||
| tableView.setContentOffset(CGPoint(x: tableView.contentOffset.x, y: targetY), animated: false) | ||
| (tableView as? ChatTranscriptUITableView)?.recordCurrentViewport() | ||
| setAtBottom(false) | ||
| } | ||
| #endif | ||
|
|
@@ -320,6 +352,7 @@ struct ChatTranscriptTableView: UIViewRepresentable { | |
| CGPoint(x: tableView.contentOffset.x, y: offsetY), | ||
| animated: false | ||
| ) | ||
| (tableView as? ChatTranscriptUITableView)?.recordCurrentViewport() | ||
| setAtBottom(snapshot.wasAtBottom) | ||
| } | ||
| } | ||
|
|
@@ -480,9 +513,4 @@ private enum ChatTranscriptTableItem: Equatable { | |
| } | ||
| } | ||
|
|
||
| private struct ChatTranscriptTableAnchor { | ||
| let id: String | ||
| let offsetFromRowTop: CGFloat | ||
| } | ||
|
|
||
| #endif | ||
Uh oh!
There was an error while loading. Please reload this page.