Merge pull request #1257 from RCGV1/fix-scroll-to-bottom

If no unread messages go straight to bottom
This commit is contained in:
Garth Vander Houwen 2025-06-12 19:00:30 -07:00 committed by GitHub
commit dc892a9543
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 20 deletions

View file

@ -180,24 +180,24 @@ struct ChannelMessageList: View {
}
.scrollDismissesKeyboard(.interactively)
.onFirstAppear {
// Find first unread message
if let firstUnreadMessageId = channel.allPrivateMessages.first(where: { !$0.read })?.messageId {
if channel.unreadMessages == 0 {
withAnimation {
scrollView.scrollTo(firstUnreadMessageId, anchor: .top)
showScrollToBottomButton = true
scrollView.scrollTo("bottomAnchor", anchor: .bottom)
hasReachedBottom = true
}
} else {
// If no unread messages, scroll to bottom
withAnimation {
scrollView.scrollTo(channel.allPrivateMessages.last?.messageId ?? 0, anchor: .bottom)
hasReachedBottom = true
if let firstUnreadMessageId = channel.allPrivateMessages.first(where: { !$0.read })?.messageId {
withAnimation {
scrollView.scrollTo(firstUnreadMessageId, anchor: .top)
showScrollToBottomButton = true
}
}
}
gotFirstUnreadMessage = true
}
.onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardDidShowNotification)) { _ in
withAnimation {
scrollView.scrollTo(channel.allPrivateMessages.last?.messageId ?? 0, anchor: .bottom)
scrollView.scrollTo("bottomAnchor", anchor: .bottom)
hasReachedBottom = true
showScrollToBottomButton = false
}
@ -205,7 +205,7 @@ struct ChannelMessageList: View {
.onChange(of: channel.allPrivateMessages) {
if hasReachedBottom {
withAnimation {
scrollView.scrollTo(channel.allPrivateMessages.last?.messageId ?? 0, anchor: .bottom)
scrollView.scrollTo("bottomAnchor", anchor: .bottom)
}
} else {
showScrollToBottomButton = true

View file

@ -167,24 +167,24 @@ struct UserMessageList: View {
}
.scrollDismissesKeyboard(.interactively)
.onFirstAppear {
// Find first unread message
if let firstUnreadMessageId = user.messageList.first(where: { !$0.read })?.messageId {
if user.unreadMessages == 0 {
withAnimation {
scrollView.scrollTo(firstUnreadMessageId, anchor: .top)
showScrollToBottomButton = true
scrollView.scrollTo("bottomAnchor", anchor: .bottom)
hasReachedBottom = true
}
} else {
// If no unread messages, scroll to bottom
withAnimation {
scrollView.scrollTo(user.messageList.last?.messageId ?? 0, anchor: .bottom)
hasReachedBottom = true
if let firstUnreadMessageId = user.messageList.first(where: { !$0.read })?.messageId {
withAnimation {
scrollView.scrollTo(firstUnreadMessageId, anchor: .top)
showScrollToBottomButton = true
}
}
}
gotFirstUnreadMessage = true
}
.onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardDidShowNotification)) { _ in
withAnimation {
scrollView.scrollTo(user.messageList.last?.messageId ?? 0, anchor: .bottom)
scrollView.scrollTo("bottomAnchor", anchor: .bottom)
hasReachedBottom = true
showScrollToBottomButton = false
}
@ -192,7 +192,7 @@ struct UserMessageList: View {
.onChange(of: user.messageList) {
if hasReachedBottom {
withAnimation {
scrollView.scrollTo(user.messageList.last?.messageId ?? 0, anchor: .bottom)
scrollView.scrollTo("bottomAnchor", anchor: .bottom)
}
} else {
showScrollToBottomButton = true