diff --git a/CHANGELOG.md b/CHANGELOG.md index c7e972c4..370b3b72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). # Upcoming -### 🔄 Changed +### ✅ Added +- Config for changing supported media types in the composer # [4.37.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.37.0) _September 18, 2023_ diff --git a/Sources/StreamChatSwiftUI/ChatChannel/Composer/ComposerConfig.swift b/Sources/StreamChatSwiftUI/ChatChannel/Composer/ComposerConfig.swift index 2bb6eb0a..58834f74 100644 --- a/Sources/StreamChatSwiftUI/ChatChannel/Composer/ComposerConfig.swift +++ b/Sources/StreamChatSwiftUI/ChatChannel/Composer/ComposerConfig.swift @@ -12,6 +12,7 @@ public struct ComposerConfig { public var inputViewMaxHeight: CGFloat public var inputViewCornerRadius: CGFloat public var inputFont: UIFont + public var gallerySupportedTypes: GallerySupportedTypes public var adjustMessageOnSend: (String) -> (String) public var adjustMessageOnRead: (String) -> (String) public var attachmentPayloadConverter: (ChatMessage) -> [AnyAttachmentPayload] @@ -21,6 +22,7 @@ public struct ComposerConfig { inputViewMaxHeight: CGFloat = 76, inputViewCornerRadius: CGFloat = 20, inputFont: UIFont = UIFont.preferredFont(forTextStyle: .body), + gallerySupportedTypes: GallerySupportedTypes = .imagesAndVideo, adjustMessageOnSend: @escaping (String) -> (String) = { $0 }, adjustMessageOnRead: @escaping (String) -> (String) = { $0 }, attachmentPayloadConverter: @escaping (ChatMessage) -> [AnyAttachmentPayload] @@ -33,9 +35,16 @@ public struct ComposerConfig { self.adjustMessageOnSend = adjustMessageOnSend self.adjustMessageOnRead = adjustMessageOnRead self.attachmentPayloadConverter = attachmentPayloadConverter + self.gallerySupportedTypes = gallerySupportedTypes } public static var defaultAttachmentPayloadConverter: (ChatMessage) -> [AnyAttachmentPayload] = { message in message.allAttachments.toAnyAttachmentPayload() } } + +public enum GallerySupportedTypes { + case imagesAndVideo + case images + case videos +} diff --git a/Sources/StreamChatSwiftUI/ChatChannel/Composer/ImagePickerView.swift b/Sources/StreamChatSwiftUI/ChatChannel/Composer/ImagePickerView.swift index 88ac9910..f4c95372 100644 --- a/Sources/StreamChatSwiftUI/ChatChannel/Composer/ImagePickerView.swift +++ b/Sources/StreamChatSwiftUI/ChatChannel/Composer/ImagePickerView.swift @@ -8,6 +8,8 @@ import SwiftUI /// Image picker for loading images. struct ImagePickerView: UIViewControllerRepresentable { + @Injected(\.utils) var utils + let sourceType: UIImagePickerController.SourceType var onAssetPicked: (AddedAsset) -> Void @@ -19,8 +21,15 @@ struct ImagePickerView: UIViewControllerRepresentable { if UIImagePickerController.isSourceTypeAvailable(sourceType) { pickerController.sourceType = sourceType } - pickerController.mediaTypes = ["public.image", "public.movie"] - + let gallerySupportedTypes = utils.composerConfig.gallerySupportedTypes + if gallerySupportedTypes == .images { + pickerController.mediaTypes = ["public.image"] + } else if gallerySupportedTypes == .videos { + pickerController.mediaTypes = ["public.movie"] + } else { + pickerController.mediaTypes = ["public.image", "public.movie"] + } + return pickerController } diff --git a/Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerViewModel.swift b/Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerViewModel.swift index cd722fe0..cc225cbf 100644 --- a/Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerViewModel.swift +++ b/Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerViewModel.swift @@ -396,11 +396,22 @@ open class MessageComposerViewModel: ObservableObject { } public func askForPhotosPermission() { - PHPhotoLibrary.requestAuthorization { (status) in + PHPhotoLibrary.requestAuthorization { [weak self] (status) in + guard let self else { return } switch status { case .authorized, .limited: log.debug("Access to photos granted.") let fetchOptions = PHFetchOptions() + let supportedTypes = self.utils.composerConfig.gallerySupportedTypes + var predicate: NSPredicate? + if supportedTypes == .images { + predicate = NSPredicate(format: "mediaType = \(PHAssetMediaType.image.rawValue)") + } else if supportedTypes == .videos { + predicate = NSPredicate(format: "mediaType = \(PHAssetMediaType.video.rawValue)") + } + if let predicate { + fetchOptions.predicate = predicate + } fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] let assets = PHAsset.fetchAssets(with: fetchOptions) DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) { [weak self] in