diff --git a/ConversationsClassic/AppCore/Actions/ConversationActions.swift b/ConversationsClassic/AppCore/Actions/ConversationActions.swift index 9294677..b3130df 100644 --- a/ConversationsClassic/AppCore/Actions/ConversationActions.swift +++ b/ConversationsClassic/AppCore/Actions/ConversationActions.swift @@ -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) diff --git a/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift index 52ed568..15fa2d6 100644 --- a/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift +++ b/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift @@ -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) diff --git a/ConversationsClassic/View/Screens/Conversation/ConversationMessageContainer.swift b/ConversationsClassic/View/Screens/Conversation/ConversationMessageContainer.swift index 7b25871..1a7ea1e 100644 --- a/ConversationsClassic/View/Screens/Conversation/ConversationMessageContainer.swift +++ b/ConversationsClassic/View/Screens/Conversation/ConversationMessageContainer.swift @@ -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)") + } +}