@@ -171,6 +171,12 @@ open class MessageComposerViewModel: ObservableObject {
171171 self . channelController = channelController
172172 self . messageController = messageController
173173 listenToCooldownUpdates ( )
174+ NotificationCenter . default. addObserver (
175+ self ,
176+ selector: #selector( applicationWillEnterForeground) ,
177+ name: UIApplication . willEnterForegroundNotification,
178+ object: nil
179+ )
174180 }
175181
176182 public func sendMessage(
@@ -401,22 +407,7 @@ open class MessageComposerViewModel: ObservableObject {
401407 switch status {
402408 case . authorized, . limited:
403409 log. debug ( " Access to photos granted. " )
404- 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- }
415- fetchOptions. sortDescriptors = [ NSSortDescriptor ( key: " creationDate " , ascending: false ) ]
416- let assets = PHAsset . fetchAssets ( with: fetchOptions)
417- DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.25 ) { [ weak self] in
418- self ? . imageAssets = assets
419- }
410+ self . fetchAssets ( )
420411 case . denied, . restricted, . notDetermined:
421412 DispatchQueue . main. async { [ weak self] in
422413 self ? . imageAssets = PHFetchResult < PHAsset > ( )
@@ -449,6 +440,25 @@ open class MessageComposerViewModel: ObservableObject {
449440
450441 // MARK: - private
451442
443+ private func fetchAssets( ) {
444+ let fetchOptions = PHFetchOptions ( )
445+ let supportedTypes = self . utils. composerConfig. gallerySupportedTypes
446+ var predicate : NSPredicate ?
447+ if supportedTypes == . images {
448+ predicate = NSPredicate ( format: " mediaType = \( PHAssetMediaType . image. rawValue) " )
449+ } else if supportedTypes == . videos {
450+ predicate = NSPredicate ( format: " mediaType = \( PHAssetMediaType . video. rawValue) " )
451+ }
452+ if let predicate {
453+ fetchOptions. predicate = predicate
454+ }
455+ fetchOptions. sortDescriptors = [ NSSortDescriptor ( key: " creationDate " , ascending: false ) ]
456+ let assets = PHAsset . fetchAssets ( with: fetchOptions)
457+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.25 ) { [ weak self] in
458+ self ? . imageAssets = assets
459+ }
460+ }
461+
452462 private func checkForMentionedUsers(
453463 commandId: String ? ,
454464 extraData: [ String : Any ]
@@ -607,4 +617,11 @@ open class MessageComposerViewModel: ObservableObject {
607617 return false
608618 }
609619 }
620+
621+ @objc
622+ private func applicationWillEnterForeground( ) {
623+ if ( imageAssets? . count ?? 0 ) > 0 {
624+ self . fetchAssets ( )
625+ }
626+ }
610627}
0 commit comments