diff --git a/NohanaImagePicker/AlbumListViewController.swift b/NohanaImagePicker/AlbumListViewController.swift index 538b5b5..3625523 100644 --- a/NohanaImagePicker/AlbumListViewController.swift +++ b/NohanaImagePicker/AlbumListViewController.swift @@ -27,14 +27,16 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity } } - weak var nohanaImagePickerController: NohanaImagePickerController! + weak var nohanaImagePickerController: NohanaImagePickerController? var photoKitAlbumList: PhotoKitAlbumList! override func viewDidLoad() { super.viewDidLoad() - title = NSLocalizedString("albumlist.title", tableName: "NohanaImagePicker", bundle: nohanaImagePickerController.assetBundle, comment: "") - setUpToolbarItems() - navigationController?.setToolbarHidden(nohanaImagePickerController.toolbarHidden ?? false, animated: false) + if let nohanaImagePickerController = nohanaImagePickerController { + title = NSLocalizedString("albumlist.title", tableName: "NohanaImagePicker", bundle: nohanaImagePickerController.assetBundle, comment: "") + setUpToolbarItems() + navigationController?.setToolbarHidden(nohanaImagePickerController.toolbarHidden ?? false, animated: false) + } setUpEmptyIndicator() setUpActivityIndicator() self.view.backgroundColor = ColorConfig.backgroundColor @@ -46,7 +48,9 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) - setToolbarTitle(nohanaImagePickerController) + if let nohanaImagePickerController = nohanaImagePickerController { + setToolbarTitle(nohanaImagePickerController) + } if let indexPathForSelectedRow = tableView.indexPathForSelectedRow { tableView.deselectRowAtIndexPath(indexPathForSelectedRow, animated: true) } @@ -63,6 +67,9 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity guard let sectionType = AlbumListViewControllerSectionType(rawValue: indexPath.section) else { fatalError("Invalid section") } + guard let nohanaImagePickerController = nohanaImagePickerController else { + return + } switch sectionType { case .Moment: nohanaImagePickerController.delegate?.nohanaImagePickerDidSelectMoment?(nohanaImagePickerController) @@ -103,7 +110,11 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity switch sectionType { case .Moment: - return nohanaImagePickerController.shouldShowMoment ? 1 : 0 + if let nohanaImagePickerController = nohanaImagePickerController { + return nohanaImagePickerController.shouldShowMoment ? 1 : 0 + } + return 0 + case .Albums: return photoKitAlbumList.count } @@ -119,7 +130,9 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity guard let cell = tableView.dequeueReusableCellWithIdentifier("MomentAlbumCell") as? AlbumCell else { fatalError("failed to dequeueReusableCellWithIdentifier(\"MomentAlbumCell\")") } - cell.titleLabel?.text = NSLocalizedString("albumlist.moment.title", tableName: "NohanaImagePicker", bundle: nohanaImagePickerController.assetBundle, comment: "") + if let nohanaImagePickerController = nohanaImagePickerController { + cell.titleLabel?.text = NSLocalizedString("albumlist.moment.title", tableName: "NohanaImagePicker", bundle: nohanaImagePickerController.assetBundle, comment: "") + } return cell case .Albums: guard let cell = tableView.dequeueReusableCellWithIdentifier("AlbumCell") as? AlbumCell else { @@ -162,8 +175,8 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity momentViewController.momentAlbumList = PhotoKitAlbumList( assetCollectionTypes: [.Moment], assetCollectionSubtypes: [.Any], - mediaType: nohanaImagePickerController.mediaType, - shouldShowEmptyAlbum: nohanaImagePickerController.shouldShowEmptyAlbum, + mediaType: nohanaImagePickerController!.mediaType, + shouldShowEmptyAlbum: nohanaImagePickerController!.shouldShowEmptyAlbum, handler: { () -> Void in dispatch_async(dispatch_get_main_queue(), { [weak momentViewController] in momentViewController?.isLoading = false @@ -181,7 +194,9 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity // MARK: - IBAction @IBAction func didPushCancel(sender: AnyObject) { - nohanaImagePickerController.delegate?.nohanaImagePickerDidCancel(nohanaImagePickerController) + if let nohanaImagePickerController = nohanaImagePickerController { + nohanaImagePickerController.delegate?.nohanaImagePickerDidCancel(nohanaImagePickerController) + } } // MARK: - EmptyIndicatable @@ -190,6 +205,9 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity func setUpEmptyIndicator() { let frame = CGRect(origin: CGPoint.zero, size: Size.screenRectWithoutAppBar(self).size) + guard let nohanaImagePickerController = nohanaImagePickerController else { + return + } emptyIndicator = AlbumListEmptyIndicator( message: NSLocalizedString("albumlist.empty.message", tableName: "NohanaImagePicker", bundle: nohanaImagePickerController.assetBundle, comment: ""), description: NSLocalizedString("albumlist.empty.description", tableName: "NohanaImagePicker", bundle: nohanaImagePickerController.assetBundle, comment: ""), diff --git a/NohanaImagePicker/AssetCell.swift b/NohanaImagePicker/AssetCell.swift index 0ed54d9..5335500 100644 --- a/NohanaImagePicker/AssetCell.swift +++ b/NohanaImagePicker/AssetCell.swift @@ -14,18 +14,19 @@ class AssetCell: UICollectionViewCell { @IBOutlet weak var pickButton: UIButton! @IBOutlet weak var overlayView: UIView! - weak var nohanaImagePickerController: NohanaImagePickerController! + weak var nohanaImagePickerController: NohanaImagePickerController? var asset: AssetType? override func willMoveToSuperview(newSuperview: UIView?) { super.willMoveToSuperview(newSuperview) - - pickButton.setImage( - UIImage(named: ImageName.AssetCell.PickButton.SizeM.dropped, inBundle: nohanaImagePickerController.assetBundle, compatibleWithTraitCollection: nil), - forState: .Normal) - pickButton.setImage( - UIImage(named: ImageName.AssetCell.PickButton.SizeM.picked, inBundle: nohanaImagePickerController.assetBundle, compatibleWithTraitCollection: nil), - forState: [.Normal, .Selected]) + if let nohanaImagePickerController = nohanaImagePickerController { + pickButton.setImage( + UIImage(named: ImageName.AssetCell.PickButton.SizeM.dropped, inBundle: nohanaImagePickerController.assetBundle, compatibleWithTraitCollection: nil), + forState: .Normal) + pickButton.setImage( + UIImage(named: ImageName.AssetCell.PickButton.SizeM.picked, inBundle: nohanaImagePickerController.assetBundle, compatibleWithTraitCollection: nil), + forState: [.Normal, .Selected]) + } } @IBAction func didPushPickButton(sender: UIButton) { @@ -33,11 +34,11 @@ class AssetCell: UICollectionViewCell { return } if pickButton.selected { - if nohanaImagePickerController.pickedAssetList.dropAsset(asset) { + if nohanaImagePickerController!.pickedAssetList.dropAsset(asset) { pickButton.selected = false } } else { - if nohanaImagePickerController.pickedAssetList.pickAsset(asset) { + if nohanaImagePickerController!.pickedAssetList.pickAsset(asset) { pickButton.selected = true } } diff --git a/NohanaImagePicker/AssetDetailListViewController.swift b/NohanaImagePicker/AssetDetailListViewController.swift index e5e92db..d5ba6cd 100644 --- a/NohanaImagePicker/AssetDetailListViewController.swift +++ b/NohanaImagePicker/AssetDetailListViewController.swift @@ -28,12 +28,14 @@ class AssetDetailListViewController: AssetListViewController { override func viewDidLoad() { super.viewDidLoad() - pickButton.setImage( - UIImage(named: ImageName.AssetCell.PickButton.SizeL.dropped, inBundle: nohanaImagePickerController.assetBundle, compatibleWithTraitCollection: nil), - forState: .Normal) - pickButton.setImage( - UIImage(named: ImageName.AssetCell.PickButton.SizeL.picked, inBundle: nohanaImagePickerController.assetBundle, compatibleWithTraitCollection: nil), - forState: [.Normal, .Selected]) + if let nohanaImagePickerController = nohanaImagePickerController { + pickButton.setImage( + UIImage(named: ImageName.AssetCell.PickButton.SizeL.dropped, inBundle: nohanaImagePickerController.assetBundle, compatibleWithTraitCollection: nil), + forState: .Normal) + pickButton.setImage( + UIImage(named: ImageName.AssetCell.PickButton.SizeL.picked, inBundle: nohanaImagePickerController.assetBundle, compatibleWithTraitCollection: nil), + forState: [.Normal, .Selected]) + } } override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { @@ -55,6 +57,9 @@ class AssetDetailListViewController: AssetListViewController { } func didChangeAssetDetailPage(indexPath:NSIndexPath) { + guard let nohanaImagePickerController = nohanaImagePickerController else { + return + } let asset = photoKitAssetList[indexPath.item] pickButton.selected = nohanaImagePickerController.pickedAssetList.isPicked(asset) ?? false pickButton.hidden = !(nohanaImagePickerController.canPickAsset(asset) ?? true) @@ -83,11 +88,11 @@ class AssetDetailListViewController: AssetListViewController { @IBAction func didPushPickButton(sender: UIButton) { let asset = photoKitAssetList[currentIndexPath.row] if pickButton.selected { - if nohanaImagePickerController.pickedAssetList.dropAsset(asset) { + if nohanaImagePickerController!.pickedAssetList.dropAsset(asset) { pickButton.selected = false } } else { - if nohanaImagePickerController.pickedAssetList.pickAsset(asset) { + if nohanaImagePickerController!.pickedAssetList.pickAsset(asset) { pickButton.selected = true } } @@ -96,8 +101,8 @@ class AssetDetailListViewController: AssetListViewController { // MARK: - UICollectionViewDelegate override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { - guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetDetailCell", forIndexPath: indexPath) as? AssetDetailCell - else { + guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetDetailCell", forIndexPath: indexPath) as? AssetDetailCell, + nohanaImagePickerController = nohanaImagePickerController else { fatalError("failed to dequeueReusableCellWithIdentifier(\"AssetDetailCell\")") } cell.invalidateIntrinsicContentSize() diff --git a/NohanaImagePicker/AssetListViewController.swift b/NohanaImagePicker/AssetListViewController.swift index fd50a83..863c2f6 100644 --- a/NohanaImagePicker/AssetListViewController.swift +++ b/NohanaImagePicker/AssetListViewController.swift @@ -11,7 +11,7 @@ import Photos class AssetListViewController: UICollectionViewController { - weak var nohanaImagePickerController: NohanaImagePickerController! + weak var nohanaImagePickerController: NohanaImagePickerController? var photoKitAssetList: PhotoKitAssetList! override func viewDidLoad() { @@ -24,6 +24,9 @@ class AssetListViewController: UICollectionViewController { var cellSize: CGSize { get { + guard let nohanaImagePickerController = nohanaImagePickerController else { + return CGSize.zero + } var numberOfColumns = nohanaImagePickerController.numberOfColumnsInLandscape if UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation) { numberOfColumns = nohanaImagePickerController.numberOfColumnsInPortrait @@ -40,7 +43,9 @@ class AssetListViewController: UICollectionViewController { override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) - setToolbarTitle(nohanaImagePickerController) + if let nohanaImagePickerController = nohanaImagePickerController { + setToolbarTitle(nohanaImagePickerController) + } view.invalidateIntrinsicContentSize() for subView in view.subviews { subView.invalidateIntrinsicContentSize() @@ -91,11 +96,14 @@ class AssetListViewController: UICollectionViewController { // MARK: - UICollectionViewDelegate override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { - nohanaImagePickerController.delegate?.nohanaImagePicker?(nohanaImagePickerController, didSelectPhotoKitAsset: photoKitAssetList[indexPath.item].originalAsset) + if let nohanaImagePickerController = nohanaImagePickerController { + nohanaImagePickerController.delegate?.nohanaImagePicker?(nohanaImagePickerController, didSelectPhotoKitAsset: photoKitAssetList[indexPath.item].originalAsset) + } } override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { - guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetCell", forIndexPath: indexPath) as? AssetCell else { + guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetCell", forIndexPath: indexPath) as? AssetCell, + nohanaImagePickerController = nohanaImagePickerController else { fatalError("failed to dequeueReusableCellWithIdentifier(\"AssetCell\")") } cell.tag = indexPath.item @@ -139,8 +147,8 @@ class AssetListViewController: UICollectionViewController { // MARK: - IBAction @IBAction func didPushDone(sender: AnyObject) { - let pickedPhotoKitAssets = nohanaImagePickerController.pickedAssetList.map{ ($0 as! PhotoKitAsset).originalAsset } - nohanaImagePickerController.delegate?.nohanaImagePicker(nohanaImagePickerController, didFinishPickingPhotoKitAssets: pickedPhotoKitAssets ) + let pickedPhotoKitAssets = nohanaImagePickerController!.pickedAssetList.map{ ($0 as! PhotoKitAsset).originalAsset } + nohanaImagePickerController!.delegate?.nohanaImagePicker(nohanaImagePickerController!, didFinishPickingPhotoKitAssets: pickedPhotoKitAssets ) } } diff --git a/NohanaImagePicker/MomentViewController.swift b/NohanaImagePicker/MomentViewController.swift index c782912..497b953 100644 --- a/NohanaImagePicker/MomentViewController.swift +++ b/NohanaImagePicker/MomentViewController.swift @@ -19,7 +19,9 @@ class MomentViewController: AssetListViewController, ActivityIndicatable { } override func updateTitle() { - title = NSLocalizedString("albumlist.moment.title", tableName: "NohanaImagePicker", bundle: nohanaImagePickerController.assetBundle, comment: "") + if let nohanaImagePickerController = nohanaImagePickerController { + title = NSLocalizedString("albumlist.moment.title", tableName: "NohanaImagePicker", bundle: nohanaImagePickerController.assetBundle, comment: "") + } } override func scrollCollectionViewToInitialPosition() { @@ -53,8 +55,9 @@ class MomentViewController: AssetListViewController, ActivityIndicatable { // MARK: - UICollectionViewDelegate override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { - guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetCell", forIndexPath: indexPath) as? AssetCell else { - return UICollectionViewCell(frame: CGRectZero) + guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetCell", forIndexPath: indexPath) as? AssetCell, + nohanaImagePickerController = nohanaImagePickerController else { + fatalError("failed to dequeueReusableCellWithIdentifier(\"AssetCell\")") } let asset = momentAlbumList[indexPath.section][indexPath.row] @@ -118,7 +121,9 @@ class MomentViewController: AssetListViewController, ActivityIndicatable { // MARK: - UICollectionViewDelegate override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { - nohanaImagePickerController.delegate?.nohanaImagePicker?(nohanaImagePickerController, didSelectPhotoKitAsset: momentAlbumList[indexPath.section][indexPath.row].originalAsset) + if let nohanaImagePickerController = nohanaImagePickerController { + nohanaImagePickerController.delegate?.nohanaImagePicker?(nohanaImagePickerController, didSelectPhotoKitAsset: momentAlbumList[indexPath.section][indexPath.row].originalAsset) + } } // MARK: - Storyboard diff --git a/NohanaImagePicker/PickedAssetList.swift b/NohanaImagePicker/PickedAssetList.swift index e6f1aae..7aba6cb 100644 --- a/NohanaImagePicker/PickedAssetList.swift +++ b/NohanaImagePicker/PickedAssetList.swift @@ -11,7 +11,7 @@ import Foundation class PickedAssetList: ItemListType { var assetlist: Array = [] - weak var nohanaImagePickerController: NohanaImagePickerController! + weak var nohanaImagePickerController: NohanaImagePickerController? // MARK: - ItemListType @@ -56,19 +56,19 @@ class PickedAssetList: ItemListType { } let assetsCountBeforePicking = self.count if asset is PhotoKitAsset { - if let canPick = nohanaImagePickerController.delegate?.nohanaImagePicker?(nohanaImagePickerController, willPickPhotoKitAsset: (asset as! PhotoKitAsset).originalAsset, pickedAssetsCount: assetsCountBeforePicking) + if let canPick = nohanaImagePickerController!.delegate?.nohanaImagePicker?(nohanaImagePickerController!, willPickPhotoKitAsset: (asset as! PhotoKitAsset).originalAsset, pickedAssetsCount: assetsCountBeforePicking) where !canPick { return false } } - guard nohanaImagePickerController.maximumNumberOfSelection == 0 || assetsCountBeforePicking < nohanaImagePickerController.maximumNumberOfSelection else { + guard nohanaImagePickerController!.maximumNumberOfSelection == 0 || assetsCountBeforePicking < nohanaImagePickerController!.maximumNumberOfSelection else { return false } assetlist.append(asset) let assetsCountAfterPicking = self.count if asset is PhotoKitAsset { let originalAsset = (asset as! PhotoKitAsset).originalAsset - nohanaImagePickerController.delegate?.nohanaImagePicker?(nohanaImagePickerController, didPickPhotoKitAsset: originalAsset, pickedAssetsCount: assetsCountAfterPicking) + nohanaImagePickerController!.delegate?.nohanaImagePicker?(nohanaImagePickerController!, didPickPhotoKitAsset: originalAsset, pickedAssetsCount: assetsCountAfterPicking) NSNotificationCenter.defaultCenter().postNotification( NSNotification( name: NotificationInfo.Asset.PhotoKit.didPick, @@ -87,7 +87,7 @@ class PickedAssetList: ItemListType { func dropAsset(asset: AssetType) -> Bool { let assetsCountBeforeDropping = self.count if asset is PhotoKitAsset { - if let canDrop = nohanaImagePickerController.delegate?.nohanaImagePicker?(nohanaImagePickerController, willDropPhotoKitAsset: (asset as! PhotoKitAsset).originalAsset, pickedAssetsCount: assetsCountBeforeDropping) where !canDrop { + if let canDrop = nohanaImagePickerController!.delegate?.nohanaImagePicker?(nohanaImagePickerController!, willDropPhotoKitAsset: (asset as! PhotoKitAsset).originalAsset, pickedAssetsCount: assetsCountBeforeDropping) where !canDrop { return false } } @@ -95,7 +95,7 @@ class PickedAssetList: ItemListType { let assetsCountAfterDropping = self.count if asset is PhotoKitAsset { let originalAsset = (asset as! PhotoKitAsset).originalAsset - nohanaImagePickerController.delegate?.nohanaImagePicker?(nohanaImagePickerController, didDropPhotoKitAsset: originalAsset, pickedAssetsCount: assetsCountAfterDropping) + nohanaImagePickerController!.delegate?.nohanaImagePicker?(nohanaImagePickerController!, didDropPhotoKitAsset: originalAsset, pickedAssetsCount: assetsCountAfterDropping) NSNotificationCenter.defaultCenter().postNotification( NSNotification( name: NotificationInfo.Asset.PhotoKit.didDrop,