refactored for bugs in missing buttons for 2 rowCount

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

View File

@ -49,52 +49,45 @@ 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
let totalSpacingBetweenAttributes = attributes.reduce(0.0) { $0 + $1.spacing }
let availableWidthAfterSpacing = collectionViewWidth - totalSpacingBetweenAttributes
let buttonCount = CGFloat(buttonAttributes.count)
//see how much of the rows width is used for
//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 maxButtonWidth = availableWidthAfterSpacing / buttonCount
//getting available button space since textlinks need their space
let buttonsAvailableSpace = collectionViewWidth - nonButtonSpace
var buttonWidth = maxButtonWidth
var buttonCalculatedPercentage: CGFloat = 0.0
let buttonEqualSpacing = buttonsAvailableSpace / buttonCount
// test sizing
let testSize = buttonWidth * 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)
var usedSpace = 0.0 let buttonPercentageWidth = buttonCalculatedPercentage * buttonsAvailableSpace
//get the width for the buttons buttonWidth = min(max(buttonPercentageWidth, Button.Size.large.minimumWidth), maxButtonWidth)
for attribute in attributes { }
if !attribute.isButton {
usedSpace += attribute.frame.width //resize the buttonAttributes
if buttonWidth >= Button.Size.large.minimumWidth && !buttonAttributes.isEmpty {
if testSize <= buttonsAvailableSpace {
for attribute in buttonAttributes {
attribute.frame.size.width = buttonCalculatedPercentage.isZero ? min(attribute.frame.size.width, buttonWidth) : buttonWidth
} }
usedSpace += attribute.spacing } else {
} for attribute in buttonAttributes {
let buttonAvailableSpace = collectionViewWidth - usedSpace attribute.frame.size.width = buttonEqualSpacing
let realPercentage = (buttonPercentage / 100)
let buttonWidth = realPercentage * buttonAvailableSpace
// print("buttonPercentage :\(realPercentage)")
// print("collectionView width:\(collectionViewWidth)")
// print("usedSpace width:\(usedSpace)")
// print("button available width:\(buttonAvailableSpace)")
// print("each button width:\(buttonWidth)\n")
// print("minimum widht:\(ButtonSize.large.minimumWidth)")
// test sizing
var testSize = 0.0
var buttonCount = 0.0
for attribute in attributes {
if attribute.isButton {
testSize += buttonWidth
buttonCount += 1
}
}
if buttonWidth >= Button.Size.large.minimumWidth {
if testSize <= buttonAvailableSpace {
for attribute in attributes {
if attribute.isButton {
attribute.frame.size.width = buttonWidth
}
}
} else {
let distributedSize = buttonAvailableSpace / buttonCount
for attribute in attributes {
if attribute.isButton {
attribute.frame.size.width = distributedSize
}
}
} }
} }
} }
@ -181,7 +174,7 @@ class ButtonGroupPositionLayout: UICollectionViewLayout {
var rows = [ButtonCollectionViewRow]() var rows = [ButtonCollectionViewRow]()
rows.append(ButtonCollectionViewRow()) rows.append(ButtonCollectionViewRow())
let collectionViewWidth = collectionView.frame.width let collectionViewWidth = collectionView.horizontalPinnedWidth() ?? collectionView.frame.width
for item in 0..<totalItems { for item in 0..<totalItems {
@ -200,7 +193,8 @@ class ButtonGroupPositionLayout: UICollectionViewLayout {
// determine if the current button will fit in the row // determine if the current button will fit in the row
let rowItemCount = rows.last?.attributes.count ?? 0 let rowItemCount = rows.last?.attributes.count ?? 0
if (layoutWidthIterator + itemSize.width) > collectionViewWidth || (rowQuantity > 0 && rowItemCount == rowQuantity) { if (layoutWidthIterator + itemSize.width) > collectionViewWidth && rowQuantity == 0
|| (rowQuantity > 0 && rowItemCount == rowQuantity) {
// If the current row width (after this item being laid out) is exceeding // If the current row width (after this item being laid out) is exceeding
// the width of the collection view content, put it in the next line // the width of the collection view content, put it in the next line
@ -318,3 +312,4 @@ class ButtonGroupPositionLayout: UICollectionViewLayout {
} }
} }