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

162 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-03-06 10:33:18 -08:00
@EnvironmentObject var appState: AppState
@EnvironmentObject var bleManager: BLEManager
@Environment(\.managedObjectContext) var context
2024-08-04 21:42:02 -07: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
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 {
2024-08-14 14:11:08 -07:00
Text("\(ackErrorVal?.display ?? "Empty Ack Error")")
.font(.caption2)
.foregroundStyle(ackErrorVal?.color ?? Color.secondary)
} 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)
2024-08-14 14:11:08 -07:00
.foregroundStyle(ackErrorVal?.color ?? Color.red)
2024-08-29 08:50:31 -07:00
.font(.caption2)
2022-11-07 18:31:12 -08:00
}
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-24 07:24:24 -07:00
Logger.data.info("📖 [App] Read message \(message.messageId) ")
2023-09-03 09:04:07 -07:00
appState.unreadDirectMessages = user.unreadMessages
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
}
.scrollDismissesKeyboard(.interactively)
2024-08-23 19:28:13 -07:00
.onFirstAppear {
withAnimation {
scrollView.scrollTo(user.messageList.last?.messageId ?? 0, anchor: .bottom)
}
2023-09-25 09:44:57 -07:00
}
.onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardDidShowNotification)) { _ in
withAnimation {
scrollView.scrollTo(user.messageList.last?.messageId ?? 0, anchor: .bottom)
}
}
2024-10-05 15:50:57 -07:00
.onChange(of: user.messageList) {
2024-08-23 19:28:13 -07:00
withAnimation {
scrollView.scrollTo(user.messageList.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
}
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
}