If you haven't got this working, try this...
Add this extension somewhere:
extension View {
/// Applies a modifier to a view conditionally.
///
/// - Parameters:
/// - condition: The condition to determine if the content should be applied.
/// - content: The modifier to apply to the view.
/// - Returns: The modified view.
@ViewBuilder func modifier<T: View>(
if condition: @autoclosure () -> Bool,
then content: (Self) -> T
) -> some View {
if condition() {
content(self)
} else {
self
}
}
/// Applies a modifier to a view conditionally.
///
/// - Parameters:
/// - condition: The condition to determine the content to be applied.
/// - trueContent: The modifier to apply to the view if the condition passes.
/// - falseContent: The modifier to apply to the view if the condition fails.
/// - Returns: The modified view.
@ViewBuilder func modifier<TrueContent: View, FalseContent: View>(
if condition: @autoclosure () -> Bool,
then trueContent: (Self) -> TrueContent,
else falseContent: (Self) -> FalseContent
) -> some View {
if condition() {
trueContent(self)
} else {
falseContent(self)
}
}
}
Create a little helper function somewhere that saves you a bunch of typing. Pass in your redactionReasons variable:
func getHideInfo(_ redactionReasons: RedactionReasons) -> Bool {
return (redactionReasons.contains(.privacy) || redactionReasons.contains(.placeholder))
}
In your view body, add this line:
let hideInfo: Bool = getHideInfo(redactionReasons)
i.e.:
var body: some View {
let hideInfo: Bool = getHideInfo(redactionReasons)
...
}
Then, on your views, Text(), Image() etc. add this;
.modifier(if: hideInfo) { $0.redacted(reason: .placeholder) } else: { $0.unredacted() }
i.e.:
Text("Hello World!")
.modifier(if: hideInfo) { $0.redacted(reason: .placeholder) } else: { $0.unredacted() }
What that does is redact the view if it's meant to be private, and unredact it if not. It's pretty easy having that one modifier on a view.
Remember to use the different controls on the preview canvas in Xcode to see the "Presentation" variants. It will show you what your widget looks like normally, privacy sensitive, placeholder and luminance reduced.
Hope this helps.