diff --git a/Meshtastic.xcodeproj/project.pbxproj b/Meshtastic.xcodeproj/project.pbxproj index 14919650..523c56ca 100644 --- a/Meshtastic.xcodeproj/project.pbxproj +++ b/Meshtastic.xcodeproj/project.pbxproj @@ -44,6 +44,7 @@ DD5394FC276993AD00AD86B1 /* SwiftProtobuf in Frameworks */ = {isa = PBXBuildFile; productRef = DD5394FB276993AD00AD86B1 /* SwiftProtobuf */; }; DD5394FE276BA0EF00AD86B1 /* PositionEntityExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD5394FD276BA0EF00AD86B1 /* PositionEntityExtension.swift */; }; DD539502276DAA6A00AD86B1 /* MapLocation.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD539501276DAA6A00AD86B1 /* MapLocation.swift */; }; + DD58C5F22919AD3C00D5BEFB /* ChannelEntityExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD58C5F12919AD3C00D5BEFB /* ChannelEntityExtension.swift */; }; DD6193752862F6E600E59241 /* ExternalNotificationConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD6193742862F6E600E59241 /* ExternalNotificationConfig.swift */; }; DD6193772862F90F00E59241 /* CannedMessagesConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD6193762862F90F00E59241 /* CannedMessagesConfig.swift */; }; DD6193792863875F00E59241 /* SerialConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD6193782863875F00E59241 /* SerialConfig.swift */; }; @@ -156,6 +157,7 @@ DD4F23CC28779A3C001D37CB /* EnvironmentMetricsLog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnvironmentMetricsLog.swift; sourceTree = ""; }; DD5394FD276BA0EF00AD86B1 /* PositionEntityExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PositionEntityExtension.swift; sourceTree = ""; }; DD539501276DAA6A00AD86B1 /* MapLocation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapLocation.swift; sourceTree = ""; }; + DD58C5F12919AD3C00D5BEFB /* ChannelEntityExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelEntityExtension.swift; sourceTree = ""; }; DD6193742862F6E600E59241 /* ExternalNotificationConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExternalNotificationConfig.swift; sourceTree = ""; }; DD6193762862F90F00E59241 /* CannedMessagesConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CannedMessagesConfig.swift; sourceTree = ""; }; DD6193782863875F00E59241 /* SerialConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SerialConfig.swift; sourceTree = ""; }; @@ -535,6 +537,7 @@ DDC4D567275499A500A4208E /* Persistence.swift */, DD5394FD276BA0EF00AD86B1 /* PositionEntityExtension.swift */, DDD9E4E3284B208E003777C5 /* UserEntityExtension.swift */, + DD58C5F12919AD3C00D5BEFB /* ChannelEntityExtension.swift */, ); path = Persistence; sourceTree = ""; @@ -777,6 +780,7 @@ DD3CC6C028E7A60700FA9159 /* MessagingEnums.swift in Sources */, DD97E96628EFD9820056DDA4 /* MeshtasticLogo.swift in Sources */, C9697F9D279336B700250207 /* LocalMBTileOverlay.swift in Sources */, + DD58C5F22919AD3C00D5BEFB /* ChannelEntityExtension.swift in Sources */, DD0F791B28713C8A00A6FDAD /* AdminMessageList.swift in Sources */, DD3CC6BC28E366DF00FA9159 /* Meshtastic.xcdatamodeld in Sources */, DD8169F9271F1A6100F4AB02 /* MeshLogger.swift in Sources */, diff --git a/Meshtastic/Helpers/MeshPackets.swift b/Meshtastic/Helpers/MeshPackets.swift index aa3f037b..516fd12d 100644 --- a/Meshtastic/Helpers/MeshPackets.swift +++ b/Meshtastic/Helpers/MeshPackets.swift @@ -1263,8 +1263,8 @@ func textMessageAppPacket(packet: MeshPacket, connectedNode: Int64, context: NSM if fetchedUsers.count <= 1 && fetchedUsers.first(where: { $0.num == packet.from }) == nil { - print("Message from another mesh, unable to manage for now") - return + //print("Message from another mesh, unable to manage for now") + //return } let newMessage = MessageEntity(context: context) @@ -1277,20 +1277,24 @@ func textMessageAppPacket(packet: MeshPacket, connectedNode: Int64, context: NSM if packet.decoded.replyID > 0 { newMessage.replyID = Int64(packet.decoded.replyID) } - if packet.to == broadcastNodeNum && fetchedUsers.count == 1 { + //if packet.to == broadcastNodeNum && fetchedUsers.count == 1 { // Save the broadcast user if it does not exist - let bcu: UserEntity = UserEntity(context: context) - bcu.shortName = "ALL" - bcu.longName = "All - Broadcast" - bcu.hwModel = "UNSET" - bcu.num = Int64(broadcastNodeNum) - bcu.userId = "BROADCASTNODE" - newMessage.toUser = bcu +// let bcu: UserEntity = UserEntity(context: context) +// bcu.shortName = "ALL" +// bcu.longName = "All - Broadcast" +// bcu.hwModel = "UNSET" +// bcu.num = Int64(broadcastNodeNum) +// bcu.userId = "BROADCASTNODE" +// newMessage.toUser = bcu - } else { + if fetchedUsers.first(where: { $0.num == packet.to }) != nil { newMessage.toUser = fetchedUsers.first(where: { $0.num == packet.to }) } - newMessage.fromUser = fetchedUsers.first(where: { $0.num == packet.from }) + if fetchedUsers.first(where: { $0.num == packet.from }) != nil { + newMessage.fromUser = fetchedUsers.first(where: { $0.num == packet.from }) + } + + newMessage.messagePayload = messageText newMessage.fromUser?.objectWillChange.send() newMessage.toUser?.objectWillChange.send() diff --git a/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModel.xcdatamodel/contents b/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModel.xcdatamodel/contents index f92b0c92..670f7ee5 100644 --- a/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModel.xcdatamodel/contents +++ b/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModel.xcdatamodel/contents @@ -1,5 +1,5 @@ - + @@ -28,6 +28,9 @@ + + + @@ -84,8 +87,8 @@ - - + + @@ -236,7 +239,7 @@ - + \ No newline at end of file diff --git a/Meshtastic/Persistence/ChannelEntityExtension.swift b/Meshtastic/Persistence/ChannelEntityExtension.swift new file mode 100644 index 00000000..4a3bde42 --- /dev/null +++ b/Meshtastic/Persistence/ChannelEntityExtension.swift @@ -0,0 +1,15 @@ +// +// ChannelEntityExtension.swift +// Meshtastic +// +// Copyright(c) Garth Vander Houwen 11/7/22. +// +import Foundation + +extension ChannelEntity { + + var allPrivateMessages: [MessageEntity] { + + self.value(forKey: "allPrivateMessages") as! [MessageEntity] + } +} diff --git a/Meshtastic/Views/Messages/ChannelMessageList.swift b/Meshtastic/Views/Messages/ChannelMessageList.swift index 84a1abb6..5e256313 100644 --- a/Meshtastic/Views/Messages/ChannelMessageList.swift +++ b/Meshtastic/Views/Messages/ChannelMessageList.swift @@ -24,7 +24,6 @@ struct ChannelMessageList: View { var maxbytes = 228 @FocusState var focusedField: Field? - @ObservedObject var user: UserEntity @ObservedObject var channel: ChannelEntity @State var showDeleteMessageAlert = false @State private var deleteMessageId: Int64 = 0 @@ -37,179 +36,175 @@ struct ChannelMessageList: View { ScrollViewReader { scrollView in ScrollView { LazyVStack { - ForEach( user.messageList ) { (message: MessageEntity) in - if user.num != userSettings.preferredNodeNum { - let currentUser: Bool = (userSettings.preferredNodeNum == message.fromUser?.num ? true : false) - if (user.num == bleManager.broadcastNodeNum || user.num != bleManager.broadcastNodeNum && message.toUser!.num != bleManager.broadcastNodeNum) { - if message.replyID > 0 { - let messageReply = user.messageList.first(where: { $0.messageId == message.replyID }) - HStack { - Text(messageReply?.messagePayload ?? "EMPTY MESSAGE").foregroundColor(.blue).font(.caption2) - .padding(10) - .overlay( - RoundedRectangle(cornerRadius: 18) - .stroke(Color.blue, lineWidth: 0.5) - ) - Image(systemName: "arrowshape.turn.up.left.fill") - .symbolRenderingMode(.hierarchical) - .imageScale(.large).foregroundColor(.blue) - .padding(.trailing) - } - } - HStack (alignment: .top) { - if currentUser { Spacer(minLength:50) } - if !currentUser { - CircleText(text: message.fromUser?.shortName ?? "????", color: currentUser ? .accentColor : Color(.darkGray), circleSize: 44, fontSize: 14) - .padding(.all, 5) - .offset(y: -5) - } - VStack(alignment: currentUser ? .trailing : .leading) { - Text(message.messagePayload ?? "EMPTY MESSAGE") - .padding(10) - .foregroundColor(.white) - .background(currentUser ? Color.blue : Color(.darkGray)) - .cornerRadius(15) - .contextMenu { - VStack{ - Text("Channel: \(message.channel)") - } - Menu("Tapback response") { - ForEach(Tapbacks.allCases) { tb in - Button(action: { - if bleManager.sendMessage(message: tb.emojiString, toUserNum: user.num, isEmoji: true, replyID: message.messageId) { - print("Sent \(tb.emojiString) Tapback") - self.context.refresh(user, mergeChanges: true) - } else { print("\(tb.emojiString) Tapback Failed") } - - }) { - Text(tb.description) - let image = tb.emojiString.image() - Image(uiImage: image!) - } - } - } + ForEach( channel.allPrivateMessages ) { (message: MessageEntity) in + let currentUser: Bool = (userSettings.preferredNodeNum == message.fromUser?.num ? true : false) + if message.replyID > 0 { + let messageReply = channel.allPrivateMessages.first(where: { $0.messageId == message.replyID }) + HStack { + Text(messageReply?.messagePayload ?? "EMPTY MESSAGE").foregroundColor(.blue).font(.caption2) + .padding(10) + .overlay( + RoundedRectangle(cornerRadius: 18) + .stroke(Color.blue, lineWidth: 0.5) + ) + Image(systemName: "arrowshape.turn.up.left.fill") + .symbolRenderingMode(.hierarchical) + .imageScale(.large).foregroundColor(.blue) + .padding(.trailing) + } + } + HStack (alignment: .top) { + if currentUser { Spacer(minLength:50) } + if !currentUser { + CircleText(text: message.fromUser?.shortName ?? "????", color: currentUser ? .accentColor : Color(.darkGray), circleSize: 44, fontSize: 14) + .padding(.all, 5) + .offset(y: -5) + } + VStack(alignment: currentUser ? .trailing : .leading) { + Text(message.messagePayload ?? "EMPTY MESSAGE") + .padding(10) + .foregroundColor(.white) + .background(currentUser ? Color.blue : Color(.darkGray)) + .cornerRadius(15) + .contextMenu { + VStack{ + Text("Channel: \(message.channel)") + } + Menu("Tapback response") { + ForEach(Tapbacks.allCases) { tb in Button(action: { - self.replyMessageId = message.messageId - self.focusedField = .messageText - print("I want to reply to \(message.messageId)") + if bleManager.sendMessage(message: tb.emojiString, toUserNum: Int64(channel.index), isEmoji: true, replyID: message.messageId) { + print("Sent \(tb.emojiString) Tapback") + self.context.refresh(channel, mergeChanges: true) + } else { print("\(tb.emojiString) Tapback Failed") } + }) { - Text("Reply") - Image(systemName: "arrowshape.turn.up.left.2.fill") + Text(tb.description) + let image = tb.emojiString.image() + Image(uiImage: image!) } - Button(action: { - UIPasteboard.general.string = message.messagePayload - }) { - Text("Copy") - Image(systemName: "doc.on.doc") - } - Menu("Message Details") { - VStack { - let messageDate = Date(timeIntervalSince1970: TimeInterval(message.messageTimestamp)) - Text("Date \(messageDate, style: .date) \(messageDate.formattedDate(format: "h:mm:ss a"))").font(.caption2).foregroundColor(.gray) - } - if currentUser && message.receivedACK { - VStack { - Text("Received Ack \(message.receivedACK ? "✔️" : "")") - } - } else if currentUser && message.ackError == 0 { - // Empty Error - Text("Waiting. . .") - } else if currentUser && message.ackError > 0 { - let ackErrorVal = RoutingError(rawValue: Int(message.ackError)) - Text("\(ackErrorVal?.display ?? "No Error" )").fixedSize(horizontal: false, vertical: true) - } - if currentUser { - VStack { - let ackDate = Date(timeIntervalSince1970: TimeInterval(message.ackTimestamp)) - let sixMonthsAgo = Calendar.current.date(byAdding: .month, value: -6, to: Date()) - if ackDate >= sixMonthsAgo! { - Text((ackDate.formattedDate(format: "h:mm:ss a"))).font(.caption2).foregroundColor(.gray) - } else { - Text("Unknown Age").font(.caption2).foregroundColor(.gray) - } - } - } - if message.ackSNR != 0 { - VStack { - Text("Ack SNR \(String(message.ackSNR))") - .font(.caption2) - .foregroundColor(.gray) - } - } - } - Divider() - Button(role: .destructive, action: { - self.showDeleteMessageAlert = true - self.deleteMessageId = message.messageId - print(deleteMessageId) - }) { - Text("Delete") - Image(systemName: "trash") - } - } - - let tapbacks = message.value(forKey: "tapbacks") as! [MessageEntity] - if tapbacks.count > 0 { - VStack (alignment: .trailing) { - HStack { - ForEach( tapbacks ) { (tapback: MessageEntity) in - VStack { - let image = tapback.messagePayload!.image(fontSize: 20) - Image(uiImage: image!).font(.caption) - Text("\(tapback.fromUser?.shortName ?? "????")") - .font(.caption2) - .foregroundColor(.gray) - .fixedSize() - .padding(.bottom, 1) - } - } - } - .padding(10) - .overlay( - RoundedRectangle(cornerRadius: 18) - .stroke(Color.gray, lineWidth: 1) - ) } } - HStack { + Button(action: { + self.replyMessageId = message.messageId + self.focusedField = .messageText + print("I want to reply to \(message.messageId)") + }) { + Text("Reply") + Image(systemName: "arrowshape.turn.up.left.2.fill") + } + Button(action: { + UIPasteboard.general.string = message.messagePayload + }) { + Text("Copy") + Image(systemName: "doc.on.doc") + } + Menu("Message Details") { + VStack { + let messageDate = Date(timeIntervalSince1970: TimeInterval(message.messageTimestamp)) + Text("Date \(messageDate, style: .date) \(messageDate.formattedDate(format: "h:mm:ss a"))").font(.caption2).foregroundColor(.gray) + } if currentUser && message.receivedACK { - // Ack Received - Text("Acknowledged").font(.caption2).foregroundColor(.gray) + VStack { + Text("Received Ack \(message.receivedACK ? "✔️" : "")") + } } else if currentUser && message.ackError == 0 { // Empty Error - Text("Waiting to be acknowledged. . .").font(.caption2).foregroundColor(.orange) + Text("Waiting. . .") } else if currentUser && message.ackError > 0 { let ackErrorVal = RoutingError(rawValue: Int(message.ackError)) Text("\(ackErrorVal?.display ?? "No Error" )").fixedSize(horizontal: false, vertical: true) - .font(.caption2).foregroundColor(.red) + } + if currentUser { + VStack { + let ackDate = Date(timeIntervalSince1970: TimeInterval(message.ackTimestamp)) + let sixMonthsAgo = Calendar.current.date(byAdding: .month, value: -6, to: Date()) + if ackDate >= sixMonthsAgo! { + Text((ackDate.formattedDate(format: "h:mm:ss a"))).font(.caption2).foregroundColor(.gray) + } else { + Text("Unknown Age").font(.caption2).foregroundColor(.gray) + } + } + } + if message.ackSNR != 0 { + VStack { + Text("Ack SNR \(String(message.ackSNR))") + .font(.caption2) + .foregroundColor(.gray) + } } } + Divider() + Button(role: .destructive, action: { + self.showDeleteMessageAlert = true + self.deleteMessageId = message.messageId + print(deleteMessageId) + }) { + Text("Delete") + Image(systemName: "trash") + } } - .padding(.bottom) - .id(user.messageList.firstIndex(of: message)) - if !currentUser { - Spacer(minLength:50) + + let tapbacks = message.value(forKey: "tapbacks") as! [MessageEntity] + if tapbacks.count > 0 { + VStack (alignment: .trailing) { + HStack { + ForEach( tapbacks ) { (tapback: MessageEntity) in + VStack { + let image = tapback.messagePayload!.image(fontSize: 20) + Image(uiImage: image!).font(.caption) + Text("\(tapback.fromUser?.shortName ?? "????")") + .font(.caption2) + .foregroundColor(.gray) + .fixedSize() + .padding(.bottom, 1) + } + } + } + .padding(10) + .overlay( + RoundedRectangle(cornerRadius: 18) + .stroke(Color.gray, lineWidth: 1) + ) } } - .padding([.leading, .trailing]) - .frame(maxWidth: .infinity) - .id(message.messageId) - .alert(isPresented: $showDeleteMessageAlert) { - Alert(title: Text("Are you sure you want to delete this message?"), message: Text("This action is permanent."), primaryButton: .destructive(Text("Delete")) { - print("OK button tapped") - if deleteMessageId > 0 { - let message = user.messageList.first(where: { $0.messageId == deleteMessageId }) - context.delete(message!) - do { - try context.save() - deleteMessageId = 0 - } catch { - print("Failed to delete message \(deleteMessageId)") - } - } - }, secondaryButton: .cancel()) + HStack { + if currentUser && message.receivedACK { + // Ack Received + Text("Acknowledged").font(.caption2).foregroundColor(.gray) + } else if currentUser && message.ackError == 0 { + // Empty Error + Text("Waiting to be acknowledged. . .").font(.caption2).foregroundColor(.orange) + } else if currentUser && message.ackError > 0 { + let ackErrorVal = RoutingError(rawValue: Int(message.ackError)) + Text("\(ackErrorVal?.display ?? "No Error" )").fixedSize(horizontal: false, vertical: true) + .font(.caption2).foregroundColor(.red) + } } } + .padding(.bottom) + .id(channel.allPrivateMessages.firstIndex(of: message)) + if !currentUser { + Spacer(minLength:50) + } + } + .padding([.leading, .trailing]) + .frame(maxWidth: .infinity) + .id(message.messageId) + .alert(isPresented: $showDeleteMessageAlert) { + Alert(title: Text("Are you sure you want to delete this message?"), message: Text("This action is permanent."), primaryButton: .destructive(Text("Delete")) { + print("OK button tapped") + if deleteMessageId > 0 { + let message = channel.allPrivateMessages.first(where: { $0.messageId == deleteMessageId }) + context.delete(message!) + do { + try context.save() + deleteMessageId = 0 + } catch { + print("Failed to delete message \(deleteMessageId)") + } + } + }, secondaryButton: .cancel()) } } } @@ -219,14 +214,14 @@ struct ChannelMessageList: View { .onAppear(perform: { self.bleManager.context = context refreshId = UUID() - if user.messageList.count > 0 { - scrollView.scrollTo(user.messageList.last!.messageId) + if channel.allPrivateMessages.count > 0 { + scrollView.scrollTo(channel.allPrivateMessages.last!.messageId) } }) - .onChange(of: user.messageList, perform: { messages in + .onChange(of: channel.allPrivateMessages, perform: { messages in refreshId = UUID() - if user.messageList.count > 0 { - scrollView.scrollTo(user.messageList.last!.messageId) + if channel.allPrivateMessages.count > 0 { + scrollView.scrollTo(channel.allPrivateMessages.last!.messageId) } }) } @@ -259,27 +254,15 @@ struct ChannelMessageList: View { Button { let userLongName = bleManager.connectedPeripheral != nil ? bleManager.connectedPeripheral.longName : "Unknown" sendPositionWithMessage = true - if user.num == bleManager.broadcastNodeNum { + if userSettings.meshtasticUsername.count > 0 { - if userSettings.meshtasticUsername.count > 0 { - - typingMessage = "📍 " + userSettings.meshtasticUsername + " has shared their position with the mesh from node " + userLongName - } else { - - typingMessage = "📍 " + userLongName + " has shared their position with the mesh." - } + typingMessage = "📍 " + userSettings.meshtasticUsername + " has shared their position with you from node " + userLongName } else { - if userSettings.meshtasticUsername.count > 0 { - - typingMessage = "📍 " + userSettings.meshtasticUsername + " has shared their position with you from node " + userLongName - - } else { - - typingMessage = "📍 " + userLongName + " has shared their position with you." - } + typingMessage = "📍 " + userLongName + " has shared their position with you." } + } label: { Image(systemName: "mappin.and.ellipse") .symbolRenderingMode(.hierarchical) @@ -304,12 +287,12 @@ struct ChannelMessageList: View { .overlay(RoundedRectangle(cornerRadius: 20).stroke(.tertiary, lineWidth: 1)) .padding(.bottom, 15) Button(action: { - if bleManager.sendMessage(message: typingMessage, toUserNum: user.num, isEmoji: false, replyID: replyMessageId) { + if bleManager.sendMessage(message: typingMessage, toUserNum: Int64(channel.index), isEmoji: false, replyID: replyMessageId) { typingMessage = "" focusedField = nil replyMessageId = 0 if sendPositionWithMessage { - if bleManager.sendLocation(destNum: user.num, wantAck: true) { + if bleManager.sendLocation(destNum: Int64(channel.index), wantAck: true) { print("Location Sent") } } @@ -325,8 +308,8 @@ struct ChannelMessageList: View { .toolbar { ToolbarItem(placement: .principal) { HStack { - CircleText(text: user.shortName ?? "???", color: .blue, circleSize: 44, fontSize: 14).fixedSize() - Text(user.longName ?? "Unknown").font(.headline) + CircleText(text: String(channel.index), color: .blue, circleSize: 44, fontSize: 30).fixedSize() + Text(String(channel.name ?? "Unknown").camelCaseToWords()).font(.headline) } } ToolbarItem(placement: .navigationBarTrailing) { diff --git a/Meshtastic/Views/Messages/Contacts.swift b/Meshtastic/Views/Messages/Contacts.swift index 62273ebd..8f33f90d 100644 --- a/Meshtastic/Views/Messages/Contacts.swift +++ b/Meshtastic/Views/Messages/Contacts.swift @@ -39,7 +39,10 @@ struct Contacts: View { CircleText(text: String(channel.index), color: Color.blue, circleSize: 52, fontSize: 32) .padding(.trailing, 5) VStack { - Text(channel.name?.camelCaseToWords() ?? "Channel \(channel.index)").font(.headline) + NavigationLink(destination: ChannelMessageList(channel: channel)) { + + Text(channel.name?.camelCaseToWords() ?? "Channel \(channel.index)").font(.headline) + } } .frame(maxWidth: .infinity, alignment: .leading) } diff --git a/Meshtastic/Views/Messages/UserMessageList.swift b/Meshtastic/Views/Messages/UserMessageList.swift index 591a698c..3e13716c 100644 --- a/Meshtastic/Views/Messages/UserMessageList.swift +++ b/Meshtastic/Views/Messages/UserMessageList.swift @@ -35,180 +35,180 @@ struct UserMessageList: View { NavigationStack { ScrollViewReader { scrollView in ScrollView { - LazyVStack { + LazyVStack { ForEach( user.messageList ) { (message: MessageEntity) in if user.num != userSettings.preferredNodeNum { let currentUser: Bool = (userSettings.preferredNodeNum == message.fromUser?.num ? true : false) - if (user.num == bleManager.broadcastNodeNum || user.num != bleManager.broadcastNodeNum && message.toUser!.num != bleManager.broadcastNodeNum) { - if message.replyID > 0 { - let messageReply = user.messageList.first(where: { $0.messageId == message.replyID }) - HStack { - Text(messageReply?.messagePayload ?? "EMPTY MESSAGE").foregroundColor(.blue).font(.caption2) + + if message.replyID > 0 { + let messageReply = user.messageList.first(where: { $0.messageId == message.replyID }) + HStack { + Text(messageReply?.messagePayload ?? "EMPTY MESSAGE").foregroundColor(.blue).font(.caption2) + .padding(10) + .overlay( + RoundedRectangle(cornerRadius: 18) + .stroke(Color.blue, lineWidth: 0.5) + ) + Image(systemName: "arrowshape.turn.up.left.fill") + .symbolRenderingMode(.hierarchical) + .imageScale(.large).foregroundColor(.blue) + .padding(.trailing) + } + } + HStack (alignment: .top) { + if currentUser { Spacer(minLength:50) } + if !currentUser { + CircleText(text: message.fromUser?.shortName ?? "????", color: currentUser ? .accentColor : Color(.darkGray), circleSize: 44, fontSize: 14) + .padding(.all, 5) + .offset(y: -5) + } + VStack(alignment: currentUser ? .trailing : .leading) { + Text(message.messagePayload ?? "EMPTY MESSAGE") + .padding(10) + .foregroundColor(.white) + .background(currentUser ? Color.blue : Color(.darkGray)) + .cornerRadius(15) + .contextMenu { + VStack{ + Text("Channel: \(message.channel)") + } + Menu("Tapback response") { + ForEach(Tapbacks.allCases) { tb in + Button(action: { + if bleManager.sendMessage(message: tb.emojiString, toUserNum: user.num, isEmoji: true, replyID: message.messageId) { + print("Sent \(tb.emojiString) Tapback") + self.context.refresh(user, mergeChanges: true) + } else { print("\(tb.emojiString) Tapback Failed") } + + }) { + Text(tb.description) + let image = tb.emojiString.image() + Image(uiImage: image!) + } + } + } + Button(action: { + self.replyMessageId = message.messageId + self.focusedField = .messageText + print("I want to reply to \(message.messageId)") + }) { + Text("Reply") + Image(systemName: "arrowshape.turn.up.left.2.fill") + } + Button(action: { + UIPasteboard.general.string = message.messagePayload + }) { + Text("Copy") + Image(systemName: "doc.on.doc") + } + Menu("Message Details") { + VStack { + let messageDate = Date(timeIntervalSince1970: TimeInterval(message.messageTimestamp)) + Text("Date \(messageDate, style: .date) \(messageDate.formattedDate(format: "h:mm:ss a"))").font(.caption2).foregroundColor(.gray) + } + if currentUser && message.receivedACK { + VStack { + Text("Received Ack \(message.receivedACK ? "✔️" : "")") + } + } else if currentUser && message.ackError == 0 { + // Empty Error + Text("Waiting. . .") + } else if currentUser && message.ackError > 0 { + let ackErrorVal = RoutingError(rawValue: Int(message.ackError)) + Text("\(ackErrorVal?.display ?? "No Error" )").fixedSize(horizontal: false, vertical: true) + } + if currentUser { + VStack { + let ackDate = Date(timeIntervalSince1970: TimeInterval(message.ackTimestamp)) + let sixMonthsAgo = Calendar.current.date(byAdding: .month, value: -6, to: Date()) + if ackDate >= sixMonthsAgo! { + Text((ackDate.formattedDate(format: "h:mm:ss a"))).font(.caption2).foregroundColor(.gray) + } else { + Text("Unknown Age").font(.caption2).foregroundColor(.gray) + } + } + } + if message.ackSNR != 0 { + VStack { + Text("Ack SNR \(String(message.ackSNR))") + .font(.caption2) + .foregroundColor(.gray) + } + } + } + Divider() + Button(role: .destructive, action: { + self.showDeleteMessageAlert = true + self.deleteMessageId = message.messageId + print(deleteMessageId) + }) { + Text("Delete") + Image(systemName: "trash") + } + } + + let tapbacks = message.value(forKey: "tapbacks") as! [MessageEntity] + if tapbacks.count > 0 { + VStack (alignment: .trailing) { + HStack { + ForEach( tapbacks ) { (tapback: MessageEntity) in + VStack { + let image = tapback.messagePayload!.image(fontSize: 20) + Image(uiImage: image!).font(.caption) + Text("\(tapback.fromUser?.shortName ?? "????")") + .font(.caption2) + .foregroundColor(.gray) + .fixedSize() + .padding(.bottom, 1) + } + } + } .padding(10) .overlay( RoundedRectangle(cornerRadius: 18) - .stroke(Color.blue, lineWidth: 0.5) + .stroke(Color.gray, lineWidth: 1) ) - Image(systemName: "arrowshape.turn.up.left.fill") - .symbolRenderingMode(.hierarchical) - .imageScale(.large).foregroundColor(.blue) - .padding(.trailing) + } + } + HStack { + if currentUser && message.receivedACK { + // Ack Received + Text("Acknowledged").font(.caption2).foregroundColor(.gray) + } else if currentUser && message.ackError == 0 { + // Empty Error + Text("Waiting to be acknowledged. . .").font(.caption2).foregroundColor(.orange) + } else if currentUser && message.ackError > 0 { + let ackErrorVal = RoutingError(rawValue: Int(message.ackError)) + Text("\(ackErrorVal?.display ?? "No Error" )").fixedSize(horizontal: false, vertical: true) + .font(.caption2).foregroundColor(.red) + } } } - HStack (alignment: .top) { - if currentUser { Spacer(minLength:50) } - if !currentUser { - CircleText(text: message.fromUser?.shortName ?? "????", color: currentUser ? .accentColor : Color(.darkGray), circleSize: 44, fontSize: 14) - .padding(.all, 5) - .offset(y: -5) - } - VStack(alignment: currentUser ? .trailing : .leading) { - Text(message.messagePayload ?? "EMPTY MESSAGE") - .padding(10) - .foregroundColor(.white) - .background(currentUser ? Color.blue : Color(.darkGray)) - .cornerRadius(15) - .contextMenu { - VStack{ - Text("Channel: \(message.channel)") - } - Menu("Tapback response") { - ForEach(Tapbacks.allCases) { tb in - Button(action: { - if bleManager.sendMessage(message: tb.emojiString, toUserNum: user.num, isEmoji: true, replyID: message.messageId) { - print("Sent \(tb.emojiString) Tapback") - self.context.refresh(user, mergeChanges: true) - } else { print("\(tb.emojiString) Tapback Failed") } - - }) { - Text(tb.description) - let image = tb.emojiString.image() - Image(uiImage: image!) - } - } - } - Button(action: { - self.replyMessageId = message.messageId - self.focusedField = .messageText - print("I want to reply to \(message.messageId)") - }) { - Text("Reply") - Image(systemName: "arrowshape.turn.up.left.2.fill") - } - Button(action: { - UIPasteboard.general.string = message.messagePayload - }) { - Text("Copy") - Image(systemName: "doc.on.doc") - } - Menu("Message Details") { - VStack { - let messageDate = Date(timeIntervalSince1970: TimeInterval(message.messageTimestamp)) - Text("Date \(messageDate, style: .date) \(messageDate.formattedDate(format: "h:mm:ss a"))").font(.caption2).foregroundColor(.gray) - } - if currentUser && message.receivedACK { - VStack { - Text("Received Ack \(message.receivedACK ? "✔️" : "")") - } - } else if currentUser && message.ackError == 0 { - // Empty Error - Text("Waiting. . .") - } else if currentUser && message.ackError > 0 { - let ackErrorVal = RoutingError(rawValue: Int(message.ackError)) - Text("\(ackErrorVal?.display ?? "No Error" )").fixedSize(horizontal: false, vertical: true) - } - if currentUser { - VStack { - let ackDate = Date(timeIntervalSince1970: TimeInterval(message.ackTimestamp)) - let sixMonthsAgo = Calendar.current.date(byAdding: .month, value: -6, to: Date()) - if ackDate >= sixMonthsAgo! { - Text((ackDate.formattedDate(format: "h:mm:ss a"))).font(.caption2).foregroundColor(.gray) - } else { - Text("Unknown Age").font(.caption2).foregroundColor(.gray) - } - } - } - if message.ackSNR != 0 { - VStack { - Text("Ack SNR \(String(message.ackSNR))") - .font(.caption2) - .foregroundColor(.gray) - } - } - } - Divider() - Button(role: .destructive, action: { - self.showDeleteMessageAlert = true - self.deleteMessageId = message.messageId - print(deleteMessageId) - }) { - Text("Delete") - Image(systemName: "trash") - } - } - - let tapbacks = message.value(forKey: "tapbacks") as! [MessageEntity] - if tapbacks.count > 0 { - VStack (alignment: .trailing) { - HStack { - ForEach( tapbacks ) { (tapback: MessageEntity) in - VStack { - let image = tapback.messagePayload!.image(fontSize: 20) - Image(uiImage: image!).font(.caption) - Text("\(tapback.fromUser?.shortName ?? "????")") - .font(.caption2) - .foregroundColor(.gray) - .fixedSize() - .padding(.bottom, 1) - } - } - } - .padding(10) - .overlay( - RoundedRectangle(cornerRadius: 18) - .stroke(Color.gray, lineWidth: 1) - ) - } - } - HStack { - if currentUser && message.receivedACK { - // Ack Received - Text("Acknowledged").font(.caption2).foregroundColor(.gray) - } else if currentUser && message.ackError == 0 { - // Empty Error - Text("Waiting to be acknowledged. . .").font(.caption2).foregroundColor(.orange) - } else if currentUser && message.ackError > 0 { - let ackErrorVal = RoutingError(rawValue: Int(message.ackError)) - Text("\(ackErrorVal?.display ?? "No Error" )").fixedSize(horizontal: false, vertical: true) - .font(.caption2).foregroundColor(.red) - } - } - } - .padding(.bottom) - .id(user.messageList.firstIndex(of: message)) - if !currentUser { - Spacer(minLength:50) - } - } - .padding([.leading, .trailing]) - .frame(maxWidth: .infinity) - .id(message.messageId) - .alert(isPresented: $showDeleteMessageAlert) { - Alert(title: Text("Are you sure you want to delete this message?"), message: Text("This action is permanent."), primaryButton: .destructive(Text("Delete")) { - print("OK button tapped") - if deleteMessageId > 0 { - let message = user.messageList.first(where: { $0.messageId == deleteMessageId }) - context.delete(message!) - do { - try context.save() - deleteMessageId = 0 - } catch { - print("Failed to delete message \(deleteMessageId)") - } - } - }, secondaryButton: .cancel()) + .padding(.bottom) + .id(user.messageList.firstIndex(of: message)) + if !currentUser { + Spacer(minLength:50) } } + .padding([.leading, .trailing]) + .frame(maxWidth: .infinity) + .id(message.messageId) + .alert(isPresented: $showDeleteMessageAlert) { + Alert(title: Text("Are you sure you want to delete this message?"), message: Text("This action is permanent."), primaryButton: .destructive(Text("Delete")) { + print("OK button tapped") + if deleteMessageId > 0 { + let message = user.messageList.first(where: { $0.messageId == deleteMessageId }) + context.delete(message!) + do { + try context.save() + deleteMessageId = 0 + } catch { + print("Failed to delete message \(deleteMessageId)") + } + } + }, secondaryButton: .cancel()) + } + } } }