Skip to content

Commit 7701739

Browse files
Config for changing supported media types in the composer
1 parent a2f7dfd commit 7701739

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
33

44
# Upcoming
55

6-
### 🔄 Changed
6+
### ✅ Added
7+
- Config for changing supported media types in the composer
78

89
# [4.37.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.37.0)
910
_September 18, 2023_

Sources/StreamChatSwiftUI/ChatChannel/Composer/ComposerConfig.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public struct ComposerConfig {
1212
public var inputViewMaxHeight: CGFloat
1313
public var inputViewCornerRadius: CGFloat
1414
public var inputFont: UIFont
15+
public var gallerySupportedTypes: GallerySupportedTypes
1516
public var adjustMessageOnSend: (String) -> (String)
1617
public var adjustMessageOnRead: (String) -> (String)
1718
public var attachmentPayloadConverter: (ChatMessage) -> [AnyAttachmentPayload]
@@ -21,6 +22,7 @@ public struct ComposerConfig {
2122
inputViewMaxHeight: CGFloat = 76,
2223
inputViewCornerRadius: CGFloat = 20,
2324
inputFont: UIFont = UIFont.preferredFont(forTextStyle: .body),
25+
gallerySupportedTypes: GallerySupportedTypes = .imagesAndVideo,
2426
adjustMessageOnSend: @escaping (String) -> (String) = { $0 },
2527
adjustMessageOnRead: @escaping (String) -> (String) = { $0 },
2628
attachmentPayloadConverter: @escaping (ChatMessage) -> [AnyAttachmentPayload]
@@ -33,9 +35,16 @@ public struct ComposerConfig {
3335
self.adjustMessageOnSend = adjustMessageOnSend
3436
self.adjustMessageOnRead = adjustMessageOnRead
3537
self.attachmentPayloadConverter = attachmentPayloadConverter
38+
self.gallerySupportedTypes = gallerySupportedTypes
3639
}
3740

3841
public static var defaultAttachmentPayloadConverter: (ChatMessage) -> [AnyAttachmentPayload] = { message in
3942
message.allAttachments.toAnyAttachmentPayload()
4043
}
4144
}
45+
46+
public enum GallerySupportedTypes {
47+
case imagesAndVideo
48+
case images
49+
case videos
50+
}

Sources/StreamChatSwiftUI/ChatChannel/Composer/ImagePickerView.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import SwiftUI
88

99
/// Image picker for loading images.
1010
struct ImagePickerView: UIViewControllerRepresentable {
11+
@Injected(\.utils) var utils
12+
1113
let sourceType: UIImagePickerController.SourceType
1214

1315
var onAssetPicked: (AddedAsset) -> Void
@@ -19,8 +21,15 @@ struct ImagePickerView: UIViewControllerRepresentable {
1921
if UIImagePickerController.isSourceTypeAvailable(sourceType) {
2022
pickerController.sourceType = sourceType
2123
}
22-
pickerController.mediaTypes = ["public.image", "public.movie"]
23-
24+
let gallerySupportedTypes = utils.composerConfig.gallerySupportedTypes
25+
if gallerySupportedTypes == .images {
26+
pickerController.mediaTypes = ["public.image"]
27+
} else if gallerySupportedTypes == .videos {
28+
pickerController.mediaTypes = ["public.movie"]
29+
} else {
30+
pickerController.mediaTypes = ["public.image", "public.movie"]
31+
}
32+
2433
return pickerController
2534
}
2635

Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerViewModel.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,22 @@ open class MessageComposerViewModel: ObservableObject {
396396
}
397397

398398
public func askForPhotosPermission() {
399-
PHPhotoLibrary.requestAuthorization { (status) in
399+
PHPhotoLibrary.requestAuthorization { [weak self] (status) in
400+
guard let self else { return }
400401
switch status {
401402
case .authorized, .limited:
402403
log.debug("Access to photos granted.")
403404
let fetchOptions = PHFetchOptions()
405+
let supportedTypes = self.utils.composerConfig.gallerySupportedTypes
406+
var predicate: NSPredicate?
407+
if supportedTypes == .images {
408+
predicate = NSPredicate(format: "mediaType = \(PHAssetMediaType.image.rawValue)")
409+
} else if supportedTypes == .videos {
410+
predicate = NSPredicate(format: "mediaType = \(PHAssetMediaType.video.rawValue)")
411+
}
412+
if let predicate {
413+
fetchOptions.predicate = predicate
414+
}
404415
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
405416
let assets = PHAsset.fetchAssets(with: fetchOptions)
406417
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) { [weak self] in

0 commit comments

Comments
 (0)