refactoring

This commit is contained in:
haranicle 2016-05-22 15:18:46 +09:00
parent 89b66281e2
commit 809f4ea82f
6 changed files with 100 additions and 110 deletions

View File

@ -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..<Int.max {
guard AlbumListViewControllerSectionType(rawValue: i) != nil else {
break
}
count = count + 1
}
return count
}
}
weak var nohanaImagePickerController: NohanaImagePickerController!
var photoKitAlbumList: PhotoKitAlbumList!
override func viewDidLoad() {
super.viewDidLoad()
title = NSLocalizedString("albumlist.title", tableName: "NohanaImagePicker", bundle: nohanaImagePickerController!.assetBundle, comment: "")
title = NSLocalizedString("albumlist.title", tableName: "NohanaImagePicker", bundle: nohanaImagePickerController.assetBundle, comment: "")
setUpToolbarItems()
navigationController?.setToolbarHidden(nohanaImagePickerController?.toolbarHidden ?? false, animated: false)
navigationController?.setToolbarHidden(nohanaImagePickerController.toolbarHidden ?? false, animated: false)
setUpEmptyIndicator()
setUpActivityIndicator()
self.view.backgroundColor = ColorConfig.backgroundColor
@ -39,28 +55,35 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity
// MARK: - UITableViewDelegate
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
guard let nohanaImagePickerController = nohanaImagePickerController else {
return
guard let sectionType = AlbumListViewControllerSectionType(rawValue: indexPath.section) else {
fatalError("Invalid section")
}
if isMomentRow(indexPath) {
switch sectionType {
case .Moment:
nohanaImagePickerController.delegate?.nohanaImagePickerDidSelectMoment?(nohanaImagePickerController)
} else {
nohanaImagePickerController.delegate?.nohanaImagePicker?(nohanaImagePickerController, didSelectPhotoKitAssetList: photoKitAlbumList[exactRow(indexPath)].assetList)
case .Albums:
nohanaImagePickerController.delegate?.nohanaImagePicker?(nohanaImagePickerController, didSelectPhotoKitAssetList: photoKitAlbumList[indexPath.row].assetList)
}
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> 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 {

View File

@ -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)
}
}

View File

@ -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

View File

@ -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 )
}

View File

@ -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)
}

View File

@ -11,7 +11,7 @@ import Foundation
class PickedAssetList: ItemListType {
var assetlist: Array<AssetType> = []
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 {