Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listening for photo library updates #94

Merged
merged 1 commit into from
Mar 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions ChattoAdditions/Source/Input/Photos/PhotosInputDataProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,20 @@

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)
func cancelPreviewImageRequest(requestID: Int32)
}

class PhotosInputPlaceholderDataProvider: PhotosInputDataProviderProtocol {
weak var delegate: PhotosInputDataProviderDelegate?

let numberOfPlaceholders: Int

Expand All @@ -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 {
Expand Down Expand Up @@ -92,16 +106,29 @@ class PhotosInputDataProvider: PhotosInputDataProviderProtocol {
}
}
}
}

class PhotosInputWithPlaceholdersDataProvider: 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, 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 {
Expand All @@ -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)
}
}
7 changes: 7 additions & 0 deletions ChattoAdditions/Source/Input/Photos/PhotosInputView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -222,3 +223,9 @@ extension PhotosInputView: UICollectionViewDelegateFlowLayout {
}
}
}

extension PhotosInputView: PhotosInputDataProviderDelegate {
func photosInpudDataProviderDidUpdate(dataProvider: PhotosInputDataProviderProtocol) {
self.collectionView.reloadData()
}
}