Private
Public Access
1
0
Files
Kordophone/kordophone2/App.swift

77 lines
2.4 KiB
Swift
Raw Normal View History

2025-08-24 16:24:21 -07:00
//
// kordophone2App.swift
// kordophone2
//
// Created by James Magahern on 8/24/25.
//
import SwiftUI
@main
struct KordophoneApp: App
{
@State var conversationListModel = ConversationListView.ViewModel()
2025-08-24 17:58:37 -07:00
@State var transcriptViewModel = ChatTranscriptView.ViewModel()
@State var entryViewModel = MessageEntryView.ViewModel()
2025-08-24 16:24:21 -07:00
private let xpcClient = XPCClient()
2025-08-24 17:58:37 -07:00
private var selectedConversation: Display.Conversation? {
guard let id = conversationListModel.selectedConversations.first else { return nil }
return conversationListModel.conversations.first { $0.id == id }
}
2025-08-24 16:24:21 -07:00
var body: some Scene {
WindowGroup {
NavigationSplitView {
ConversationListView(model: $conversationListModel)
.frame(minWidth: 330.0)
2025-08-24 17:58:37 -07:00
.xpcClient(xpcClient)
2025-08-24 16:24:21 -07:00
.task {
await refreshConversations()
}
} detail: {
2025-08-24 17:58:37 -07:00
ConversationView(transcriptModel: $transcriptViewModel, entryModel: $entryViewModel)
.xpcClient(xpcClient)
.selectedConversation(selectedConversation)
.navigationTitle("Kordophone")
.navigationSubtitle(selectedConversation?.displayName ?? "")
.onChange(of: conversationListModel.selectedConversations) { oldValue, newValue in
transcriptViewModel.displayedConversation = newValue.first
}
2025-08-24 16:24:21 -07:00
}
}
}
private func refreshConversations() async {
do {
let conversations = try await xpcClient.getConversations()
conversationListModel.conversations = conversations.map { Display.Conversation(from: $0) }
} catch {
reportError(error)
}
}
private func reportError(_ e: Error) {
// Just printing for now.
print("Error: \(e.localizedDescription)")
}
}
2025-08-24 17:58:37 -07:00
extension EnvironmentValues
{
@Entry var xpcClient: XPCClient = XPCClient()
@Entry var selectedConversation: Display.Conversation? = nil
}
extension View
{
func xpcClient(_ client: XPCClient) -> some View {
environment(\.xpcClient, client)
}
func selectedConversation(_ convo: Display.Conversation?) -> some View {
environment(\.selectedConversation, convo)
}
}