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

Implement grid layout #43

Merged
merged 2 commits into from
Jan 14, 2025
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
38 changes: 38 additions & 0 deletions Development/Development/BookCollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,41 @@ struct BookPlatformList: View, PreviewProvider {

return Preview()
}

#Preview("Simple grid layout") {

struct Book: View {

@State var selected: Set<Item.ID> = .init()

var body: some View {

CollectionView(
layout: .grid,
content: {
SelectableForEach(
data: Item.mock(),
selection: .multiple(
selected: selected,
canSelectMore: selected.count < 3,
onChange: { e, action in
switch action {
case .selected:
selected.insert(e)
case .deselected:
selected.remove(e)
}
}
),
selectionIdentifier: \.id,
cell: { index, item in
Cell(index: index, item: item)
}
)
}
)
}
}

return Book()
}
74 changes: 58 additions & 16 deletions Sources/CollectionView/CollectionViewLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,36 +155,73 @@ public enum CollectionViewLayouts {
}

public struct Grid: CollectionViewLayoutType {

public let gridItems: [GridItem]

public let columns: [GridItem]
public let spacing: CGFloat?
public let direction: CollectionViewListDirection

public var showsIndicators: Bool = false

public var contentPadding: EdgeInsets

public var spacing: CGFloat?

public init(
columns: [GridItem],
gridItems: [GridItem],
direction: CollectionViewListDirection,
spacing: CGFloat? = nil,
contentPadding: EdgeInsets = .init()
) {
self.columns = columns
self.direction = direction
self.contentPadding = contentPadding
self.gridItems = gridItems
self.spacing = spacing
self.contentPadding = contentPadding
}

public consuming func contentPadding(_ contentPadding: EdgeInsets) -> Self {

self.contentPadding = contentPadding

return self
}

public consuming func showsIndicators(_ showsIndicators: Bool) -> Self {

self.showsIndicators = showsIndicators

return self
}

public func body(content: Content) -> some View {
ScrollView {
LazyVGrid(columns: columns, spacing: spacing) {
content
switch direction {
case .vertical:

ScrollView(.vertical, showsIndicators: showsIndicators) {
LazyVGrid(
columns: gridItems,
spacing: spacing
) {
content
}
.padding(contentPadding)
}
.padding(contentPadding)

case .horizontal:

ScrollView(.horizontal, showsIndicators: showsIndicators) {
LazyHGrid(
rows: gridItems,
spacing: spacing
) {
content
}
.padding(contentPadding)
}

}
}

public consuming func contentPadding(_ contentPadding: EdgeInsets) -> Self {

self.contentPadding = contentPadding

return self
}
}

}
Expand All @@ -202,10 +239,15 @@ extension CollectionViewLayoutType where Self == CollectionViewLayouts.List<Empt
extension CollectionViewLayoutType where Self == CollectionViewLayouts.Grid {

public static func grid(
columns: [GridItem],
gridItems: [GridItem],
direction: CollectionViewListDirection,
spacing: CGFloat? = nil
) -> Self {
CollectionViewLayouts.Grid(columns: columns, spacing: spacing)
CollectionViewLayouts.Grid(
gridItems: gridItems,
direction: direction,
spacing: spacing
)
}

}