From b0f8fbc864b061c7d87db2f1b7fb16971d4ce375 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Mon, 28 Aug 2023 15:24:07 -0500 Subject: [PATCH] refactored button group Signed-off-by: Matt Bruce --- .../Buttons/ButtonGroup/ButtonGroup.swift | 51 ++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift b/VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift index cae6a126..a45fb847 100644 --- a/VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift +++ b/VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift @@ -13,15 +13,15 @@ import Combine /// A button group contains combinations of related CTAs including ``Button``, ``TextLink``, and ``TextLinkCaret``. This group component controls a combination's orientation, spacing, size and allowable size pairings. @objc(VDSButtonGroup) -open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, ButtongGroupPositionLayoutDelegate { - +open class ButtonGroup: View { + //-------------------------------------------------- // MARK: - Enums //-------------------------------------------------- public enum Alignment: String, CaseIterable { case left, center, right } - + public enum ChildWidth { case percentage(CGFloat) case value(CGFloat) @@ -30,23 +30,24 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- - //An object containing number of Button components per row for iPhones + /// An object containing number of Button components per row for iPhones open var rowQuantityPhone: Int = 0 { didSet { setNeedsUpdate() } } - - //An object containing number of Button components per row for iPads + + /// An object containing number of Button components per row for iPads open var rowQuantityTablet: Int = 0 { didSet { setNeedsUpdate() } } - - // An object containing number of Button components per row + + /// An object containing number of Button components per row open var rowQuantity: Int { UIDevice.isIPad ? rowQuantityTablet : rowQuantityPhone } - - //If provided, aligns TextLink/TextLinkCaret alignment when rowQuantity is set one. + + /// If provided, aligns TextLink/TextLinkCaret alignment when rowQuantity is set one. open var alignment: Alignment = .center { didSet { setNeedsUpdate() }} /// Array of Buttonable Views that are shown in the group. open var buttons: [Buttonable] = [] { didSet { setNeedsUpdate() }} - - /// If provided, width of Button components will be rendered based on this value. If omitted, default button widths are rendered. + private var _childWidth: ChildWidth? + + /// If provided, width of Button components will be rendered based on this value. If omitted, default button widths are rendered. open var childWidth: ChildWidth? { get { _childWidth } set { @@ -75,9 +76,9 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega $0.position = .center $0.delegate = self } - + fileprivate lazy var collectionView: SelfSizingCollectionView = { - + return SelfSizingCollectionView(frame: .zero, collectionViewLayout: positionLayout).with { $0.backgroundColor = .clear $0.showsHorizontalScrollIndicator = false @@ -89,7 +90,7 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega $0.register(ButtonGroupCollectionViewCell.self, forCellWithReuseIdentifier: "collectionViewCell") } }() - + //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- @@ -109,18 +110,18 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega } } } - + //-------------------------------------------------- // MARK: - Initializers //-------------------------------------------------- required public init() { super.init(frame: .zero) } - + public override init(frame: CGRect) { super.init(frame: .zero) } - + public required init?(coder: NSCoder) { super.init(coder: coder) } @@ -165,18 +166,20 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega self.collectionView.collectionViewLayout.invalidateLayout() } } - +} + +extension ButtonGroup: UICollectionViewDataSource, UICollectionViewDelegate { //-------------------------------------------------- // MARK: - UICollectionViewDataSource //-------------------------------------------------- public func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } - + public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return buttons.count } - + public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let button = buttons[indexPath.row] guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as? ButtonGroupCollectionViewCell else { return UICollectionViewCell() } @@ -202,8 +205,10 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega return false } } - - public func collectionView(_ collectionView: UICollectionView, buttonableAtIndexPath indexPath: IndexPath) -> Buttonable { +} + +extension ButtonGroup : ButtongGroupPositionLayoutDelegate { + func collectionView(_ collectionView: UICollectionView, buttonableAtIndexPath indexPath: IndexPath) -> Buttonable { buttons[indexPath.row] } }