Peter Hajas 1 year ago
parent
commit
e2e9074f8c
  1. 6
      Kordophone/Kordophone.xcodeproj/project.pbxproj
  2. 2
      Kordophone/Shared/ConversationView.swift
  3. 14
      Kordophone/Shared/Image+Data.swift
  4. 29
      Kordophone/Shared/MessageView.swift
  5. 14
      KordophoneKit/Sources/KordophoneKit/Attachment.swift
  6. 4
      KordophoneKit/Sources/KordophoneKit/Connection.swift
  7. 6
      KordophoneKit/Sources/KordophoneKit/Message.swift

6
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 = "<group>"; };
4F8A6609280A93B0007A7431 /* Tests_macOSLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tests_macOSLaunchTests.swift; sourceTree = "<group>"; };
4FE014DD28348AAF001C492D /* Image+Data.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Image+Data.swift"; sourceTree = "<group>"; };
/* 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 */,
);

2
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()

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

29
Kordophone/Shared/MessageView.swift

@ -4,9 +4,15 @@ import KordophoneKit
struct MessageView : View {
var message: Message
var attachmentData: Data?
var body: some View {
HStack {
VStack {
attachmentData.map {
Image(data: $0)
}
Text(message.text)
}
.multilineTextAlignment(.leading)
}
.padding()
@ -14,3 +20,26 @@ struct MessageView : View {
.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
}
}
}
}
}
}

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

4
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 {

6
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 {

Loading…
Cancel
Save