diff --git a/MVMCoreUI/Atoms/Views/CarouselIndicator/IndicatorViews/BarsIndicatorView.swift b/MVMCoreUI/Atoms/Views/CarouselIndicator/IndicatorViews/BarsIndicatorView.swift new file mode 100644 index 00000000..e20b5489 --- /dev/null +++ b/MVMCoreUI/Atoms/Views/CarouselIndicator/IndicatorViews/BarsIndicatorView.swift @@ -0,0 +1,141 @@ +// +// BarIndicatorView.swift +// MVMCoreUI +// +// Created by Kevin Christiano on 2/3/20. +// Copyright © 2020 Verizon Wireless. All rights reserved. +// + +import UIKit + + +open class BarsIndicatorView: View, IndicatorViewProtocol { + //-------------------------------------------------- + // MARK: - Properties + //-------------------------------------------------- + + let stackView: StackView = { + let stackView = StackView() + stackView.axis = .horizontal + stackView.distribution = .equalSpacing + stackView.spacing = 6 + return stackView + }() + + public var barsReference: [(view: View, constraint: NSLayoutConstraint)] = [] + + // Dimensions are based on InVision Design Guidelines. + public static let indicatorBarWidth: CGFloat = 24 + public static let indicatorBarHeight: (selected: CGFloat, unselected: CGFloat) = (selected: 4, unselected: 1) + + public var enabledColor: UIColor { + return (superview as? CarouselIndicator)?.indicatorTintColor ?? .black + } + + public var currentIndexColor: UIColor { + return (superview as? CarouselIndicator)?.currentIndicatorColor ?? .black + } + + public var disabledColor: UIColor { + return (superview as? CarouselIndicator)?.disabledIndicatorColor ?? .mvmCoolGray3 + } + + /// Returns the numberOfPages count from its parent CarouselIndicator. + public var numberOfPages: Int? { + return (superview as? CarouselIndicator)?.numberOfPages + } + + /// Returns the numberOfPages count from its parent CarouselIndicator. + public var currentIndex: Int? { + return (superview as? CarouselIndicator)?.currentIndex + } + + open var isEnabled: Bool = true { + didSet { + barsReference.forEach { view, heightConstraint in + view.backgroundColor = isEnabled ? enabledColor : disabledColor + } + } + } + + //-------------------------------------------------- + // MARK: - Initializers + //-------------------------------------------------- + + public init() { + super.init(frame: .zero) + } + + public required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + //-------------------------------------------------- + // MARK: - Setup + //-------------------------------------------------- + + open override func setupView() { + super.setupView() + + guard subviews.isEmpty else { return } + + addSubview(stackView) + isUserInteractionEnabled = false + + stackView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true + stackView.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor).isActive = true + trailingAnchor.constraint(lessThanOrEqualTo: stackView.trailingAnchor).isActive = true + + generateBars() + } + + //-------------------------------------------------- + // MARK: - Methods + //-------------------------------------------------- + + func generateBars() { + + guard let numberOfPages = numberOfPages, + let currentIndex = currentIndex + else { return } + + var bars = [(View, NSLayoutConstraint)]() + + for i in 0..