Skip to content

Commit

Permalink
Config for changing supported media types in the composer
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmitrevski committed Sep 22, 2023
1 parent a2f7dfd commit 7701739
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7701739

Please sign in to comment.