-
Notifications
You must be signed in to change notification settings - Fork 513
Expand file tree
/
Copy pathConnectionSecurityLevelBlockView.swift
More file actions
255 lines (238 loc) · 9.19 KB
/
Copy pathConnectionSecurityLevelBlockView.swift
File metadata and controls
255 lines (238 loc) · 9.19 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import SFSafeSymbols
import Shared
import SwiftUI
struct ConnectionSecurityLevelBlockView: View {
@Environment(\.dismiss) private var dismiss
@StateObject private var viewModel: ConnectionSecurityLevelBlockViewModel
@State private var showSettings = false
@State private var showHomeNetworkSettings = false
@State private var showConnectionSecurityPreferences = false
let server: Server
private let learnMoreLink = AppConstants.WebURLs.companionAppConnectionSecurityLevel
init(server: Server) {
self._viewModel = .init(wrappedValue: ConnectionSecurityLevelBlockViewModel(server: server))
self.server = server
}
var body: some View {
NavigationView {
ScrollView {
content
}
.navigationViewStyle(.stack)
.toolbar {
ToolbarItem(placement: .topBarLeading) {
Button {
reload()
} label: {
Image(systemSymbol: .arrowClockwise)
}
}
ToolbarItem(placement: .topBarTrailing) {
if !viewModel.requirements.isEmpty {
Link(destination: learnMoreLink) {
Image(systemSymbol: .questionmark)
}
}
}
}
.safeAreaInset(edge: .bottom) {
bottomButtons
}
.onAppear {
viewModel.loadRequirements()
}
#if targetEnvironment(macCatalyst)
.fullScreenCover(isPresented: $showHomeNetworkSettings) {
homeNetworkView
}
.fullScreenCover(isPresented: $showConnectionSecurityPreferences) {
connectionPreferencesView
}
.fullScreenCover(isPresented: $showSettings) {
settingsView
}
#else
.sheet(isPresented: $showHomeNetworkSettings) {
homeNetworkView
}
.sheet(isPresented: $showConnectionSecurityPreferences) {
connectionPreferencesView
}
.sheet(isPresented: $showSettings) {
settingsView
}
#endif
.onReceive(NotificationCenter.default.publisher(for: .locationPermissionDidChange)) { notification in
if let userInfo = notification.userInfo {
let state = LocationPermissionState(userInfo: userInfo)
switch state {
case .notDetermined:
Current.Log.info("Location permission not determined")
case .denied, .restricted:
Current.urlOpener.open(
URL(string: UIApplication.openSettingsURLString)!,
options: [:],
completionHandler: nil
)
case .authorizedWhenInUse, .authorizedAlways:
// Handle permission change - reload requirements to update UI
viewModel.loadRequirements()
}
}
}
}
.navigationViewStyle(.stack)
}
private var settingsView: some View {
embed(UINavigationController(rootViewController: SettingsViewController()))
.onDisappear {
Current.sceneManager.webViewWindowControllerPromise.then(\.webViewControllerPromise)
.done { webView in
dismiss()
webView.refresh()
}
}
}
private var content: some View {
VStack(spacing: DesignSystem.Spaces.two) {
Image(systemSymbol: .lockFill)
.resizable()
.foregroundStyle(.haPrimary)
.scaledToFit()
.frame(width: 80, height: 80)
Text(L10n.ConnectionSecurityLevelBlock.title)
.font(.title2)
.fontWeight(.semibold)
Text(L10n.ConnectionSecurityLevelBlock.body)
.font(.callout)
.foregroundColor(.secondary)
.multilineTextAlignment(.center)
.padding(.horizontal, DesignSystem.Spaces.two)
if viewModel.requirements.isEmpty {
Link(destination: learnMoreLink) {
Text(L10n.ConnectionSecurityLevelBlock.Requirement.LearnMore.title)
}
.buttonStyle(.secondaryButton)
} else {
VStack(alignment: .leading, spacing: DesignSystem.Spaces.two) {
Text(L10n.ConnectionSecurityLevelBlock.Requirement.title)
.font(DesignSystem.Font.callout.bold())
.foregroundStyle(.secondary)
ForEach(viewModel.requirements, id: \.self) { requirement in
requirementItem(systemSymbol: requirement.systemSymbol, title: requirement.title)
.onTapGesture {
switch requirement {
case .homeNetworkMissing:
showHomeNetworkSettings = true
case .locationPermission:
Current.locationManager.requestLocationPermission()
case .notOnHomeNetwork:
Current.Log.info("No action for notOnHomeNetwork requirement")
}
}
}
}
.frame(maxWidth: DesignSystem.Button.maxWidth)
.padding(.top)
}
}
.padding(DesignSystem.Spaces.three)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(uiColor: .systemBackground))
}
private func openSettings() {
if Current.isCatalyst {
Current.sceneManager.activateAnyScene(for: .settings)
} else {
showSettings = true
}
}
private func requirementItem(systemSymbol: SFSymbol, title: String) -> some View {
HStack {
Spacer()
Image(systemSymbol: systemSymbol)
.font(DesignSystem.Font.title2)
Text(title)
.font(DesignSystem.Font.callout)
Spacer()
}
.foregroundStyle(.haPrimary)
.frame(minHeight: 40)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(DesignSystem.Spaces.one)
.background(.regularMaterial)
.clipShape(Capsule())
}
private var bottomButtons: some View {
VStack(spacing: DesignSystem.Spaces.one) {
Button(action: {
openSettings()
}) {
Text(L10n.ConnectionSecurityLevelBlock.OpenSettings.title)
}
.buttonStyle(.primaryButton)
Button(action: {
showConnectionSecurityPreferences = true
}) {
Text(L10n.ConnectionSecurityLevelBlock.ChangePreference.title)
}
.buttonStyle(.secondaryButton)
}
.frame(maxWidth: Sizes.maxWidthForLargerScreens)
.padding(.horizontal, DesignSystem.Spaces.two)
.padding(.top)
}
private var homeNetworkView: some View {
NavigationView(content: {
HomeNetworkInputView(onNext: { context in
server.update { info in
if let ssid = context.networkName {
info.connection.internalSSIDs = [ssid]
}
if let hardwareAddress = context.hardwareAddress {
info.connection.internalHardwareAddresses = [hardwareAddress]
}
showHomeNetworkSettings = false
}
})
.navigationViewStyle(.stack)
.toolbar {
ToolbarItem(placement: .topBarLeading) {
CloseButton {
showHomeNetworkSettings = false
}
}
}
})
.onDisappear {
reload()
}
}
private var connectionPreferencesView: some View {
NavigationView {
OnboardingPermissionsNavigationView(
onboardingServer: server,
steps: [.localAccess, .updatePreferencesSuccess]
)
.toolbar {
ToolbarItem(placement: .topBarLeading) {
CloseButton {
showConnectionSecurityPreferences = false
}
}
}
.navigationViewStyle(.stack)
}
.onDisappear {
reload()
}
}
private func reload() {
Current.sceneManager.webViewWindowControllerPromise.then(\.webViewControllerPromise).done { webView in
webView.refresh()
}
}
}
#Preview {
ConnectionSecurityLevelBlockView(server: ServerFixture.standard)
}