diff --git a/CHANGELOG.md b/CHANGELOG.md index 0362d5d8..8950b0e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### 🐞 Fixed - Fix marked read while the app is in the background +- Fix recently saved images to camera roll don't show up # [4.41.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.41.0) _November 03, 2023_ diff --git a/Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerViewModel.swift b/Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerViewModel.swift index cc225cbf..3b8ba1c1 100644 --- a/Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerViewModel.swift +++ b/Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerViewModel.swift @@ -171,6 +171,12 @@ open class MessageComposerViewModel: ObservableObject { self.channelController = channelController self.messageController = messageController listenToCooldownUpdates() + NotificationCenter.default.addObserver( + self, + selector: #selector(applicationWillEnterForeground), + name: UIApplication.willEnterForegroundNotification, + object: nil + ) } public func sendMessage( @@ -401,22 +407,7 @@ open class MessageComposerViewModel: ObservableObject { 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 - self?.imageAssets = assets - } + self.fetchAssets() case .denied, .restricted, .notDetermined: DispatchQueue.main.async { [weak self] in self?.imageAssets = PHFetchResult() @@ -449,6 +440,25 @@ open class MessageComposerViewModel: ObservableObject { // MARK: - private + private func fetchAssets() { + 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 + self?.imageAssets = assets + } + } + private func checkForMentionedUsers( commandId: String?, extraData: [String: Any] @@ -607,4 +617,11 @@ open class MessageComposerViewModel: ObservableObject { return false } } + + @objc + private func applicationWillEnterForeground() { + if (imageAssets?.count ?? 0) > 0 { + self.fetchAssets() + } + } }