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 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
|
||||
//--------------------------------------------------
|
||||
@ -122,32 +148,17 @@ extension Tabs {
|
||||
}
|
||||
|
||||
open override func updateView() {
|
||||
//label properties
|
||||
label.text = text
|
||||
label.textPosition = textPosition
|
||||
label.textStyle = size.textStyle
|
||||
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
|
||||
if orientation == .horizontal {
|
||||
otherSpace = size == .medium ? VDSLayout.Spacing.space3X.value : VDSLayout.Spacing.space4X.value
|
||||
} else {
|
||||
otherSpace = VDSLayout.Spacing.space2X.value
|
||||
}
|
||||
//constaints
|
||||
labelWidthConstraint?.isActive = false
|
||||
labelWidthConstraint = widthConstraint
|
||||
labelWidthConstraint?.isActive = true
|
||||
labelLeadingConstraint?.constant = leadingSpace
|
||||
labelTopConstraint?.constant = otherSpace
|
||||
labelBottomConstraint?.constant = -otherSpace
|
||||
|
||||
@ -160,12 +171,10 @@ extension Tabs {
|
||||
removeBorders()
|
||||
|
||||
if selected {
|
||||
var indicator: UIRectEdge = .left
|
||||
if orientation == .horizontal {
|
||||
indicator = indicatorPosition.value
|
||||
}
|
||||
addBorder(side: indicator, width: indicatorWidth, color: indicatorColorConfiguration.getColor(self))
|
||||
addBorder(side: indicatorSide, width: indicatorWidth, color: indicatorColorConfiguration.getColor(self))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -113,9 +113,44 @@ open class Tabs: View {
|
||||
private var borderlineViewBottomConstraint: NSLayoutConstraint?
|
||||
private var borderlineViewHeightConstraint: NSLayoutConstraint?
|
||||
private var borderlineViewWidthConstraint: 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
|
||||
//--------------------------------------------------
|
||||
@ -212,15 +247,10 @@ open class Tabs: View {
|
||||
super.updateView()
|
||||
|
||||
// Update the stackview properties
|
||||
if orientation == .horizontal && fillContainer {
|
||||
tabStackView.distribution = .fillEqually
|
||||
} else {
|
||||
tabStackView.distribution = orientation == .horizontal ? .fillProportionally : .fill
|
||||
}
|
||||
|
||||
tabStackView.axis = orientation == .horizontal ? .horizontal : .vertical
|
||||
tabStackView.alignment = orientation == .horizontal ? .fill : .leading
|
||||
tabStackView.spacing = orientation == .horizontal ? VDSLayout.Spacing.space6X.value : VDSLayout.Spacing.space4X.value
|
||||
tabStackView.distribution = stackViewDistribution
|
||||
tabStackView.axis = stackViewAxis
|
||||
tabStackView.alignment = stackViewAlignment
|
||||
tabStackView.spacing = stackViewSpacing
|
||||
|
||||
// Update tab appearance based on properties
|
||||
for (index, tabItem) in tabViews.enumerated() {
|
||||
@ -228,19 +258,26 @@ open class Tabs: View {
|
||||
tabItem.index = index
|
||||
tabItem.minWidth = minWidth
|
||||
tabItem.size = size
|
||||
tabItem.textPosition = orientation == .horizontal && fillContainer ? .center : .left
|
||||
tabItem.textPosition = textPosition
|
||||
tabItem.orientation = orientation
|
||||
tabItem.surface = surface
|
||||
tabItem.indicatorPosition = indicatorPosition
|
||||
tabItem.accessibilityLabel = "\(tabItem.text) \(tabItem.selected ? "selected" : "unselected") \(index+1) of \(tabViews.count)"
|
||||
}
|
||||
|
||||
|
||||
updateWidth()
|
||||
|
||||
setNeedsLayout()
|
||||
layoutIfNeeded()
|
||||
}
|
||||
|
||||
private func updateWidth() {
|
||||
// Deactivate old constraint
|
||||
contentViewWidthConstraint?.isActive = false
|
||||
|
||||
// Apply width
|
||||
if orientation == .vertical {
|
||||
scrollView.isScrollEnabled = false
|
||||
contentViewWidthConstraint = contentView.widthAnchor.constraint(equalTo: widthAnchor)
|
||||
} else {
|
||||
// Apply overflow
|
||||
@ -254,13 +291,10 @@ open class Tabs: View {
|
||||
contentViewWidthConstraint = contentView.widthAnchor.constraint(equalTo: widthAnchor)
|
||||
}
|
||||
}
|
||||
scrollView.isScrollEnabled = orientation == .horizontal && overflow == .scroll
|
||||
scrollView.isScrollEnabled = scrollIsEnabled
|
||||
|
||||
// Activate old constraint
|
||||
contentViewWidthConstraint?.isActive = true
|
||||
|
||||
setNeedsLayout()
|
||||
layoutIfNeeded()
|
||||
}
|
||||
|
||||
open override func layoutSubviews() {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user