secretive/Sources/Secretive/Views/CreateSecretView.swift

249 lines
8.4 KiB
Swift
Raw Permalink Normal View History

2020-03-04 07:14:38 +00:00
import SwiftUI
import SecretKit
2020-09-22 06:12:50 +00:00
struct CreateSecretView<StoreType: SecretStoreModifiable>: View {
2020-09-22 06:12:50 +00:00
@ObservedObject var store: StoreType
@Binding var showing: Bool
2020-09-22 06:12:50 +00:00
@State private var name = ""
@State private var requiresAuthentication = true
2020-03-04 07:14:38 +00:00
var body: some View {
2020-03-07 08:00:09 +00:00
VStack {
HStack {
VStack {
HStack {
Text("create_secret_title")
2022-12-19 00:19:54 +00:00
.font(.largeTitle)
2020-03-07 08:00:09 +00:00
Spacer()
}
HStack {
Text("create_secret_name_label")
TextField("create_secret_name_placeholder", text: $name)
2022-12-19 00:19:54 +00:00
.focusable()
2020-03-07 08:00:09 +00:00
}
ThumbnailPickerView(items: [
ThumbnailPickerView.Item(value: true, name: "create_secret_require_authentication_title", description: "create_secret_require_authentication_description", thumbnail: AuthenticationView()),
ThumbnailPickerView.Item(value: false, name: "create_secret_notify_title",
description: "create_secret_notify_description",
thumbnail: NotificationView())
], selection: $requiresAuthentication)
2020-03-04 07:14:38 +00:00
}
}
2020-03-07 08:00:09 +00:00
HStack {
Spacer()
Button("create_secret_cancel_button") {
2020-09-22 06:12:50 +00:00
showing = false
2020-03-04 07:14:38 +00:00
}
2020-09-22 06:12:50 +00:00
.keyboardShortcut(.cancelAction)
Button("create_secret_create_button", action: save)
2020-09-22 06:12:50 +00:00
.disabled(name.isEmpty)
.keyboardShortcut(.defaultAction)
2020-03-04 07:14:38 +00:00
}
2020-03-07 08:00:09 +00:00
}.padding()
2020-03-04 07:14:38 +00:00
}
2020-03-04 07:14:38 +00:00
func save() {
try! store.create(name: name, requiresAuthentication: requiresAuthentication)
2020-09-22 06:12:50 +00:00
showing = false
2020-03-04 07:14:38 +00:00
}
2022-12-19 00:19:54 +00:00
}
struct ThumbnailPickerView<ValueType: Hashable>: View {
private let items: [Item<ValueType>]
@Binding var selection: ValueType
init(items: [ThumbnailPickerView<ValueType>.Item<ValueType>], selection: Binding<ValueType>) {
self.items = items
_selection = selection
}
var body: some View {
HStack(alignment: .top) {
ForEach(items) { item in
VStack(alignment: .leading, spacing: 15) {
item.thumbnail
.frame(height: 200)
.overlay(RoundedRectangle(cornerRadius: 10)
.stroke(lineWidth: item.value == selection ? 15 : 0))
.clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous))
.foregroundColor(.accentColor)
VStack(alignment: .leading, spacing: 5) {
Text(item.name)
.bold()
Text(item.description)
.fixedSize(horizontal: false, vertical: true)
}
}
.frame(width: 250)
.onTapGesture {
withAnimation(.spring()) {
selection = item.value
}
}
}
.padding(5)
}
}
}
extension ThumbnailPickerView {
struct Item<ValueType: Hashable>: Identifiable {
let id = UUID()
let value: ValueType
let name: LocalizedStringKey
let description: LocalizedStringKey
2022-12-19 00:19:54 +00:00
let thumbnail: AnyView
init<ViewType: View>(value: ValueType, name: LocalizedStringKey, description: LocalizedStringKey, thumbnail: ViewType) {
2022-12-19 00:19:54 +00:00
self.value = value
self.name = name
self.description = description
self.thumbnail = AnyView(thumbnail)
}
}
}
@MainActor class SystemBackground: ObservableObject {
static let shared = SystemBackground()
@Published var image: NSImage?
private init() {
if let mainScreen = NSScreen.main, let imageURL = NSWorkspace.shared.desktopImageURL(for: mainScreen) {
image = NSImage(contentsOf: imageURL)
} else {
image = nil
}
}
}
struct SystemBackgroundView: View {
let anchor: UnitPoint
var body: some View {
if let image = SystemBackground.shared.image {
Image(nsImage: image)
.resizable()
.scaleEffect(3, anchor: anchor)
.clipped()
.allowsHitTesting(false)
} else {
Rectangle()
.foregroundColor(Color(.systemPurple))
}
}
2020-03-04 07:14:38 +00:00
}
2022-12-19 00:19:54 +00:00
struct AuthenticationView: View {
var body: some View {
ZStack {
SystemBackgroundView(anchor: .center)
GeometryReader { geometry in
VStack {
Image(systemName: "touchid")
.resizable()
.aspectRatio(contentMode: .fit)
.foregroundColor(Color(.systemRed))
Text(verbatim: "Touch ID Prompt")
2022-12-19 00:19:54 +00:00
.font(.headline)
.foregroundColor(.primary)
.redacted(reason: .placeholder)
VStack {
Text(verbatim: "Touch ID Detail prompt.Detail two.")
2022-12-19 00:19:54 +00:00
.font(.caption2)
.foregroundColor(.primary)
Text(verbatim: "Touch ID Detail prompt.Detail two.")
2022-12-19 00:19:54 +00:00
.font(.caption2)
.foregroundColor(.primary)
}
.redacted(reason: .placeholder)
RoundedRectangle(cornerRadius: 5)
.frame(width: geometry.size.width, height: 20, alignment: .center)
.foregroundColor(.accentColor)
RoundedRectangle(cornerRadius: 5)
.frame(width: geometry.size.width, height: 20, alignment: .center)
.foregroundColor(Color(.unemphasizedSelectedContentBackgroundColor))
}
}
.padding()
.frame(width: 150)
.background(
RoundedRectangle(cornerRadius: 15)
.foregroundStyle(.ultraThickMaterial)
)
.padding()
}
}
}
struct NotificationView: View {
var body: some View {
ZStack {
SystemBackgroundView(anchor: .topTrailing)
VStack {
Rectangle()
.background(Color.clear)
.foregroundStyle(.thinMaterial)
.frame(height: 35)
VStack {
HStack {
Spacer()
HStack {
Image(nsImage: NSApplication.shared.applicationIconImage)
.resizable()
.frame(width: 64, height: 64)
.foregroundColor(.primary)
VStack(alignment: .leading) {
Text(verbatim: "Secretive")
2022-12-19 00:19:54 +00:00
.font(.title)
.foregroundColor(.primary)
Text(verbatim: "Secretive wants to sign")
2022-12-19 00:19:54 +00:00
.font(.body)
.foregroundColor(.primary)
}
}.padding()
.redacted(reason: .placeholder)
.background(
RoundedRectangle(cornerRadius: 15)
.foregroundStyle(.ultraThickMaterial)
)
}
Spacer()
}
.padding()
}
}
}
}
#if DEBUG
struct CreateSecretView_Previews: PreviewProvider {
static var previews: some View {
Group {
CreateSecretView(store: Preview.StoreModifiable(), showing: .constant(true))
AuthenticationView().environment(\.colorScheme, .dark)
AuthenticationView().environment(\.colorScheme, .light)
NotificationView().environment(\.colorScheme, .dark)
NotificationView().environment(\.colorScheme, .light)
}
}
}
#endif