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
attributes.last?.spacing = 0
//filter only the buttons since this is the only
//object we can change the frames for.
let buttonAttributes = attributes.filter{$0.isButton} let buttonAttributes = attributes.filter{$0.isButton}
attributes.last?.spacing = 0 if !buttonAttributes.isEmpty {
let buttonCount = CGFloat(buttonAttributes.count)
///Calculate the available space in the row
let totalSpacingBetweenAttributes = attributes.reduce(0.0) { $0 + $1.spacing } ///Calculate the spaces between items in the row
let availableWidthAfterSpacing = collectionViewWidth - totalSpacingBetweenAttributes let totalSpacingBetweenAttributes = attributes.reduce(0.0) { $0 + $1.spacing }
let buttonCount = CGFloat(buttonAttributes.count)
//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
let buttonsAvailableSpace = collectionViewWidth - nonButtonSpace - totalSpacingBetweenAttributes
//getting available button space since textlinks need their space var maxButtonWidth = buttonsAvailableSpace / buttonCount
let buttonsAvailableSpace = collectionViewWidth - nonButtonSpace var buttonCalculatedPercentage: CGFloat = 0.0
var buttonWidth = maxButtonWidth let buttonEqualSpacing = buttonsAvailableSpace / buttonCount
var buttonCalculatedPercentage: CGFloat = 0.0
let buttonEqualSpacing = buttonsAvailableSpace / buttonCount // test sizing
let testSize = maxButtonWidth * CGFloat(buttonAttributes.count)
// test sizing
let testSize = buttonWidth * CGFloat(buttonAttributes.count) //check to see if you have buttons and there is a percentage
if let buttonPercentage, hasButtons, buttonPercentage > 0 {
//check to see if you have buttons and there is a percentage buttonCalculatedPercentage = CGFloat(buttonPercentage / 100.0)
if let buttonPercentage, hasButtons, buttonPercentage > 0 { let buttonPercentageWidth = buttonCalculatedPercentage * buttonsAvailableSpace
buttonCalculatedPercentage = CGFloat(buttonPercentage / 100.0) maxButtonWidth = min(max(buttonPercentageWidth, Button.Size.large.minimumWidth), maxButtonWidth)
let buttonPercentageWidth = buttonCalculatedPercentage * buttonsAvailableSpace }
buttonWidth = min(max(buttonPercentageWidth, Button.Size.large.minimumWidth), maxButtonWidth)
} //resize the buttonAttributes
if maxButtonWidth >= Button.Size.large.minimumWidth && !buttonAttributes.isEmpty {
//resize the buttonAttributes if testSize <= buttonsAvailableSpace {
if buttonWidth >= Button.Size.large.minimumWidth && !buttonAttributes.isEmpty { for attribute in buttonAttributes {
if testSize <= buttonsAvailableSpace { attribute.frame.size.width = buttonCalculatedPercentage.isZero ? min(attribute.frame.size.width, maxButtonWidth) : maxButtonWidth
for attribute in buttonAttributes { }
attribute.frame.size.width = buttonCalculatedPercentage.isZero ? min(attribute.frame.size.width, buttonWidth) : buttonWidth } else {
} for attribute in buttonAttributes {
} else { attribute.frame.size.width = buttonEqualSpacing
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 {
} }