Skip to content

Commit

Permalink
Merge pull request #13 from hyperoslo/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
zenangst committed Aug 26, 2015
2 parents e0df2b5 + 4f80265 commit 6215145
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 204 deletions.
74 changes: 14 additions & 60 deletions Source/BottomView/BottomContainerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ protocol BottomContainerViewDelegate {
func pickerButtonDidPress()
func doneButtonDidPress()
func cancelButtonDidPress()
func imageWrapperDidPress()
func imageStackViewDidPress()
}

class BottomContainerView: UIView {
Expand Down Expand Up @@ -40,13 +40,12 @@ class BottomContainerView: UIView {
return button
}()

lazy var imageWrapper: ImageWrapper = {
let view = ImageWrapper()
lazy var stackView: ImageStackView = {
let view = ImageStackView(frame: CGRect(x: 0, y: 0, width: 80, height: 80))
view.setTranslatesAutoresizingMaskIntoConstraints(false)

return view
}()

lazy var configuration: PickerConfiguration = {
let configuration = PickerConfiguration()
return configuration
Expand Down Expand Up @@ -75,9 +74,9 @@ class BottomContainerView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)

[borderPickerButton, pickerButton, doneButton, imageWrapper, topSeparator].map { self.addSubview($0) }
backgroundColor = self.configuration.backgroundColor
imageWrapper.addGestureRecognizer(self.tapGestureRecognizer)
[borderPickerButton, pickerButton, doneButton, stackView, topSeparator].map { self.addSubview($0) }
backgroundColor = configuration.backgroundColor
stackView.addGestureRecognizer(tapGestureRecognizer)

setupConstraints()
}
Expand Down Expand Up @@ -128,19 +127,19 @@ class BottomContainerView: UIView {
relatedBy: .Equal, toItem: self, attribute: .Right,
multiplier: 1, constant: -(UIScreen.mainScreen().bounds.width - (ButtonPicker.Dimensions.buttonBorderSize + UIScreen.mainScreen().bounds.width)/2)/2))

addConstraint(NSLayoutConstraint(item: imageWrapper, attribute: .Width,
addConstraint(NSLayoutConstraint(item: stackView, attribute: .Width,
relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute,
multiplier: 1, constant: ImageWrapper.Dimensions.imageSize))
multiplier: 1, constant: ImageStackView.Dimensions.imageSize))

addConstraint(NSLayoutConstraint(item: imageWrapper, attribute: .Height,
addConstraint(NSLayoutConstraint(item: stackView, attribute: .Height,
relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute,
multiplier: 1, constant: ImageWrapper.Dimensions.imageSize))
multiplier: 1, constant: ImageStackView.Dimensions.imageSize))

