diff --git a/Meshtastic/Persistence/ChannelEntityExtension.swift b/Meshtastic/Persistence/ChannelEntityExtension.swift index 8f6562e5..dfa373be 100644 --- a/Meshtastic/Persistence/ChannelEntityExtension.swift +++ b/Meshtastic/Persistence/ChannelEntityExtension.swift @@ -12,4 +12,10 @@ extension ChannelEntity { self.value(forKey: "allPrivateMessages") as? [MessageEntity] ?? [MessageEntity]() } + + var unreadMessages: Int { + + let unreadMessages = allPrivateMessages.filter{ ($0 as AnyObject).read == false } + return unreadMessages.count + } } diff --git a/Meshtastic/Views/Messages/ChannelMessageList.swift b/Meshtastic/Views/Messages/ChannelMessageList.swift index b95d18b9..4186dd2a 100644 --- a/Meshtastic/Views/Messages/ChannelMessageList.swift +++ b/Meshtastic/Views/Messages/ChannelMessageList.swift @@ -226,6 +226,18 @@ struct ChannelMessageList: View { } }, secondaryButton: .cancel()) } + .onAppear { + if !message.read { + message.read = true + message.toUser?.objectWillChange.send() + do { + try context.save() + print("Read message \(message.messageId) ") + } catch { + print("Failed to read message \(message.messageId)") + } + } + } } } } diff --git a/Meshtastic/Views/Messages/Contacts.swift b/Meshtastic/Views/Messages/Contacts.swift index 35d327b4..c1f89ac2 100644 --- a/Meshtastic/Views/Messages/Contacts.swift +++ b/Meshtastic/Views/Messages/Contacts.swift @@ -47,7 +47,7 @@ struct Contacts: View { ZStack { Image(systemName: "circle.fill") - .opacity(channel.allPrivateMessages.count > 0 ? 1 : 0) + .opacity(channel.unreadMessages > 0 ? 1 : 0) .font(.system(size: 10)) .foregroundColor(.accentColor) .brightness(0.2) diff --git a/Meshtastic/Views/Messages/UserMessageList.swift b/Meshtastic/Views/Messages/UserMessageList.swift index c85d11d5..d6fbacdc 100644 --- a/Meshtastic/Views/Messages/UserMessageList.swift +++ b/Meshtastic/Views/Messages/UserMessageList.swift @@ -364,8 +364,7 @@ struct UserMessageList: View { .toolbar { ToolbarItem(placement: .principal) { HStack { - CircleText(text: user.shortName ?? "???", color: Color(UIColor(hex: UInt32(user.num))), circleSize: 44, fontSize: 14, textColor: UIColor(hex: UInt32(user.num)).isLight() ? .black : .white ).fixedSize() - Text(user.longName ?? "unknown".localized).font(.headline) + CircleText(text: user.shortName ?? "???", color: Color(UIColor(hex: UInt32(user.num))), circleSize: 44, fontSize: (user.shortName ?? "???").isEmoji() ? 32 : (user.shortName?.count ?? 0 == 4 ? 14 : (user.shortName?.count ?? 0 == 3 ? 18 : 22)), brightness: 0.0, textColor: UIColor(hex: UInt32(user.num)).isLight() ? .black : .white) } } ToolbarItem(placement: .navigationBarTrailing) {