Skip to content

Commit

Permalink
Merge pull request #7 from gregttn/refactoring
Browse files Browse the repository at this point in the history
Refactor constructor using default parameters
  • Loading branch information
ninjaprox committed Aug 24, 2015
2 parents 2411e91 + 027724b commit c86d8b8
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 121 deletions.
191 changes: 77 additions & 114 deletions NVActivityIndicatorView/NVActivityIndicatorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,75 @@ public enum NVActivityIndicatorType {
case Pacman
case BallGridBeat
case SemiCircleSpin

private func animation() -> NVActivityIndicatorAnimationDelegate {
switch self {
case .Blank:
return NVActivityIndicatorAnimationBlank()
case .BallPulse:
return NVActivityIndicatorAnimationBallPulse()
case .BallGridPulse:
return NVActivityIndicatorAnimationBallGridPulse()
case .BallClipRotate:
return NVActivityIndicatorAnimationBallClipRotate()
case .SquareSpin:
return NVActivityIndicatorAnimationSquareSpin()
case .BallClipRotatePulse:
return NVActivityIndicatorAnimationBallClipRotatePulse()
case .BallClipRotateMultiple:
return NVActivityIndicatorAnimationBallClipRotateMultiple()
case .BallPulseRise:
return NVActivityIndicatorAnimationBallPulseRise()
case .BallRotate:
return NVActivityIndicatorAnimationBallRotate()
case .CubeTransition:
return NVActivityIndicatorAnimationCubeTransition()
case .BallZigZag:
return NVActivityIndicatorAnimationBallZigZag()
case .BallZigZagDeflect:
return NVActivityIndicatorAnimationBallZigZagDeflect()
case .BallTrianglePath:
return NVActivityIndicatorAnimationBallTrianglePath()
case .BallScale:
return NVActivityIndicatorAnimationBallScale()
case .LineScale:
return NVActivityIndicatorAnimationLineScale()
case .LineScaleParty:
return NVActivityIndicatorAnimationLineScaleParty()
case .BallScaleMultiple:
return NVActivityIndicatorAnimationBallScaleMultiple()
case .BallPulseSync:
return NVActivityIndicatorAnimationBallPulseSync()
case .BallBeat:
return NVActivityIndicatorAnimationBallBeat()
case .LineScalePulseOut:
return NVActivityIndicatorAnimationLineScalePulseOut()
case .LineScalePulseOutRapid:
return NVActivityIndicatorAnimationLineScalePulseOutRapid()
case .BallScaleRipple:
return NVActivityIndicatorAnimationBallScaleRipple()
case .BallScaleRippleMultiple:
return NVActivityIndicatorAnimationBallScaleRippleMultiple()
case .BallSpinFadeLoader:
return NVActivityIndicatorAnimationBallSpinFadeLoader()
case .LineSpinFadeLoader:
return NVActivityIndicatorAnimationLineSpinFadeLoader()
case .TriangleSkewSpin:
return NVActivityIndicatorAnimationTriangleSkewSpin()
case .Pacman:
return NVActivityIndicatorAnimationPacman()
case .BallGridBeat:
return NVActivityIndicatorAnimationBallGridBeat()
case .SemiCircleSpin:
return NVActivityIndicatorAnimationSemiCircleSpin()
}
}
}

