-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Big changes. Supporting new grid-based list. (#1)
- Loading branch information
Showing
17 changed files
with
465 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// BaseGrid.swift | ||
// | ||
// Copyright 2022 FlowAllocator LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import SwiftUI | ||
|
||
// Grid-based list | ||
struct BaseGrid<Element, Header, Rows>: View | ||
where Element: Identifiable, | ||
Header: View, | ||
Rows: View | ||
{ | ||
typealias Config = TablerGridConfig<Element> | ||
typealias HeaderContent = (Binding<TablerSort<Element>?>) -> Header | ||
typealias RowContent = () -> Rows | ||
|
||
let config: Config | ||
@ViewBuilder let headerContent: HeaderContent | ||
@ViewBuilder let rowsContent: RowContent | ||
|
||
var body: some View { | ||
BaseTable(config: config, | ||
headerContent: headerContent) { buildHeader in | ||
|
||
VStack(spacing: config.rowSpacing) { | ||
buildHeader() | ||
|
||
ScrollView { | ||
LazyVGrid(columns: config.gridItems, | ||
alignment: config.alignment, | ||
spacing: config.rowSpacing) { | ||
rowsContent() | ||
} | ||
} | ||
} | ||
.padding(config.paddingInsets) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// TablerGrid.swift | ||
// | ||
// Copyright 2022 FlowAllocator LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import SwiftUI | ||
|
||
/// Grid-based table | ||
public struct TablerGrid<Element, Header, Row, Results>: View | ||
where Element: Identifiable, | ||
Header: View, | ||
Row: View, | ||
Results: RandomAccessCollection, | ||
Results.Element == Element | ||
{ | ||
public typealias Config = TablerGridConfig<Element> | ||
public typealias Hovered = Element.ID? | ||
public typealias HeaderContent = (Binding<TablerSort<Element>?>) -> Header | ||
public typealias RowContent = (Element) -> Row | ||
|
||
// MARK: Parameters | ||
|
||
private let config: Config | ||
private let headerContent: HeaderContent | ||
private let rowContent: RowContent | ||
private var results: Results | ||
|
||
public init(_ config: Config, | ||
@ViewBuilder headerContent: @escaping HeaderContent, | ||
@ViewBuilder rowContent: @escaping RowContent, | ||
results: Results) | ||
{ | ||
self.config = config | ||
self.headerContent = headerContent | ||
self.rowContent = rowContent | ||
self.results = results | ||
} | ||
|
||
// MARK: Locals | ||
|
||
@State private var hovered: Hovered = nil | ||
|
||
// MARK: Views | ||
|
||
public var body: some View { | ||
BaseGrid(config: config, | ||
headerContent: headerContent) { | ||
ForEach(results.filter(config.filter ?? { _ in true })) { element in | ||
BaseGridRow(config: config, | ||
element: element, | ||
hovered: $hovered) { | ||
|
||
// TODO how to provide a continuous hover block (selection, etc.)? | ||
rowContent(element) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
public extension TablerGrid { | ||
// omitting Header | ||
init(_ config: Config, | ||
@ViewBuilder rowContent: @escaping RowContent, | ||
results: Results) | ||
where Header == EmptyView | ||
{ | ||
self.init(config, | ||
headerContent: { _ in EmptyView() }, | ||
rowContent: rowContent, | ||
results: results) | ||
} | ||
} |
Oops, something went wrong.