From a28d60e128bf0f0546c9370287e0bb46194ecb94 Mon Sep 17 00:00:00 2001 From: fmodf Date: Thu, 11 Jul 2024 17:46:57 +0200 Subject: [PATCH] wip --- .../Middlewares/DatabaseMiddleware.swift | 7 ++- .../Reducers/ConversationReducer.swift | 3 ++ .../AppCore/State/ConversationState.swift | 2 + ConversationsClassic/Helpers/Const.swift | 5 +- .../ConversationMessageContainer.swift | 52 ++++++++++++++++++- 5 files changed, 65 insertions(+), 4 deletions(-) diff --git a/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift index 15fa2d6..42def9f 100644 --- a/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift +++ b/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift @@ -301,6 +301,12 @@ private extension DatabaseMiddleware { .publisher(in: database._db, scheduling: .immediate) .sink { _ in } receiveValue: { messages in + // messages + DispatchQueue.main.async { + store.dispatch(.conversationAction(.messagesUpdated(messages: messages))) + } + + // attachments var attachments: [Attachment] = [] for message in messages { do { @@ -314,7 +320,6 @@ private extension DatabaseMiddleware { } } DispatchQueue.main.async { - store.dispatch(.conversationAction(.messagesUpdated(messages: messages))) store.dispatch(.conversationAction(.attachmentsUpdated(attachments: attachments))) } } diff --git a/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift b/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift index cc76c62..028c17e 100644 --- a/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift +++ b/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift @@ -17,6 +17,9 @@ extension ConversationState { state.replyText = text.makeReply } + case .attachmentsUpdated(let attachments): + state.currentAttachments = attachments + default: break } diff --git a/ConversationsClassic/AppCore/State/ConversationState.swift b/ConversationsClassic/AppCore/State/ConversationState.swift index 9267bbd..c392f76 100644 --- a/ConversationsClassic/AppCore/State/ConversationState.swift +++ b/ConversationsClassic/AppCore/State/ConversationState.swift @@ -2,6 +2,7 @@ struct ConversationState: Stateable { var currentChat: Chat? var currentRoster: Roster? var currentMessages: [Message] + var currentAttachments: [Attachment] var replyText: String } @@ -10,6 +11,7 @@ struct ConversationState: Stateable { extension ConversationState { init() { currentMessages = [] + currentAttachments = [] replyText = "" } } diff --git a/ConversationsClassic/Helpers/Const.swift b/ConversationsClassic/Helpers/Const.swift index 5f65fd9..0c42f7a 100644 --- a/ConversationsClassic/Helpers/Const.swift +++ b/ConversationsClassic/Helpers/Const.swift @@ -39,5 +39,8 @@ enum Const { static let galleryGridSize = UIScreen.main.bounds.width / 3 // Size for map preview for location messages - static let mapPreviewSize = UIScreen.main.bounds.width * 0.75 + static let mapPreviewSize = UIScreen.main.bounds.width * 0.5 + + // Size for attachment preview + static let attachmentPreviewSize = UIScreen.main.bounds.width * 0.5 } diff --git a/ConversationsClassic/View/Screens/Conversation/ConversationMessageContainer.swift b/ConversationsClassic/View/Screens/Conversation/ConversationMessageContainer.swift index 1a7ea1e..16b84c1 100644 --- a/ConversationsClassic/View/Screens/Conversation/ConversationMessageContainer.swift +++ b/ConversationsClassic/View/Screens/Conversation/ConversationMessageContainer.swift @@ -57,7 +57,6 @@ private struct EmbededMapView: View { } ) .frame(width: Const.mapPreviewSize, height: Const.mapPreviewSize) - .cornerRadius(10) .onTapGesture { let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: location)) mapItem.name = "Location" @@ -67,9 +66,58 @@ private struct EmbededMapView: View { } private struct AttachmentView: View { + @EnvironmentObject var store: AppStore + let attachmentId: String var body: some View { - Text("Attachment \(attachmentId)") + if let attachment = store.state.conversationsState.currentAttachments.first(where: { $0.id == attachmentId }) { + if let localPath = attachment.localPath { + Image(systemName: "questionmark.square") + .resizable() + .aspectRatio(contentMode: .fill) + .frame(width: Const.attachmentPreviewSize, height: Const.attachmentPreviewSize) + .cornerRadius(10) + } else { + AttachmentPlaceholderView(placeholderImageName: progressImageName(attachment.type)) + } + } else { + AttachmentPlaceholderView(placeholderImageName: nil) + } + } + + private func progressImageName(_ type: AttachmentType) -> String { + switch type { + case .image: + return "photo" + case .audio: + return "music.note" + case .movie: + return "film" + case .file: + return "doc" + } + } +} + +private struct AttachmentPlaceholderView: View { + let placeholderImageName: String? + + var body: some View { + Rectangle() + .foregroundColor(.Material.Background.dark) + .frame(width: Const.attachmentPreviewSize, height: Const.attachmentPreviewSize) + .overlay { + ZStack { + ProgressView() + .scaleEffect(1.5) + .progressViewStyle(CircularProgressViewStyle(tint: .Material.Elements.active)) + if let imageName = placeholderImageName { + Image(systemName: imageName) + .font(.body1) + .foregroundColor(.Material.Elements.active) + } + } + } } }