refactored button group

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-08-28 15:24:07 -05:00
parent 94fcd7bf60
commit b0f8fbc864

View File

@ -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. /// 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) @objc(VDSButtonGroup)
open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, ButtongGroupPositionLayoutDelegate { open class ButtonGroup: View {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Enums // MARK: - Enums
//-------------------------------------------------- //--------------------------------------------------
public enum Alignment: String, CaseIterable { public enum Alignment: String, CaseIterable {
case left, center, right case left, center, right
} }
public enum ChildWidth { public enum ChildWidth {
case percentage(CGFloat) case percentage(CGFloat)
case value(CGFloat) case value(CGFloat)
@ -30,23 +30,24 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Public Properties // 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() } } 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() } } 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 } 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() }} open var alignment: Alignment = .center { didSet { setNeedsUpdate() }}
/// Array of Buttonable Views that are shown in the group. /// Array of Buttonable Views that are shown in the group.
open var buttons: [Buttonable] = [] { didSet { setNeedsUpdate() }} 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? 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? { open var childWidth: ChildWidth? {
get { _childWidth } get { _childWidth }
set { set {
@ -75,9 +76,9 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega
$0.position = .center $0.position = .center
$0.delegate = self $0.delegate = self
} }
fileprivate lazy var collectionView: SelfSizingCollectionView = { fileprivate lazy var collectionView: SelfSizingCollectionView = {
return SelfSizingCollectionView(frame: .zero, collectionViewLayout: positionLayout).with { return SelfSizingCollectionView(frame: .zero, collectionViewLayout: positionLayout).with {
$0.backgroundColor = .clear $0.backgroundColor = .clear
$0.showsHorizontalScrollIndicator = false $0.showsHorizontalScrollIndicator = false
@ -89,7 +90,7 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega
$0.register(ButtonGroupCollectionViewCell.self, forCellWithReuseIdentifier: "collectionViewCell") $0.register(ButtonGroupCollectionViewCell.self, forCellWithReuseIdentifier: "collectionViewCell")
} }
}() }()
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Overrides // MARK: - Overrides
//-------------------------------------------------- //--------------------------------------------------
@ -109,18 +110,18 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega
} }
} }
} }
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Initializers // MARK: - Initializers
//-------------------------------------------------- //--------------------------------------------------
required public init() { required public init() {
super.init(frame: .zero) super.init(frame: .zero)
} }
public override init(frame: CGRect) { public override init(frame: CGRect) {
super.init(frame: .zero) super.init(frame: .zero)
} }
public required init?(coder: NSCoder) { public required init?(coder: NSCoder) {
super.init(coder: coder) super.init(coder: coder)
} }
@ -165,18 +166,20 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega
self.collectionView.collectionViewLayout.invalidateLayout() self.collectionView.collectionViewLayout.invalidateLayout()
} }
} }
}
extension ButtonGroup: UICollectionViewDataSource, UICollectionViewDelegate {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - UICollectionViewDataSource // MARK: - UICollectionViewDataSource
//-------------------------------------------------- //--------------------------------------------------
public func numberOfSections(in collectionView: UICollectionView) -> Int { public func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1 return 1
} }
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return buttons.count return buttons.count
} }
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let button = buttons[indexPath.row] let button = buttons[indexPath.row]
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as? ButtonGroupCollectionViewCell else { return UICollectionViewCell() } 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 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] buttons[indexPath.row]
} }
} }