From 7efc0096afddea6c949e7c6ce4ddc41ade2dd592 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Fri, 16 Aug 2024 15:25:26 -0500 Subject: [PATCH] refactored more code Signed-off-by: Matt Bruce --- .../ButtonGroupPositionLayout.swift | 80 ++++++++++--------- 1 file changed, 43 insertions(+), 37 deletions(-) diff --git a/VDS/Components/Buttons/ButtonGroup/ButtonGroupPositionLayout.swift b/VDS/Components/Buttons/ButtonGroup/ButtonGroupPositionLayout.swift index 8c71ac4e..02a93556 100644 --- a/VDS/Components/Buttons/ButtonGroup/ButtonGroupPositionLayout.swift +++ b/VDS/Components/Buttons/ButtonGroup/ButtonGroupPositionLayout.swift @@ -49,49 +49,54 @@ class ButtonCollectionViewRow { func layout(for position: ButtonGroup.Alignment, with collectionViewWidth: CGFloat){ var offset = 0.0 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} - 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 - if let buttonPercentage, hasButtons, buttonPercentage > 0 { - buttonCalculatedPercentage = CGFloat(buttonPercentage / 100.0) - let buttonPercentageWidth = buttonCalculatedPercentage * buttonsAvailableSpace - buttonWidth = min(max(buttonPercentageWidth, Button.Size.large.minimumWidth), maxButtonWidth) - } - - //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 - } - } else { - for attribute in buttonAttributes { - attribute.frame.size.width = buttonEqualSpacing + if !buttonAttributes.isEmpty { + 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 + //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 } + + //getting available button space since textlinks need their space + let buttonsAvailableSpace = collectionViewWidth - nonButtonSpace - totalSpacingBetweenAttributes + var maxButtonWidth = buttonsAvailableSpace / buttonCount + var buttonCalculatedPercentage: CGFloat = 0.0 + let buttonEqualSpacing = buttonsAvailableSpace / buttonCount + + // test sizing + let testSize = maxButtonWidth * CGFloat(buttonAttributes.count) + + //check to see if you have buttons and there is a percentage + if let buttonPercentage, hasButtons, buttonPercentage > 0 { + buttonCalculatedPercentage = CGFloat(buttonPercentage / 100.0) + let buttonPercentageWidth = buttonCalculatedPercentage * buttonsAvailableSpace + maxButtonWidth = min(max(buttonPercentageWidth, Button.Size.large.minimumWidth), maxButtonWidth) + } + + //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 { case .left: break @@ -313,3 +318,4 @@ class ButtonGroupPositionLayout: UICollectionViewLayout { } +