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

159 lines
5.1 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 SwiftUI
import CoreData
2024-06-03 02:17:55 -07:00
import OSLog
2022-01-01 08:03:46 -08:00
2022-11-05 08:26:27 -07:00
struct UserMessageList: View {
2023-09-03 09:04:07 -07:00
@StateObject var appState = AppState.shared
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
// View State Items
2023-09-04 06:31:38 -07:00
@ObservedObject var user: UserEntity
2022-01-01 22:55:25 -08:00
@State private var replyMessageId: Int64 = 0
2023-03-06 10:33:18 -08:00
2022-12-24 22:06:28 -08: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 {
2022-11-07 18:31:12 -08:00
LazyVStack {
ForEach( user.messageList ) { (message: MessageEntity) in
2023-03-04 08:52:40 -08:00
if user.num != bleManager.connectedPeripheral?.num ?? -1 {
let currentUser: Bool = (Int64(UserDefaults.preferredPeripheralNum) == message.fromUser?.num ?? -1 ? true : false)
2023-03-06 10:33:18 -08:00
2022-11-07 18:31:12 -08:00
if message.replyID > 0 {
let messageReply = user.messageList.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)
2022-01-05 08:31:30 -08:00
}
2022-11-07 18:31:12 -08:00
}
2023-03-06 10:33:18 -08:00
HStack(alignment: .top) {
if currentUser { Spacer(minLength: 50) }
2022-11-07 18:31:12 -08:00
VStack(alignment: currentUser ? .trailing : .leading) {
HStack {
MessageText(
message: message,
tapBackDestination: .user(user),
isCurrentUser: currentUser
) {
self.replyMessageId = message.messageId
self.messageFieldFocused = true
}
if currentUser && message.canRetry || (message.receivedACK && !message.realACK) {
RetryButton(message: message, destination: .user(user))
}
2024-02-17 13:26:09 -07:00
}
2023-03-06 10:33:18 -08:00
2024-02-17 13:51:17 -07:00
TapbackResponses(message: message) {
appState.unreadDirectMessages = user.unreadMessages
UIApplication.shared.applicationIconBadgeNumber = appState.unreadChannelMessages + appState.unreadDirectMessages
2022-01-01 08:03:46 -08:00
}
2024-02-17 13:51:17 -07:00
2022-11-07 18:31:12 -08:00
HStack {
let ackErrorVal = RoutingError(rawValue: Int(message.ackError))
2022-11-07 18:31:12 -08:00
if currentUser && message.receivedACK {
// Ack Received
if message.realACK {
Text("\(ackErrorVal?.display ?? "Empty Ack Error")").font(.caption2).foregroundColor(.gray)
} else {
2024-04-10 20:06:01 -07:00
Text("Acknowledged by another node").font(.caption2).foregroundColor(.orange)
}
2022-11-07 18:31:12 -08:00
} else if currentUser && message.ackError == 0 {
// Empty Error
2024-04-10 20:06:01 -07:00
Text("Waiting to be acknowledged. . .").font(.caption2).foregroundColor(.yellow)
2022-11-07 18:31:12 -08:00
} else if currentUser && message.ackError > 0 {
2022-12-30 17:24:01 -08:00
Text("\(ackErrorVal?.display ?? "Empty Ack Error")").fixedSize(horizontal: false, vertical: true)
2022-11-07 18:31:12 -08:00
.font(.caption2).foregroundColor(.red)
}
2022-10-17 18:52:49 -07:00
}
2022-01-01 15:45:00 -08:00
}
2022-11-07 18:31:12 -08:00
.padding(.bottom)
.id(user.messageList.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-10-15 01:00:13 -07:00
}
2022-01-01 08:03:46 -08:00
}
2022-11-07 18:31:12 -08:00
.padding([.leading, .trailing])
.frame(maxWidth: .infinity)
.id(message.messageId)
2023-08-29 07:53:52 -07:00
.onAppear {
if !message.read {
2023-08-29 08:03:01 -07:00
message.read = true
do {
try context.save()
2024-06-03 02:17:55 -07:00
Logger.data.info("📖 Read message \(message.messageId) ")
2023-09-03 09:04:07 -07:00
appState.unreadDirectMessages = user.unreadMessages
UIApplication.shared.applicationIconBadgeNumber = appState.unreadChannelMessages + appState.unreadDirectMessages
2023-09-03 09:04:07 -07:00
2023-08-29 08:03:01 -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 08:03:01 -07:00
}
2023-08-29 07:53:52 -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)
2023-09-25 09:44:57 -07:00
.onAppear {
if self.bleManager.context == nil {
self.bleManager.context = context
}
2022-10-17 18:52:49 -07:00
if user.messageList.count > 0 {
scrollView.scrollTo(user.messageList.last!.messageId)
}
2023-09-25 09:44:57 -07:00
}
2023-03-06 10:33:18 -08:00
.onChange(of: user.messageList, perform: { _ in
2022-10-17 18:52:49 -07:00
if user.messageList.count > 0 {
scrollView.scrollTo(user.messageList.last!.messageId)
}
2022-01-02 10:05:13 -08:00
})
2022-01-01 15:45:00 -08:00
}
2023-03-06 10:33:18 -08:00
TextMessageField(
2024-02-17 13:26:09 -07:00
destination: .user(user),
replyMessageId: $replyMessageId,
isFocused: $messageFieldFocused
) {
context.refresh(user, 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 {
2023-09-02 18:02:51 -07:00
CircleText(text: user.shortName ?? "?", color: Color(UIColor(hex: UInt32(user.num))), circleSize: 44)
2022-01-01 08:03:46 -08:00
}
}
ToolbarItem(placement: .navigationBarTrailing) {
ZStack {
ConnectedDevice(
bluetoothOn: bleManager.isSwitchedOn,
deviceConnected: bleManager.connectedPeripheral != nil,
2023-09-02 17:37:35 -07:00
name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "?")
2022-01-01 08:03:46 -08:00
}
}
}
2022-12-24 22:06:28 -08:00
}
2022-01-01 08:03:46 -08:00
}