diff --git a/Kordophone/Kordophone.xcodeproj/project.pbxproj b/Kordophone/Kordophone.xcodeproj/project.pbxproj index 9725690..272f837 100644 --- a/Kordophone/Kordophone.xcodeproj/project.pbxproj +++ b/Kordophone/Kordophone.xcodeproj/project.pbxproj @@ -31,6 +31,8 @@ 4F8A660E280A93B0007A7431 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F8A65E4280A93AE007A7431 /* ContentView.swift */; }; 4F8A660F280A93B0007A7431 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4F8A65E5280A93AF007A7431 /* Assets.xcassets */; }; 4F8A6610280A93B0007A7431 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4F8A65E5280A93AF007A7431 /* Assets.xcassets */; }; + 4FE014DE28348AAF001C492D /* Image+Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FE014DD28348AAF001C492D /* Image+Data.swift */; }; + 4FE014DF28348AAF001C492D /* Image+Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FE014DD28348AAF001C492D /* Image+Data.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -71,6 +73,7 @@ 4F8A6603280A93B0007A7431 /* Tests macOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Tests macOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 4F8A6607280A93B0007A7431 /* Tests_macOS.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tests_macOS.swift; sourceTree = ""; }; 4F8A6609280A93B0007A7431 /* Tests_macOSLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tests_macOSLaunchTests.swift; sourceTree = ""; }; + 4FE014DD28348AAF001C492D /* Image+Data.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Image+Data.swift"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -147,6 +150,7 @@ 4F48B6AC282A3D00001B669B /* ConversationRow.swift */, 4F48B6AF282AD977001B669B /* ConversationView.swift */, 4F48B6B2282AF3A6001B669B /* MessageView.swift */, + 4FE014DD28348AAF001C492D /* Image+Data.swift */, 4F8A65E5280A93AF007A7431 /* Assets.xcassets */, ); path = Shared; @@ -360,6 +364,7 @@ 4F8A660D280A93B0007A7431 /* ContentView.swift in Sources */, 4F48B6AD282A3D00001B669B /* ConversationRow.swift in Sources */, 4F8A660B280A93B0007A7431 /* KordophoneApp.swift in Sources */, + 4FE014DE28348AAF001C492D /* Image+Data.swift in Sources */, 4F48B6B3282AF3A6001B669B /* MessageView.swift in Sources */, 4F48B6B0282AD977001B669B /* ConversationView.swift in Sources */, ); @@ -375,6 +380,7 @@ 4F8A660E280A93B0007A7431 /* ContentView.swift in Sources */, 4F48B6AE282A3D00001B669B /* ConversationRow.swift in Sources */, 4F8A660C280A93B0007A7431 /* KordophoneApp.swift in Sources */, + 4FE014DF28348AAF001C492D /* Image+Data.swift in Sources */, 4F48B6B4282AF3A6001B669B /* MessageView.swift in Sources */, 4F48B6B1282AD977001B669B /* ConversationView.swift in Sources */, ); diff --git a/Kordophone/Shared/ConversationView.swift b/Kordophone/Shared/ConversationView.swift index 87f7cc0..7131d45 100644 --- a/Kordophone/Shared/ConversationView.swift +++ b/Kordophone/Shared/ConversationView.swift @@ -18,7 +18,7 @@ struct ConversationView : View { if message.sentByMe { Spacer() } - MessageView(message: message) + MessageViewWrapper(message: message) .id(message) if !message.sentByMe { Spacer() diff --git a/Kordophone/Shared/Image+Data.swift b/Kordophone/Shared/Image+Data.swift new file mode 100644 index 0000000..3ff8ee6 --- /dev/null +++ b/Kordophone/Shared/Image+Data.swift @@ -0,0 +1,14 @@ + +import SwiftUI + +extension Image { + init(data: Data) { + #if canImport(AppKit) + self.init(nsImage: NSImage(data: data) ?? .init()) + #elseif canImport(UIKit) + self.init(uiImage: UIImage(data: data) ?? .init()) + #else + EmptyView() + #endif + } +} diff --git a/Kordophone/Shared/MessageView.swift b/Kordophone/Shared/MessageView.swift index 7bdb5ee..d33d641 100644 --- a/Kordophone/Shared/MessageView.swift +++ b/Kordophone/Shared/MessageView.swift @@ -4,13 +4,42 @@ import KordophoneKit struct MessageView : View { var message: Message + var attachmentData: Data? var body: some View { HStack { - Text(message.text) - .multilineTextAlignment(.leading) + VStack { + attachmentData.map { + Image(data: $0) + } + Text(message.text) + } + .multilineTextAlignment(.leading) } .padding() .background(Color.blue) .padding() } } + +struct MessageViewWrapper : View { + var message: Message + @State private var attachmentData: Data? + @EnvironmentObject private var connection: Connection + + var body: some View { + MessageView(message: message, attachmentData: attachmentData) + .onChange(of: message) { _ in + print(message) + if let attachment = message.attachments.first { + print(attachment) + Task { + let data = await connection.data(for: attachment) + DispatchQueue.main.async { + print(data) + self.attachmentData = data + } + } + } + } + } +} diff --git a/KordophoneKit/Sources/KordophoneKit/Attachment.swift b/KordophoneKit/Sources/KordophoneKit/Attachment.swift new file mode 100644 index 0000000..8b3ff15 --- /dev/null +++ b/KordophoneKit/Sources/KordophoneKit/Attachment.swift @@ -0,0 +1,14 @@ + +import Foundation + +public struct Attachment { + var guid: String + + init(guid: String) { + self.guid = guid + } +} + +extension Attachment : Hashable, Codable, Identifiable { + public var id: String { guid } +} diff --git a/KordophoneKit/Sources/KordophoneKit/Connection.swift b/KordophoneKit/Sources/KordophoneKit/Connection.swift index e4eea83..596c317 100644 --- a/KordophoneKit/Sources/KordophoneKit/Connection.swift +++ b/KordophoneKit/Sources/KordophoneKit/Connection.swift @@ -91,6 +91,10 @@ public class Connection { method: "POST", body: bodyData) } + + public func data(for attachment: Attachment) async -> Data? { + await authenticatedRequest(service: "attachment", queryItems: ["guid" : attachment.guid]) + } } extension Connection { diff --git a/KordophoneKit/Sources/KordophoneKit/Message.swift b/KordophoneKit/Sources/KordophoneKit/Message.swift index 357f9af..477bd5d 100644 --- a/KordophoneKit/Sources/KordophoneKit/Message.swift +++ b/KordophoneKit/Sources/KordophoneKit/Message.swift @@ -12,7 +12,10 @@ struct RawMessage : Codable { let message = Message(guid: guid, date: .from(kordophoneDate: date), sender: sender, - text: text) + text: text, + attachments: (fileTransferGUIDs ?? [String]()).map { + Attachment(guid: $0) + }) return message } @@ -23,6 +26,7 @@ public struct Message { public var date: Date = .now public var sender: String? public var text: String + public var attachments: [Attachment] = .init() } extension Message {