public class NVActivityIndicatorView: UIView {
private let DEFAULT_TYPE: NVActivityIndicatorType = .Pacman
private let DEFAULT_COLOR = UIColor.whiteColor()
private let DEFAULT_SIZE: CGSize = CGSize(width: 40, height: 40)
private static let DEFAULT_TYPE: NVActivityIndicatorType = .Pacman
private static let DEFAULT_COLOR = UIColor.whiteColor()
private static let DEFAULT_SIZE: CGSize = CGSize(width: 40, height: 40)

private var type: NVActivityIndicatorType
private var color: UIColor
Expand All @@ -65,64 +128,27 @@ public class NVActivityIndicatorView: UIView {
:returns: The activity indicator view
*/
required public init(coder aDecoder: NSCoder) {
self.type = DEFAULT_TYPE
self.color = DEFAULT_COLOR
self.size = DEFAULT_SIZE
self.type = NVActivityIndicatorView.DEFAULT_TYPE
self.color = NVActivityIndicatorView.DEFAULT_COLOR
self.size = NVActivityIndicatorView.DEFAULT_SIZE
super.init(coder: aDecoder);
}

/**
Create a activity indicator view with specified type, color, size and size

:param: frame view's frame
:param: type animation type, value of NVActivityIndicatorType enum
:param: color color of activity indicator view
:param: size actual size of animation in view
:param: type animation type, value of NVActivityIndicatorType enum. Default type is pacman.
:param: color color of activity indicator view. Default color is white.
:param: size actual size of animation in view. Default size is 40

:returns: The activity indicator view
*/
public init(frame: CGRect, type: NVActivityIndicatorType, color: UIColor?, size: CGSize?) {
public init(frame: CGRect, type: NVActivityIndicatorType = DEFAULT_TYPE, color: UIColor = DEFAULT_COLOR, size: CGSize = DEFAULT_SIZE) {
self.type = type
self.color = DEFAULT_COLOR
self.size = DEFAULT_SIZE
self.color = color
self.size = size
super.init(frame: frame)

if let _color = color {
self.color = _color
}
if let _size = size {
self.size = _size
}
}

/**
Create a activity indicator view with specified type, color and default size

- Default size is 40

:param: frame view's frame
:param: value animation type, value of NVActivityIndicatorType enum
:param: color color of activity indicator view

:returns: The activity indicator view
*/
convenience public init(frame: CGRect, type: NVActivityIndicatorType, color: UIColor?) {
self.init(frame: frame, type: type, color: color, size: nil)
}

/**
Create a activity indicator view with specified type and default color, size

- Default color is white
- Default size is 40

:param: view view's frame
:param: value animation type, value of NVActivityIndicatorType enum

:returns: The activity indicator view
*/
convenience public init(frame: CGRect, type: NVActivityIndicatorType) {
self.init(frame: frame, type: type, color: nil)
}

/**
Expand Down Expand Up @@ -151,74 +177,11 @@ public class NVActivityIndicatorView: UIView {
}

// MARK: Privates

private func setUpAnimation() {
let animation: protocol<NVActivityIndicatorAnimationDelegate> = animationOfType(self.type)
let animation: protocol<NVActivityIndicatorAnimationDelegate> = self.type.animation()

self.layer.sublayers = nil
animation.setUpAnimationInLayer(self.layer, size: self.size, color: self.color)
}

private func animationOfType(type: NVActivityIndicatorType) -> protocol<NVActivityIndicatorAnimationDelegate> {
switch type {
case .Blank:
return NVActivityIndicatorAnimationBlank()
case .BallPulse:
return NVActivityIndicatorAnimationBallPulse()
case .BallGridPulse:
return NVActivityIndicatorAnimationBallGridPulse()
case .BallClipRotate:
return NVActivityIndicatorAnimationBallClipRotate()
case .SquareSpin:
return NVActivityIndicatorAnimationSquareSpin()
case .BallClipRotatePulse:
return NVActivityIndicatorAnimationBallClipRotatePulse()
case .BallClipRotateMultiple:
return NVActivityIndicatorAnimationBallClipRotateMultiple()
case .BallPulseRise:
return NVActivityIndicatorAnimationBallPulseRise()
case .BallRotate:
return NVActivityIndicatorAnimationBallRotate()
case .CubeTransition:
return NVActivityIndicatorAnimationCubeTransition()
case .BallZigZag:
return NVActivityIndicatorAnimationBallZigZag()
case .BallZigZagDeflect:
return NVActivityIndicatorAnimationBallZigZagDeflect()
case .BallTrianglePath:
return NVActivityIndicatorAnimationBallTrianglePath()
case .BallScale:
return NVActivityIndicatorAnimationBallScale()
case .LineScale:
return NVActivityIndicatorAnimationLineScale()
case .LineScaleParty:
return NVActivityIndicatorAnimationLineScaleParty()
case .BallScaleMultiple:
return NVActivityIndicatorAnimationBallScaleMultiple()
case .BallPulseSync:
return NVActivityIndicatorAnimationBallPulseSync()
case .BallBeat:
return NVActivityIndicatorAnimationBallBeat()
case .LineScalePulseOut:
return NVActivityIndicatorAnimationLineScalePulseOut()
case .LineScalePulseOutRapid:
return NVActivityIndicatorAnimationLineScalePulseOutRapid()
case .BallScaleRipple:
return NVActivityIndicatorAnimationBallScaleRipple()
case .BallScaleRippleMultiple:
return NVActivityIndicatorAnimationBallScaleRippleMultiple()
case .BallSpinFadeLoader:
return NVActivityIndicatorAnimationBallSpinFadeLoader()
case .LineSpinFadeLoader:
return NVActivityIndicatorAnimationLineSpinFadeLoader()
case .TriangleSkewSpin:
return NVActivityIndicatorAnimationTriangleSkewSpin()
case .Pacman:
return NVActivityIndicatorAnimationPacman()
case .BallGridBeat:
return NVActivityIndicatorAnimationBallGridBeat()
case .SemiCircleSpin:
return NVActivityIndicatorAnimationSemiCircleSpin()
}
}
}
41 changes: 34 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,55 @@ Firstly, import NVActivityIndicatorView
import NVActivityIndicatorView
```

Then, there are 4 ways you can do:
Then, there are multiple ways you can create NVActivityIndicatorView:

- Use it in storyboard by changing class of any `UIView` to `NVActivityIndicatorView`
This will use default values 40, white, .Pacman for size, color and type respectively.

- Create with specified type and size 40, color white as default
- Create with specified type, color and size

```swift
NVActivityIndicatorView(frame: frame, type: type)
NVActivityIndicatorView(frame: frame, type: type, color: color, size: size)
```

- Create with specified type, color and size 40 as default
Any of the last three arguments can be omitted. If an argument is omitted it will use the default values which are 40, white, .Pacman for size, color and type respectively.
Therefore, you can also create NVActivityIndicatorView using any of the following:

- specify only frame, type and color
```swift
NVActivityIndicatorView(frame: frame, type: type, color: color)
```

- Create with specified type, color, size
- specify only frame, type and size
```swift
NVActivityIndicatorView(frame: frame, type: type, size: size)
```

- specify only frame, size and color
```swift
NVActivityIndicatorView(frame: frame, type: type, color: color, size: size)
NVActivityIndicatorView(frame: frame, size: size, color: color)
```

- specify only frame and type
```swift
NVActivityIndicatorView(frame: frame, type: type)
```

- specify only frame and color
```swift
NVActivityIndicatorView(frame: frame, color: color)
```

- specify only frame and size
```swift
NVActivityIndicatorView(frame: frame, size: size)
```

- specify only frame
```swift
NVActivityIndicatorView(frame: frame)
```

# Acknowledgment

Thanks [Connor Atherton](https://github.com/ConnorAtherton) for great loaders and [Danil Gontovnik](https://github.com/gontovnik) for kick-start.
Expand All @@ -79,4 +106,4 @@ Thanks [Connor Atherton](https://github.com/ConnorAtherton) for great loaders an

The MIT License (MIT)

Copyright (c) 2015 Nguyen Vinh [@ninjaprox](http://twitter.com/ninjaprox)
Copyright (c) 2015 Nguyen Vinh [@ninjaprox](http://twitter.com/ninjaprox)

0 comments on commit c86d8b8

Please sign in to comment.