refactored naming conventions and merged percentage and width into an enum

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-08-28 15:14:35 -05:00
parent 5756b299e0
commit d84ccbb5c2

View File

@ -18,57 +18,56 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Enums // MARK: - Enums
//-------------------------------------------------- //--------------------------------------------------
public enum ButtonPosition: String, CaseIterable { public enum Alignment: String, CaseIterable {
case left, center, right case left, center, right
} }
public enum ChildWidth {
case percentage(CGFloat)
case value(CGFloat)
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Public Properties // MARK: - Public Properties
//-------------------------------------------------- //--------------------------------------------------
//An object containing number of Button components per row, in each viewport //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
open var rowQuantityTablet: Int = 0 { didSet { setNeedsUpdate() } } open var rowQuantityTablet: Int = 0 { didSet { setNeedsUpdate() } }
// 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 buttonPosition: ButtonPosition = .center { didSet { setNeedsUpdate() }} open var alignment: Alignment = .center { didSet { setNeedsUpdate() }}
/// 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. /// If provided, width of Button components will be rendered based on this value. If omitted, default button widths are rendered.
open var buttonWidth: CGFloat? { private var _childWidth: ChildWidth?
didSet { open var childWidth: ChildWidth? {
if let buttonWidth, let buttonPercentage, buttonWidth > 0, buttonPercentage > 0{ get { _childWidth }
self.buttonPercentage = nil set {
} if let newValue {
buttons.forEach { button in switch newValue {
if let button = button as? Button { case .percentage(let percentage):
button.width = buttonWidth if percentage <= 100.0, rowQuantity > 0 {
_childWidth = newValue
}
case .value(let value):
if value > 0 {
_childWidth = newValue
}
} }
} else {
_childWidth = nil
} }
setNeedsUpdate() setNeedsUpdate()
} }
} }
var _buttonPercentage: CGFloat?
open var buttonPercentage: CGFloat? {
get { _buttonPercentage }
set {
if let newValue, newValue <= 100.0, rowQuantity > 0 {
_buttonPercentage = newValue
} else {
_buttonPercentage = nil
}
if let buttonWidth, let buttonPercentage, buttonWidth > 0, buttonPercentage > 0 {
self.buttonWidth = nil
}
positionLayout.buttonPercentage = buttonPercentage
setNeedsUpdate()
}
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Private Properties // MARK: - Private Properties
//-------------------------------------------------- //--------------------------------------------------
@ -141,8 +140,20 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega
/// Used to make changes to the View based off a change events or from local properties. /// Used to make changes to the View based off a change events or from local properties.
open override func updateView() { open override func updateView() {
super.updateView() super.updateView()
positionLayout.position = buttonPosition positionLayout.position = alignment
positionLayout.rowQuantity = rowQuantity positionLayout.rowQuantity = rowQuantity
var percentage: CGFloat?
var width: CGFloat?
if let childWidth {
switch childWidth {
case .value(let value): width = value
case .percentage(let value): percentage = value
}
}
buttons.forEach { ($0 as? Button)?.width = width }
positionLayout.buttonPercentage = percentage
collectionView.reloadData() collectionView.reloadData()
} }