Meshtastic-Apple/Meshtastic/Views/Messages/ChannelMessageList.swift

178 lines
5.9 KiB
Swift
Raw Normal View History

//
2022-01-01 08:03:46 -08:00
// UserMessageList.swift
// MeshtasticApple
2022-01-01 08:03:46 -08:00
//
// Created by Garth Vander Houwen on 12/24/21.
//
import CoreData
import MeshtasticProtobufs
2024-06-03 02:17:55 -07:00
import OSLog
import SwiftUI
2022-01-01 08:03:46 -08:00
2022-11-05 08:26:27 -07:00
struct ChannelMessageList: View {
@EnvironmentObject var appState: AppState
2022-01-01 08:03:46 -08:00
@Environment(\.managedObjectContext) var context
@EnvironmentObject var bleManager: BLEManager
2023-03-06 10:33:18 -08:00
2022-01-01 08:03:46 -08:00
// Keyboard State
@FocusState var messageFieldFocused: Bool
2023-03-06 10:33:18 -08:00
2023-12-20 19:34:35 -08:00
@ObservedObject var myInfo: MyInfoEntity
@ObservedObject var channel: ChannelEntity
2022-01-01 22:55:25 -08:00
@State private var replyMessageId: Int64 = 0
@AppStorage("preferredPeripheralNum") private var preferredPeripheralNum = -1
2023-03-06 10:33:18 -08:00
2022-11-05 08:26:27 -07:00
var body: some View {
2023-09-16 08:48:36 -07:00
VStack {
2022-01-01 15:45:00 -08:00
ScrollViewReader { scrollView in
ScrollView {
LazyVStack {
2022-11-07 18:31:12 -08:00
ForEach( channel.allPrivateMessages ) { (message: MessageEntity) in
let currentUser: Bool = (Int64(preferredPeripheralNum) == message.fromUser?.num ? true : false)
2022-11-07 18:31:12 -08:00
if message.replyID > 0 {
let messageReply = channel.allPrivateMessages.first(where: { $0.messageId == message.replyID })
HStack {
Text(messageReply?.messagePayload ?? "EMPTY MESSAGE").foregroundColor(.accentColor).font(.caption2)
2022-11-07 18:31:12 -08:00
.padding(10)
.overlay(
RoundedRectangle(cornerRadius: 18)
.stroke(Color.blue, lineWidth: 0.5)
)
Image(systemName: "arrowshape.turn.up.left.fill")
.symbolRenderingMode(.hierarchical)
.imageScale(.large).foregroundColor(.accentColor)
2022-11-07 18:31:12 -08:00
.padding(.trailing)
}
}
HStack(alignment: .bottom) {
2023-03-06 10:33:18 -08:00
if currentUser { Spacer(minLength: 50) }
2022-11-07 18:31:12 -08:00
if !currentUser {
2023-09-02 18:02:51 -07:00
CircleText(text: message.fromUser?.shortName ?? "?", color: Color(UIColor(hex: UInt32(message.fromUser?.num ?? 0))), circleSize: 44)
2022-11-07 18:31:12 -08:00
.padding(.all, 5)
2024-04-26 18:06:23 -07:00
.offset(y: -7)
2022-11-07 18:31:12 -08:00
}
2022-11-07 18:31:12 -08:00
VStack(alignment: currentUser ? .trailing : .leading) {
let isDetectionSensorMessage = message.portNum == Int32(PortNum.detectionSensorApp.rawValue)
if !currentUser && message.fromUser != nil {
2024-04-26 15:49:38 -07:00
Text("\(message.fromUser?.longName ?? "unknown".localized ) (\(message.fromUser?.userId ?? "?"))")
.font(.caption)
.foregroundColor(.gray)
.offset(y: 8)
}
HStack {
MessageText(
message: message,
tapBackDestination: .channel(channel),
isCurrentUser: currentUser
) {
self.replyMessageId = message.messageId
self.messageFieldFocused = true
}
if currentUser && message.canRetry {
RetryButton(message: message, destination: .channel(channel))
}
2024-02-17 13:26:09 -07:00
}
2024-02-17 13:51:17 -07:00
TapbackResponses(message: message) {
appState.unreadChannelMessages = myInfo.unreadMessages
context.refresh(myInfo, mergeChanges: true)
2022-11-07 18:31:12 -08:00
}
2022-11-07 18:31:12 -08:00
HStack {
2024-08-20 18:50:28 -07:00
let ackErrorVal = RoutingError(rawValue: Int(message.ackError))
if currentUser && message.receivedACK {
2022-12-30 17:24:01 -08:00
Text("\(ackErrorVal?.display ?? "Empty Ack Error")").fixedSize(horizontal: false, vertical: true)
2024-08-20 18:50:28 -07:00
.foregroundStyle(ackErrorVal?.color ?? Color.red)
2024-08-14 14:11:08 -07:00
.font(.caption2)
2024-08-20 18:50:28 -07:00
} else if currentUser && message.ackError == 0 {
// Empty Error
Text("Waiting to be acknowledged. . .").font(
.caption2)
.foregroundColor(.orange)
2024-08-23 20:25:58 -07:00
} else if currentUser && !isDetectionSensorMessage {
Text("\(ackErrorVal?.display ?? "Empty Ack Error")").fixedSize(horizontal: false, vertical: true)
.foregroundStyle(ackErrorVal?.color ?? Color.red)
.font(.caption2)
2022-11-07 18:31:12 -08:00
}
2022-10-15 01:00:13 -07:00
}
2022-01-01 08:03:46 -08:00
}
2022-11-07 18:31:12 -08:00
.padding(.bottom)
.id(channel.allPrivateMessages.firstIndex(of: message))
2024-02-05 09:28:47 -07:00
2022-11-07 18:31:12 -08:00
if !currentUser {
2023-03-06 10:33:18 -08:00
Spacer(minLength: 50)
2022-11-07 18:31:12 -08:00
}
}
.padding([.leading, .trailing])
.frame(maxWidth: .infinity)
.id(message.messageId)
2023-08-29 18:51:57 -07:00
.onAppear {
if !message.read {
message.read = true
do {
try context.save()
2024-06-24 07:24:24 -07:00
Logger.data.info("📖 [App] Read message \(message.messageId) ")
2023-09-03 16:47:35 -07:00
appState.unreadChannelMessages = myInfo.unreadMessages
context.refresh(myInfo, mergeChanges: true)
2023-08-29 18:51:57 -07:00
} catch {
2024-06-03 02:17:55 -07:00
Logger.data.error("Failed to read message \(message.messageId): \(error.localizedDescription)")
2023-08-29 18:51:57 -07:00
}
}
}
2022-01-01 08:03:46 -08:00
}
}
2022-01-01 15:45:00 -08:00
}
2022-10-15 01:34:50 -07:00
.padding([.top])
2022-09-17 12:41:27 -07:00
.scrollDismissesKeyboard(.immediately)
2024-08-23 19:28:13 -07:00
.onFirstAppear {
withAnimation {
scrollView.scrollTo(channel.allPrivateMessages.last?.messageId ?? 0, anchor: .bottom)
}
}
2024-10-05 15:50:57 -07:00
.onChange(of: channel.allPrivateMessages) {
2024-08-23 19:28:13 -07:00
withAnimation {
scrollView.scrollTo(channel.allPrivateMessages.last?.messageId ?? 0, anchor: .bottom)
2022-10-17 18:52:49 -07:00
}
2024-10-05 15:50:57 -07:00
}
2022-01-01 15:45:00 -08:00
}
TextMessageField(
2024-02-17 13:26:09 -07:00
destination: .channel(channel),
replyMessageId: $replyMessageId,
isFocused: $messageFieldFocused
) {
context.refresh(channel, mergeChanges: true)
2022-01-01 08:03:46 -08:00
}
2022-01-01 15:45:00 -08:00
}
2022-01-01 08:03:46 -08:00
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .principal) {
HStack {
CircleText(text: String(channel.index), color: .accentColor, circleSize: 44).fixedSize()
Text(String(channel.name ?? "unknown".localized).camelCaseToWords()).font(.headline)
2022-01-01 08:03:46 -08:00
}
}
ToolbarItem(placement: .navigationBarTrailing) {
ZStack {
ConnectedDevice(
bluetoothOn: bleManager.isSwitchedOn,
deviceConnected: bleManager.connectedPeripheral != nil,
name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "?",
// mqttProxyConnected defaults to false, so if it's not enabled it will still be false
mqttProxyConnected: bleManager.mqttProxyConnected && (channel.uplinkEnabled || channel.downlinkEnabled),
mqttUplinkEnabled: channel.uplinkEnabled,
mqttDownlinkEnabled: channel.downlinkEnabled,
mqttTopic: bleManager.mqttManager.topic
)
2022-01-01 08:03:46 -08:00
}
}
}
2022-11-05 08:26:27 -07:00
}
2022-01-01 08:03:46 -08:00
}