2021-12-24 23:41:18 -08:00
|
|
|
//
|
2022-01-01 08:03:46 -08:00
|
|
|
// UserMessageList.swift
|
2022-06-09 22:11:54 -07:00
|
|
|
// 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
|
|
|
|
2024-07-10 21:17:14 -05: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
|
2024-02-05 23:31:54 -07:00
|
|
|
@FocusState var messageFieldFocused: Bool
|
2022-11-11 16:22:50 -08:00
|
|
|
// 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 {
|
2022-06-04 07:41:52 -07:00
|
|
|
ForEach( user.messageList ) { (message: MessageEntity) in
|
2023-03-04 08:52:40 -08:00
|
|
|
if user.num != bleManager.connectedPeripheral?.num ?? -1 {
|
2023-11-20 18:15:14 -08:00
|
|
|
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 {
|
2022-11-17 17:05:14 -08:00
|
|
|
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)
|
2022-11-17 17:05:14 -08:00
|
|
|
.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) {
|
2024-02-17 13:59:42 -07:00
|
|
|
HStack {
|
|
|
|
|
MessageText(
|
|
|
|
|
message: message,
|
|
|
|
|
tapBackDestination: .user(user),
|
|
|
|
|
isCurrentUser: currentUser
|
|
|
|
|
) {
|
|
|
|
|
self.replyMessageId = message.messageId
|
|
|
|
|
self.messageFieldFocused = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if currentUser && message.canRetry || (message.receivedACK && !message.realACK) {
|
2024-02-21 00:28:55 -07:00
|
|
|
RetryButton(message: message, destination: .user(user))
|
2024-02-17 13:59:42 -07:00
|
|
|
}
|
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 {
|
2022-12-13 17:47:23 -08:00
|
|
|
let ackErrorVal = RoutingError(rawValue: Int(message.ackError))
|
2022-11-07 18:31:12 -08:00
|
|
|
if currentUser && message.receivedACK {
|
|
|
|
|
// Ack Received
|
2023-03-13 19:17:43 -07:00
|
|
|
if message.realACK {
|
2024-08-14 14:11:08 -07:00
|
|
|
Text("\(ackErrorVal?.display ?? "Empty Ack Error")")
|
|
|
|
|
.font(.caption2)
|
|
|
|
|
.foregroundStyle(ackErrorVal?.color ?? Color.secondary)
|
2023-03-13 19:17:43 -07:00
|
|
|
} else {
|
2024-04-10 20:06:01 -07:00
|
|
|
Text("Acknowledged by another node").font(.caption2).foregroundColor(.orange)
|
2023-03-13 19:17:43 -07:00
|
|
|
}
|
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()
|
2025-03-31 22:06:00 -07:00
|
|
|
Logger.data.info("📖 [App] Read message \(message.messageId, privacy: .public) ")
|
2023-09-03 09:04:07 -07:00
|
|
|
appState.unreadDirectMessages = user.unreadMessages
|
|
|
|
|
|
2023-08-29 08:03:01 -07:00
|
|
|
} catch {
|
2025-03-31 22:06:00 -07:00
|
|
|
Logger.data.error("Failed to read message \(message.messageId, privacy: .public): \(error.localizedDescription, privacy: .public)")
|
2023-08-29 08:03:01 -07:00
|
|
|
}
|
2023-08-29 07:53:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-01-01 21:44:47 -08:00
|
|
|
}
|
2022-01-01 08:03:46 -08:00
|
|
|
}
|
|
|
|
|
}
|
2022-01-01 15:45:00 -08:00
|
|
|
}
|
2025-02-06 20:01:26 -05:00
|
|
|
.scrollDismissesKeyboard(.interactively)
|
2024-08-23 19:28:13 -07:00
|
|
|
.onFirstAppear {
|
|
|
|
|
withAnimation {
|
|
|
|
|
scrollView.scrollTo(user.messageList.last?.messageId ?? 0, anchor: .bottom)
|
2022-10-06 08:56:15 -07:00
|
|
|
}
|
2023-09-25 09:44:57 -07:00
|
|
|
}
|
2025-02-06 20:01:26 -05: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
|
|
|
|
2024-02-05 23:31:54 -07:00
|
|
|
TextMessageField(
|
2024-02-17 13:26:09 -07:00
|
|
|
destination: .user(user),
|
2024-02-05 23:31:54 -07:00
|
|
|
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
|
|
|
}
|