refactored code logic into properties and separate functions
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
fd733bc23f
commit
3b264d2355
@ -66,6 +66,32 @@ extension Tabs {
|
|||||||
private var indicatorColorConfiguration = SurfaceColorConfiguration(VDSColor.paletteRed, VDSColor.elementsPrimaryOndark)
|
private var indicatorColorConfiguration = SurfaceColorConfiguration(VDSColor.paletteRed, VDSColor.elementsPrimaryOndark)
|
||||||
private var indicatorWidth: CGFloat = 4.0
|
private var indicatorWidth: CGFloat = 4.0
|
||||||
|
|
||||||
|
private var indicatorSide: UIRectEdge {
|
||||||
|
guard orientation == .horizontal else { return .left }
|
||||||
|
return indicatorPosition.value
|
||||||
|
}
|
||||||
|
|
||||||
|
private var leadingSpace: CGFloat {
|
||||||
|
guard orientation == .vertical else { return 0 }
|
||||||
|
return size == .medium ? VDSLayout.Spacing.space4X.value : VDSLayout.Spacing.space6X.value
|
||||||
|
}
|
||||||
|
|
||||||
|
private var otherSpace: CGFloat {
|
||||||
|
if orientation == .horizontal {
|
||||||
|
return size == .medium ? VDSLayout.Spacing.space3X.value : VDSLayout.Spacing.space4X.value
|
||||||
|
} else {
|
||||||
|
return VDSLayout.Spacing.space2X.value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private var widthConstraint: NSLayoutConstraint {
|
||||||
|
if let width, orientation == .vertical {
|
||||||
|
return label.widthAnchor.constraint(equalToConstant: width)
|
||||||
|
} else {
|
||||||
|
return label.widthAnchor.constraint(greaterThanOrEqualToConstant: minWidth)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Initializers
|
// MARK: - Initializers
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -122,32 +148,17 @@ extension Tabs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
open override func updateView() {
|
open override func updateView() {
|
||||||
|
//label properties
|
||||||
label.text = text
|
label.text = text
|
||||||
label.textPosition = textPosition
|
label.textPosition = textPosition
|
||||||
label.textStyle = size.textStyle
|
label.textStyle = size.textStyle
|
||||||
label.textColor = textColorConfiguration.getColor(self)
|
label.textColor = textColorConfiguration.getColor(self)
|
||||||
labelWidthConstraint?.isActive = false
|
|
||||||
if let width, orientation == .vertical {
|
|
||||||
labelWidthConstraint = label.widthAnchor.constraint(equalToConstant: width)
|
|
||||||
} else {
|
|
||||||
labelWidthConstraint = label.widthAnchor.constraint(greaterThanOrEqualToConstant: minWidth)
|
|
||||||
}
|
|
||||||
labelWidthConstraint?.isActive = true
|
|
||||||
|
|
||||||
var leadingSpace: CGFloat
|
|
||||||
if orientation == .horizontal {
|
|
||||||
leadingSpace = 0
|
|
||||||
} else {
|
|
||||||
leadingSpace = size == .medium ? VDSLayout.Spacing.space4X.value : VDSLayout.Spacing.space6X.value
|
|
||||||
}
|
|
||||||
labelLeadingConstraint?.constant = leadingSpace
|
|
||||||
|
|
||||||
var otherSpace: CGFloat
|
//constaints
|
||||||
if orientation == .horizontal {
|
labelWidthConstraint?.isActive = false
|
||||||
otherSpace = size == .medium ? VDSLayout.Spacing.space3X.value : VDSLayout.Spacing.space4X.value
|
labelWidthConstraint = widthConstraint
|
||||||
} else {
|
labelWidthConstraint?.isActive = true
|
||||||
otherSpace = VDSLayout.Spacing.space2X.value
|
labelLeadingConstraint?.constant = leadingSpace
|
||||||
}
|
|
||||||
labelTopConstraint?.constant = otherSpace
|
labelTopConstraint?.constant = otherSpace
|
||||||
labelBottomConstraint?.constant = -otherSpace
|
labelBottomConstraint?.constant = -otherSpace
|
||||||
|
|
||||||
@ -160,12 +171,10 @@ extension Tabs {
|
|||||||
removeBorders()
|
removeBorders()
|
||||||
|
|
||||||
if selected {
|
if selected {
|
||||||
var indicator: UIRectEdge = .left
|
addBorder(side: indicatorSide, width: indicatorWidth, color: indicatorColorConfiguration.getColor(self))
|
||||||
if orientation == .horizontal {
|
|
||||||
indicator = indicatorPosition.value
|
|
||||||
}
|
|
||||||
addBorder(side: indicator, width: indicatorWidth, color: indicatorColorConfiguration.getColor(self))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -113,9 +113,44 @@ open class Tabs: View {
|
|||||||
private var borderlineViewBottomConstraint: NSLayoutConstraint?
|
private var borderlineViewBottomConstraint: NSLayoutConstraint?
|
||||||
private var borderlineViewHeightConstraint: NSLayoutConstraint?
|
private var borderlineViewHeightConstraint: NSLayoutConstraint?
|
||||||
private var borderlineViewWidthConstraint: NSLayoutConstraint?
|
private var borderlineViewWidthConstraint: NSLayoutConstraint?
|
||||||
|
|
||||||
private var contentViewWidthConstraint: NSLayoutConstraint?
|
private var contentViewWidthConstraint: NSLayoutConstraint?
|
||||||
|
|
||||||
|
//--------------------------------------------------
|
||||||
|
// MARK: - Configuration Properties
|
||||||
|
//--------------------------------------------------
|
||||||
|
private var stackViewAxis: NSLayoutConstraint.Axis {
|
||||||
|
orientation == .horizontal ? .horizontal : .vertical
|
||||||
|
}
|
||||||
|
|
||||||
|
private var stackViewAlignment: UIStackView.Alignment {
|
||||||
|
orientation == .horizontal ? .fill : .leading
|
||||||
|
}
|
||||||
|
|
||||||
|
private var stackViewDistribution: UIStackView.Distribution{
|
||||||
|
if orientation == .horizontal && fillContainer {
|
||||||
|
return .fillEqually
|
||||||
|
} else {
|
||||||
|
return orientation == .horizontal ? .fillProportionally : .fill
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private var stackViewSpacing: CGFloat {
|
||||||
|
switch orientation {
|
||||||
|
case .vertical:
|
||||||
|
return size == .medium ? VDSLayout.Spacing.space4X.value : VDSLayout.Spacing.space6X.value
|
||||||
|
case .horizontal:
|
||||||
|
return size == .medium ? VDSLayout.Spacing.space6X.value : VDSLayout.Spacing.space8X.value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private var scrollIsEnabled: Bool {
|
||||||
|
orientation == .horizontal && overflow == .scroll
|
||||||
|
}
|
||||||
|
|
||||||
|
private var textPosition: TextPosition {
|
||||||
|
orientation == .horizontal && fillContainer ? .center : .left
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Initializers
|
// MARK: - Initializers
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -212,15 +247,10 @@ open class Tabs: View {
|
|||||||
super.updateView()
|
super.updateView()
|
||||||
|
|
||||||
// Update the stackview properties
|
// Update the stackview properties
|
||||||
if orientation == .horizontal && fillContainer {
|
tabStackView.distribution = stackViewDistribution
|
||||||
tabStackView.distribution = .fillEqually
|
tabStackView.axis = stackViewAxis
|
||||||
} else {
|
tabStackView.alignment = stackViewAlignment
|
||||||
tabStackView.distribution = orientation == .horizontal ? .fillProportionally : .fill
|
tabStackView.spacing = stackViewSpacing
|
||||||
}
|
|
||||||
|
|
||||||
tabStackView.axis = orientation == .horizontal ? .horizontal : .vertical
|
|
||||||
tabStackView.alignment = orientation == .horizontal ? .fill : .leading
|
|
||||||
tabStackView.spacing = orientation == .horizontal ? VDSLayout.Spacing.space6X.value : VDSLayout.Spacing.space4X.value
|
|
||||||
|
|
||||||
// Update tab appearance based on properties
|
// Update tab appearance based on properties
|
||||||
for (index, tabItem) in tabViews.enumerated() {
|
for (index, tabItem) in tabViews.enumerated() {
|
||||||
@ -228,19 +258,26 @@ open class Tabs: View {
|
|||||||
tabItem.index = index
|
tabItem.index = index
|
||||||
tabItem.minWidth = minWidth
|
tabItem.minWidth = minWidth
|
||||||
tabItem.size = size
|
tabItem.size = size
|
||||||
tabItem.textPosition = orientation == .horizontal && fillContainer ? .center : .left
|
tabItem.textPosition = textPosition
|
||||||
tabItem.orientation = orientation
|
tabItem.orientation = orientation
|
||||||
tabItem.surface = surface
|
tabItem.surface = surface
|
||||||
tabItem.indicatorPosition = indicatorPosition
|
tabItem.indicatorPosition = indicatorPosition
|
||||||
tabItem.accessibilityLabel = "\(tabItem.text) \(tabItem.selected ? "selected" : "unselected") \(index+1) of \(tabViews.count)"
|
tabItem.accessibilityLabel = "\(tabItem.text) \(tabItem.selected ? "selected" : "unselected") \(index+1) of \(tabViews.count)"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
updateWidth()
|
||||||
|
|
||||||
|
setNeedsLayout()
|
||||||
|
layoutIfNeeded()
|
||||||
|
}
|
||||||
|
|
||||||
|
private func updateWidth() {
|
||||||
// Deactivate old constraint
|
// Deactivate old constraint
|
||||||
contentViewWidthConstraint?.isActive = false
|
contentViewWidthConstraint?.isActive = false
|
||||||
|
|
||||||
// Apply width
|
// Apply width
|
||||||
if orientation == .vertical {
|
if orientation == .vertical {
|
||||||
scrollView.isScrollEnabled = false
|
|
||||||
contentViewWidthConstraint = contentView.widthAnchor.constraint(equalTo: widthAnchor)
|
contentViewWidthConstraint = contentView.widthAnchor.constraint(equalTo: widthAnchor)
|
||||||
} else {
|
} else {
|
||||||
// Apply overflow
|
// Apply overflow
|
||||||
@ -254,13 +291,10 @@ open class Tabs: View {
|
|||||||
contentViewWidthConstraint = contentView.widthAnchor.constraint(equalTo: widthAnchor)
|
contentViewWidthConstraint = contentView.widthAnchor.constraint(equalTo: widthAnchor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
scrollView.isScrollEnabled = orientation == .horizontal && overflow == .scroll
|
scrollView.isScrollEnabled = scrollIsEnabled
|
||||||
|
|
||||||
// Activate old constraint
|
// Activate old constraint
|
||||||
contentViewWidthConstraint?.isActive = true
|
contentViewWidthConstraint?.isActive = true
|
||||||
|
|
||||||
setNeedsLayout()
|
|
||||||
layoutIfNeeded()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
open override func layoutSubviews() {
|
open override func layoutSubviews() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user