Sources/OrgApp/DecryptSettings.swift
77 lines · 3053 bytes
1import SwiftUI
2
3/// "Use Face ID to Decrypt" and the files with a saved passphrase, for a settings form.
4public struct DecryptSettings: View {
5 @Bindable var passphrases: CryptPassphrases
6 @State private var confirmingOff = false
7
8 public init(passphrases: CryptPassphrases = .shared) {
9 self.passphrases = passphrases
10 }
11
12 public var body: some View {
13 Section {
14 Toggle(CryptPassphrases.title, isOn: Binding {
15 passphrases.setting == .on
16 } set: { on in
17 if on {
18 passphrases.turnOn()
19 } else if passphrases.savedPaths.isEmpty {
20 passphrases.turnOff()
21 } else {
22 confirmingOff = true
23 }
24 })
25 } footer: {
26 VStack(alignment: .leading, spacing: 6) {
27 Text(CryptPassphrases.explanation)
28 if !CryptPassphrases.protectsItems { Text(CryptPassphrases.caveat) }
29 }
30 .font(.caption)
31 .foregroundStyle(.secondary)
32 .fixedSize(horizontal: false, vertical: true)
33 }
34 .onAppear { passphrases.refresh() }
35 .alert(String(localized: "Remove Saved Passphrases?"), isPresented: $confirmingOff) {
36 Button("Cancel", role: .cancel) {}
37 Button("Turn Off", role: .destructive) { passphrases.turnOff() }
38 } message: {
39 Text(CryptPassphrases.removal(passphrases.savedPaths.count))
40 }
41 if !passphrases.savedPaths.isEmpty {
42 Section("Saved Passphrases") {
43 ForEach(passphrases.savedPaths, id: \.self) { path in
44 HStack {
45 VStack(alignment: .leading, spacing: 2) {
46 Text((path as NSString).lastPathComponent)
47 Text((path as NSString).deletingLastPathComponent).font(.caption).foregroundStyle(.secondary).lineLimit(1).truncationMode(.middle)
48 }
49 Spacer()
50 Button("Forget") { passphrases.forget(path) }.buttonStyle(.borderless)
51 }
52 }
53 }
54 }
55 }
56}
57
58/// Asks whether to use Face ID to decrypt, once, at launch, when an earlier version saved
59/// passphrases and the setting is undecided.
60public struct DecryptQuestion: ViewModifier {
61 @Bindable var passphrases: CryptPassphrases
62
63 public init(passphrases: CryptPassphrases = .shared) {
64 self.passphrases = passphrases
65 }
66
67 public func body(content: Content) -> some View {
68 content
69 .task { passphrases.askIfSavedBefore() }
70 .alert(CryptPassphrases.question, isPresented: Binding(get: { passphrases.asking }, set: { _ in })) {
71 Button("Not Now", role: .cancel) { passphrases.answer(enable: false) }
72 Button("Enable") { passphrases.answer(enable: true) }
73 } message: {
74 Text(CryptPassphrases.explanation)
75 }
76 }
77}