diff --git a/OTP/KeyData.swift b/OTP/KeyData.swift index 8db3666..cf25ff6 100644 --- a/OTP/KeyData.swift +++ b/OTP/KeyData.swift @@ -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]) { diff --git a/OTP/KeyStore.swift b/OTP/KeyStore.swift index e0e1ec1..b973492 100644 --- a/OTP/KeyStore.swift +++ b/OTP/KeyStore.swift @@ -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) { diff --git a/OTP/Views/AddKeyButton.swift b/OTP/Views/AddKeyButton.swift new file mode 100644 index 0000000..f66f1e9 --- /dev/null +++ b/OTP/Views/AddKeyButton.swift @@ -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) + } +} diff --git a/OTP/Views/AppView.swift b/OTP/Views/AppView.swift index cab152c..00d5b60 100644 --- a/OTP/Views/AppView.swift +++ b/OTP/Views/AppView.swift @@ -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 diff --git a/OTP/Views/FolderView.swift b/OTP/Views/FolderView.swift index 026e225..7746623 100644 --- a/OTP/Views/FolderView.swift +++ b/OTP/Views/FolderView.swift @@ -27,6 +27,11 @@ struct FolderView: View { } .listStyle(.insetGrouped) .navigationTitle(folder.name) + .toolbar { + ToolbarItem(placement: .navigationBarTrailing) { + AddKeyButton(folderID: folder.id, canAddFolder: false) + } + } } }