Sources/OrgApp/DecryptSettings.swift

63ae15b64d76b8b7c4d413cf6a6c5434c50d402d
orgstar/Sources/OrgApp/DecryptSettings.swift

76 lines · 2968 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            }
29            .font(.caption)
30            .foregroundStyle(.secondary)
31            .fixedSize(horizontal: false, vertical: true)
32        }
33        .onAppear { passphrases.refresh() }
34        .alert(String(localized: "Remove Saved Passphrases?"), isPresented: $confirmingOff) {
35            Button("Cancel", role: .cancel) {}
36            Button("Turn Off", role: .destructive) { passphrases.turnOff() }
37        } message: {
38            Text(CryptPassphrases.removal(passphrases.savedPaths.count))
39        }
40        if !passphrases.savedPaths.isEmpty {
41            Section("Saved Passphrases") {
42                ForEach(passphrases.savedPaths, id: \.self) { path in
43                    HStack {
44                        VStack(alignment: .leading, spacing: 2) {
45                            Text((path as NSString).lastPathComponent)
46                            Text((path as NSString).deletingLastPathComponent).font(.caption).foregroundStyle(.secondary).lineLimit(1).truncationMode(.middle)
47                        }
48                        Spacer()
49                        Button("Forget") { passphrases.forget(path) }.buttonStyle(.borderless)
50                    }
51                }
52            }
53        }
54    }
55}
56
57/// Asks whether to use Face ID to decrypt, once, at launch, when an earlier version saved
58/// passphrases and the setting is undecided.
59public struct DecryptQuestion: ViewModifier {
60    @Bindable var passphrases: CryptPassphrases
61
62    public init(passphrases: CryptPassphrases = .shared) {
63        self.passphrases = passphrases
64    }
65
66    public func body(content: Content) -> some View {
67        content
68            .task { passphrases.askIfSavedBefore() }
69            .alert(CryptPassphrases.question, isPresented: Binding(get: { passphrases.asking }, set: { _ in })) {
70                Button("Not Now", role: .cancel) { passphrases.answer(enable: false) }
71                Button("Enable") { passphrases.answer(enable: true) }
72            } message: {
73                Text(CryptPassphrases.explanation)
74            }
75    }
76}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form