secretive/Sources/Secretive/Views/CreateSecretView.swift

167 lines
5.6 KiB
Swift
Raw 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 {
2022-02-27 23:09:59 +00:00
2020-09-22 06:12:50 +00:00
@ObservedObject var store: StoreType
@Binding var showing: Bool
2022-02-27 23:09:59 +00:00
2020-09-22 06:12:50 +00:00
@State private var name = ""
@State private var requiresAuthentication = true
2022-02-27 23:08:25 +00:00
@State private var test: ThumbnailPickerView.Item = ThumbnailPickerView.Item(name: "Test", thumbnail: Text("Hello"))
2022-02-27 23:09:59 +00:00
2020-03-04 07:14:38 +00:00
var body: some View {
2020-03-07 08:00:09 +00:00
VStack {
HStack {
VStack {
HStack {
2022-02-27 23:08:25 +00:00
Text("Create a New Secret")
.font(.largeTitle)
2020-03-07 08:00:09 +00:00
Spacer()
}
HStack {
Text("Name:")
2022-02-27 23:08:25 +00:00
TextField("Shhhhh", text: $name)
.focusable()
2020-03-07 08:00:09 +00:00
}
2022-02-27 23:08:25 +00:00
ThumbnailPickerView(items: [
ThumbnailPickerView.Item(name: "Requires Authentication Before Use", thumbnail: Text("")),
ThumbnailPickerView.Item(name: "Notify on Use", thumbnail: Text(""))
], selection: $test)
2022-02-27 23:09:59 +00:00
// HStack {
// VStack(spacing: 20) {
// Picker("", selection: $requiresAuthentication) {
// Text("Requires Authentication (Biometrics or Password) before each use").tag(true)
// Text("Authentication not required when Mac is unlocked").tag(false)
// }
// .pickerStyle(SegmentedPickerStyle())
// }
// Spacer()
// }
2020-03-04 07:14:38 +00:00
}
}
2020-03-07 08:00:09 +00:00
HStack {
Spacer()
2020-09-22 06:12:50 +00:00
Button("Cancel") {
showing = false
2020-03-04 07:14:38 +00:00
}
2020-09-22 06:12:50 +00:00
.keyboardShortcut(.cancelAction)
Button("Create", action: save)
.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
}
2022-02-27 23:09:59 +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-02-27 21:30:18 +00:00
2022-02-27 23:08:25 +00:00
struct ThumbnailPickerView: View {
2022-02-27 23:09:59 +00:00
2022-02-27 23:08:25 +00:00
let items: [Item]
let selection: Binding<Item>
2022-02-27 23:09:59 +00:00
2022-02-27 23:08:25 +00:00
var body: some View {
HStack {
ForEach(items) { item in
Text(item.name)
}
}
}
2022-02-27 23:09:59 +00:00
2022-02-27 23:08:25 +00:00
}
extension ThumbnailPickerView {
2022-02-27 23:09:59 +00:00
2022-02-27 23:08:25 +00:00
struct Item: Identifiable {
let id = UUID()
let name: String
let thumbnail: AnyView
2022-02-27 23:09:59 +00:00
2022-02-27 23:08:25 +00:00
init<ViewType: View>(name: String, thumbnail: ViewType) {
self.name = name
self.thumbnail = AnyView(thumbnail)
}
}
2022-02-27 23:09:59 +00:00
2022-02-27 23:08:25 +00:00
}
@available(macOS 12.0, *)
struct AuthenticationView: View {
2022-02-27 23:09:59 +00:00
2022-02-27 23:08:25 +00:00
var body: some View {
ZStack {
if let mainScreen = NSScreen.main, let imageURL = NSWorkspace.shared.desktopImageURL(for: mainScreen) {
AsyncImage(url: imageURL) { phase in
switch phase {
case .empty, .failure:
Rectangle()
.foregroundColor(Color(.systemPurple))
case .success(let image):
2022-02-27 23:09:59 +00:00
image
.resizable()
.scaleEffect(3)
2022-02-27 23:08:25 +00:00
@unknown default:
Rectangle()
.foregroundColor(Color(.systemPurple))
}
}
}
RoundedRectangle(cornerRadius: 15)
.aspectRatio(0.8, contentMode: .fit)
.foregroundColor(Color(.windowBackgroundColor))
.padding()
VStack {
Spacer()
Image(systemName: "touchid")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 100)
.foregroundColor(Color(.systemRed))
Spacer()
Text("Touch ID Prompt")
.font(.largeTitle)
.redacted(reason: .placeholder)
Spacer()
VStack {
Text("Touch ID Detail prompt.Detail two.")
.font(.title3)
Text("Touch ID Detail prompt.Detail two.")
.font(.title3)
}
.redacted(reason: .placeholder)
Spacer()
RoundedRectangle(cornerRadius: 10)
.frame(width: 275, height: 40, alignment: .center)
.foregroundColor(Color(.controlAccentColor))
RoundedRectangle(cornerRadius: 10)
.frame(width: 275, height: 40, alignment: .center)
.foregroundColor(Color(.unemphasizedSelectedContentBackgroundColor))
}.padding().padding()
}
}
2022-02-27 23:09:59 +00:00
2022-02-27 23:08:25 +00:00
}
2022-02-27 21:30:18 +00:00
#if DEBUG
struct CreateSecretView_Previews: PreviewProvider {
2022-02-27 23:09:59 +00:00
2022-02-27 21:30:18 +00:00
static var previews: some View {
Group {
CreateSecretView(store: Preview.StoreModifiable(), showing: .constant(true))
2022-02-27 23:08:25 +00:00
if #available(macOS 12.0, *) {
AuthenticationView().environment(\.colorScheme, .dark)
AuthenticationView().environment(\.colorScheme, .light)
} else {
// Fallback on earlier versions
}
2022-02-27 21:30:18 +00:00
}
}
}
#endif