addConstraint(NSLayoutConstraint(item: imageWrapper, attribute: .CenterY,
addConstraint(NSLayoutConstraint(item: stackView, attribute: .CenterY,
relatedBy: .Equal, toItem: self, attribute: .CenterY,
multiplier: 1, constant: 0))

addConstraint(NSLayoutConstraint(item: imageWrapper, attribute: .CenterX,
addConstraint(NSLayoutConstraint(item: stackView, attribute: .CenterX,
relatedBy: .Equal, toItem: self, attribute: .Left,
multiplier: 1, constant: UIScreen.mainScreen().bounds.width/4 - ButtonPicker.Dimensions.buttonBorderSize/4))

Expand All @@ -165,53 +164,8 @@ class BottomContainerView: UIView {
}
}

func handleTapGestureRecognizer(gesture: UITapGestureRecognizer) {
delegate?.imageWrapperDidPress()
}

// MARK: - Wrapper methods

func updateWrapperImages(array: NSMutableArray) {
switch array.count {
case 1:
imageWrapper.firstImageView.image = array.firstObject as? UIImage
imageWrapper.secondImageView.image = nil
imageWrapper.secondImageView.alpha = 0
if pastCount < 1 {
animateImageView(imageWrapper.firstImageView)
}
case 0:
imageWrapper.firstImageView.image = nil
case 2:
imageWrapper.firstImageView.image = array[1] as? UIImage
imageWrapper.secondImageView.image = array.firstObject as? UIImage
imageWrapper.secondImageView.alpha = 1
imageWrapper.thirdImageView.alpha = 0
if pastCount < 2 {
animateImageView(imageWrapper.secondImageView)
}
case 3:
imageWrapper.firstImageView.image = array[2] as? UIImage
imageWrapper.secondImageView.image = array[1] as? UIImage
imageWrapper.thirdImageView.image = array.firstObject as? UIImage
imageWrapper.thirdImageView.alpha = 1
imageWrapper.fourthImageView.alpha = 0
if pastCount < 3 {
animateImageView(imageWrapper.thirdImageView)
}
default:
imageWrapper.fourthImageView.alpha = 1
imageWrapper.firstImageView.image = array.lastObject as? UIImage
imageWrapper.secondImageView.image = array[array.count - 2] as? UIImage
imageWrapper.thirdImageView.image = array[array.count - 3] as? UIImage
imageWrapper.fourthImageView.image = array[array.count - 4] as? UIImage
if pastCount < array.count {
animateImageView(imageWrapper.fourthImageView)
}
}

pastCount = array.count
pickerButton.photoNumber = array.count
func handleTapGestureRecognizer(recognizer: UITapGestureRecognizer) {
delegate?.imageStackViewDidPress()
}

private func animateImageView(imageView: UIImageView) {
Expand Down
43 changes: 32 additions & 11 deletions Source/BottomView/ButtonPicker.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import UIKit

protocol ButtonPickerDelegate {
protocol ButtonPickerDelegate: class {

func buttonDidPress()
}
Expand All @@ -25,15 +25,9 @@ class ButtonPicker: UIButton {
lazy var configuration: PickerConfiguration = {
let configuration = PickerConfiguration()
return configuration
}()

internal var photoNumber: Int = 0 {
didSet {
numberLabel.text = photoNumber == 0 ? "" : "\(photoNumber)"
}
}
}()

var delegate: ButtonPickerDelegate?
weak var delegate: ButtonPickerDelegate?

// MARK: - Initializers

Expand All @@ -42,12 +36,34 @@ class ButtonPicker: UIButton {

[numberLabel].map { self.addSubview($0) }

subscribe()
setupButton()
setupConstraints()
}

deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}

func subscribe() {
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "recalculatePhotosCount:",
name: ImageStack.Notifications.imageDidPush,
object: nil)

NSNotificationCenter.defaultCenter().addObserver(self,
selector: "recalculatePhotosCount:",
name: ImageStack.Notifications.imageDidDrop,
object: nil)

NSNotificationCenter.defaultCenter().addObserver(self,
selector: "recalculatePhotosCount:",
name: ImageStack.Notifications.stackDidReload,
object: nil)
}

required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
fatalError("init(coder:) has not been implemented")
}

// MARK: - Configuration
Expand All @@ -73,10 +89,15 @@ class ButtonPicker: UIButton {

// MARK: - Actions

func recalculatePhotosCount(notification: NSNotification) {
if let sender = notification.object as? ImageStack {
numberLabel.text = sender.images.isEmpty ? "" : String(sender.images.count)
}
}

func pickerButtonDidPress(button: UIButton) {
backgroundColor = .whiteColor()
numberLabel.textColor = .blackColor()
photoNumber = photoNumber + 1
numberLabel.sizeToFit()
delegate?.buttonDidPress()
}
Expand Down
32 changes: 32 additions & 0 deletions Source/BottomView/ImageStack.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import UIKit

public class ImageStack {

public struct Notifications {
public static let imageDidPush = "imageDidPush:"
public static let imageDidDrop = "imageDidDrop:"
public static let stackDidReload = "stackDidReload:"
public static let imageKey = "image"
}

public var images = [UIImage]()

public func pushImage(image: UIImage) {
images.append(image)
NSNotificationCenter.defaultCenter().postNotificationName(Notifications.imageDidPush, object: self, userInfo: ["image" : image])
}

public func dropImage(image: UIImage) {
images = images.filter() {$0 != image}
NSNotificationCenter.defaultCenter().postNotificationName(Notifications.imageDidDrop, object: self, userInfo: ["image" : image])
}

public func resetImages(newImages: [UIImage]) {
images = newImages
NSNotificationCenter.defaultCenter().postNotificationName(Notifications.stackDidReload, object: self, userInfo: nil)
}

public func containsImage(image: UIImage) -> Bool {
return contains(images, image)
}
}
105 changes: 0 additions & 105 deletions Source/BottomView/ImageWrapper.swift

This file was deleted.

Loading

0 comments on commit 6215145

Please sign in to comment.