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