-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContentView.swift
163 lines (153 loc) · 4.91 KB
/
ContentView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import SwiftUI
import Models
import OSLog
struct ContentView: View {
@State private var isOtpScanPresented: Bool = false
@State private var isSettingsPresented: Bool = false
@State private var items: [Otp]? = nil
@Environment(\.notifier) private var notifier
@Environment(\.keychain) private var keychain
private var logger: Logger = .init(ContentView.self)
private func load() async {
await notifier.execute {
self.items = try await keychain.getAll()
}
}
@ViewBuilder private func otpList(items: [Otp]) -> some View {
if items.count == 0 {
ContentUnavailableView(
label: {
Label("No OTPs Registered", systemImage: "list.bullet")
},
description: {
Text("Add OTPs by scanning their QR codes, or adding it manually")
},
actions: {
NavigationLink {
AddOtpEntryView()
} label: {
Label("Add Manually", systemImage: "plus")
}
#if !os(macOS)
Button("Scan QR Code", systemImage: "qrcode.viewfinder") {
isOtpScanPresented = true
}
#endif
})
.scaledToFill()
}
List {
Section {
ForEach(items) { item in
OtpEntryView(otp: item)
.refreshable {
await load()
}
#if os(macOS)
.padding()
#else
.padding(.top, 2)
.padding(.bottom, 6)
.labelStyle(.titleAndIcon)
#endif
}
} footer: {
Text("Logos provided by [Logo.dev](https://logo.dev)")
}
}
}
var body: some View {
NavigationStack {
VStack {
switch items {
case .none:
ProgressView("Loading OTPs")
case .some(let otps):
otpList(items: otps)
}
}
.toolbar {
ToolbarItem(placement: .primaryAction) {
Menu {
NavigationLink {
AddOtpEntryView()
} label: {
Label("Manual", systemImage: "plus")
}
#if !os(macOS)
Button("Scan QR Code", systemImage: "qrcode.viewfinder") {
isOtpScanPresented = true
}
#endif
} label: {
Label("New OTP", systemImage: "plus")
}
}
ToolbarItem(placement: .automatic) {
#if os(macOS)
SettingsLink()
#else
Button("Settings", systemImage: "gear") {
isSettingsPresented.toggle()
}
#endif
}
}
.navigationTitle("OVault")
.onOpenURL { url in
Task {
await notifier.execute {
switch url.scheme {
case "otpauth", "ovault-otpauth":
let otp = try Otp.from(url: url)
try await keychain.store(otp: otp)
await load()
break
default:
logger.info("URL opened with scheme \(url.scheme ?? "Unknown"), ignoring")
break
}
}
}
}
.task {
await load()
}
#if !os(macOS)
.sheet(isPresented: $isOtpScanPresented) {
OtpQrScannerView()
.withNotifierSupport()
}
#endif
}
.scrollDismissesKeyboard(.interactively)
.refreshable {
await load()
}
.sheet(isPresented: $isSettingsPresented) {
NavigationStack {
SettingsView()
.navigationTitle("Settings")
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button("Close") {
isSettingsPresented.toggle()
}
}
}
}
}
}
}
#if DEBUG
#Preview("With items") {
ContentView()
.previewEnvironment()
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
#Preview("Empty") {
ContentView()
.previewEnvironment(withData: false)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
#endif