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

Allow customization of Exhibition display #39

Merged
merged 1 commit into from
Mar 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftUI

struct ContentView: View {
var body: some View {
exhibition
Exhibition()
}
}

Expand Down
22 changes: 14 additions & 8 deletions Example/Exhibition.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@
import Exhibition
import SwiftUI

public let exhibition = Exhibition(
exhibits: [
CustomButton_Previews.exhibit,
CustomDatePicker_Previews.exhibit,
CustomToggle_Previews.exhibit,
]
)
public struct Exhibition: View {
public var body: some View {
NavigationView {
ExhibitListView(
exhibits: [
CustomButton_Previews.exhibit,
CustomDatePicker_Previews.exhibit,
CustomToggle_Previews.exhibit,
]
)
}
}
}

struct Exhibition_Previews: PreviewProvider {
static var previews: some View {
exhibition
Exhibition()
}
}
18 changes: 12 additions & 6 deletions Exhibition.swifttemplate
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
import Exhibition
import SwiftUI

public let exhibition = Exhibition(
exhibits: [<% for type in types.types where type.inheritedTypes.contains("ExhibitProvider") { %>
<%= type.name %>.exhibit,<% } %>
]
)
public struct Exhibition: View {
public var body: some View {
NavigationView {
ExhibitListView(
exhibits: [<% for type in types.types where type.inheritedTypes.contains("ExhibitProvider") { %>
<%= type.name %>.exhibit,<% } %>
]
)
}
}
}

struct Exhibition_Previews: PreviewProvider {
static var previews: some View {
exhibition
Exhibition()
}
}
// sourcery:end
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct DoublingStringParameterView: ParameterView {
}
}

exhibition
Exhibition()
.parameterView(DoublingStringParameterView.self)
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SwiftUI

public struct Exhibition: View {
public struct ExhibitListView: View {

let exhibits: [Exhibit]

Expand All @@ -13,6 +13,8 @@ public struct Exhibition: View {
@State var preferredColorScheme: ColorScheme = .light
@State var layoutDirection: LayoutDirection = .leftToRight

@Environment(\.presentationMode) var presentationMode

private var sections: [String: [Exhibit]] {
Dictionary(grouping: searchResults, by: \.section)
}
Expand All @@ -33,48 +35,57 @@ public struct Exhibition: View {
public init(exhibits: [Exhibit]) {
self.exhibits = exhibits
}

public var body: some View {
NavigationView {
List(sections.sorted(by: keyAscending), id: \.key) { section in
if section.key.isEmpty {
List(sections.sorted(by: keyAscending), id: \.key) { section in
if section.key.isEmpty {
ForEach(section.value) { exhibit in
NavigationLink(exhibit.id, destination: debuggable(exhibit))
}
} else {
Section {
ForEach(section.value) { exhibit in
NavigationLink(exhibit.id, destination: debuggable(exhibit))
}
} else {
Section {
ForEach(section.value) { exhibit in
NavigationLink(exhibit.id, destination: debuggable(exhibit))
}
} header: {
Text(section.key)
}
} header: {
Text(section.key)
}
}
.modify {
if #available(macOS 12.0, *) {
$0.searchable(text: $searchText)
} else {
$0
}
.modify {
if #available(macOS 12.0, *) {
$0.searchable(text: $searchText)
} else {
$0
}
}
.navigationTitle("Exhibit")
.toolbar {
ToolbarItem {
Button {
rootDebugViewPresented = true
} label: {
Image(systemName: "gear")
}
}
.navigationTitle("Exhibit")
.toolbar {
ToolbarItem {
Button {
rootDebugViewPresented = true
} label: {
Image(systemName: "gear")
}
.if(presentationMode.wrappedValue.isPresented) {
$0.toolbar {
#if !os(macOS) && !os(watchOS)
ToolbarItem(placement: .navigationBarLeading) {
Button("Close") {
presentationMode.wrappedValue.dismiss()
}
}
#endif
}
.sheet(isPresented: $rootDebugViewPresented) {
DebugView(
context: .init(),
preferredColorScheme: $preferredColorScheme,
layoutDirection: $layoutDirection
)
}
}
.sheet(isPresented: $rootDebugViewPresented) {
DebugView(
context: .init(),
preferredColorScheme: $preferredColorScheme,
layoutDirection: $layoutDirection
)
}
.preferredColorScheme(preferredColorScheme)
.environment(\.layoutDirection, layoutDirection)
Expand Down Expand Up @@ -117,9 +128,9 @@ public struct Exhibition: View {
}
}

struct Exhibition_Previews: PreviewProvider {
struct ExhibitListView_Previews: PreviewProvider {
static var previews: some View {
Exhibition(
ExhibitListView(
exhibits: [
.init(name: "Text", section: "Section 1") { context in
Text(context.parameter(name: "Content", defaultValue: "Text"))
Expand Down
40 changes: 40 additions & 0 deletions Sources/Exhibition/Utilities/View+Modify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,44 @@ extension View {
) -> some View {
block(self)
}

/// Conditionally apply modifiers to a given view
///
/// See `ifLet` for version that takes an optional.
///
/// - Parameters:
/// - condition: Condition returning true/false for execution of the following blocks
/// - then: If condition is true, apply modifiers here.
/// - else: If condition is false, apply modifiers here.
/// - Returns: Self with modifiers applied accordingly.
@ViewBuilder public func `if`<Then: View, Else: View>(
_ condition: @autoclosure () -> Bool,
@ViewBuilder then: (Self) -> Then,
@ViewBuilder else: (Self) -> Else
) -> some View {
if condition() {
then(self)
} else {
`else`(self)
}
}

/// Conditionally apply modifiers to a given view
///
/// See `ifLet` for version that takes an optional.
///
/// - Parameters:
/// - condition: Condition returning true/false for execution of the following blocks
/// - then: If condition is true, apply modifiers here.
/// - Returns: Self with modifiers applied, or unchanged self if condition was false
@ViewBuilder public func `if`<Then: View>(
_ condition: @autoclosure () -> Bool,
@ViewBuilder then: (Self) -> Then
) -> some View {
if condition() {
then(self)
} else {
self
}
}
}