-
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.
- Loading branch information
Reed Es
committed
Mar 5, 2022
1 parent
2c6b82a
commit a883c07
Showing
5 changed files
with
209 additions
and
35 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
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,122 @@ | ||
// | ||
// TablerGridC.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, with support for bound values through Core Data | ||
public struct TablerGridC<Element, Header, Row, RowBack>: View | ||
where Element: Identifiable & NSFetchRequestResult & ObservableObject, | ||
Header: View, | ||
Row: View, | ||
RowBack: View | ||
{ | ||
public typealias Config = TablerGridConfig<Element> | ||
public typealias Context = TablerContext<Element> | ||
public typealias Hovered = Element.ID? | ||
public typealias HeaderContent = (Binding<Context>) -> Header | ||
public typealias ProjectedValue = ObservedObject<Element>.Wrapper | ||
public typealias RowContent = (ProjectedValue) -> Row | ||
public typealias RowBackground = (Element) -> RowBack | ||
public typealias Fetched = FetchedResults<Element> | ||
|
||
// MARK: Parameters | ||
|
||
private let config: Config | ||
private let headerContent: HeaderContent | ||
private let rowContent: RowContent | ||
private let rowBackground: RowBackground | ||
private var results: Fetched | ||
|
||
public init(_ config: Config = .init(), | ||
@ViewBuilder header: @escaping HeaderContent, | ||
@ViewBuilder row: @escaping RowContent, | ||
@ViewBuilder rowBackground: @escaping RowBackground, | ||
results: Fetched) | ||
{ | ||
self.config = config | ||
headerContent = header | ||
rowContent = row | ||
self.rowBackground = rowBackground | ||
self.results = results | ||
_context = State(initialValue: TablerContext(config)) | ||
} | ||
|
||
// MARK: Locals | ||
|
||
@State private var hovered: Hovered = nil | ||
@State private var context: Context | ||
|
||
// MARK: Views | ||
|
||
public var body: some View { | ||
BaseGrid(context: $context, | ||
header: headerContent) { | ||
ForEach(results) { rawElem in | ||
ObservableHolder(element: rawElem) { obsElem in | ||
rowContent(obsElem) | ||
.modifier(GridItemMod(config, rawElem, $hovered)) | ||
.background(rowBackground(rawElem)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
public extension TablerGridC { | ||
// omitting Header | ||
init(_ config: Config, | ||
@ViewBuilder row: @escaping RowContent, | ||
@ViewBuilder rowBackground: @escaping RowBackground, | ||
results: Fetched) | ||
where Header == EmptyView | ||
{ | ||
self.init(config, | ||
header: { _ in EmptyView() }, | ||
row: row, | ||
rowBackground: rowBackground, | ||
results: results) | ||
} | ||
|
||
// omitting Background | ||
init(_ config: Config, | ||
@ViewBuilder header: @escaping HeaderContent, | ||
@ViewBuilder row: @escaping RowContent, | ||
results: Fetched) | ||
where RowBack == EmptyView | ||
{ | ||
self.init(config, | ||
header: header, | ||
row: row, | ||
rowBackground: { _ in EmptyView() }, | ||
results: results) | ||
} | ||
|
||
// omitting Header AND Background | ||
init(_ config: Config, | ||
@ViewBuilder row: @escaping RowContent, | ||
results: Fetched) | ||
where Header == EmptyView, RowBack == EmptyView | ||
{ | ||
self.init(config, | ||
header: { _ in EmptyView() }, | ||
row: row, | ||
rowBackground: { _ in EmptyView() }, | ||
results: results) | ||
} | ||
|
||
} |
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