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

Reset with first album when there're no album after user granted some of photos #330

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Sources/Controller/ImagePickerController+Albums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extension ImagePickerController: AlbumsViewControllerDelegate {
}

func select(album: PHAssetCollection) {
self.currentAlbum = album
assetsViewController.showAssets(in: album)
albumButton.setTitle((album.localizedTitle ?? "") + " ", for: .normal)
albumButton.sizeToFit()
Expand Down
31 changes: 28 additions & 3 deletions Sources/Controller/ImagePickerController+Closure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,37 @@ import Photos
}

private func authorize(_ authorized: @escaping () -> Void) {
PHPhotoLibrary.requestAuthorization { (status) in
switch status {
if #available(iOS 14, *) {
let immediateStatus = PHPhotoLibrary.authorizationStatus(for: .readWrite)
switch immediateStatus {
case .authorized,
.limited:
DispatchQueue.main.async(execute: authorized)
default:
PHPhotoLibrary.requestAuthorization(for: .readWrite) { (status) in
switch status {
case .authorized,
.limited:
DispatchQueue.main.async(execute: authorized)
default:
break
}
}
}
} else {
let immediateStatus = PHPhotoLibrary.authorizationStatus()
switch immediateStatus {
case .authorized:
DispatchQueue.main.async(execute: authorized)
default:
break
PHPhotoLibrary.requestAuthorization { (status) in
switch status {
case .authorized:
DispatchQueue.main.async(execute: authorized)
default:
break
}
}
}
}
}
Expand Down
33 changes: 32 additions & 1 deletion Sources/Controller/ImagePickerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import Photos
let dropdownTransitionDelegate = DropdownTransitionDelegate()
let zoomTransitionDelegate = ZoomTransitionDelegate()

lazy var albums: [PHAssetCollection] = {
private func loadAlbums() -> [PHAssetCollection] {
// We don't want collections without assets.
// I would like to do that with PHFetchOptions: fetchOptions.predicate = NSPredicate(format: "estimatedAssetCount > 0")
// But that doesn't work...
Expand All @@ -74,6 +74,11 @@ import Photos
let assetsFetchResult = PHAsset.fetchAssets(in: $0, options: fetchOptions)
return assetsFetchResult.count > 0
}
}
var currentAlbum: PHAssetCollection?

lazy var albums: [PHAssetCollection] = {
self.loadAlbums()
}()

public init(selectedAssets: [PHAsset] = []) {
Expand All @@ -86,6 +91,16 @@ import Photos
fatalError("init(coder:) has not been implemented")
}

open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
PHPhotoLibrary.shared().register(self)
}

open override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
PHPhotoLibrary.shared().unregisterChangeObserver(self)
}

public override func viewDidLoad() {
super.viewDidLoad()

Expand Down Expand Up @@ -161,3 +176,19 @@ import Photos
albumButton.isHidden = albums.count < 2
}
}

extension ImagePickerController: PHPhotoLibraryChangeObserver {
public func photoLibraryDidChange(_ changeInstance: PHChange) {
if let currentAlbum = currentAlbum,
changeInstance.changeDetails(for: currentAlbum) != nil {
return
}
DispatchQueue.main.async {
self.albums = self.loadAlbums()
self.updateAlbumButton()
if let firstAlbum = self.albums.first {
self.select(album: firstAlbum)
}
}
}
}