refactored more code

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-08-16 15:25:26 -05:00
parent 5a255aac52
commit 7efc0096af

View File

@ -49,41 +49,44 @@ class ButtonCollectionViewRow {
func layout(for position: ButtonGroup.Alignment, with collectionViewWidth: CGFloat){ func layout(for position: ButtonGroup.Alignment, with collectionViewWidth: CGFloat){
var offset = 0.0 var offset = 0.0
let height = rowHeight let height = rowHeight
let buttonAttributes = attributes.filter{$0.isButton}
attributes.last?.spacing = 0 attributes.last?.spacing = 0
///Calculate the available space in the row //filter only the buttons since this is the only
let totalSpacingBetweenAttributes = attributes.reduce(0.0) { $0 + $1.spacing } //object we can change the frames for.
let availableWidthAfterSpacing = collectionViewWidth - totalSpacingBetweenAttributes let buttonAttributes = attributes.filter{$0.isButton}
if !buttonAttributes.isEmpty {
let buttonCount = CGFloat(buttonAttributes.count) let buttonCount = CGFloat(buttonAttributes.count)
///Calculate the spaces between items in the row
let totalSpacingBetweenAttributes = attributes.reduce(0.0) { $0 + $1.spacing }
//see how much of the rows width is used for //see how much of the rows width is used for
//non-buttons that are BaseButton Subclasses that are not "Button" //non-buttons that are BaseButton Subclasses that are not "Button"
let nonButtonSpace = attributes.filter { !$0.isButton }.reduce(0.0) { $0 + $1.frame.width + $1.spacing } let nonButtonSpace = attributes.filter { !$0.isButton }.reduce(0.0) { $0 + $1.frame.width + $1.spacing }
let maxButtonWidth = availableWidthAfterSpacing / buttonCount
//getting available button space since textlinks need their space //getting available button space since textlinks need their space
let buttonsAvailableSpace = collectionViewWidth - nonButtonSpace let buttonsAvailableSpace = collectionViewWidth - nonButtonSpace - totalSpacingBetweenAttributes
var buttonWidth = maxButtonWidth var maxButtonWidth = buttonsAvailableSpace / buttonCount
var buttonCalculatedPercentage: CGFloat = 0.0 var buttonCalculatedPercentage: CGFloat = 0.0
let buttonEqualSpacing = buttonsAvailableSpace / buttonCount let buttonEqualSpacing = buttonsAvailableSpace / buttonCount
// test sizing // test sizing
let testSize = buttonWidth * CGFloat(buttonAttributes.count) let testSize = maxButtonWidth * CGFloat(buttonAttributes.count)
//check to see if you have buttons and there is a percentage //check to see if you have buttons and there is a percentage
if let buttonPercentage, hasButtons, buttonPercentage > 0 { if let buttonPercentage, hasButtons, buttonPercentage > 0 {
buttonCalculatedPercentage = CGFloat(buttonPercentage / 100.0) buttonCalculatedPercentage = CGFloat(buttonPercentage / 100.0)
let buttonPercentageWidth = buttonCalculatedPercentage * buttonsAvailableSpace let buttonPercentageWidth = buttonCalculatedPercentage * buttonsAvailableSpace
buttonWidth = min(max(buttonPercentageWidth, Button.Size.large.minimumWidth), maxButtonWidth) maxButtonWidth = min(max(buttonPercentageWidth, Button.Size.large.minimumWidth), maxButtonWidth)
} }
//resize the buttonAttributes //resize the buttonAttributes
if buttonWidth >= Button.Size.large.minimumWidth && !buttonAttributes.isEmpty { if maxButtonWidth >= Button.Size.large.minimumWidth && !buttonAttributes.isEmpty {
if testSize <= buttonsAvailableSpace { if testSize <= buttonsAvailableSpace {
for attribute in buttonAttributes { for attribute in buttonAttributes {
attribute.frame.size.width = buttonCalculatedPercentage.isZero ? min(attribute.frame.size.width, buttonWidth) : buttonWidth attribute.frame.size.width = buttonCalculatedPercentage.isZero ? min(attribute.frame.size.width, maxButtonWidth) : maxButtonWidth
} }
} else { } else {
for attribute in buttonAttributes { for attribute in buttonAttributes {
@ -91,7 +94,9 @@ class ButtonCollectionViewRow {
} }
} }
} }
}
//update the offset based on position
switch position { switch position {
case .left: case .left:
break break
@ -313,3 +318,4 @@ class ButtonGroupPositionLayout: UICollectionViewLayout {
} }