This commit is contained in:
fmodf 2024-07-11 17:14:31 +02:00
parent f89831f82d
commit 07993baddc
3 changed files with 27 additions and 11 deletions

View file

@ -2,6 +2,7 @@ enum ConversationAction: Codable {
case makeConversationActive(chat: Chat, roster: Roster?)
case messagesUpdated(messages: [Message])
case attachmentsUpdated(attachments: [Attachment])
case sendMessage(from: String, to: String, body: String)
case setReplyText(String)

View file

@ -301,8 +301,21 @@ private extension DatabaseMiddleware {
.publisher(in: database._db, scheduling: .immediate)
.sink { _ in
} receiveValue: { messages in
var attachments: [Attachment] = []
for message in messages {
do {
try self.database._db.read { db in
if let attachment = try message.attachment.fetchOne(db) {
attachments.append(attachment)
}
}
} catch {
print("Failed to fetch attachment for message \(message.id): \(error)")
}
}
DispatchQueue.main.async {
store.dispatch(.conversationAction(.messagesUpdated(messages: messages)))
store.dispatch(.conversationAction(.attachmentsUpdated(attachments: attachments)))
}
}
.store(in: &conversationCancellables)

View file

@ -6,18 +6,12 @@ struct ConversationMessageContainer: View {
let isOutgoing: Bool
var body: some View {
if let msgText = message.body {
if msgText.isLocation {
EmbededMapView(location: msgText.getLatLon)
} else {
Text(message.body ?? "...")
.font(.body2)
.foregroundColor(.Material.Text.main)
.multilineTextAlignment(.leading)
.padding(10)
}
if let msgText = message.body, msgText.isLocation {
EmbededMapView(location: msgText.getLatLon)
} else if let attachmentId = message.attachmentId {
AttachmentView(attachmentId: attachmentId)
} else {
Text("...")
Text(message.body ?? "...")
.font(.body2)
.foregroundColor(.Material.Text.main)
.multilineTextAlignment(.leading)
@ -71,3 +65,11 @@ private struct EmbededMapView: View {
}
}
}
private struct AttachmentView: View {
let attachmentId: String
var body: some View {
Text("Attachment \(attachmentId)")
}
}