2025-08-24 17:58:37 -07:00
|
|
|
//
|
|
|
|
|
// MessageEntryView.swift
|
|
|
|
|
// kordophone2
|
|
|
|
|
//
|
|
|
|
|
// Created by James Magahern on 8/24/25.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
|
|
struct MessageEntryView: View
|
|
|
|
|
{
|
|
|
|
|
@Binding var viewModel: ViewModel
|
|
|
|
|
@Environment(\.selectedConversation) private var selectedConversation
|
|
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
|
VStack(spacing: 0.0) {
|
|
|
|
|
Rectangle()
|
|
|
|
|
.fill(.separator)
|
|
|
|
|
.frame(height: 1.0)
|
|
|
|
|
|
|
|
|
|
HStack {
|
|
|
|
|
TextField("", text: $viewModel.draftText, axis: .vertical)
|
|
|
|
|
.focusEffectDisabled(true)
|
|
|
|
|
.lineLimit(nil)
|
|
|
|
|
.scrollContentBackground(.hidden)
|
|
|
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
|
|
|
.font(.body)
|
|
|
|
|
.scrollDisabled(true)
|
|
|
|
|
.padding(8.0)
|
|
|
|
|
.background {
|
|
|
|
|
RoundedRectangle(cornerRadius: 6.0)
|
|
|
|
|
.fill(.background)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Button("Send") {
|
|
|
|
|
viewModel.sendDraft(to: selectedConversation)
|
|
|
|
|
}
|
|
|
|
|
.disabled(viewModel.draftText.isEmpty)
|
|
|
|
|
.keyboardShortcut(.defaultAction)
|
|
|
|
|
}
|
|
|
|
|
.padding(10.0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Types
|
|
|
|
|
|
|
|
|
|
@Observable
|
|
|
|
|
class ViewModel
|
|
|
|
|
{
|
|
|
|
|
var draftText: String = ""
|
|
|
|
|
|
|
|
|
|
func sendDraft(to convo: Display.Conversation?) {
|
|
|
|
|
guard let convo else { return }
|
|
|
|
|
guard !draftText.isEmpty else { return }
|
|
|
|
|
|
|
|
|
|
let messageText = self.draftText
|
2025-08-24 18:41:42 -07:00
|
|
|
.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
|
|
|
2025-08-24 17:58:37 -07:00
|
|
|
self.draftText = ""
|
|
|
|
|
|
|
|
|
|
Task {
|
|
|
|
|
let xpc = XPCClient()
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
try await xpc.sendMessage(conversationId: convo.id, message: messageText)
|
|
|
|
|
} catch {
|
|
|
|
|
print("Sending error: \(error)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#Preview {
|
|
|
|
|
@Previewable @State var model = MessageEntryView.ViewModel()
|
|
|
|
|
|
|
|
|
|
VStack {
|
|
|
|
|
Spacer()
|
|
|
|
|
MessageEntryView(viewModel: $model)
|
|
|
|
|
}
|
|
|
|
|
}
|