Private
Public Access
1
0
Files
Kordophone/kordophone2/Transcript/TranscriptView.swift

181 lines
5.5 KiB
Swift
Raw Normal View History

2025-08-24 23:38:35 -07:00
//
// TranscriptView.swift
// kordophone2
//
// Created by James Magahern on 8/24/25.
//
import SwiftUI
struct TranscriptView: View
{
@Binding var model: ViewModel
@Environment(\.xpcClient) private var xpcClient
var body: some View {
ScrollView {
LazyVStack(spacing: 17.0) {
ForEach($model.displayItems.reversed()) { item in
displayItemView(item.wrappedValue)
.id(item.id)
.scaleEffect(CGSize(width: 1.0, height: -1.0))
.transition(
.push(from: .bottom)
.combined(with: .opacity)
)
}
}
.padding()
}
.scaleEffect(CGSize(width: 1.0, height: -1.0))
2025-08-29 19:45:27 -06:00
.id(model.displayedConversation)
2025-08-24 23:38:35 -07:00
.task { await watchForMessageListChanges() }
}
private func watchForMessageListChanges() async {
for await event in xpcClient.eventStream() {
switch event {
case .attachmentDownloaded(let attachmentId):
model.attachmentDownloaded(id: attachmentId)
case .messagesUpdated(let conversationId):
if conversationId == model.displayedConversation {
model.setNeedsReload(animated: true)
}
case .updateStreamReconnected:
2025-08-25 00:13:55 -07:00
await model.triggerSync()
2025-08-24 23:38:35 -07:00
default:
break
}
}
}
@ViewBuilder
private func displayItemView(_ item: DisplayItem) -> some View {
switch item {
case .message(let message):
TextBubbleItemView(text: message.text, isFromMe: message.isFromMe)
case .date(let date):
DateItemView(date: date)
case .attachment(let attachment):
if attachment.isPreviewDownloaded {
ImageItemView(
isFromMe: attachment.sender.isMe,
attachment: attachment
)
} else {
PlaceholderImageItemView(
isFromMe: attachment.sender.isMe,
size: attachment.size
)
}
}
}
// MARK: - Types
@Observable
class ViewModel
{
var displayItems: [DisplayItem] = []
var displayedConversation: Display.Conversation.ID? = nil
2025-08-25 00:13:55 -07:00
internal var needsReload: NeedsReload = .no
2025-08-24 23:38:35 -07:00
internal var messages: [Display.Message]
2025-08-25 00:13:55 -07:00
internal let client = XPCClient()
2025-08-24 23:38:35 -07:00
init(messages: [Display.Message] = []) {
self.messages = messages
observeDisplayedConversation()
rebuildDisplayItems()
}
func setNeedsReload(animated: Bool) {
2025-08-25 00:13:55 -07:00
guard case .no = needsReload else {
return
}
2025-08-24 23:38:35 -07:00
needsReload = .yes(animated)
Task { @MainActor [weak self] in
guard let self else { return }
await reloadMessages()
}
}
func attachmentDownloaded(id: String) {
// TODO: should be smarter here
2025-08-29 19:45:27 -06:00
setNeedsReload(animated: false)
2025-08-24 23:38:35 -07:00
}
private func observeDisplayedConversation() {
withObservationTracking {
_ = displayedConversation
} onChange: {
Task { @MainActor [weak self] in
guard let self else { return }
2025-08-25 00:37:48 -07:00
await markAsRead()
2025-08-25 00:13:55 -07:00
await triggerSync()
2025-08-24 23:38:35 -07:00
setNeedsReload(animated: false)
observeDisplayedConversation()
}
}
}
2025-08-25 00:37:48 -07:00
func markAsRead() async {
guard let displayedConversation else { return }
do {
try await client.markConversationAsRead(conversationId: displayedConversation)
} catch {
print("Error triggering sync: \(error)")
}
}
2025-08-25 00:13:55 -07:00
func triggerSync() async {
guard let displayedConversation else { return }
do {
try await client.syncConversation(conversationId: displayedConversation)
} catch {
print("Error triggering sync: \(error)")
}
}
2025-08-24 23:38:35 -07:00
private func reloadMessages() async {
guard case .yes(let animated) = needsReload else { return }
needsReload = .no
guard let displayedConversation else { return }
do {
let clientMessages = try await client.getMessages(conversationId: displayedConversation)
.map { Display.Message(from: $0) }
self.messages = clientMessages
self.rebuildDisplayItems(animated: animated)
} catch {
print("Message fetch error: \(error)")
}
}
// MARK: - Types
enum NeedsReload
{
case no
case yes(Bool) // animated
}
}
}
#Preview {
@Previewable @State var model = TranscriptView.ViewModel(messages: [
.init(sender: .me, text: "Hello, how are you?"),
.init(sender: .counterpart("Bob"), text: "I am doing fine!")
])
TranscriptView(model: $model)
}