Implements attachment previewing
This commit is contained in:
148
kordophone2/Transcript/TranscriptDisplayItemViews.swift
Normal file
148
kordophone2/Transcript/TranscriptDisplayItemViews.swift
Normal file
@@ -0,0 +1,148 @@
|
||||
//
|
||||
// TranscriptDisplayItemViews.swift
|
||||
// kordophone2
|
||||
//
|
||||
// Created by James Magahern on 8/24/25.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct BubbleView<Content: View>: View
|
||||
{
|
||||
let isFromMe: Bool
|
||||
let content: () -> Content
|
||||
|
||||
init(isFromMe: Bool, @ViewBuilder content: @escaping () -> Content) {
|
||||
self.isFromMe = isFromMe
|
||||
self.content = content
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
HStack(alignment: .bottom) {
|
||||
if isFromMe { Spacer(minLength: .minimumBubbleHorizontalPadding) }
|
||||
|
||||
content()
|
||||
.mask {
|
||||
UnevenRoundedRectangle(cornerRadii: RectangleCornerRadii(
|
||||
topLeading: isFromMe ? .dominantCornerRadius : .minorCornerRadius,
|
||||
bottomLeading: .dominantCornerRadius,
|
||||
bottomTrailing: .dominantCornerRadius,
|
||||
topTrailing: isFromMe ? .minorCornerRadius : .dominantCornerRadius,
|
||||
))
|
||||
}
|
||||
|
||||
if !isFromMe { Spacer(minLength: .minimumBubbleHorizontalPadding) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct TextBubbleItemView: View
|
||||
{
|
||||
let text: String
|
||||
let isFromMe: Bool
|
||||
|
||||
var body: some View {
|
||||
let bubbleColor: Color = isFromMe ? .blue : Color(.systemGray)
|
||||
let textColor: Color = isFromMe ? .white : .primary
|
||||
|
||||
BubbleView(isFromMe: isFromMe) {
|
||||
HStack {
|
||||
Text(text)
|
||||
.foregroundStyle(textColor)
|
||||
.multilineTextAlignment(.leading)
|
||||
}
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
.padding(.horizontal, 16.0)
|
||||
.padding(.vertical, 10.0)
|
||||
.background(bubbleColor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ImageItemView: View
|
||||
{
|
||||
let isFromMe: Bool
|
||||
let attachment: Display.ImageAttachment
|
||||
|
||||
@State private var img: NSImage?
|
||||
@Environment(\.xpcClient) var xpcClient
|
||||
|
||||
var body: some View {
|
||||
BubbleView(isFromMe: isFromMe) {
|
||||
if let img {
|
||||
Image(nsImage: img)
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
} else {
|
||||
ProgressView()
|
||||
}
|
||||
}
|
||||
.task {
|
||||
do {
|
||||
let handle = try await xpcClient.openAttachmentFileHandle(
|
||||
attachmentId: attachment.id,
|
||||
preview: true
|
||||
)
|
||||
|
||||
try? handle.seek(toOffset: 0)
|
||||
if let data = try? handle.readToEnd(),
|
||||
let ns = NSImage(data: data) {
|
||||
img = ns
|
||||
}
|
||||
} catch {
|
||||
print("Attachment file handle acquisition error: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct PlaceholderImageItemView: View
|
||||
{
|
||||
let isFromMe: Bool
|
||||
let size: CGSize
|
||||
|
||||
init(isFromMe: Bool, size: CGSize?) {
|
||||
self.isFromMe = isFromMe
|
||||
self.size = size ?? CGSize(width: 250.0, height: 100.0)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
BubbleView(isFromMe: isFromMe) {
|
||||
Color.gray
|
||||
.frame(width: size.width, height: size.height)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct DateItemView: View
|
||||
{
|
||||
let date: Date
|
||||
|
||||
private let formatter: DateFormatter = {
|
||||
let f = DateFormatter()
|
||||
f.dateFormat = "EEEE HH:mm"
|
||||
|
||||
return f
|
||||
}()
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
Spacer(minLength: 34.0)
|
||||
|
||||
Text(formatter.string(from: date))
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
.padding(.vertical, 4)
|
||||
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate extension CGFloat {
|
||||
static let dominantCornerRadius = 16.0
|
||||
static let minorCornerRadius = 4.0
|
||||
static let minimumBubbleHorizontalPadding = 80.0
|
||||
}
|
||||
61
kordophone2/Transcript/TranscriptDisplayItems.swift
Normal file
61
kordophone2/Transcript/TranscriptDisplayItems.swift
Normal file
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// TranscriptDisplayItems.swift
|
||||
// kordophone2
|
||||
//
|
||||
// Created by James Magahern on 8/24/25.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
extension TranscriptView.ViewModel
|
||||
{
|
||||
internal func rebuildDisplayItems(animated: Bool = false) {
|
||||
var displayItems: [DisplayItem] = []
|
||||
var lastDate: Date = .distantPast
|
||||
|
||||
let client = XPCClient()
|
||||
let dateAnnotationTimeInterval: TimeInterval = 60 * 60 * 30 // 30m
|
||||
for message in messages {
|
||||
if message.date.timeIntervalSince(lastDate) > dateAnnotationTimeInterval {
|
||||
lastDate = message.date
|
||||
displayItems.append(.date(message.date))
|
||||
}
|
||||
|
||||
for attachment in message.attachments {
|
||||
displayItems.append(.attachment(attachment))
|
||||
|
||||
if !attachment.isPreviewDownloaded {
|
||||
Task.detached {
|
||||
try await client.downloadAttachment(attachmentId: attachment.id, preview: true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
displayItems.append(.message(message))
|
||||
}
|
||||
|
||||
let animation: Animation? = animated ? .default : nil
|
||||
withAnimation(animation) {
|
||||
self.displayItems = displayItems
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum DisplayItem: Identifiable
|
||||
{
|
||||
case message(Display.Message)
|
||||
case attachment(Display.ImageAttachment)
|
||||
case date(Date)
|
||||
|
||||
var id: String {
|
||||
switch self {
|
||||
case .message(let message):
|
||||
message.id
|
||||
case .attachment(let attachment):
|
||||
attachment.id
|
||||
case .date(let date):
|
||||
date.description
|
||||
}
|
||||
}
|
||||
}
|
||||
152
kordophone2/Transcript/TranscriptView.swift
Normal file
152
kordophone2/Transcript/TranscriptView.swift
Normal file
@@ -0,0 +1,152 @@
|
||||
//
|
||||
// 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))
|
||||
.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:
|
||||
model.setNeedsReload(animated: false)
|
||||
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
|
||||
|
||||
internal var needsReload: NeedsReload = .yes(false)
|
||||
internal var messages: [Display.Message]
|
||||
|
||||
init(messages: [Display.Message] = []) {
|
||||
self.messages = messages
|
||||
observeDisplayedConversation()
|
||||
rebuildDisplayItems()
|
||||
}
|
||||
|
||||
func setNeedsReload(animated: Bool) {
|
||||
needsReload = .yes(animated)
|
||||
Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
await reloadMessages()
|
||||
}
|
||||
}
|
||||
|
||||
func attachmentDownloaded(id: String) {
|
||||
// TODO: should be smarter here
|
||||
setNeedsReload(animated: true)
|
||||
}
|
||||
|
||||
private func observeDisplayedConversation() {
|
||||
withObservationTracking {
|
||||
_ = displayedConversation
|
||||
} onChange: {
|
||||
Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
|
||||
setNeedsReload(animated: false)
|
||||
observeDisplayedConversation()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func reloadMessages() async {
|
||||
guard case .yes(let animated) = needsReload else { return }
|
||||
needsReload = .no
|
||||
|
||||
guard let displayedConversation else { return }
|
||||
|
||||
do {
|
||||
let client = XPCClient()
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user