generated from StanfordBDHG/SwiftPackageTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
1 parent
1cf188c
commit 5e08d98
Showing
6 changed files
with
196 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import SwiftUI | ||
|
||
|
||
extension View { | ||
/// Applies the given transform if the given condition evaluates to `true`. | ||
/// | ||
/// ### Usage | ||
/// | ||
/// ```swift | ||
/// struct ConditionalModifierTestView: View { | ||
/// @State var condition = false | ||
/// | ||
/// var body: some View { | ||
/// VStack { | ||
/// Text("Condition present") | ||
/// .if(condition) { view in | ||
/// view | ||
/// .hidden() | ||
/// } | ||
/// | ||
/// Button("Toggle Condition") { | ||
/// condition.toggle() | ||
/// } | ||
/// } | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// - Parameters: | ||
/// - condition: The condition to evaluate. | ||
/// - transform: The transform to apply to the source `View`. | ||
/// - Returns: Either the original `View` or the modified `View` if the condition is `true`. | ||
@ViewBuilder public func `if`<Content: View>(_ condition: Bool, transform: (Self) -> Content) -> some View { | ||
if condition { | ||
transform(self) | ||
} else { | ||
self | ||
} | ||
} | ||
|
||
/// Applies the given transform if the given condition closure evaluates to `true`. | ||
/// | ||
/// ### Usage | ||
/// | ||
/// ```swift | ||
/// struct ConditionalModifierTestView: View { | ||
/// @State var closureCondition = false | ||
/// | ||
/// var body: some View { | ||
/// VStack { | ||
/// Text("Closure Condition present") | ||
/// .if(condition: { | ||
/// closureCondition | ||
/// }, transform: { view in | ||
/// view | ||
/// .hidden() | ||
/// }) | ||
/// | ||
/// Button("Toggle Closure Condition") { | ||
/// closureCondition.toggle() | ||
/// } | ||
/// } | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// - Parameters: | ||
/// - condition: The condition closure to evaluate. | ||
/// - transform: The transform to apply to the source `View`. | ||
/// - Returns: Either the original `View` or the modified `View` if the condition closure is `true`. | ||
@ViewBuilder public func `if`<Content: View>(condition: () -> Bool, transform: (Self) -> Content) -> some View { | ||
if condition() { | ||
transform(self) | ||
} else { | ||
self | ||
} | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
Tests/UITests/TestApp/ViewsTests/ConditionalModifierTestView.swift
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,54 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2022 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import SpeziViews | ||
import SwiftUI | ||
|
||
|
||
struct ConditionalModifierTestView: View { | ||
@State var condition = false | ||
@State var closureCondition = false | ||
|
||
|
||
var body: some View { | ||
VStack { | ||
Text("Condition present") | ||
.if(condition) { view in | ||
view | ||
.hidden() | ||
} | ||
|
||
Button("Toggle Condition") { | ||
condition.toggle() | ||
} | ||
.buttonStyle(.borderedProminent) | ||
.padding(.bottom, 20) | ||
|
||
Divider() | ||
|
||
Text("Closure Condition present") | ||
.if(condition: { | ||
closureCondition | ||
}, transform: { view in | ||
view | ||
.hidden() | ||
}) | ||
.padding(.top, 20) | ||
|
||
Button("Toggle Closure Condition") { | ||
condition.toggle() | ||
} | ||
.buttonStyle(.borderedProminent) | ||
} | ||
} | ||
} | ||
|
||
|
||
#Preview { | ||
ConditionalModifierTestView() | ||
} |
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