-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptView.swift
More file actions
535 lines (482 loc) · 24.8 KB
/
EncryptView.swift
File metadata and controls
535 lines (482 loc) · 24.8 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
* Copyright 2017 - 2025 Riigi Infosüsteemi Amet
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
import SwiftUI
import QuickLook
import FactoryKit
import CryptoObjCWrapper
import CommonsLib
import UtilsLib
struct EncryptView: View {
@Environment(\.presentationMode) var presentationMode
@AppTheme private var theme
@AppTypography private var typography
@Environment(NavigationPathManager.self) private var pathManager
@Environment(LanguageSettings.self) private var languageSettings
private let nameUtil: NameUtilProtocol
private let recipientUtil: RecipientUtilProtocol
private let fileUtil: FileUtilProtocol
@Environment(\.dismiss) private var dismiss
@State private var selectedTab: EncryptViewTab = .files
@State private var selectedRecipient: Addressee?
@State private var viewModel: EncryptViewModel
@State private var containerTitle: String = ""
@State private var encryptDecryptLabel: String = ""
@State private var encryptDecryptAccessibilityLabel: String = ""
@State private var tempContainerURL: URL?
@State private var isFileSaved: Bool = false
@State private var isWithEncryption: Bool = false
@State private var isWithDecryption: Bool = false
@State private var encryptionButtonEnabled = true
@State private var showRenameModal = false
@State private var showRemoveDataFileModal = false
@State private var showRemoveRecipientModal = false
@State private var newContainerName = Constants.Container.DefaultName
@State private var isImportingAddedFiles: Bool = false
@State private var showingShareSheet = false
@State private var selectedDataFile: URL?
@State private var showSivaMessage = false
@AccessibilityFocusState private var focusedField: AccessibilityField?
private func containerTitle() async -> String {
return await viewModel.isInitialCryptoContainer(
cryptoContainer: viewModel.cryptoContainer,
isNestedContainer: isNestedContainer
) ? languageSettings.localized("Container encryption") : containerFilesTitle
}
private func encryptDecryptLabel() async -> String {
if await viewModel.isDecryptButtonShown(
cryptoContainer: viewModel.cryptoContainer,
isNestedContainer: isNestedContainer
) {
return languageSettings.localized("Decrypt")
} else if await viewModel.isEncryptButtonShown(
cryptoContainer: viewModel.cryptoContainer,
isNestedContainer: isNestedContainer
) {
return languageSettings.localized("Encrypt")
} else {
return ""
}
}
private func encryptDecryptAccessibilityLabel() async -> String {
if await viewModel.isDecryptButtonShown(
cryptoContainer: viewModel.cryptoContainer,
isNestedContainer: isNestedContainer
) {
return languageSettings.localized("Decrypt container")
} else if await viewModel.isEncryptButtonShown(
cryptoContainer: viewModel.cryptoContainer,
isNestedContainer: isNestedContainer
) {
return languageSettings.localized("Encrypt container")
} else {
return ""
}
}
private var containerNameTitle: String {
languageSettings.localized("Container name")
}
private var containerRecipientsTitle: String {
languageSettings.localized("Container recipients")
}
private var containerFilesTitle: String {
languageSettings.localized("Container files")
}
private var shareTitle: String {
languageSettings.localized("Share")
}
private var shareAccessibilityTitle: String {
languageSettings.localized("Share container")
}
private var closeIcon: String {
!isNestedContainer ? "ic_m3_close_48pt_wght400" :
"ic_m3_arrow_back_ios_48pt_wght400"
}
private var closeIconAccessibility: String {
!isNestedContainer ? languageSettings.localized("Close container") :
languageSettings.localized("Back")
}
private var encryptLabel: String {
languageSettings.localized("Encrypt")
}
private var nextLabel: String {
languageSettings.localized("Next")
}
private var addMoreFilesLabel: String {
languageSettings.localized("Add more files")
}
private var isNestedContainer: Bool {
viewModel.isNestedContainer()
}
@State private var containerLoadingTask: Task<Void, Never>?
init(
isWithEncryption: Bool = false,
isWithDecryption: Bool = false,
nameUtil: NameUtilProtocol = Container.shared.nameUtil(),
recipientUtil: RecipientUtilProtocol = Container.shared.recipientUtil(),
fileUtil: FileUtilProtocol = Container.shared.fileUtil()
) {
_viewModel = State(wrappedValue: Container.shared.encryptViewModel())
self.isWithEncryption = isWithEncryption
self.isWithDecryption = isWithDecryption
self.nameUtil = nameUtil
self.recipientUtil = recipientUtil
self.fileUtil = fileUtil
}
var body: some View {
ZStack {
TopBarContainer(
title: containerTitle,
leftIcon: closeIcon,
leftIconAccessibility:
languageSettings.localized(closeIconAccessibility).lowercased(),
onLeftClick: {
Task {
if await viewModel.handleBackButton() {
dismiss()
}
}
},
content: {
VStack(alignment: .leading, spacing: Dimensions.Padding.ZeroPadding) {
ScrollView {
VStack {
ContainerNameView(
icon: "ic_m3_stylus_note_48pt_wght400",
containerNameTitle: containerNameTitle,
name: $viewModel.containerName,
isEditContainerButtonShown: viewModel.isEditButtonShown,
isSaveButtonShown: viewModel.isContainerEncrypted || viewModel.isContainerDecrypted,
isEncryptButtonShown: viewModel.isEncryptButtonShown,
showLeftActionButton: false,
showRightActionButton: viewModel.isEncryptButtonShown ||
viewModel.isDecryptButtonShown,
leftActionButtonName: "",
rightActionButtonName: encryptDecryptLabel,
leftActionButtonAccessibilityLabel: "",
rightActionButtonAccessibilityLabel: encryptDecryptAccessibilityLabel.lowercased(),
onLeftActionButtonClick: {
// Do nothing
},
onRightActionButtonClick: {
if viewModel.isEncryptButtonShown {
if encryptionButtonEnabled {
encryptionButtonEnabled = false
Task {
await viewModel.encryptContainer()
await updateAsyncLabels()
await viewModel.updateAsyncProperties()
Toast.show(languageSettings.localized(
"Container successfully encrypted"
))
encryptionButtonEnabled = true
}
}
} else if viewModel.isDecryptButtonShown {
isWithEncryption = false
isWithDecryption = false
pathManager.navigate(to: .decryptRootView)
}
},
onSaveContainerButtonClick: {
tempContainerURL = viewModel.createCopyOfContainerForSaving(
containerURL: viewModel.containerURL
)
if fileUtil.fileExists(fileLocation: tempContainerURL) {
viewModel.isShowingContainerFileSaver = true
}
},
onRenameContainerButtonClick: {
showRenameModal = true
}
)
.background(
FileSaverHandler(
isPresented: $viewModel.isShowingContainerFileSaver,
fileURL: tempContainerURL,
languageSettings: languageSettings,
onComplete: {
viewModel.removeSavedFilesDirectory()
},
isFileSaved: $isFileSaved
)
)
.onChange(of: viewModel.isNestedContainer()) {
Task {
await updateAsyncLabels()
await viewModel.updateAsyncProperties()
}
}
if viewModel.isContainerWithoutRecipients && !isNestedContainer {
VStack(alignment: .leading, spacing: Dimensions.Padding.XSPadding) {
Text(verbatim: languageSettings.localized("Container files"))
.foregroundStyle(theme.onSurfaceVariant)
.font(typography.labelLarge)
.frame(maxWidth: .infinity, alignment: .leading)
.accessibilityAddTraits([.isHeader])
CryptoDataFilesSection(
viewModel: viewModel,
showOpenFileButton: true,
showSaveFileButton: true,
showRemoveFileButton: true,
isNestedContainer: isNestedContainer,
selectedDataFile: $selectedDataFile,
showSivaMessage: $showSivaMessage,
isFileSaved: $isFileSaved,
showRemoveDataFileModal: $showRemoveDataFileModal
)
.background(
FileSaverHandler(
isPresented: $viewModel.isShowingFileSaver,
fileURL: viewModel.selectedDataFile,
languageSettings: languageSettings,
onComplete: {
viewModel.removeSavedFilesDirectory()
},
isFileSaved: $isFileSaved
)
)
.quickLookPreview($viewModel.previewFile)
}
.padding(.vertical, Dimensions.Padding.MPadding)
} else {
TabView(selectedTab: $selectedTab, titles: [
containerFilesTitle,
containerRecipientsTitle
]) {
if selectedTab == .files {
if viewModel.shouldShowDatafiles {
CryptoDataFilesSection(
viewModel: viewModel,
showOpenFileButton: viewModel.isContainerUnlocked,
showSaveFileButton: viewModel.isContainerUnlocked,
showRemoveFileButton: viewModel.isContainerWithoutRecipients,
isNestedContainer: isNestedContainer,
selectedDataFile: $selectedDataFile,
showSivaMessage: $showSivaMessage,
isFileSaved: $isFileSaved,
showRemoveDataFileModal: $showRemoveDataFileModal
)
} else {
CryptoDataFilesLockedSection()
.environment(languageSettings)
}
} else {
RecipientsListView(
recipients: viewModel.recipients,
selectedRecipient: $selectedRecipient,
showRemoveRecipientButton: viewModel.isRecipientRemoveButtonShown(),
showRemoveRecipientModal: $showRemoveRecipientModal,
nameUtil: nameUtil,
recipientUtil: recipientUtil
)
.environment(languageSettings)
}
}
}
}
}
.padding(Dimensions.Padding.SPadding)
if viewModel.isShareButtonShown {
if let containerFile = viewModel.containerURL {
ShareButtonBottomBar(
iconName: "ic_m3_ios_share_48pt_wght400",
label: shareTitle,
accessibilityLabel: shareAccessibilityTitle,
containerUrl: containerFile
)
}
} else if !viewModel.isContainerEncrypted && !viewModel.isContainerDecrypted {
let rightButtonLabel = viewModel.isContainerWithoutRecipients ? nextLabel : encryptLabel
let rightButtonIconName = viewModel.isContainerWithoutRecipients
? "ic_m3_arrow_forward_48pt_wght400"
: "ic_m3_encrypted_48pt_wght400"
UnsignedBottomBarView(
showLeftButton: viewModel.isContainerWithoutRecipients,
leftButtonIconName: "ic_m3_add_48pt_wght400",
leftButtonLabel: addMoreFilesLabel,
leftButtonAccessibilityLabel: addMoreFilesLabel.lowercased(),
leftButtonAction: {
isImportingAddedFiles = true
},
rightButtonEnabled: viewModel.isContainerWithoutRecipients || encryptionButtonEnabled,
rightButtonIconName: rightButtonIconName,
rightButtonLabel: rightButtonLabel,
rightButtonAccessibilityLabel: rightButtonLabel.lowercased(),
rightButtonAction: {
if viewModel.isContainerWithoutRecipients {
pathManager.replaceLast(to: .encryptRecipientView)
} else {
if encryptionButtonEnabled {
encryptionButtonEnabled = false
Task {
await viewModel.encryptContainer()
await updateAsyncLabels()
await viewModel.updateAsyncProperties()
encryptionButtonEnabled = true
isWithEncryption = false
}
}
}
}
)
.fileImporter(
isPresented: $isImportingAddedFiles,
allowedContentTypes: [.item],
allowsMultipleSelection: true
) { result in
switch result {
case .success(let urls):
Task {
for url in urls {
guard url.startAccessingSecurityScopedResource() else { continue }
}
isImportingAddedFiles = false
await viewModel.addDataFiles(urls)
for url in urls {
url.stopAccessingSecurityScopedResource()
}
}
case .failure:
isImportingAddedFiles = false
}
}
}
}
.onAppear {
containerLoadingTask = Task {
if isWithEncryption {
await viewModel.encryptContainer()
await updateAsyncLabels()
await viewModel.updateAsyncProperties()
encryptionButtonEnabled = true
isWithEncryption = false
} else if isWithDecryption {
await updateAsyncLabels()
await viewModel.updateAsyncProperties()
Toast.show(languageSettings.localized(
"Container successfully decrypted"
))
isWithDecryption = false
} else {
await viewModel.loadContainerData(
cryptoContainer: nil
)
await updateAsyncLabels()
await viewModel.updateAsyncProperties()
}
}
}
.onDisappear {
containerLoadingTask?.cancel()
}
}
)
if showRenameModal {
CryptoRenameModalView(
encryptViewModel: viewModel,
showRenameModal: $showRenameModal,
newContainerName: $newContainerName
)
}
if showRemoveRecipientModal {
ConfirmModalView(
title: languageSettings.localized("Remove recipient"),
message: languageSettings.localized("Remove recipient from container"),
onConfirm: {
Task {
await handleRemoveRecipient()
}
}, onCancel: {
showRemoveRecipientModal = false
}
)
}
if showRemoveDataFileModal {
ConfirmModalView(
title: languageSettings.localized("Remove datafile"),
message: languageSettings.localized(
viewModel.dataFiles.count == 1 ?
"Remove last datafile from container message" :
"Remove datafile from container message"
),
onConfirm: {
Task {
await handleRemoveDataFile()
}
},
onCancel: { showRemoveDataFileModal = false }
)
}
}
.animation(.easeInOut, value: showRenameModal)
.onChange(of: viewModel.errorMessage) { _, error in
guard let error else { return }
Toast.show(
languageSettings.localized(error.key, [error.args.joined(separator: ", ")])
)
viewModel.resetErrorMessage()
encryptionButtonEnabled = true
}
.onChange(of: viewModel.successMessage) { _, message in
guard let message else { return }
Toast.show(
languageSettings.localized(message.key, [message.args.joined(separator: ", ")])
)
viewModel.resetSuccessMessage()
}
}
func updateAsyncLabels() async {
let containerTitle = await containerTitle()
let encryptDecryptLabel = await self.encryptDecryptLabel()
let encryptDecryptAccessibilityLabel = await self.encryptDecryptAccessibilityLabel()
await MainActor.run {
self.containerTitle = containerTitle
self.encryptDecryptLabel = encryptDecryptLabel
self.encryptDecryptAccessibilityLabel = encryptDecryptAccessibilityLabel
}
}
private func handleRemoveRecipient() async {
guard let recipient = selectedRecipient else {
Toast.show(languageSettings.localized("Failed to remove recipient from container"))
return
}
await viewModel.removeRecipient(recipient)
selectedRecipient = nil
showRemoveRecipientModal = false
}
private func handleRemoveDataFile() async {
guard let dataFile = selectedDataFile else {
Toast.show(languageSettings.localized("Failed to remove datafile from container", [""]))
return
}
await viewModel.removeDataFile(dataFile)
selectedDataFile = nil
showRemoveDataFileModal = false
if viewModel.dataFiles.count == 1, viewModel.isLastDataFileRemoved {
if await viewModel.handleBackButton() {
dismiss()
}
}
}
}
#Preview {
EncryptView()
.environment(Container.shared.languageSettings())
.environment(Container.shared.themeSettings())
}