Add new keys directly to folders
This commit is contained in:
parent
643452459b
commit
ffd9b8434e
|
@ -29,8 +29,8 @@ struct KeyData: Codable {
|
||||||
try container.encode(folders, forKey: .folders)
|
try container.encode(folders, forKey: .folders)
|
||||||
}
|
}
|
||||||
|
|
||||||
mutating func addKey(_ key: TOTPKey) {
|
mutating func addKey(_ key: TOTPKey, folderID: UUID? = nil) {
|
||||||
entries.append(Entry(key: key))
|
entries.append(Entry(key: key, folderID: folderID))
|
||||||
}
|
}
|
||||||
|
|
||||||
mutating func addOrUpdateEntries(_ entries: [Entry]) {
|
mutating func addOrUpdateEntries(_ entries: [Entry]) {
|
||||||
|
|
|
@ -54,8 +54,8 @@ class KeyStore: ObservableObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func addKey(_ key: TOTPKey) {
|
func addKey(_ key: TOTPKey, folderID: UUID? = nil) {
|
||||||
data.addKey(key)
|
data.addKey(key, folderID: folderID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateKey(entryID id: UUID, newKey: TOTPKey) {
|
func updateKey(entryID id: UUID, newKey: TOTPKey) {
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,10 +12,6 @@ import Combine
|
||||||
struct AppView: View {
|
struct AppView: View {
|
||||||
@ObservedObject private var store: KeyStore
|
@ObservedObject private var store: KeyStore
|
||||||
@ObservedObject private var entryHolder: CodeHolder
|
@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
|
@State private var isPresentingPreferences = false
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
|
@ -43,44 +39,12 @@ struct AppView: View {
|
||||||
}
|
}
|
||||||
|
|
||||||
ToolbarItem(placement: .navigationBarTrailing) {
|
ToolbarItem(placement: .navigationBarTrailing) {
|
||||||
Menu {
|
AddKeyButton(folderID: nil, canAddFolder: true)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.tint(.blue)
|
.tint(.blue)
|
||||||
.sheet(isPresented: $isPresentingPreferences, content: self.preferencesSheet)
|
.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 {
|
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 {
|
struct CodeEntry: Identifiable, Equatable, Hashable {
|
||||||
let entry: KeyData.Entry
|
let entry: KeyData.Entry
|
||||||
let code: TOTPCode
|
let code: TOTPCode
|
||||||
|
|
|
@ -27,6 +27,11 @@ struct FolderView: View {
|
||||||
}
|
}
|
||||||
.listStyle(.insetGrouped)
|
.listStyle(.insetGrouped)
|
||||||
.navigationTitle(folder.name)
|
.navigationTitle(folder.name)
|
||||||
|
.toolbar {
|
||||||
|
ToolbarItem(placement: .navigationBarTrailing) {
|
||||||
|
AddKeyButton(folderID: folder.id, canAddFolder: false)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue