From 809f4ea82f8fb7ce596108942e3fc99cd1433f90 Mon Sep 17 00:00:00 2001 From: haranicle Date: Sun, 22 May 2016 15:18:46 +0900 Subject: [PATCH] refactoring --- .../AlbumListViewController.swift | 155 ++++++++++-------- NohanaImagePicker/AssetCell.swift | 12 +- .../AssetDetailListViewController.swift | 16 +- .../AssetListViewController.swift | 14 +- NohanaImagePicker/MomentViewController.swift | 5 +- NohanaImagePicker/PickedAssetList.swift | 8 +- 6 files changed, 100 insertions(+), 110 deletions(-) diff --git a/NohanaImagePicker/AlbumListViewController.swift b/NohanaImagePicker/AlbumListViewController.swift index 6d90bdf..a773c58 100644 --- a/NohanaImagePicker/AlbumListViewController.swift +++ b/NohanaImagePicker/AlbumListViewController.swift @@ -11,14 +11,30 @@ import Photos class AlbumListViewController: UITableViewController, EmptyIndicatable, ActivityIndicatable { - weak var nohanaImagePickerController: NohanaImagePickerController? + enum AlbumListViewControllerSectionType: Int { + case Moment = 0 + case Albums + + static func count() -> Int { + var count: Int = 0 + for i in 0.. CGFloat { - guard let nohanaImagePickerController = nohanaImagePickerController else { - return 0 + guard let sectionType = AlbumListViewControllerSectionType(rawValue: indexPath.section) else { + fatalError("Invalid section") } - if nohanaImagePickerController.shouldShowMoment && indexPath.row == 0 { + switch sectionType { + case .Moment: return 52 + case .Albums: + return 82 } - return 82 } // MARK: - UITableViewDataSource + override func numberOfSectionsInTableView(tableView: UITableView) -> Int { + return AlbumListViewControllerSectionType.count() + } + override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if let emptyIndicator = emptyIndicator { updateVisibilityOfEmptyIndicator(emptyIndicator) @@ -69,55 +92,66 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity updateVisibilityOfActivityIndicator(activityIndicator) } - guard nohanaImagePickerController?.shouldShowMoment ?? false else { + guard let sectionType = AlbumListViewControllerSectionType(rawValue: section) else { + fatalError("Invalid section") + } + + switch sectionType { + case .Moment: + return nohanaImagePickerController.shouldShowMoment ? 1 : 0 + case .Albums: return photoKitAlbumList.count } - return photoKitAlbumList.count != 0 ? photoKitAlbumList.count + 1 : 0 } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { - guard !isMomentRow(indexPath) else { + guard let sectionType = AlbumListViewControllerSectionType(rawValue: indexPath.section) else { + fatalError("Invalid section") + } + + switch sectionType { + case .Moment: 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: "") + 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 { + fatalError("failed to dequeueReusableCellWithIdentifier(\"AlbumCell\")") + } + let albumList = photoKitAlbumList[indexPath.row] + cell.titleLabel.text = albumList.title + cell.tag = indexPath.row + let imageSize = CGSize( + width: cell.thumbnailImageView.frame.size.width * UIScreen.mainScreen().scale, + height: cell.thumbnailImageView.frame.size.width * UIScreen.mainScreen().scale + ) + if let lastAsset = albumList.last { + lastAsset.image(imageSize, handler: { (imageData) -> Void in + dispatch_async(dispatch_get_main_queue(), { () -> Void in + if let imageData = imageData { + if cell.tag == indexPath.row { + cell.thumbnailImageView.image = imageData.image + } + } + }) + }) + } else { + cell.thumbnailImageView.image = nil + } return cell } - - guard let cell = tableView.dequeueReusableCellWithIdentifier("AlbumCell") as? AlbumCell else { - fatalError("failed to dequeueReusableCellWithIdentifier(\"AlbumCell\")") - } - let albumList = photoKitAlbumList[exactRow(indexPath)] - cell.titleLabel.text = albumList.title - cell.tag = exactRow(indexPath) - let imageSize = CGSize( - width: cell.thumbnailImageView.frame.size.width * UIScreen.mainScreen().scale, - height: cell.thumbnailImageView.frame.size.width * UIScreen.mainScreen().scale - ) - if let lastAsset = albumList.last { - lastAsset.image(imageSize, handler: { (imageData) -> Void in - dispatch_async(dispatch_get_main_queue(), { () -> Void in - if let imageData = imageData { - if cell.tag == self.exactRow(indexPath) { - cell.thumbnailImageView.image = imageData.image - } - } - }) - }) - } else { - cell.thumbnailImageView.image = nil - } - return cell } // MARK: - Storyboard override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { - guard let nohanaImagePickerController = nohanaImagePickerController else { - return + guard let sectionType = AlbumListViewControllerSectionType(rawValue: tableView.indexPathForSelectedRow!.section) else { + fatalError("Invalid section") } - - if isMomentRow(tableView.indexPathForSelectedRow!) { + switch sectionType { + case .Moment: let momentViewController = segue.destinationViewController as! MomentViewController momentViewController.nohanaImagePickerController = nohanaImagePickerController momentViewController.momentAlbumList = PhotoKitAlbumList( @@ -131,9 +165,9 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity momentViewController.collectionView?.reloadData() }) }) - } else { + case .Albums: let assetListViewController = segue.destinationViewController as! AssetListViewController - assetListViewController.photoKitAssetList = photoKitAlbumList[exactRow(tableView.indexPathForSelectedRow!)] + assetListViewController.photoKitAssetList = photoKitAlbumList[tableView.indexPathForSelectedRow!.row] assetListViewController.nohanaImagePickerController = nohanaImagePickerController } } @@ -141,25 +175,9 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity // MARK: - IBAction @IBAction func didPushCancel(sender: AnyObject) { - guard let nohanaImagePickerController = nohanaImagePickerController else { - return - } nohanaImagePickerController.delegate?.nohanaImagePickerDidCancel(nohanaImagePickerController) } - // MARK: - Private - - private func exactRow(indexPath: NSIndexPath) -> Int { - guard nohanaImagePickerController?.shouldShowMoment ?? false else { - return indexPath.row - } - return indexPath.row - 1 - } - - private func isMomentRow(indexPath: NSIndexPath) -> Bool { - return indexPath.row == 0 && (nohanaImagePickerController?.shouldShowMoment ?? false) - } - // MARK: - EmptyIndicatable var emptyIndicator: UIView? @@ -167,8 +185,8 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity func setUpEmptyIndicator() { let frame = CGRect(origin: CGPoint.zero, size: Size.screenRectWithoutAppBar(self).size) emptyIndicator = AlbumListEmptyIndicator( - message: NSLocalizedString("albumlist.empty.message", tableName: "NohanaImagePicker", bundle: nohanaImagePickerController!.assetBundle, comment: ""), - description: NSLocalizedString("albumlist.empty.description", tableName: "NohanaImagePicker", bundle: nohanaImagePickerController!.assetBundle, comment: ""), + message: NSLocalizedString("albumlist.empty.message", tableName: "NohanaImagePicker", bundle: nohanaImagePickerController.assetBundle, comment: ""), + description: NSLocalizedString("albumlist.empty.description", tableName: "NohanaImagePicker", bundle: nohanaImagePickerController.assetBundle, comment: ""), frame: frame) } @@ -210,14 +228,11 @@ extension UIViewController { self.toolbarItems = [leftSpace, infoButton, rightSpace] } - func setToolbarTitle(nohanaImagePickerController:NohanaImagePickerController?) { + func setToolbarTitle(nohanaImagePickerController:NohanaImagePickerController) { guard toolbarItems?.count >= 2 else { return } - guard - let infoButton = toolbarItems?[1], - let nohanaImagePickerController = nohanaImagePickerController - else { + guard let infoButton = toolbarItems?[1] else { return } if nohanaImagePickerController.maximumNumberOfSelection == 0 { diff --git a/NohanaImagePicker/AssetCell.swift b/NohanaImagePicker/AssetCell.swift index 2430269..957bd1e 100644 --- a/NohanaImagePicker/AssetCell.swift +++ b/NohanaImagePicker/AssetCell.swift @@ -14,17 +14,17 @@ 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), + 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), + UIImage(named: ImageName.AssetCell.PickButton.SizeM.picked, inBundle: nohanaImagePickerController.assetBundle, compatibleWithTraitCollection: nil), forState: [.Normal, .Selected]) } @@ -44,11 +44,11 @@ class AssetCell: UICollectionViewCell { self.overlayView.hidden = !pickButton.selected } - func update(asset: AssetType, nohanaImagePickerController: NohanaImagePickerController?) { + func update(asset: AssetType, nohanaImagePickerController: NohanaImagePickerController) { self.asset = asset self.nohanaImagePickerController = nohanaImagePickerController - self.pickButton.selected = nohanaImagePickerController?.pickedAssetList.isPicked(asset) ?? false + self.pickButton.selected = nohanaImagePickerController.pickedAssetList.isPicked(asset) ?? false self.overlayView.hidden = !pickButton.selected - self.pickButton.hidden = !(nohanaImagePickerController?.canPickAsset(asset) ?? true) + self.pickButton.hidden = !(nohanaImagePickerController.canPickAsset(asset) ?? true) } } \ No newline at end of file diff --git a/NohanaImagePicker/AssetDetailListViewController.swift b/NohanaImagePicker/AssetDetailListViewController.swift index fb7ce98..d6355d9 100644 --- a/NohanaImagePicker/AssetDetailListViewController.swift +++ b/NohanaImagePicker/AssetDetailListViewController.swift @@ -29,10 +29,10 @@ class AssetDetailListViewController: AssetListViewController { override func viewDidLoad() { super.viewDidLoad() pickButton.setImage( - UIImage(named: ImageName.AssetCell.PickButton.SizeL.dropped, inBundle: nohanaImagePickerController?.assetBundle, compatibleWithTraitCollection: nil), + 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), + UIImage(named: ImageName.AssetCell.PickButton.SizeL.picked, inBundle: nohanaImagePickerController.assetBundle, compatibleWithTraitCollection: nil), forState: [.Normal, .Selected]) } @@ -46,11 +46,8 @@ class AssetDetailListViewController: AssetListViewController { func didChangeAssetDetailPage(indexPath:NSIndexPath) { let asset = photoKitAssetList[indexPath.item] - pickButton.selected = nohanaImagePickerController?.pickedAssetList.isPicked(asset) ?? false - pickButton.hidden = !(nohanaImagePickerController?.canPickAsset(asset) ?? true) - guard let nohanaImagePickerController = nohanaImagePickerController else { - return - } + pickButton.selected = nohanaImagePickerController.pickedAssetList.isPicked(asset) ?? false + pickButton.hidden = !(nohanaImagePickerController.canPickAsset(asset) ?? true) nohanaImagePickerController.delegate?.nohanaImagePicker?(nohanaImagePickerController, assetDetailListViewController: self, didChangeAssetDetailPage: indexPath, photoKitAsset: asset.originalAsset) } @@ -69,9 +66,6 @@ class AssetDetailListViewController: AssetListViewController { // MARK: - IBAction @IBAction func didPushPickButton(sender: UIButton) { - guard let nohanaImagePickerController = nohanaImagePickerController else { - return - } let asset = photoKitAssetList[currentIndexPath.row] if pickButton.selected { if nohanaImagePickerController.pickedAssetList.dropAsset(asset) { @@ -90,7 +84,7 @@ class AssetDetailListViewController: AssetListViewController { guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetDetailCell", forIndexPath: indexPath) as? AssetDetailCell, nohanaImagePickerController = nohanaImagePickerController else { - return UICollectionViewCell(frame: CGRectZero) + fatalError("failed to dequeueReusableCellWithIdentifier(\"AssetDetailCell\")") } cell.scrollView.zoomScale = 1 cell.tag = indexPath.item diff --git a/NohanaImagePicker/AssetListViewController.swift b/NohanaImagePicker/AssetListViewController.swift index faf6f1d..66f5b54 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,9 +24,6 @@ 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 @@ -76,16 +73,13 @@ class AssetListViewController: UICollectionViewController { // MARK: - UICollectionViewDelegate override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { - guard let nohanaImagePickerController = nohanaImagePickerController else { - return - } 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, nohanaImagePickerController = nohanaImagePickerController else { - return UICollectionViewCell(frame: CGRectZero) + fatalError("failed to dequeueReusableCellWithIdentifier(\"AssetCell\")") } cell.tag = indexPath.item cell.update(photoKitAssetList[indexPath.row], nohanaImagePickerController: nohanaImagePickerController) @@ -128,10 +122,6 @@ class AssetListViewController: UICollectionViewController { // MARK: - IBAction @IBAction func didPushDone(sender: AnyObject) { - guard let nohanaImagePickerController = nohanaImagePickerController else { - return - } - 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 50010b4..89c3b3a 100644 --- a/NohanaImagePicker/MomentViewController.swift +++ b/NohanaImagePicker/MomentViewController.swift @@ -19,7 +19,7 @@ class MomentViewController: AssetListViewController, ActivityIndicatable { } override func updateTitle() { - title = NSLocalizedString("albumlist.moment.title", tableName: "NohanaImagePicker", bundle: nohanaImagePickerController!.assetBundle, comment: "") + title = NSLocalizedString("albumlist.moment.title", tableName: "NohanaImagePicker", bundle: nohanaImagePickerController.assetBundle, comment: "") } override func scrollCollectionViewToInitialPosition() { @@ -123,9 +123,6 @@ class MomentViewController: AssetListViewController, ActivityIndicatable { // MARK: - UICollectionViewDelegate override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { - guard let nohanaImagePickerController = nohanaImagePickerController else { - return - } nohanaImagePickerController.delegate?.nohanaImagePicker?(nohanaImagePickerController, didSelectPhotoKitAsset: momentAlbumList[indexPath.section][indexPath.row].originalAsset) } diff --git a/NohanaImagePicker/PickedAssetList.swift b/NohanaImagePicker/PickedAssetList.swift index cc58197..e6f1aae 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 @@ -54,9 +54,6 @@ class PickedAssetList: ItemListType { guard !isPicked(asset) else { return false } - guard let nohanaImagePickerController = nohanaImagePickerController else { - return false - } let assetsCountBeforePicking = self.count if asset is PhotoKitAsset { if let canPick = nohanaImagePickerController.delegate?.nohanaImagePicker?(nohanaImagePickerController, willPickPhotoKitAsset: (asset as! PhotoKitAsset).originalAsset, pickedAssetsCount: assetsCountBeforePicking) @@ -88,9 +85,6 @@ class PickedAssetList: ItemListType { } func dropAsset(asset: AssetType) -> Bool { - guard let nohanaImagePickerController = nohanaImagePickerController else { - return false - } let assetsCountBeforeDropping = self.count if asset is PhotoKitAsset { if let canDrop = nohanaImagePickerController.delegate?.nohanaImagePicker?(nohanaImagePickerController, willDropPhotoKitAsset: (asset as! PhotoKitAsset).originalAsset, pickedAssetsCount: assetsCountBeforeDropping) where !canDrop {