From 57d6a8c721c655e5644bfcb9111a95f85cf53a76 Mon Sep 17 00:00:00 2001 From: Benjamin Faershtein <119711889+RCGV1@users.noreply.github.com> Date: Thu, 12 Jun 2025 14:12:00 -0700 Subject: [PATCH] Fixed messages not scrolling to bottom --- .../Views/Messages/ChannelMessageList.swift | 20 +++++++++---------- .../Views/Messages/UserMessageList.swift | 20 +++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Meshtastic/Views/Messages/ChannelMessageList.swift b/Meshtastic/Views/Messages/ChannelMessageList.swift index 9dd3b5cf..42bd5c12 100644 --- a/Meshtastic/Views/Messages/ChannelMessageList.swift +++ b/Meshtastic/Views/Messages/ChannelMessageList.swift @@ -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 diff --git a/Meshtastic/Views/Messages/UserMessageList.swift b/Meshtastic/Views/Messages/UserMessageList.swift index e84686d6..7b27b4f2 100644 --- a/Meshtastic/Views/Messages/UserMessageList.swift +++ b/Meshtastic/Views/Messages/UserMessageList.swift @@ -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