From 9062c4cec0ed5b602526f5dd1453adde32929685 Mon Sep 17 00:00:00 2001 From: Anton Schukin Date: Thu, 31 Mar 2016 16:50:53 +0100 Subject: [PATCH] Listening for photo library updates --- .../Photos/PhotosInputDataProvider.swift | 41 +++++++++++++++++-- .../Source/Input/Photos/PhotosInputView.swift | 7 ++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/ChattoAdditions/Source/Input/Photos/PhotosInputDataProvider.swift b/ChattoAdditions/Source/Input/Photos/PhotosInputDataProvider.swift index 5ba02cb..09fd979 100644 --- a/ChattoAdditions/Source/Input/Photos/PhotosInputDataProvider.swift +++ b/ChattoAdditions/Source/Input/Photos/PhotosInputDataProvider.swift @@ -24,7 +24,12 @@ import PhotosUI +protocol PhotosInputDataProviderDelegate: class { + func photosInpudDataProviderDidUpdate(dataProvider: PhotosInputDataProviderProtocol) +} + protocol PhotosInputDataProviderProtocol { + weak var delegate: PhotosInputDataProviderDelegate? { get set } var count: Int { get } func requestPreviewImageAtIndex(index: Int, targetSize: CGSize, completion: (UIImage) -> Void) -> Int32 func requestFullImageAtIndex(index: Int, completion: (UIImage) -> Void) @@ -32,6 +37,7 @@ protocol PhotosInputDataProviderProtocol { } class PhotosInputPlaceholderDataProvider: PhotosInputDataProviderProtocol { + weak var delegate: PhotosInputDataProviderDelegate? let numberOfPlaceholders: Int @@ -54,13 +60,21 @@ class PhotosInputPlaceholderDataProvider: PhotosInputDataProviderProtocol { } } -class PhotosInputDataProvider: PhotosInputDataProviderProtocol { +@objc +class PhotosInputDataProvider: NSObject, PhotosInputDataProviderProtocol, PHPhotoLibraryChangeObserver { + weak var delegate: PhotosInputDataProviderDelegate? private var imageManager = PHCachingImageManager() private var fetchResult: PHFetchResult! - init() { + override init() { let options = PHFetchOptions() options.sortDescriptors = [ NSSortDescriptor(key: "modificationDate", ascending: false) ] self.fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: options) + super.init() + PHPhotoLibrary.sharedPhotoLibrary().registerChangeObserver(self) + } + + deinit { + PHPhotoLibrary.sharedPhotoLibrary().unregisterChangeObserver(self) } var count: Int { @@ -92,16 +106,29 @@ class PhotosInputDataProvider: PhotosInputDataProviderProtocol { } } } + + // MARK: PHPhotoLibraryChangeObserver + + func photoLibraryDidChange(changeInstance: PHChange) { + // Photos may call this method on a background queue; switch to the main queue to update the UI. + dispatch_async(dispatch_get_main_queue()) { () -> Void in + if let changeDetails = changeInstance.changeDetailsForFetchResult(self.fetchResult) { + self.fetchResult = changeDetails.fetchResultAfterChanges + self.delegate?.photosInpudDataProviderDidUpdate(self) + } + } + } } -class PhotosInputWithPlaceholdersDataProvider: PhotosInputDataProviderProtocol { - +class PhotosInputWithPlaceholdersDataProvider: PhotosInputDataProviderProtocol, PhotosInputDataProviderDelegate { + weak var delegate: PhotosInputDataProviderDelegate? private let photosDataProvider: PhotosInputDataProviderProtocol private let placeholdersDataProvider: PhotosInputDataProviderProtocol init(photosDataProvider: PhotosInputDataProviderProtocol, placeholdersDataProvider: PhotosInputDataProviderProtocol) { self.photosDataProvider = photosDataProvider self.placeholdersDataProvider = placeholdersDataProvider + self.photosDataProvider.delegate = self } var count: Int { @@ -127,4 +154,10 @@ class PhotosInputWithPlaceholdersDataProvider: PhotosInputDataProviderProtocol { func cancelPreviewImageRequest(requestID: Int32) { return self.photosDataProvider.cancelPreviewImageRequest(requestID) } + + // MARK: PhotosInputDataProviderDelegate + + func photosInpudDataProviderDidUpdate(dataProvider: PhotosInputDataProviderProtocol) { + self.delegate?.photosInpudDataProviderDidUpdate(self) + } } diff --git a/ChattoAdditions/Source/Input/Photos/PhotosInputView.swift b/ChattoAdditions/Source/Input/Photos/PhotosInputView.swift index 91e3eb6..47261e7 100644 --- a/ChattoAdditions/Source/Input/Photos/PhotosInputView.swift +++ b/ChattoAdditions/Source/Input/Photos/PhotosInputView.swift @@ -125,6 +125,7 @@ class PhotosInputView: UIView, PhotosInputViewProtocol { private func replacePlaceholderItemsWithPhotoItems() { let newDataProvider = PhotosInputWithPlaceholdersDataProvider(photosDataProvider: PhotosInputDataProvider(), placeholdersDataProvider: PhotosInputPlaceholderDataProvider()) + newDataProvider.delegate = self self.dataProvider = newDataProvider self.cellProvider = PhotosInputCellProvider(collectionView: self.collectionView, dataProvider: newDataProvider) self.collectionView.reloadSections(NSIndexSet(index: 0)) @@ -222,3 +223,9 @@ extension PhotosInputView: UICollectionViewDelegateFlowLayout { } } } + +extension PhotosInputView: PhotosInputDataProviderDelegate { + func photosInpudDataProviderDidUpdate(dataProvider: PhotosInputDataProviderProtocol) { + self.collectionView.reloadData() + } +}