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

Add the option to specify scroll direction #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ struct PeopleView: View {
vSpacing: 50,
hSpacing: 20,
vPadding: 100,
hPadding: 20) { person in
hPadding: 20,
scrollDirection: .horizontal) { person in
GridCell(person: person)
}
}
Expand Down Expand Up @@ -130,7 +131,6 @@ struct PeopleView: View {
Version `0.1.1` of `QGrid ` contains a very limited set of features. It could be extended by implementing the following tasks:

     ☘️ Parameterize spacing&padding configuration depending on the device orientation
     ☘️ Add the option to specify scroll direction
     ☘️ Add content-only initializer to QGrid struct, without a collection of identified data as argument (As in SwiftUI’s `List`)
     ☘️ Add support for other platforms (watchOS)
     ☘️ Add `Stack` layout option (as in `UICollectionView`)
Expand Down
20 changes: 19 additions & 1 deletion Sources/QGrid/QGrid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import SwiftUI
public struct QGrid<Data, Content>: View
where Data : RandomAccessCollection, Content : View, Data.Element : Identifiable {
private struct QGridIndex : Identifiable { var id: Int }
public enum ScrollDirection { case vertical, horizontal, both, none }

// MARK: - STORED PROPERTIES

Expand All @@ -41,6 +42,7 @@ public struct QGrid<Data, Content>: View
private let hSpacing: CGFloat
private let vPadding: CGFloat
private let hPadding: CGFloat
private let scrollDirection: ScrollDirection

private let data: [Data.Element]
private let content: (Data.Element) -> Content
Expand All @@ -58,6 +60,7 @@ public struct QGrid<Data, Content>: View
/// - hSpacing: Horizontal spacing: The distance between each cell in grid's row. If not provided, the default value will be used.
/// - vPadding: Vertical padding: The distance between top/bottom edge of the grid and the parent view. If not provided, the default value will be used.
/// - hPadding: Horizontal padding: The distance between leading/trailing edge of the grid and the parent view. If not provided, the default value will be used.
/// - scrollDirection: Scrolling direction: The direction of scrolling behaviour. If not provided, the default `.vertical` value will be used.
/// - content: A closure returning the content of the individual cell
public init(_ data: Data,
columns: Int,
Expand All @@ -66,6 +69,7 @@ public struct QGrid<Data, Content>: View
hSpacing: CGFloat = 10,
vPadding: CGFloat = 10,
hPadding: CGFloat = 10,
scrollDirection: ScrollDirection = .vertical,
content: @escaping (Data.Element) -> Content) {
self.data = data.map { $0 }
self.content = content
Expand All @@ -75,6 +79,7 @@ public struct QGrid<Data, Content>: View
self.hSpacing = hSpacing
self.vPadding = vPadding
self.hPadding = hPadding
self.scrollDirection = scrollDirection
}

// MARK: - COMPUTED PROPERTIES
Expand All @@ -96,7 +101,7 @@ public struct QGrid<Data, Content>: View
/// Declares the content and behavior of this view.
public var body : some View {
GeometryReader { geometry in
ScrollView(showsIndicators: false) {
ScrollView(self.scrollAxes, showsIndicators: false) {
VStack(spacing: self.vSpacing) {
ForEach((0..<self.rows).map { QGridIndex(id: $0) }) { row in
self.rowAtIndex(row.id * self.cols,
Expand Down Expand Up @@ -137,5 +142,18 @@ public struct QGrid<Data, Content>: View
let width = geometry.size.width - hSpacings - hPadding * 2
return width / CGFloat(self.cols)
}

private var scrollAxes: Axis.Set {
switch scrollDirection {
case .vertical:
return [.vertical]
case .horizontal:
return [.horizontal]
case .both:
return [.vertical, .horizontal]
case .none:
return []
}
}
}