Skip to content

Commit

Permalink
fixed #234
Browse files Browse the repository at this point in the history
  • Loading branch information
wade-hawk committed Nov 9, 2019
1 parent 0cdcae6 commit d6cf40d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
4 changes: 3 additions & 1 deletion TLPhotoPicker/Classes/TLAlbumPopView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ extension PopupViewProtocol where Self: UIView {
return frame
}
func setupPopupFrame() {
if self.originalFrame != self.popupView.frame {
if self.originalFrame == CGRect.zero {
self.originalFrame = self.popupView.frame
}else {
self.originalFrame.size.height = self.popupView.frame.height
}
}
func show(_ show: Bool, duration: TimeInterval = 0.1) {
Expand Down
7 changes: 7 additions & 0 deletions TLPhotoPicker/Classes/TLPhotoLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class TLPhotoLibrary {
lazy var imageManager: PHCachingImageManager = {
return PHCachingImageManager()
}()
internal var assetCollections: [PHFetchResult<PHAssetCollection>] = []
internal var albums: PHFetchResult<PHCollection>? = nil

deinit {
// print("deinit TLPhotoLibrary")
Expand Down Expand Up @@ -164,6 +166,8 @@ extension TLPhotoLibrary {
}

func fetchCollection(configure: TLPhotosPickerConfigure) {
self.albums = nil
self.assetCollections = []
let useCameraButton = configure.usedCameraButton
let options = getOption(configure: configure)
let fetchCollectionOption = configure.fetchCollectionOption
Expand All @@ -173,6 +177,7 @@ extension TLPhotoLibrary {
let fetchCollection = PHAssetCollection.fetchAssetCollections(with: .album,
subtype: subType,
options: collectionOption)
self.assetCollections.append(fetchCollection)
var collections = [PHAssetCollection]()
fetchCollection.enumerateObjects { (collection, index, _) in
if configure.allowedAlbumCloudShared == false && collection.assetCollectionSubtype == .albumCloudShared {
Expand Down Expand Up @@ -202,6 +207,7 @@ extension TLPhotoLibrary {
let fetchCollection = PHAssetCollection.fetchAssetCollections(with: .smartAlbum,
subtype: subType,
options: collectionOption)
self.assetCollections.append(fetchCollection)
if
let collection = fetchCollection.firstObject,
result.contains(where: { $0.localIdentifier == collection.localIdentifier }) == false
Expand Down Expand Up @@ -262,6 +268,7 @@ extension TLPhotoLibrary {
//Album
let collectionOption = fetchCollectionOption[.topLevelUserCollections]
let albumsResult = PHCollectionList.fetchTopLevelUserCollections(with: collectionOption)
self?.albums = albumsResult
albumsResult.enumerateObjects({ (collection, index, stop) -> Void in
guard let collection = collection as? PHAssetCollection else { return }
var assetsCollection = TLAssetsCollection(collection: collection)
Expand Down
47 changes: 45 additions & 2 deletions TLPhotoPicker/Classes/TLPhotosPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -738,14 +738,57 @@ extension TLPhotosPickerViewController: PHLivePhotoViewDelegate {

// MARK: - PHPhotoLibraryChangeObserver
extension TLPhotosPickerViewController: PHPhotoLibraryChangeObserver {
private func getChanges(_ changeInstance: PHChange) -> PHFetchResultChangeDetails<PHAsset>? {
func isChangesCount<T>(changeDetails: PHFetchResultChangeDetails<T>?) -> Bool {
guard let changeDetails = changeDetails else {
return false
}
let before = changeDetails.fetchResultBeforeChanges.count
let after = changeDetails.fetchResultAfterChanges.count
return before != after
}

func isAlbumsChanges() -> Bool {
guard let albums = self.photoLibrary.albums else {
return false
}
let changeDetails = changeInstance.changeDetails(for: albums)
return isChangesCount(changeDetails: changeDetails)
}

func isCollectionsChanges() -> Bool {
for fetchResultCollection in self.photoLibrary.assetCollections {
let changeDetails = changeInstance.changeDetails(for: fetchResultCollection)
if isChangesCount(changeDetails: changeDetails) == true {
return true
}
}
return false
}

if isAlbumsChanges() || isCollectionsChanges() {
DispatchQueue.main.async {
self.albumPopView.show(false, duration: self.configure.popup.duration)
self.photoLibrary.fetchCollection(configure: self.configure)
}
return nil
}else {
guard let changeFetchResult = self.focusedCollection?.fetchResult else { return nil }
guard let changes = changeInstance.changeDetails(for: changeFetchResult) else { return nil }
return changes
}
}

public func photoLibraryDidChange(_ changeInstance: PHChange) {
var addIndex = 0
if getfocusedIndex() == 0 {
addIndex = self.usedCameraButton ? 1 : 0
}
DispatchQueue.main.async {
guard let changeFetchResult = self.focusedCollection?.fetchResult else { return }
guard let changes = changeInstance.changeDetails(for: changeFetchResult) else { return }
guard let changes = self.getChanges(changeInstance) else {
return
}

if changes.hasIncrementalChanges, self.configure.groupByFetch == nil {
var deletedSelectedAssets = false
var order = 0
Expand Down

0 comments on commit d6cf40d

Please sign in to comment.