Skip to content

Commit

Permalink
add PHPhotoLibraryChangeObserver - Fixes #24
Browse files Browse the repository at this point in the history
  • Loading branch information
tilltue committed Sep 3, 2017
1 parent 349c092 commit d84970d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
2 changes: 2 additions & 0 deletions TLPhotoPicker/Classes/TLAssetsCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ extension TLPHAsset: Equatable {
}

struct TLAssetsCollection {
var phAssetCollection: PHAssetCollection? = nil
var fetchResult: PHFetchResult<PHAsset>? = nil
var thumbnail: UIImage? = nil
var useCameraButton: Bool = false
Expand All @@ -73,6 +74,7 @@ struct TLAssetsCollection {
}

init(collection: PHAssetCollection) {
self.phAssetCollection = collection
self.title = collection.localizedTitle ?? ""
self.localIdentifier = collection.localIdentifier
}
Expand Down
13 changes: 11 additions & 2 deletions TLPhotoPicker/Classes/TLPhotoLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,20 @@ class TLPhotoLibrary {

//MARK: - Load Collection
extension TLPhotoLibrary {
func fetchCollection(allowedVideo: Bool = true, useCameraButton: Bool = true, mediaType: PHAssetMediaType? = nil) {

func getOption() -> PHFetchOptions {
let options = PHFetchOptions()
let sortOrder = [NSSortDescriptor(key: "creationDate", ascending: false)]
options.sortDescriptors = sortOrder
return options
}

func fetchResult(collection: TLAssetsCollection?) -> PHFetchResult<PHAsset>? {
guard let phAssetCollection = collection?.phAssetCollection else { return nil }
return PHAsset.fetchAssets(in: phAssetCollection, options: getOption())
}

func fetchCollection(allowedVideo: Bool = true, useCameraButton: Bool = true, mediaType: PHAssetMediaType? = nil) {
let options = getOption()

@discardableResult
func getSmartAlbum(subType: PHAssetCollectionSubtype, result: inout [TLAssetsCollection]) -> TLAssetsCollection? {
Expand Down
36 changes: 35 additions & 1 deletion TLPhotoPicker/Classes/TLPhotosPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ open class TLPhotosPickerViewController: UIViewController {

deinit {
//print("deinit TLPhotosPickerViewController")
PHPhotoLibrary.shared().unregisterChangeObserver(self)
}

required public init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -267,6 +268,10 @@ extension TLPhotosPickerViewController {
}
}

fileprivate func registerChangeObserver() {
PHPhotoLibrary.shared().register(self)
}

fileprivate func getfocusedIndex() -> Int {
guard let focused = self.focusedCollection, let result = self.collections.index(where: { $0 == focused }) else { return 0 }
return result
Expand Down Expand Up @@ -398,6 +403,7 @@ extension TLPhotosPickerViewController: TLPhotoLibraryDelegate {
func loadCompleteAllCollection(collections: [TLAssetsCollection]) {
self.collections = collections
self.reloadTableView()
self.registerChangeObserver()
}

func focusCollection(collection: TLAssetsCollection) {
Expand Down Expand Up @@ -523,8 +529,36 @@ extension TLPhotosPickerViewController: PHLivePhotoViewDelegate {
}
}

// MARK: - PHPhotoLibraryChangeObserver
extension TLPhotosPickerViewController: PHPhotoLibraryChangeObserver {
public func photoLibraryDidChange(_ changeInstance: PHChange) {
guard let changeFetchResult = self.focusedCollection?.fetchResult else { return }
guard let changes = changeInstance.changeDetails(for: changeFetchResult) else { return }
guard let fetchResult = self.photoLibrary.fetchResult(collection: self.focusedCollection) else { return }
let addIndex = self.usedCameraButton ? 1 : 0
DispatchQueue.main.sync {
if changes.hasIncrementalChanges {
self.collectionView.performBatchUpdates({ [weak self] _ in
self?.focusedCollection?.fetchResult = fetchResult
if let removed = changes.removedIndexes, removed.count > 0 {
self?.collectionView.deleteItems(at: removed.map { IndexPath(item: $0+addIndex, section:0) })
}
if let inserted = changes.insertedIndexes, inserted.count > 0 {
self?.collectionView.insertItems(at: inserted.map { IndexPath(item: $0+addIndex, section:0) })
}
if let changed = changes.changedIndexes, changed.count > 0 {
self?.collectionView.reloadItems(at: changed.map { IndexPath(item: $0+addIndex, section:0) })
}
})
}else {
self.focusedCollection?.fetchResult = fetchResult
self.collectionView.reloadData()
}
}
}
}

// UICollectionView delegate & datasource
// MARK: - UICollectionView delegate & datasource
extension TLPhotosPickerViewController: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDataSourcePrefetching {
fileprivate func getSelectedAssets(_ asset: TLPHAsset) -> TLPHAsset? {
if let index = self.selectedAssets.index(where: { $0.phAsset == asset.phAsset }) {
Expand Down

0 comments on commit d84970d

Please sign in to comment.