refactor to allow custom spacing

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-03-27 14:40:05 -05:00
parent 516674f17e
commit d22a7a12b6

View File

@ -146,6 +146,8 @@ class ButtonLayoutAttributes: UICollectionViewLayoutAttributes{
class ButtonGroupPositionLayout: UICollectionViewLayout { class ButtonGroupPositionLayout: UICollectionViewLayout {
weak var delegate: ButtongGroupPositionLayoutDelegate? weak var delegate: ButtongGroupPositionLayoutDelegate?
var verticalSpacer: ((ButtonCollectionViewRow, ButtonCollectionViewRow?) -> CGFloat)?
var axisSpacer: ((NSLayoutConstraint.Axis, ButtonBase, ButtonBase) -> CGFloat)?
// Total height of the content. Will be used to configure the scrollview content // Total height of the content. Will be used to configure the scrollview content
var layoutHeight: CGFloat = 0.0 var layoutHeight: CGFloat = 0.0
@ -226,7 +228,7 @@ class ButtonGroupPositionLayout: UICollectionViewLayout {
let neighbor = delegate.collectionView(collectionView, buttonBaseAtIndexPath: IndexPath(item: nextItem, section: section)) let neighbor = delegate.collectionView(collectionView, buttonBaseAtIndexPath: IndexPath(item: nextItem, section: section))
// get the spacing to go between the current and next button // get the spacing to go between the current and next button
itemSpacing = ButtonGroupConstants.getSpacing(for: .horizontal, with: itemButtonBase, neighboring: neighbor) itemSpacing = getAxisSpacing(for: .horizontal, with: itemButtonBase, neighboring: neighbor)
} }
// create the custom layout attribute // create the custom layout attribute
@ -255,7 +257,7 @@ class ButtonGroupPositionLayout: UICollectionViewLayout {
if item > 0 { if item > 0 {
let prevRow = rows[item - 1] let prevRow = rows[item - 1]
rowSpacing = ButtonGroupConstants.getVerticalSpacing(for: prevRow, neighboringRow: row) rowSpacing = getVerticalSpacing(for: prevRow, neighboringRow: row)
row.rowY = layoutHeight + rowSpacing row.rowY = layoutHeight + rowSpacing
layoutHeight += rowSpacing layoutHeight += rowSpacing
} }
@ -300,5 +302,19 @@ class ButtonGroupPositionLayout: UICollectionViewLayout {
} }
return collectionView.bounds.width return collectionView.bounds.width
} }
private func getAxisSpacing(for axis: NSLayoutConstraint.Axis, with primary: ButtonBase, neighboring: ButtonBase) -> CGFloat {
guard let axisSpacer else {
return ButtonGroupConstants.getSpacing(for: axis, with: primary, neighboring: neighboring)
}
return axisSpacer(axis, primary, neighboring)
}
private func getVerticalSpacing(for row: ButtonCollectionViewRow, neighboringRow: ButtonCollectionViewRow?) -> CGFloat {
guard let verticalSpacer else {
return ButtonGroupConstants.getVerticalSpacing(for: row, neighboringRow: neighboringRow)
}
return verticalSpacer(row, neighboringRow)
}
} }