diff --git a/NohanaImagePicker/ActivityIndicatable.swift b/NohanaImagePicker/ActivityIndicatable.swift index db783dd..114acb3 100644 --- a/NohanaImagePicker/ActivityIndicatable.swift +++ b/NohanaImagePicker/ActivityIndicatable.swift @@ -16,12 +16,12 @@ public protocol ActivityIndicatable { func isProgressing() -> Bool - func updateVisibility(of activityIndicator: UIView) + func updateVisibilityOfActivityIndicator(_ activityIndicator: UIView) } public extension ActivityIndicatable where Self: UIViewController { - func updateVisibility(of activityIndicator: UIView) { + func updateVisibilityOfActivityIndicator(_ activityIndicator: UIView) { if isProgressing() { if !view.subviews.contains(activityIndicator) { view.addSubview(activityIndicator) diff --git a/NohanaImagePicker/AlbumListViewController.swift b/NohanaImagePicker/AlbumListViewController.swift index 8faaf15..162beee 100644 --- a/NohanaImagePicker/AlbumListViewController.swift +++ b/NohanaImagePicker/AlbumListViewController.swift @@ -43,7 +43,7 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity if let nohanaImagePickerController = nohanaImagePickerController { title = NSLocalizedString("albumlist.title", tableName: "NohanaImagePicker", bundle: nohanaImagePickerController.assetBundle, comment: "") setUpToolbarItems() - navigationController?.setToolbarHidden(nohanaImagePickerController.toolbarHidden ?? false, animated: false) + navigationController?.setToolbarHidden(nohanaImagePickerController.toolbarHidden ,animated: false) } setUpEmptyIndicator() setUpActivityIndicator() @@ -153,8 +153,10 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity width: cell.thumbnailImageView.frame.size.width * UIScreen.main.scale, height: cell.thumbnailImageView.frame.size.width * UIScreen.main.scale ) - if let lastAsset = albumList.last { - lastAsset.image(imageSize, handler: { (imageData) -> Void in + let albumCount = albumList.count + if albumCount > 0 { + let lastAsset = albumList[albumCount - 1] + lastAsset.image(targetSize: imageSize, handler: { (imageData) -> Void in DispatchQueue.main.async(execute: { () -> Void in if let imageData = imageData { if cell.tag == (indexPath as NSIndexPath).row { @@ -262,7 +264,8 @@ extension UIViewController { } func setToolbarTitle(_ nohanaImagePickerController:NohanaImagePickerController) { - guard toolbarItems?.count >= 2 else { + let count: Int? = toolbarItems?.count + guard count != nil && count! >= 2 else { return } guard let infoButton = toolbarItems?[1] else { @@ -283,8 +286,8 @@ extension UIViewController { // MARK: - Notification func addPickPhotoKitAssetNotificationObservers() { - NotificationCenter.default.addObserver(self, selector: #selector(AlbumListViewController.didPickPhotoKitAsset(_:)), name: NSNotification.Name(rawValue: NotificationInfo.Asset.PhotoKit.didPick), object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(AlbumListViewController.didDropPhotoKitAsset(_:)), name: NSNotification.Name(rawValue: NotificationInfo.Asset.PhotoKit.didDrop), object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(AlbumListViewController.didPickPhotoKitAsset(_:)), name: NotificationInfo.Asset.PhotoKit.didPick, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(AlbumListViewController.didDropPhotoKitAsset(_:)), name: NotificationInfo.Asset.PhotoKit.didDrop, object: nil) } func didPickPhotoKitAsset(_ notification: Notification) { diff --git a/NohanaImagePicker/AssetCell.swift b/NohanaImagePicker/AssetCell.swift index 39fd1c4..653c86e 100644 --- a/NohanaImagePicker/AssetCell.swift +++ b/NohanaImagePicker/AssetCell.swift @@ -55,8 +55,8 @@ class AssetCell: UICollectionViewCell { func update(asset: AssetType, nohanaImagePickerController: NohanaImagePickerController) { self.asset = asset self.nohanaImagePickerController = nohanaImagePickerController - self.pickButton.isSelected = nohanaImagePickerController.pickedAssetList.isPicked(asset) ?? false + self.pickButton.isSelected = nohanaImagePickerController.pickedAssetList.isPicked(asset) self.overlayView.isHidden = !pickButton.isSelected - self.pickButton.isHidden = !(nohanaImagePickerController.canPickAsset(asset) ?? true) + self.pickButton.isHidden = !(nohanaImagePickerController.canPickAsset(asset) ) } } diff --git a/NohanaImagePicker/AssetDetailListViewController.swift b/NohanaImagePicker/AssetDetailListViewController.swift index 2bf0dc0..24821c4 100644 --- a/NohanaImagePicker/AssetDetailListViewController.swift +++ b/NohanaImagePicker/AssetDetailListViewController.swift @@ -67,13 +67,14 @@ class AssetDetailListViewController: AssetListViewController { return } let asset = photoKitAssetList[(indexPath as NSIndexPath).item] - pickButton.isSelected = nohanaImagePickerController.pickedAssetList.isPicked(asset) ?? false - pickButton.isHidden = !(nohanaImagePickerController.canPickAsset(asset) ?? true) + pickButton.isSelected = nohanaImagePickerController.pickedAssetList.isPicked(asset) + pickButton.isHidden = !(nohanaImagePickerController.canPickAsset(asset) ) nohanaImagePickerController.delegate?.nohanaImagePicker?(nohanaImagePickerController, assetDetailListViewController: self, didChangeAssetDetailPage: indexPath, photoKitAsset: asset.originalAsset) } override func scrollCollectionView(to indexPath: IndexPath) { - guard photoKitAssetList?.count > 0 else { + let count: Int? = photoKitAssetList?.count + guard count != nil && count! > 0 else { return } DispatchQueue.main.async { @@ -121,7 +122,7 @@ class AssetDetailListViewController: AssetListViewController { height: cellSize.height * UIScreen.main.scale ) let asset = photoKitAssetList[(indexPath as NSIndexPath).item] - asset.image(imageSize) { (imageData) -> Void in + asset.image(targetSize: imageSize) { (imageData) -> Void in DispatchQueue.main.async(execute: { () -> Void in if let imageData = imageData { if cell.tag == (indexPath as NSIndexPath).item { diff --git a/NohanaImagePicker/AssetListViewController.swift b/NohanaImagePicker/AssetListViewController.swift index 7698061..950f955 100644 --- a/NohanaImagePicker/AssetListViewController.swift +++ b/NohanaImagePicker/AssetListViewController.swift @@ -79,7 +79,8 @@ class AssetListViewController: UICollectionViewController { } func scrollCollectionView(to indexPath: IndexPath) { - guard photoKitAssetList?.count > 0 else { + let count: Int? = photoKitAssetList?.count + guard count != nil && count! > 0 else { return } DispatchQueue.main.async { @@ -116,14 +117,14 @@ class AssetListViewController: UICollectionViewController { fatalError("failed to dequeueReusableCellWithIdentifier(\"AssetCell\")") } cell.tag = indexPath.item - cell.update(photoKitAssetList[indexPath.row], nohanaImagePickerController: nohanaImagePickerController) + cell.update(asset: photoKitAssetList[indexPath.row], nohanaImagePickerController: nohanaImagePickerController) let imageSize = CGSize( width: cellSize.width * UIScreen.main.scale, height: cellSize.height * UIScreen.main.scale ) let asset = photoKitAssetList[indexPath.item] - asset.image(imageSize) { (imageData) -> Void in + asset.image(targetSize: imageSize) { (imageData) -> Void in DispatchQueue.main.async(execute: { () -> Void in if let imageData = imageData { if cell.tag == indexPath.item { diff --git a/NohanaImagePicker/ContractingAnimationController.swift b/NohanaImagePicker/ContractingAnimationController.swift index 9a3a12f..55e1f15 100644 --- a/NohanaImagePicker/ContractingAnimationController.swift +++ b/NohanaImagePicker/ContractingAnimationController.swift @@ -46,8 +46,7 @@ class ContractingAnimationController: NSObject, UIViewControllerAnimatedTransiti func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { guard let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from) as? AssetDetailListViewController, - let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) as? AssetListViewController, - let containerView = transitionContext.containerView + let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) as? AssetListViewController else { return } @@ -69,8 +68,8 @@ class ContractingAnimationController: NSObject, UIViewControllerAnimatedTransiti contractingImageView.clipsToBounds = true contractingImageView.frame = Size.contractingAnimationFromCellRect(fromVC, fromCell: fromCell, contractingImageSize: contractingImageView.image!.size) - containerView.addSubview(toVC.view) - containerView.addSubview(contractingImageView) + transitionContext.containerView.addSubview(toVC.view) + transitionContext.containerView.addSubview(contractingImageView) toVC.view.alpha = 0 fromCell.alpha = 0 toCell.alpha = 0 diff --git a/NohanaImagePicker/EmptyIndicatable.swift b/NohanaImagePicker/EmptyIndicatable.swift index 3d42527..5d0f38c 100644 --- a/NohanaImagePicker/EmptyIndicatable.swift +++ b/NohanaImagePicker/EmptyIndicatable.swift @@ -16,11 +16,11 @@ public protocol EmptyIndicatable { func isEmpty() -> Bool - func updateVisibility(of emptyIndicator: UIView) + func updateVisibilityOfEmptyIndicator(_ emptyIndicator: UIView) } public extension EmptyIndicatable where Self: UIViewController { - func updateVisibility(of emptyIndicator: UIView) { + func updateVisibilityOfEmptyIndicator(_ emptyIndicator: UIView) { if isEmpty(){ if !view.subviews.contains(emptyIndicator) { view.addSubview(emptyIndicator) diff --git a/NohanaImagePicker/ExpandingAnimationController.swift b/NohanaImagePicker/ExpandingAnimationController.swift index 2a2d09d..b31f36e 100644 --- a/NohanaImagePicker/ExpandingAnimationController.swift +++ b/NohanaImagePicker/ExpandingAnimationController.swift @@ -42,8 +42,7 @@ class ExpandingAnimationController: NSObject, UIViewControllerAnimatedTransition func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { guard let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from) as? AssetListViewController, - let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) as? AssetDetailListViewController, - let containerView = transitionContext.containerView + let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) as? AssetDetailListViewController else { return } @@ -53,8 +52,8 @@ class ExpandingAnimationController: NSObject, UIViewControllerAnimatedTransition expandingImageView.clipsToBounds = true expandingImageView.frame = Size.expandingAnimationFromCellRect(fromVC, fromCell: fromCell) - containerView.addSubview(toVC.view) - containerView.addSubview(expandingImageView) + transitionContext.containerView.addSubview(toVC.view) + transitionContext.containerView.addSubview(expandingImageView) toVC.view.alpha = 0 toVC.collectionView?.isHidden = true toVC.view.backgroundColor = UIColor.black diff --git a/NohanaImagePicker/ItemListType.swift b/NohanaImagePicker/ItemListType.swift index 06963d9..633cf72 100644 --- a/NohanaImagePicker/ItemListType.swift +++ b/NohanaImagePicker/ItemListType.swift @@ -21,9 +21,15 @@ public protocol ItemListType: Collection { subscript (index: Int) -> Item { get } } +extension ItemListType { + public func index(after i: Int) -> Int { + return i + 1 + } +} + public protocol AssetType { var identifier:Int { get } - func image(targetSize:CGSize, handler: (ImageData?) -> Void) + func image(targetSize:CGSize, handler: @escaping (ImageData?) -> Void) } public struct ImageData { diff --git a/NohanaImagePicker/MomentViewController.swift b/NohanaImagePicker/MomentViewController.swift index 1e2eaa9..4bf9a7e 100644 --- a/NohanaImagePicker/MomentViewController.swift +++ b/NohanaImagePicker/MomentViewController.swift @@ -33,7 +33,8 @@ class MomentViewController: AssetListViewController, ActivityIndicatable { } override func scrollCollectionView(to indexPath: IndexPath) { - guard momentAlbumList?.count > 0 else { + let count: Int? = momentAlbumList?.count + guard count != nil && count! > 0 else { return } DispatchQueue.main.async { @@ -78,13 +79,13 @@ class MomentViewController: AssetListViewController, ActivityIndicatable { let asset = momentAlbumList[indexPath.section][indexPath.row] cell.tag = indexPath.item - cell.update(asset, nohanaImagePickerController: nohanaImagePickerController) + cell.update(asset: asset, nohanaImagePickerController: nohanaImagePickerController) let imageSize = CGSize( width: cellSize.width * UIScreen.main.scale, height: cellSize.height * UIScreen.main.scale ) - asset.image(imageSize) { (imageData) -> Void in + asset.image(targetSize: imageSize) { (imageData) -> Void in DispatchQueue.main.async(execute: { () -> Void in if let imageData = imageData { if cell.tag == indexPath.item { diff --git a/NohanaImagePicker/NohanaImagePickerController.swift b/NohanaImagePicker/NohanaImagePickerController.swift index b298063..c3032da 100644 --- a/NohanaImagePicker/NohanaImagePickerController.swift +++ b/NohanaImagePicker/NohanaImagePickerController.swift @@ -128,11 +128,11 @@ open class NohanaImagePickerController: UIViewController { } open func pickAsset(_ asset: AssetType) { - pickedAssetList.pickAsset(asset) + _ = pickedAssetList.pickAsset(asset) } open func dropAsset(_ asset: AssetType) { - pickedAssetList.dropAsset(asset) + _ = pickedAssetList.dropAsset(asset) } } diff --git a/NohanaImagePicker/NotificationInfo.swift b/NohanaImagePicker/NotificationInfo.swift index 1210977..e7362cb 100644 --- a/NohanaImagePicker/NotificationInfo.swift +++ b/NohanaImagePicker/NotificationInfo.swift @@ -17,12 +17,12 @@ struct NotificationInfo { struct Asset { struct PhotoKit { - static let didPick = "jp.co.nohana.NotificationName.Asset.PhotoKit.didPick" + static let didPick = Notification.Name("jp.co.nohana.NotificationName.Asset.PhotoKit.didPick") static let didPickUserInfoKeyAsset = "asset" static let didPickUserInfoKeyPickedAssetsCount = "pickedAssetsCount" - static let didDrop = "jp.co.nohana.NotificationName.Asset.PhotoKit.didDrop" + static let didDrop = Notification.Name("jp.co.nohana.NotificationName.Asset.PhotoKit.didDrop") static let didDropUserInfoKeyAsset = "asset" static let didDropUserInfoKeyPickedAssetsCount = "pickedAssetsCount" } } -} \ No newline at end of file +} diff --git a/NohanaImagePicker/PhotoKitAlbumList.swift b/NohanaImagePicker/PhotoKitAlbumList.swift index b933821..8eb7e78 100644 --- a/NohanaImagePicker/PhotoKitAlbumList.swift +++ b/NohanaImagePicker/PhotoKitAlbumList.swift @@ -48,8 +48,8 @@ public class PhotoKitAlbumList: ItemListType { } open func update(_ handler:(() -> Void)?) { - DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default).async { () -> Void in - var albumListFetchResult: [PHFetchResult] = [] as! [PHFetchResult] + DispatchQueue.global(qos: .default).async { + var albumListFetchResult: [PHFetchResult] = [] for type in self.assetCollectionTypes { albumListFetchResult = albumListFetchResult + [PHAssetCollection.fetchAssetCollections(with: type, subtype: .any, options: nil)] } @@ -58,19 +58,16 @@ public class PhotoKitAlbumList: ItemListType { var tmpAlbumList:[Item] = [] let isAssetCollectionSubtypeAny = self.assetCollectionSubtypes.contains(.any) for fetchResult in albumListFetchResult { - fetchResult.enumerateObjects { (album, index, stop) -> Void in - guard let album = album as? PHAssetCollection else { - return - } + fetchResult.enumerateObjects({ (album, index, stop) in if self.assetCollectionSubtypes.contains(album.assetCollectionSubtype) || isAssetCollectionSubtypeAny { if self.shouldShowEmptyAlbum || PHAsset.fetchAssets(in: album, options: PhotoKitAssetList.fetchOptions(self.mediaType)).count != 0 { tmpAlbumList.append(PhotoKitAssetList(album: album, mediaType: self.mediaType)) } } - } + }) } if self.assetCollectionTypes == [.moment] { - self.albumList = tmpAlbumList.sorted{ $0.date?.timeIntervalSince1970 < $1.date?.timeIntervalSince1970 } + self.albumList = tmpAlbumList.sorted{ $0.date!.timeIntervalSince1970 < $1.date!.timeIntervalSince1970 } } else { self.albumList = tmpAlbumList } diff --git a/NohanaImagePicker/PhotoKitAsset.swift b/NohanaImagePicker/PhotoKitAsset.swift index 55909b6..0899409 100644 --- a/NohanaImagePicker/PhotoKitAsset.swift +++ b/NohanaImagePicker/PhotoKitAsset.swift @@ -15,7 +15,7 @@ */ import Photos -open class PhotoKitAsset: AssetType { +public class PhotoKitAsset :AssetType { let asset: PHAsset @@ -23,7 +23,7 @@ open class PhotoKitAsset: AssetType { self.asset = asset } - open var originalAsset: PHAsset { + public var originalAsset: PHAsset { get { return asset as PHAsset } @@ -31,17 +31,17 @@ open class PhotoKitAsset: AssetType { // MARK: - AssetType - open var identifier:Int { + public var identifier:Int { get { return asset.localIdentifier.hash } } - open func image(targetSize:CGSize, handler: @escaping (ImageData?) -> Void) { + public func image(targetSize:CGSize, handler: @escaping (ImageData?) -> Void) { let option = PHImageRequestOptions() option.isNetworkAccessAllowed = true - - PHImageManager.default().requestImage( + + _ = PHImageManager.default().requestImage( for: self.asset, targetSize: targetSize, contentMode: .aspectFit, diff --git a/NohanaImagePicker/PhotoKitAssetList.swift b/NohanaImagePicker/PhotoKitAssetList.swift index a0f41f9..935d8ce 100644 --- a/NohanaImagePicker/PhotoKitAssetList.swift +++ b/NohanaImagePicker/PhotoKitAssetList.swift @@ -20,7 +20,7 @@ open class PhotoKitAssetList :ItemListType { fileprivate let mediaType: MediaType open let assetList: PHAssetCollection - fileprivate var fetchResult: PHFetchResult! + fileprivate var fetchResult: PHFetchResult! init(album: PHAssetCollection, mediaType: MediaType) { self.assetList = album @@ -63,12 +63,7 @@ open class PhotoKitAssetList :ItemListType { } open subscript (index: Int) -> Item { - get { - guard let asset = fetchResult[index] as? PHAsset else { - fatalError("invalid index") - } - return Item(asset: asset) - } + return Item(asset: fetchResult.object(at: index)) } // MARK: - CollectionType diff --git a/NohanaImagePicker/SwipeInteractionController.swift b/NohanaImagePicker/SwipeInteractionController.swift index 4bf0a8f..ec056a0 100644 --- a/NohanaImagePicker/SwipeInteractionController.swift +++ b/NohanaImagePicker/SwipeInteractionController.swift @@ -21,11 +21,12 @@ class SwipeInteractionController: UIPercentDrivenInteractiveTransition { var viewController: UIViewController? func attachToViewController(_ viewController: UIViewController) { - guard viewController.navigationController?.viewControllers.count > 1 else { + let count: Int? = viewController.navigationController?.viewControllers.count + guard count != nil && count! > 1 else { return } let target = viewController.navigationController?.value(forKey: "_cachedInteractionController") - let gesture = UIScreenEdgePanGestureRecognizer(target: target, action: Selector("handleNavigationTransition:")) + let gesture = UIScreenEdgePanGestureRecognizer(target: target, action: Selector(("handleNavigationTransition:"))) gesture.edges = .left viewController.view.addGestureRecognizer(gesture) }