-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathRootViewController.swift
194 lines (175 loc) · 8.29 KB
/
RootViewController.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
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
//
// RootViewController.swift
// FileProviderExtensionUI
//
// Created by Philipp Schmid on 29.06.21.
// Copyright © 2021 Skymatic GmbH. All rights reserved.
//
import CocoaLumberjackSwift
import CryptomatorCloudAccessCore
import CryptomatorCommonCore
import CryptomatorFileProvider
import Dependencies
import FileProviderUI
import MSAL
import Promises
import UIKit
class RootViewController: FPUIActionExtensionViewController {
private lazy var coordinator: FileProviderCoordinator = {
#if SNAPSHOTS
return FileProviderCoordinatorSnapshotMock(extensionContext: extensionContext, hostViewController: self)
#else
return .init(extensionContext: extensionContext, hostViewController: self)
#endif
}()
@Dependency(\.fileProviderConnector) private var fileProviderConnector
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
NotificationCenter.default.addObserver(self,
selector: #selector(extensionHostDidEnterBackground),
name: NSNotification.Name.NSExtensionHostDidEnterBackground,
object: nil)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/**
To prevent a visible dismiss of the `RootViewController` when the FileProviderExtensionUI was in the background and becomes active again, we cancel the request as soon as the host app (Files app) switches to the background.
*/
@objc func extensionHostDidEnterBackground() {
cancel()
}
override func viewDidLoad() {
super.viewDidLoad()
RootViewController.oneTimeSetup()
}
@objc func cancel() {
extensionContext.cancelRequest(withError: NSError(domain: FPUIErrorDomain, code: Int(FPUIExtensionErrorCode.userCancelled.rawValue), userInfo: nil))
}
static var oneTimeSetup: () -> Void = {
// Set up logger
LoggerSetup.oneTimeSetup()
// Set up cloud storage services
DropboxSetup.constants = DropboxSetup(appKey: CloudAccessSecrets.dropboxAppKey, sharedContainerIdentifier: nil, keychainService: CryptomatorConstants.mainAppBundleId, forceForegroundSession: true)
GoogleDriveSetup.constants = GoogleDriveSetup(clientId: CloudAccessSecrets.googleDriveClientId, redirectURL: CloudAccessSecrets.googleDriveRedirectURL!, sharedContainerIdentifier: nil)
do {
let oneDriveConfiguration = MSALPublicClientApplicationConfig(clientId: CloudAccessSecrets.oneDriveClientId, redirectUri: CloudAccessSecrets.oneDriveRedirectURI, authority: nil)
oneDriveConfiguration.cacheConfig.keychainSharingGroup = CryptomatorConstants.mainAppBundleId
let oneDriveClientApplication = try MSALPublicClientApplication(configuration: oneDriveConfiguration)
OneDriveSetup.constants = OneDriveSetup(clientApplication: oneDriveClientApplication, sharedContainerIdentifier: nil)
} catch {
DDLogError("Setting up OneDrive failed with error: \(error)")
}
PCloudSetup.constants = PCloudSetup(appKey: CloudAccessSecrets.pCloudAppKey, sharedContainerIdentifier: nil)
return {}
}()
func retryUpload(for itemIdentifiers: [NSFileProviderItemIdentifier], domainIdentifier: NSFileProviderDomainIdentifier) {
let getXPCPromise: Promise<XPC<UploadRetrying>> = fileProviderConnector.getXPC(serviceName: .uploadRetryingService, domainIdentifier: domainIdentifier)
getXPCPromise.then { xpc in
return wrap {
xpc.proxy.retryUpload(for: itemIdentifiers, reply: $0)
}
}.then {
if let error = $0 {
throw error
}
self.extensionContext.completeRequest()
}.catch { error in
DDLogError("Retry upload failed with error: \(error)")
self.extensionContext.cancelRequest(withError: NSError(domain: FPUIErrorDomain, code: Int(FPUIExtensionErrorCode.failed.rawValue), userInfo: nil))
}.always { [fileProviderConnector] in
fileProviderConnector.invalidateXPC(getXPCPromise)
}
}
func showDomainNotFoundAlert() {
let alertController = RetryUploadAlertControllerFactory.createDomainNotFoundAlert(okAction: { [weak self] in
self?.cancel()
})
present(alertController, animated: true)
}
func showUploadProgressAlert(for itemIdentifiers: [NSFileProviderItemIdentifier], domainIdentifier: NSFileProviderDomainIdentifier) {
let getXPCPromise: Promise<XPC<UploadRetrying>> = fileProviderConnector.getXPC(serviceName: .uploadRetryingService, domainIdentifier: domainIdentifier)
let progressAlert = RetryUploadAlertControllerFactory.createUploadProgressAlert(dismissAction: { [weak self] in
self?.cancel()
}, retryAction: { [weak self] in
self?.retryUpload(for: itemIdentifiers, domainIdentifier: domainIdentifier)
})
getXPCPromise.then { xpc -> Promise<Void> in
let observeProgressPromise = progressAlert.observeProgress(itemIdentifier: itemIdentifiers[0], proxy: xpc.proxy)
let alertActionPromise = progressAlert.alertActionTriggered
return race([observeProgressPromise, alertActionPromise])
}.always { [fileProviderConnector] in
self.extensionContext.completeRequest()
fileProviderConnector.invalidateXPC(getXPCPromise)
}
present(progressAlert, animated: true)
}
func showEvictFileFromCacheAlert(for itemIdentifiers: [NSFileProviderItemIdentifier], domainIdentifier: NSFileProviderDomainIdentifier) {
let alertController = UIAlertController(title: LocalizedString.getValue("fileProvider.clearFileFromCache.title"),
message: LocalizedString.getValue("fileProvider.clearFileFromCache.message"),
preferredStyle: .alert)
let deleteAction = UIAlertAction(title: LocalizedString.getValue("common.button.clear"), style: .destructive) { _ in
alertController.dismiss(animated: true) {
self.evictFilesFromCache(with: itemIdentifiers, domainIdentifier: domainIdentifier)
}
}
let cancelAction = UIAlertAction(title: LocalizedString.getValue("common.button.cancel"), style: .cancel) { _ in
self.cancel()
}
alertController.addAction(deleteAction)
alertController.addAction(cancelAction)
alertController.preferredAction = cancelAction
present(alertController, animated: true)
}
func evictFilesFromCache(with itemIdentifiers: [NSFileProviderItemIdentifier], domainIdentifier: NSFileProviderDomainIdentifier) {
let getXPCPromise: Promise<XPC<CacheManaging>> = fileProviderConnector.getXPC(serviceName: .cacheManaging, domainIdentifier: domainIdentifier)
getXPCPromise.then { xpc in
xpc.proxy.evictFilesFromCache(with: itemIdentifiers)
}.catch { error in
let alertController = UIAlertController(title: LocalizedString.getValue("common.alert.error.title"),
message: error.localizedDescription,
preferredStyle: .alert)
let okAction = UIAlertAction(title: LocalizedString.getValue("common.button.ok"), style: .default) { _ in
self.extensionContext.completeRequest()
}
alertController.addAction(okAction)
alertController.preferredAction = okAction
self.present(alertController, animated: true)
}.then {
self.extensionContext.completeRequest()
}.always { [fileProviderConnector] in
fileProviderConnector.invalidateXPC(getXPCPromise)
}
}
// MARK: - FPUIActionExtensionViewController
override func prepare(forError error: Error) {
coordinator.startWith(error: error)
}
override func prepare(forAction actionIdentifier: String, itemIdentifiers: [NSFileProviderItemIdentifier]) {
let action = FileProviderAction(rawValue: actionIdentifier)
switch action {
case .retryWaitingUpload:
if let domainIdentifier = itemIdentifiers.first?.domainIdentifier {
showUploadProgressAlert(for: itemIdentifiers, domainIdentifier: domainIdentifier)
} else {
showDomainNotFoundAlert()
}
case .retryFailedUpload:
if let domainIdentifier = itemIdentifiers.first?.domainIdentifier {
retryUpload(for: itemIdentifiers, domainIdentifier: domainIdentifier)
} else {
showDomainNotFoundAlert()
}
case .evictFileFromCache:
if let domainIdentifier = itemIdentifiers.first?.domainIdentifier {
showEvictFileFromCacheAlert(for: itemIdentifiers, domainIdentifier: domainIdentifier)
} else {
showDomainNotFoundAlert()
}
case .none:
let error = NSError(domain: NSCocoaErrorDomain, code: NSFeatureUnsupportedError, userInfo: [:])
extensionContext.cancelRequest(withError: error)
}
}
}