2021-08-24 22:19:31 +00:00
|
|
|
//
|
|
|
|
// EditKeyForm.swift
|
|
|
|
// OTP
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 8/21/21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import OTPKit
|
|
|
|
|
|
|
|
struct EditKeyForm: View {
|
|
|
|
let dismiss: (DismissAction) -> Void
|
|
|
|
let showCancelButton: Bool
|
|
|
|
let focusOnAppear: Bool
|
|
|
|
@State private var editedKey: EditedKey
|
|
|
|
@FocusState private var issuerFocused: Bool
|
|
|
|
|
|
|
|
init(editingKey: TOTPKey?, showCancelButton: Bool = true, focusOnAppear: Bool = false, dismiss: @escaping (DismissAction) -> Void) {
|
|
|
|
self.dismiss = dismiss
|
|
|
|
self.showCancelButton = showCancelButton
|
|
|
|
self.focusOnAppear = focusOnAppear
|
|
|
|
if let totpKey = editingKey,
|
|
|
|
let edited = EditedKey(totpKey: totpKey) {
|
|
|
|
self.editedKey = edited
|
|
|
|
} else {
|
|
|
|
self.editedKey = EditedKey()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var isValid: Bool {
|
|
|
|
editedKey.toTOTPKey() != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
Form {
|
|
|
|
TextField("Issuer", text: $editedKey.issuer)
|
|
|
|
.focused($issuerFocused)
|
|
|
|
TextField("Account", text: $editedKey.label)
|
|
|
|
.disableAutocorrection(true)
|
|
|
|
.autocapitalization(.none)
|
|
|
|
|
|
|
|
HStack {
|
|
|
|
TextField("Secret", text: $editedKey.secret)
|
|
|
|
Button {
|
|
|
|
self.editedKey.secret = UIPasteboard.general.string ?? ""
|
|
|
|
} label: {
|
|
|
|
Label("Paste", systemImage: "doc.on.clipboard")
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
.foregroundColor(.accentColor)
|
|
|
|
}
|
|
|
|
.buttonStyle(.plain)
|
|
|
|
.disabled(!UIPasteboard.general.hasStrings)
|
|
|
|
}
|
|
|
|
|
|
|
|
HStack {
|
|
|
|
Text("Period")
|
|
|
|
Spacer()
|
|
|
|
Picker("Period", selection: $editedKey.period) {
|
|
|
|
Text("30 sec").tag(EditedKey.Period.thirty)
|
|
|
|
Text("60 sec").tag(EditedKey.Period.sixty)
|
|
|
|
}
|
|
|
|
.pickerStyle(.segmented)
|
|
|
|
.frame(maxWidth: 200)
|
|
|
|
}
|
|
|
|
|
|
|
|
HStack {
|
|
|
|
Text("Digits")
|
|
|
|
Spacer()
|
|
|
|
Picker("Digits", selection: $editedKey.digits) {
|
|
|
|
Text("6").tag(EditedKey.Digits.six)
|
|
|
|
Text("8").tag(EditedKey.Digits.eight)
|
|
|
|
}
|
|
|
|
.pickerStyle(.segmented)
|
|
|
|
.frame(maxWidth: 200)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onAppear {
|
|
|
|
if focusOnAppear {
|
|
|
|
// todo: this just doesn't work :/
|
|
|
|
// https://developer.apple.com/forums/thread/681962
|
|
|
|
// FB9551099
|
|
|
|
issuerFocused = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onSubmit {
|
|
|
|
if isValid {
|
|
|
|
dismiss(.save(editedKey.toTOTPKey()!))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.toolbar {
|
|
|
|
ToolbarItem(placement: .navigationBarLeading) {
|
|
|
|
// ToolbarContentBuilder doesn't support conditionals, so this has to go inside the ToolbarItem
|
|
|
|
if showCancelButton {
|
|
|
|
Button("Cancel") {
|
|
|
|
dismiss(.cancel)
|
|
|
|
}
|
2021-08-25 04:12:06 +00:00
|
|
|
.tint(.accentColor)
|
2021-08-24 22:19:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ToolbarItem(placement: .navigationBarTrailing) {
|
|
|
|
Button("Save") {
|
|
|
|
dismiss(.save(editedKey.toTOTPKey()!))
|
|
|
|
}
|
2021-08-25 04:12:06 +00:00
|
|
|
.tint(.accentColor)
|
2021-08-24 22:19:31 +00:00
|
|
|
.disabled(!isValid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct EditKeyForm_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
EditKeyForm(editingKey: nil) { (_) in }
|
|
|
|
}
|
|
|
|
}
|