Skip to content

Commit

Permalink
Fix recently saved images to camera roll don't show up
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmitrevski committed Nov 13, 2023
1 parent 41ff6a0 commit 216f16e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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<PHAsset>()
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -607,4 +617,11 @@ open class MessageComposerViewModel: ObservableObject {
return false
}
}

@objc
private func applicationWillEnterForeground() {
if (imageAssets?.count ?? 0) > 0 {
self.fetchAssets()
}
}
}

0 comments on commit 216f16e

Please sign in to comment.