diff --git a/MVMCoreUI/Atoms/Views/CarouselIndicator/CarouselIndicator.swift b/MVMCoreUI/Atoms/Views/CarouselIndicator/CarouselIndicator.swift index 441fa416..73e513e3 100644 --- a/MVMCoreUI/Atoms/Views/CarouselIndicator/CarouselIndicator.swift +++ b/MVMCoreUI/Atoms/Views/CarouselIndicator/CarouselIndicator.swift @@ -58,14 +58,18 @@ open class CarouselIndicator: Control, CarouselPageControlProtocol { /// The view control relative to the state of the indicator type. private(set) var indicatorView: IndicatorView? { + willSet { + indicatorView?.removeFromSuperview() + } didSet { - topConstraint = indicatorView?.topAnchor.constraint(equalTo: topAnchor, constant: PaddingTwo) + guard let indicatorView = indicatorView else { return } + addSubview(indicatorView) + + topConstraint = indicatorView.topAnchor.constraint(equalTo: topAnchor, constant: PaddingTwo) topConstraint?.isActive = true - if let indicatorView = indicatorView { - bottomConstraint = bottomAnchor.constraint(equalTo: indicatorView.bottomAnchor, constant: PaddingTwo) - bottomConstraint?.isActive = true - } + bottomConstraint = bottomAnchor.constraint(equalTo: indicatorView.bottomAnchor, constant: PaddingTwo) + bottomConstraint?.isActive = true } } @@ -137,6 +141,10 @@ open class CarouselIndicator: Control, CarouselPageControlProtocol { indicatorView = BarsIndicatorView() } + if isBarIndicator() { + (indicatorView as? BarsIndicatorView)?.generateBars() + } + updateUI() } } @@ -304,7 +312,7 @@ open class CarouselIndicator: Control, CarouselPageControlProtocol { /// Convenience to determine if current view is displaying bars. func isBarIndicator() -> Bool { - return indicatorType != .bar && numberOfPages > hybridThreshold + return indicatorType == .bar || indicatorType == .hybrid && numberOfPages <= hybridThreshold } public func scrollViewDidScroll(_ collectionView: UICollectionView) { } diff --git a/MVMCoreUI/Atoms/Views/CarouselIndicator/CarouselIndicatorModel.swift b/MVMCoreUI/Atoms/Views/CarouselIndicator/CarouselIndicatorModel.swift index fd39d90c..70c685a1 100644 --- a/MVMCoreUI/Atoms/Views/CarouselIndicator/CarouselIndicatorModel.swift +++ b/MVMCoreUI/Atoms/Views/CarouselIndicator/CarouselIndicatorModel.swift @@ -9,7 +9,7 @@ import Foundation -public class CarouselIndicatorModel: MoleculeModelProtocol { +public class CarouselIndicatorModel: CarouselPagingModelProtocol { //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- @@ -35,7 +35,7 @@ public class CarouselIndicatorModel: MoleculeModelProtocol { public var disabledIndicatorColor: Color? = Color(uiColor: .mvmCoolGray3) public var indicatorTintColor: Color? = Color(uiColor: .mvmBlack) public var currentIndicatorColor: Color? = Color(uiColor: .mvmBlack) -// public var position: Float? + public var position: Float? //-------------------------------------------------- // MARK: - Keys diff --git a/MVMCoreUI/Atoms/Views/CarouselIndicator/IndicatorViews/BarsIndicatorView.swift b/MVMCoreUI/Atoms/Views/CarouselIndicator/IndicatorViews/BarsIndicatorView.swift index 91fa6e29..9f2cb365 100644 --- a/MVMCoreUI/Atoms/Views/CarouselIndicator/IndicatorViews/BarsIndicatorView.swift +++ b/MVMCoreUI/Atoms/Views/CarouselIndicator/IndicatorViews/BarsIndicatorView.swift @@ -19,7 +19,7 @@ open class BarsIndicatorView: View, IndicatorViewProtocol { stackView.axis = .horizontal stackView.distribution = .equalSpacing stackView.spacing = 6 - stackView.heightAnchor.constraint(greaterThanOrEqualToConstant: BarsIndicatorView.indicatorBarHeight.selected).isActive = true + stackView.heightAnchor.constraint(lessThanOrEqualToConstant: BarsIndicatorView.indicatorBarHeight.selected).isActive = true return stackView }() @@ -49,16 +49,6 @@ open class BarsIndicatorView: View, IndicatorViewProtocol { return parentCarouselIndicator?.disabledIndicatorColor ?? .mvmCoolGray3 } - /// Returns the numberOfPages count from its parent CarouselIndicator. - public var numberOfPages: Int? { - return parentCarouselIndicator?.numberOfPages - } - - /// Returns the numberOfPages count from its parent CarouselIndicator. - public var currentIndex: Int? { - return parentCarouselIndicator?.currentIndex - } - open var isEnabled: Bool = true { didSet { barReferences.forEach { view, heightConstraint in @@ -92,8 +82,6 @@ open class BarsIndicatorView: View, IndicatorViewProtocol { stackView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true stackView.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor).isActive = true trailingAnchor.constraint(greaterThanOrEqualTo: stackView.trailingAnchor).isActive = true - - generateBars() } //-------------------------------------------------- @@ -102,8 +90,8 @@ open class BarsIndicatorView: View, IndicatorViewProtocol { func generateBars() { - guard let numberOfPages = numberOfPages, - let currentIndex = currentIndex + guard let numberOfPages = parentCarouselIndicator?.numberOfPages, + let currentIndex = parentCarouselIndicator?.currentIndex else { return } var bars = [(View, NSLayoutConstraint)]() @@ -135,6 +123,8 @@ open class BarsIndicatorView: View, IndicatorViewProtocol { public func updateUI(previousIndex: Int, newIndex: Int, totalCount: Int, isAnimated: Bool) { + guard !barReferences.isEmpty else { return } + let expression = { self.barReferences[previousIndex].view.backgroundColor = self.enabledColor self.barReferences[newIndex].view.backgroundColor = self.currentIndexColor diff --git a/MVMCoreUI/Organisms/CarouselModel.swift b/MVMCoreUI/Organisms/CarouselModel.swift index 9194e32c..a455e635 100644 --- a/MVMCoreUI/Organisms/CarouselModel.swift +++ b/MVMCoreUI/Organisms/CarouselModel.swift @@ -8,6 +8,7 @@ import UIKit + @objcMembers public class CarouselModel: MoleculeModelProtocol { //-------------------------------------------------- // MARK: - Properties