110 lines
3.2 KiB
Swift
110 lines
3.2 KiB
Swift
//
|
|
// AddKeyButton.swift
|
|
// OTP
|
|
//
|
|
// Created by Shadowfacts on 8/25/21.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
struct AddKeyButton: View {
|
|
let folderID: UUID?
|
|
let canAddFolder: Bool
|
|
@ObservedObject private var store: KeyStore = .shared
|
|
@State private var isPresentingScanner = false
|
|
@State private var isPresentingScanFailedAlert = false
|
|
@State private var isPresentingAddURLSheet = false
|
|
@State private var isPresentingManualAddFormSheet = false
|
|
|
|
init(folderID: UUID?, canAddFolder: Bool) {
|
|
self.folderID = folderID
|
|
self.canAddFolder = canAddFolder
|
|
}
|
|
|
|
var body: some View {
|
|
Menu {
|
|
Section {
|
|
Button {
|
|
isPresentingScanner = true
|
|
} label: {
|
|
Label("Scan QR", systemImage: "qrcode.viewfinder")
|
|
}
|
|
Button {
|
|
isPresentingAddURLSheet = true
|
|
} label: {
|
|
Label("From URL", systemImage: "link")
|
|
}
|
|
Button {
|
|
isPresentingManualAddFormSheet = true
|
|
} label: {
|
|
Label("Enter Manually", systemImage: "textbox")
|
|
}
|
|
}
|
|
|
|
if canAddFolder {
|
|
Section {
|
|
Button {
|
|
store.addFolder()
|
|
} label: {
|
|
Label("New Folder", systemImage: "folder.badge.plus")
|
|
}
|
|
}
|
|
}
|
|
} label: {
|
|
Label("Add Key", systemImage: "plus.circle")
|
|
}
|
|
.accessibilityLabel("Add Key")
|
|
.sheet(isPresented: $isPresentingScanner, content: self.scannerSheet)
|
|
.sheet(isPresented: $isPresentingManualAddFormSheet, content: self.manualAddFormSheet)
|
|
.sheet(isPresented: $isPresentingAddURLSheet, content: self.addURLSheet)
|
|
}
|
|
|
|
private func scannerSheet() -> some View {
|
|
AddQRView() { (action) in
|
|
self.isPresentingScanner = false
|
|
switch action {
|
|
case .cancel:
|
|
break
|
|
case .save(let key):
|
|
store.addKey(key, folderID: folderID)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func addURLSheet() -> some View {
|
|
NavigationView {
|
|
AddURLForm { (action) in
|
|
self.isPresentingAddURLSheet = false
|
|
switch action {
|
|
case .cancel:
|
|
break
|
|
case .save(let key):
|
|
store.addKey(key, folderID: folderID)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func manualAddFormSheet() -> some View {
|
|
NavigationView {
|
|
EditKeyForm(editingKey: nil, focusOnAppear: true) { (action) in
|
|
self.isPresentingManualAddFormSheet = false
|
|
switch action {
|
|
case .cancel:
|
|
break
|
|
case .save(let key):
|
|
store.addKey(key, folderID: folderID)
|
|
}
|
|
}
|
|
.navigationTitle("Add Key")
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
struct AddKeyButton_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
AddKeyButton(folderID: nil, canAddFolder: true)
|
|
}
|
|
}
|