From 194f62850c85c53a2d1fbf9b020c9a410422b2f1 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Wed, 3 Sep 2025 09:37:48 -0700 Subject: [PATCH] Remove gotFirstUnreadMessage bool that is breaking things (#1370) --- .../Views/Messages/ChannelMessageList.swift | 43 +++++++------------ .../Views/Messages/UserMessageList.swift | 39 +++++++---------- 2 files changed, 31 insertions(+), 51 deletions(-) diff --git a/Meshtastic/Views/Messages/ChannelMessageList.swift b/Meshtastic/Views/Messages/ChannelMessageList.swift index 306810b0..170a7370 100644 --- a/Meshtastic/Views/Messages/ChannelMessageList.swift +++ b/Meshtastic/Views/Messages/ChannelMessageList.swift @@ -23,8 +23,6 @@ struct ChannelMessageList: View { // Scroll state @State private var showScrollToBottomButton = false @State private var hasReachedBottom = false - @State private var gotFirstUnreadMessage: Bool = false - @State private var messageToHighlight: Int64 = 0 @FetchRequest private var allPrivateMessages: FetchedResults @@ -155,37 +153,29 @@ struct ChannelMessageList: View { Spacer(minLength: 50) } } -// .overlay { -// RoundedRectangle(cornerRadius: 18) -// .stroke(.blue, lineWidth: 2) -// .opacity(((messageToHighlight == message.messageId) || (replyMessageId == message.messageId)) ? 1 : 0) -// } .padding([.leading, .trailing]) .frame(maxWidth: .infinity) .id(message.messageId) .onAppear { - if gotFirstUnreadMessage { - if !message.read { - message.read = true - do { - for unreadMessage in allPrivateMessages.filter({ !$0.read }) { - unreadMessage.read = true - } - try context.save() - Logger.data.info("📖 [App] Read message \(message.messageId, privacy: .public) ") - appState.unreadChannelMessages = myInfo.unreadMessages - context.refresh(myInfo, mergeChanges: true) - } catch { - Logger.data.error("Failed to read message \(message.messageId, privacy: .public): \(error.localizedDescription, privacy: .public)") + if !message.read { + message.read = true + do { + for unreadMessage in allPrivateMessages.filter({ !$0.read }) { + unreadMessage.read = true } - } - // Check if we've reached the bottom message - if message.messageId == allPrivateMessages.last?.messageId { - hasReachedBottom = true - showScrollToBottomButton = false + try context.save() + Logger.data.info("📖 [App] Read message \(message.messageId, privacy: .public) ") + appState.unreadChannelMessages = myInfo.unreadMessages + context.refresh(myInfo, mergeChanges: true) + } catch { + Logger.data.error("Failed to read message \(message.messageId, privacy: .public): \(error.localizedDescription, privacy: .public)") } } - } + // Check if we've reached the bottom message + if message.messageId == allPrivateMessages.last?.messageId { + hasReachedBottom = true + showScrollToBottomButton = false + } } } // Invisible spacer to detect reaching bottom Color.clear @@ -210,7 +200,6 @@ struct ChannelMessageList: View { withAnimation { scrollView.scrollTo(firstUnreadMessageId, anchor: .top) showScrollToBottomButton = true - gotFirstUnreadMessage = true } } } diff --git a/Meshtastic/Views/Messages/UserMessageList.swift b/Meshtastic/Views/Messages/UserMessageList.swift index 15af373b..0eafa195 100644 --- a/Meshtastic/Views/Messages/UserMessageList.swift +++ b/Meshtastic/Views/Messages/UserMessageList.swift @@ -22,7 +22,6 @@ struct UserMessageList: View { // Scroll state @State private var showScrollToBottomButton = false @State private var hasReachedBottom = false - @State private var gotFirstUnreadMessage: Bool = false @State private var messageToHighlight: Int64 = 0 var body: some View { @@ -123,34 +122,27 @@ struct UserMessageList: View { Spacer(minLength: 50) } } -// .overlay { -// RoundedRectangle(cornerRadius: 10) -// .stroke(.blue, lineWidth: 2) -// .opacity(((messageToHighlight == message.messageId) || (replyMessageId == message.messageId)) ? 1 : 0) -// } .padding([.leading, .trailing]) .frame(maxWidth: .infinity) .id(message.messageId) .onAppear { - if gotFirstUnreadMessage { - if !message.read { - message.read = true - do { - for unreadMessage in user.messageList.filter({ !$0.read }) { - unreadMessage.read = true - } - try context.save() - Logger.data.info("📖 [App] Read message \(message.messageId, privacy: .public) ") - appState.unreadDirectMessages = user.unreadMessages - } catch { - Logger.data.error("Failed to read message \(message.messageId, privacy: .public): \(error.localizedDescription, privacy: .public)") + if !message.read { + message.read = true + do { + for unreadMessage in user.messageList.filter({ !$0.read }) { + unreadMessage.read = true } + try context.save() + Logger.data.info("📖 [App] Read message \(message.messageId, privacy: .public) ") + appState.unreadDirectMessages = user.unreadMessages + } catch { + Logger.data.error("Failed to read message \(message.messageId, privacy: .public): \(error.localizedDescription, privacy: .public)") } - // Check if we've reached the bottom message - if message.messageId == user.messageList.last?.messageId { - hasReachedBottom = true - showScrollToBottomButton = false - } + } + // Check if we've reached the bottom message + if message.messageId == user.messageList.last?.messageId { + hasReachedBottom = true + showScrollToBottomButton = false } } } @@ -180,7 +172,6 @@ struct UserMessageList: View { } } } - gotFirstUnreadMessage = true } .onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardDidShowNotification)) { _ in withAnimation {