Skip to content

Commit dab2cbb

Browse files
coybitninjaprox
authored andcommitted
Make NVActivityIndicatorType conform to CaseIterable (#250)
1 parent a7c5538 commit dab2cbb

3 files changed

Lines changed: 18 additions & 14 deletions

File tree

Example/NVActivityIndicatorViewExample/ViewController.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ import NVActivityIndicatorView
3131

3232
class ViewController: UIViewController, NVActivityIndicatorViewable {
3333

34+
private let presentingIndicatorTypes = {
35+
return NVActivityIndicatorType.allCases.filter { $0 != .blank }
36+
}()
37+
3438
override func viewDidLoad() {
3539
super.viewDidLoad()
3640

@@ -41,30 +45,30 @@ class ViewController: UIViewController, NVActivityIndicatorViewable {
4145
let cellWidth = Int(self.view.frame.width / CGFloat(cols))
4246
let cellHeight = Int(self.view.frame.height / CGFloat(rows))
4347

44-
(NVActivityIndicatorType.ballPulse.rawValue ... NVActivityIndicatorType.circleStrokeSpin.rawValue).forEach {
45-
let x = ($0 - 1) % cols * cellWidth
46-
let y = ($0 - 1) / cols * cellHeight
48+
for (index, indicatorType) in presentingIndicatorTypes.enumerated() {
49+
let x = index % cols * cellWidth
50+
let y = index / cols * cellHeight
4751
let frame = CGRect(x: x, y: y, width: cellWidth, height: cellHeight)
4852
let activityIndicatorView = NVActivityIndicatorView(frame: frame,
49-
type: NVActivityIndicatorType(rawValue: $0)!)
53+
type: indicatorType)
5054
let animationTypeLabel = UILabel(frame: frame)
5155

52-
animationTypeLabel.text = String($0)
56+
animationTypeLabel.text = String(index)
5357
animationTypeLabel.sizeToFit()
5458
animationTypeLabel.textColor = UIColor.white
5559
animationTypeLabel.frame.origin.x += 5
5660
animationTypeLabel.frame.origin.y += CGFloat(cellHeight) - animationTypeLabel.frame.size.height
5761

5862
activityIndicatorView.padding = 20
59-
if $0 == NVActivityIndicatorType.orbit.rawValue {
63+
if indicatorType == NVActivityIndicatorType.orbit {
6064
activityIndicatorView.padding = 0
6165
}
6266
self.view.addSubview(activityIndicatorView)
6367
self.view.addSubview(animationTypeLabel)
6468
activityIndicatorView.startAnimating()
6569

6670
let button: UIButton = UIButton(frame: frame)
67-
button.tag = $0
71+
button.tag = index
6872
#if swift(>=4.2)
6973
button.addTarget(self,
7074
action: #selector(buttonTapped(_:)),
@@ -80,8 +84,10 @@ class ViewController: UIViewController, NVActivityIndicatorViewable {
8084

8185
@objc func buttonTapped(_ sender: UIButton) {
8286
let size = CGSize(width: 30, height: 30)
87+
let selectedIndicatorIndex = sender.tag
88+
let indicatorType = presentingIndicatorTypes[selectedIndicatorIndex]
8389

84-
startAnimating(size, message: "Loading...", type: NVActivityIndicatorType(rawValue: sender.tag)!, fadeInAnimation: nil)
90+
startAnimating(size, message: "Loading...", type: indicatorType, fadeInAnimation: nil)
8591

8692
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.5) {
8793
NVActivityIndicatorPresenter.sharedInstance.setMessage("Authenticating...")

Example/NVActivityIndicatorViewTests/NVActivityIndicatorTypeTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class NVActivityIndicatorTypeTests: XCTestCase {
7575
}
7676
}
7777

78-
func testAllTypes() {
79-
XCTAssertEqual(NVActivityIndicatorType.allTypes.last, NVActivityIndicatorType.circleStrokeSpin)
78+
func testAllCases() {
79+
XCTAssertEqual(NVActivityIndicatorType.allCases.last, NVActivityIndicatorType.circleStrokeSpin)
8080
}
8181
}

Source/NVActivityIndicatorView/NVActivityIndicatorView.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ import UIKit
6464
- AudioEqualizer: AudioEqualizer animation.
6565
- CircleStrokeSpin: CircleStrokeSpin animation.
6666
*/
67-
public enum NVActivityIndicatorType: Int {
67+
public enum NVActivityIndicatorType: CaseIterable {
6868
/**
6969
Blank.
7070

@@ -264,8 +264,6 @@ public enum NVActivityIndicatorType: Int {
264264
*/
265265
case circleStrokeSpin
266266

267-
static let allTypes = (blank.rawValue ... circleStrokeSpin.rawValue).map { NVActivityIndicatorType(rawValue: $0)! }
268-
269267
// swiftlint:disable:next cyclomatic_complexity function_body_length
270268
func animation() -> NVActivityIndicatorAnimationDelegate {
271269
switch self {
@@ -524,7 +522,7 @@ public final class NVActivityIndicatorView: UIView {
524522

525523
// swiftlint:disable:next identifier_name
526524
func _setTypeName(_ typeName: String) {
527-
for item in NVActivityIndicatorType.allTypes {
525+
for item in NVActivityIndicatorType.allCases {
528526
if String(describing: item).caseInsensitiveCompare(typeName) == ComparisonResult.orderedSame {
529527
type = item
530528
break

0 commit comments

Comments
 (0)