This commit is contained in:
fmodf 2024-07-11 17:46:57 +02:00
parent 07993baddc
commit a28d60e128
5 changed files with 65 additions and 4 deletions

View file

@ -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)))
}
}

View file

@ -17,6 +17,9 @@ extension ConversationState {
state.replyText = text.makeReply
}
case .attachmentsUpdated(let attachments):
state.currentAttachments = attachments
default:
break
}

View file

@ -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 = ""
}
}

View file

@ -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
}

View file

@ -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)
}
}
}
}
}