Skip to content

Commit 5890004

Browse files
Pass extra data in attachments
1 parent 8f9e99b commit 5890004

File tree

5 files changed

+44
-16
lines changed

5 files changed

+44
-16
lines changed

Sources/StreamChatSwiftUI/ChatChannel/Composer/AddedImageAttachmentsView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright © 2022 Stream.io Inc. All rights reserved.
2+
// Copyright © 2023 Stream.io Inc. All rights reserved.
33
//
44

55
import SwiftUI
@@ -44,7 +44,7 @@ public struct AddedImageAttachmentsView: View {
4444
if attachment.type == .video {
4545
VideoIndicatorView()
4646

47-
if let duration = attachment.extraData["duration"] as? String {
47+
if let duration = attachment.extraData["duration"]?.stringValue {
4848
VideoDurationIndicatorView(duration: duration)
4949
}
5050
}

Sources/StreamChatSwiftUI/ChatChannel/Composer/ComposerModels.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright © 2022 Stream.io Inc. All rights reserved.
2+
// Copyright © 2023 Stream.io Inc. All rights reserved.
33
//
44

55
import StreamChat
@@ -24,14 +24,14 @@ public struct AddedAsset: Identifiable, Equatable {
2424
public let id: String
2525
public let url: URL
2626
public let type: AssetType
27-
public var extraData: [String: Any] = [:]
27+
public var extraData: [String: RawJSON] = [:]
2828

2929
public init(
3030
image: UIImage,
3131
id: String,
3232
url: URL,
3333
type: AssetType,
34-
extraData: [String: Any] = [:]
34+
extraData: [String: RawJSON] = [:]
3535
) {
3636
self.image = image
3737
self.id = id
@@ -41,6 +41,16 @@ public struct AddedAsset: Identifiable, Equatable {
4141
}
4242
}
4343

44+
extension AddedAsset {
45+
func toAttachmentPayload() throws -> AnyAttachmentPayload {
46+
try AnyAttachmentPayload(
47+
localFileURL: url,
48+
attachmentType: type == .video ? .video : .image,
49+
extraData: extraData
50+
)
51+
}
52+
}
53+
4454
/// Type of asset added to the composer.
4555
public enum AssetType {
4656
case image

Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerViewModel.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright © 2022 Stream.io Inc. All rights reserved.
2+
// Copyright © 2023 Stream.io Inc. All rights reserved.
33
//
44

55
import Combine
@@ -204,13 +204,7 @@ open class MessageComposerViewModel: ObservableObject {
204204
}
205205

206206
do {
207-
var attachments = try addedAssets.map { added in
208-
try AnyAttachmentPayload(
209-
localFileURL: added.url,
210-
attachmentType: added.type == .video ? .video : .image
211-
)
212-
}
213-
207+
var attachments = try addedAssets.map { try $0.toAttachmentPayload() }
214208
attachments += try addedFileURLs.map { url in
215209
_ = url.startAccessingSecurityScopedResource()
216210
return try AnyAttachmentPayload(localFileURL: url, attachmentType: .file)

Sources/StreamChatSwiftUI/ChatChannel/Composer/PhotoAttachmentPickerView.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright © 2022 Stream.io Inc. All rights reserved.
2+
// Copyright © 2023 Stream.io Inc. All rights reserved.
33
//
44

55
import Photos
@@ -94,7 +94,8 @@ public struct PhotoAttachmentCell: View {
9494
id: asset.localIdentifier,
9595
url: assetURL,
9696
type: assetType,
97-
extraData: asset.mediaType == .video ? ["duration": asset.durationString] : [:]
97+
extraData: asset
98+
.mediaType == .video ? ["duration": .string(asset.durationString)] : [:]
9899
)
99100
)
100101
}

StreamChatSwiftUITests/Tests/ChatChannel/MessageComposerViewModel_Tests.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright © 2022 Stream.io Inc. All rights reserved.
2+
// Copyright © 2023 Stream.io Inc. All rights reserved.
33
//
44

55
@testable import StreamChat
@@ -554,6 +554,29 @@ class MessageComposerViewModel_Tests: StreamChatTestCase {
554554
XCTAssert(viewModel.mentionedUsers.isEmpty)
555555
}
556556

557+
func test_addedAsset_extraData() {
558+
// Given
559+
let image = UIImage(systemName: "person")!
560+
let url = URL.newTemporaryFileURL()
561+
let addedAsset = AddedAsset(
562+
image: image,
563+
id: "imageId",
564+
url: url,
565+
type: .image,
566+
extraData: ["test": "test"]
567+
)
568+
569+
// When
570+
try! image.pngData()?.write(to: url)
571+
let attachment = try! addedAsset.toAttachmentPayload()
572+
let payload = attachment.payload as! ImageAttachmentPayload
573+
let extraData = payload.extraData
574+
575+
// Then
576+
XCTAssert(extraData?["test"] == "test")
577+
try! FileManager.default.removeItem(at: url)
578+
}
579+
557580
// MARK: - private
558581

559582
private func makeComposerViewModel() -> MessageComposerViewModel {

0 commit comments

Comments
 (0)