Impressia/Vernissage/Widgets/ContentWarning.swift

84 lines
2.8 KiB
Swift
Raw Normal View History

2023-01-09 14:48:41 +01:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
2023-03-28 10:35:38 +02:00
// Licensed under the Apache License 2.0.
2023-01-09 14:48:41 +01:00
//
2023-01-09 14:48:41 +01:00
import SwiftUI
struct ContentWarning<Content: View, Blurred: View>: View {
2023-01-09 14:48:41 +01:00
private let spoilerText: String?
private let content: () -> Content
private let blurred: () -> Blurred
2023-01-09 14:48:41 +01:00
@State private var showSensitive = false
init(spoilerText: String?,
@ViewBuilder content: @escaping () -> Content,
@ViewBuilder blurred: @escaping () -> Blurred) {
2023-01-09 14:48:41 +01:00
self.spoilerText = spoilerText
self.content = content
self.blurred = blurred
2023-01-09 14:48:41 +01:00
}
2023-01-09 14:48:41 +01:00
var body: some View {
if self.showSensitive {
ZStack {
content()
2023-01-09 14:48:41 +01:00
.transition(.opacity)
2023-01-09 14:48:41 +01:00
VStack(alignment: .trailing) {
HStack(alignment: .top) {
Spacer()
Button {
withAnimation {
self.showSensitive = false
}
} label: {
Image(systemName: "eye.slash")
.font(.title2)
.shadow(color: Color.systemBackground, radius: 0.3)
.padding()
}
2023-01-09 14:48:41 +01:00
}
Spacer()
}
.foregroundColor(.mainTextColor)
}
} else {
ZStack {
self.blurred()
2023-01-09 14:48:41 +01:00
VStack(alignment: .center) {
Spacer()
Image(systemName: "eye.slash.fill")
.font(.title2)
.shadow(color: Color.systemBackground, radius: 0.3)
2023-03-13 13:53:36 +01:00
Text("global.title.contentWarning", comment: "Sensitive content")
2023-01-09 14:48:41 +01:00
.font(.title2)
.shadow(color: Color.systemBackground, radius: 0.3)
if let spoilerText {
Text(spoilerText)
.font(.body)
.multilineTextAlignment(.center)
.shadow(color: Color.systemBackground, radius: 0.3)
}
Button {
withAnimation {
self.showSensitive = true
}
} label: {
2023-03-13 13:53:36 +01:00
Text("global.title.seePost", comment: "See post")
2023-01-09 14:48:41 +01:00
.shadow(color: Color.systemBackground, radius: 0.3)
}
.buttonStyle(.bordered)
Spacer()
}
.foregroundColor(.mainTextColor)
}
.transition(.opacity)
}
}
}