Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent negative layout results in case the available width is zero #17

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/GridStack/GridCalculator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ struct GridCalculator {
minimumCellWidth: CGFloat,
cellSpacing: CGFloat
) -> GridDefinition {
// Width for views inside a NavigationView might be reported as 0 on the first pass
guard availableWidth != 0 else {
return (columnWidth: 0, columnCount: 0)
}

/**
* 1. Subtract the cell spacing once from all the available width
* 2. Add the cell spacing to each cell Width
Expand Down
14 changes: 13 additions & 1 deletion Tests/GridStackTests/GridStackTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,23 @@ final class GridCalculatorTests: XCTestCase {
XCTAssertEqual(columnWidth, 280)
}

func test_zeroAvailableWidth_givesZeroColumnsAndColumnWidth() {
let (columnWidth, columnCount) = subject.calculate(
availableWidth: 0,
minimumCellWidth: 290,
cellSpacing: 10
)

XCTAssertEqual(columnCount, 0)
XCTAssertEqual(columnWidth, 0)
}

static var allTests = [
("test_cellsFitExactlyWithoutSpacing", test_threeCellsFitExactlyWithoutSpacing),
("test_cellsFitExactlyWithSpacing", test_threeCellsFitExactlyWithSpacing),
("test_threeCellsJustDontFit", test_threeCellsJustDontFit),
("test_minimumCellWidthIsWiderThanAvailableSpace_givesOneColumn", test_minimumCellWidthIsWiderThanAvailableSpace_givesOneColumn),
("test_minimumCellWidthWithSpacingIsWiderThanAvailableSpace_givesOneColumn", test_minimumCellWidthWithSpacingIsWiderThanAvailableSpace_givesOneColumn)
("test_minimumCellWidthWithSpacingIsWiderThanAvailableSpace_givesOneColumn", test_minimumCellWidthWithSpacingIsWiderThanAvailableSpace_givesOneColumn),
("test_zeroAvailableWidth_givesZeroColumnsAndColumnWidth", test_zeroAvailableWidth_givesZeroColumnsAndColumnWidth)
]
}