Add new keys directly to folders

This commit is contained in:
Shadowfacts 2021-08-25 11:35:22 -04:00
parent 643452459b
commit ffd9b8434e
5 changed files with 120 additions and 82 deletions

View File

@ -29,8 +29,8 @@ struct KeyData: Codable {
try container.encode(folders, forKey: .folders)
}
mutating func addKey(_ key: TOTPKey) {
entries.append(Entry(key: key))
mutating func addKey(_ key: TOTPKey, folderID: UUID? = nil) {
entries.append(Entry(key: key, folderID: folderID))
}
mutating func addOrUpdateEntries(_ entries: [Entry]) {

View File

@ -54,8 +54,8 @@ class KeyStore: ObservableObject {
}
}
func addKey(_ key: TOTPKey) {
data.addKey(key)
func addKey(_ key: TOTPKey, folderID: UUID? = nil) {
data.addKey(key, folderID: folderID)
}
func updateKey(entryID id: UUID, newKey: TOTPKey) {

View File

@ -0,0 +1,110 @@
//
// 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")
.tint(.accentColor)
}
.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)
}
}

View File

@ -12,10 +12,6 @@ import Combine
struct AppView: View {
@ObservedObject private var store: KeyStore
@ObservedObject private var entryHolder: CodeHolder
@State private var isPresentingScanner = false
@State private var isPresentingScanFailedAlert = false
@State private var isPresentingAddURLSheet = false
@State private var isPresentingManualAddFormSheet = false
@State private var isPresentingPreferences = false
init() {
@ -43,44 +39,12 @@ struct AppView: View {
}
ToolbarItem(placement: .navigationBarTrailing) {
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")
}
}
Section {
Button {
store.addFolder()
} label: {
Label("New Folder", systemImage: "folder.badge.plus")
}
}
} label: {
Label("Add Key", systemImage: "plus.circle")
.tint(.accentColor)
}
AddKeyButton(folderID: nil, canAddFolder: true)
}
}
}
.tint(.blue)
.sheet(isPresented: $isPresentingPreferences, content: self.preferencesSheet)
.sheet(isPresented: $isPresentingScanner, content: self.scannerSheet)
.sheet(isPresented: $isPresentingManualAddFormSheet, content: self.manualAddFormSheet)
.sheet(isPresented: $isPresentingAddURLSheet, content: self.addURLSheet)
}
private func preferencesSheet() -> some View {
@ -89,47 +53,6 @@ struct AppView: View {
}
}
private func scannerSheet() -> some View {
AddQRView() { (action) in
self.isPresentingScanner = false
switch action {
case .cancel:
break
case .save(let key):
store.addKey(key)
}
}
}
private func addURLSheet() -> some View {
NavigationView {
AddURLForm { (action) in
self.isPresentingAddURLSheet = false
switch action {
case .cancel:
break
case .save(let key):
store.addKey(key)
}
}
}
}
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)
}
}
.navigationTitle("Add Key")
}
}
struct CodeEntry: Identifiable, Equatable, Hashable {
let entry: KeyData.Entry
let code: TOTPCode

View File

@ -27,6 +27,11 @@ struct FolderView: View {
}
.listStyle(.insetGrouped)
.navigationTitle(folder.name)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
AddKeyButton(folderID: folder.id, canAddFolder: false)
}
}
}
}