-
Notifications
You must be signed in to change notification settings - Fork 8
/
SceneDelegate.swift
375 lines (308 loc) · 15.2 KB
/
SceneDelegate.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
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
/*
Infomaniak kDrive - iOS App
Copyright (C) 2023 Infomaniak Network SA
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import InfomaniakCore
import InfomaniakCoreCommonUI
import InfomaniakDI
import kDriveCore
import kDriveResources
import SafariServices
import UIKit
import VersionChecker
final class SceneDelegate: UIResponder, UIWindowSceneDelegate, AccountManagerDelegate {
@LazyInjectService var lockHelper: AppLockHelper
@LazyInjectService var accountManager: AccountManageable
@LazyInjectService var driveInfosManager: DriveInfosManager
@LazyInjectService var backgroundTasksService: BackgroundTasksServiceable
@LazyInjectService var appNavigable: AppNavigable
@LazyInjectService var appRestorationService: AppRestorationServiceable
var shortcutItemToProcess: UIApplicationShortcutItem?
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
Log.sceneDelegate("scene session options")
guard let windowScene = (scene as? UIWindowScene) else {
return
}
if let shortcutItem = connectionOptions.shortcutItem {
shortcutItemToProcess = shortcutItem
}
prepareWindowScene(windowScene)
accountManager.delegate = self
NotificationCenter.default.addObserver(self, selector: #selector(reloadDrive), name: .reloadDrive, object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleLocateUploadNotification),
name: .locateUploadActionTapped,
object: nil
)
let isRestoration: Bool = session.stateRestorationActivity != nil
Log.sceneDelegate("user activity isRestoration:\(isRestoration) \(session.stateRestorationActivity)")
guard let userActivity = connectionOptions.userActivities.first ?? session.stateRestorationActivity else {
Log.sceneDelegate("no user activity")
return
}
guard userActivity.activityType == SceneActivityIdentifier.mainSceneActivityType else {
Log.sceneDelegate("unsupported user activity type:\(userActivity.activityType)")
return
}
scene.userActivity = userActivity
guard let userInfo = userActivity.userInfo else {
Log.sceneDelegate("activity has no metadata to process")
return
}
Log.sceneDelegate("restore from \(userActivity.activityType)")
Log.sceneDelegate("selectedIndex:\(userInfo[SceneRestorationKeys.selectedIndex.rawValue])")
}
private func prepareWindowScene(_ windowScene: UIWindowScene) {
let newWindow = UIWindow(windowScene: windowScene)
window = newWindow
newWindow.makeKeyAndVisible()
setGlobalWindowTint()
appNavigable.updateTheme()
}
func configure(window: UIWindow?, session: UISceneSession, with activity: NSUserActivity) -> Bool {
Log.sceneDelegate("configure session with")
return true
}
func sceneDidDisconnect(_ scene: UIScene) {
Log.sceneDelegate("sceneDidDisconnect \(scene)")
}
func sceneWillResignActive(_ scene: UIScene) {
Log.sceneDelegate("sceneWillResignActive \(scene)")
}
func sceneWillEnterForeground(_ scene: UIScene) {
Log.sceneDelegate("sceneWillEnterForeground \(scene) \(window)")
@InjectService var uploadQueue: UploadQueue
uploadQueue.pausedNotificationSent = false
let currentState = RootViewControllerState.getCurrentState()
let session = scene.session
let isRestoration: Bool = session.stateRestorationActivity != nil
Log.sceneDelegate("user activity isRestoration:\(isRestoration) \(session.stateRestorationActivity)")
appNavigable.prepareRootViewController(currentState: currentState, restoration: isRestoration)
switch currentState {
case .mainViewController, .appLock:
UserDefaults.shared.numberOfConnections += 1
UserDefaults.shared.openingUntilReview -= 1
Task {
await appNavigable.refreshCacheScanLibraryAndUpload(preload: false, isSwitching: false)
}
uploadEditedFiles()
case .onboarding, .updateRequired, .preloading: break
}
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
Task {
if try await VersionChecker.standard.checkAppVersionStatus() == .updateIsRequired {
appNavigable.prepareRootViewController(currentState: .updateRequired, restoration: false)
}
}
}
func sceneDidBecomeActive(_ scene: UIScene) {
Log.sceneDelegate("sceneDidBecomeActive \(scene)")
guard let shortcutItem = shortcutItemToProcess else {
return
}
guard let rootViewController = window?.rootViewController as? MainTabViewController else {
return
}
rootViewController.dismiss(animated: false)
guard let navController = rootViewController.selectedViewController as? UINavigationController,
let viewController = navController.topViewController,
let driveFileManager = accountManager.currentDriveFileManager else {
return
}
switch shortcutItem.type {
case Constants.applicationShortcutScan:
let openMediaHelper = OpenMediaHelper(driveFileManager: driveFileManager)
openMediaHelper.openScan(rootViewController, false)
MatomoUtils.track(eventWithCategory: .shortcuts, name: "scan")
case Constants.applicationShortcutSearch:
let viewModel = SearchFilesViewModel(driveFileManager: driveFileManager)
viewController.present(
SearchViewController.instantiateInNavigationController(viewModel: viewModel),
animated: true
)
MatomoUtils.track(eventWithCategory: .shortcuts, name: "search")
case Constants.applicationShortcutUpload:
let openMediaHelper = OpenMediaHelper(driveFileManager: driveFileManager)
openMediaHelper.openMedia(rootViewController, .library)
MatomoUtils.track(eventWithCategory: .shortcuts, name: "upload")
case Constants.applicationShortcutSupport:
UIApplication.shared.open(URLConstants.support.url)
MatomoUtils.track(eventWithCategory: .shortcuts, name: "support")
default:
break
}
shortcutItemToProcess = nil
}
func sceneDidEnterBackground(_ scene: UIScene) {
Log.sceneDelegate("sceneDidEnterBackground \(scene)")
backgroundTasksService.scheduleBackgroundRefresh()
if UserDefaults.shared.isAppLockEnabled,
!(window?.rootViewController?.isKind(of: LockedAppViewController.self) ?? false) {
lockHelper.setTime()
}
}
// MARK: - Window Scene
func windowScene(_ windowScene: UIWindowScene,
didUpdate previousCoordinateSpace: UICoordinateSpace,
interfaceOrientation previousInterfaceOrientation: UIInterfaceOrientation,
traitCollection previousTraitCollection: UITraitCollection) {
Log.sceneDelegate("windowScene didUpdate")
}
func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem) async -> Bool {
Log.sceneDelegate("windowScene performActionFor :\(shortcutItem)")
shortcutItemToProcess = shortcutItem
return true
}
// MARK: - Deeplink
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else {
Log.sceneDelegate("scene unable to navigate to url", level: .error)
return
}
Task {
let success = await DeeplinkParser().parse(url: url)
Log.sceneDelegate("scene open url: \(url) success: \(success)")
}
}
// MARK: - Handoff support
func scene(_ scene: UIScene, willContinueUserActivityWithType userActivityType: String) {
Log.sceneDelegate("scene willContinueUserActivityWithType")
}
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
Log.sceneDelegate("scene continue userActivity")
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let incomingURL = userActivity.webpageURL,
let components = URLComponents(url: incomingURL, resolvingAgainstBaseURL: true) else {
return
}
UniversalLinksHelper.handlePath(components.path)
}
func scene(_ scene: UIScene, didFailToContinueUserActivityWithType userActivityType: String, error: Error) {
Log.sceneDelegate("scene didFailToContinueUserActivityWithType")
}
// MARK: - Account manager delegate
func currentAccountNeedsAuthentication() {
Task { @MainActor in
let switchUser = SwitchUserViewController.instantiateInNavigationController()
appNavigable.setRootViewController(switchUser, animated: true)
}
}
// MARK: - Reload drive notification
@objc func reloadDrive(_ notification: Notification) {
Task {
await self.appNavigable.refreshCacheScanLibraryAndUpload(preload: false, isSwitching: false)
}
}
@objc func handleLocateUploadNotification(_ notification: Notification) {
if let parentId = notification.userInfo?["parentId"] as? Int,
let driveFileManager = accountManager.currentDriveFileManager,
let folder = driveFileManager.getCachedFile(id: parentId) {
appNavigable.present(file: folder, driveFileManager: driveFileManager)
}
}
}
// TODO: Refactor with router like pattern and split code away from this class
extension SceneDelegate {
func uploadEditedFiles() {
Log.sceneDelegate("uploadEditedFiles")
guard let folderURL = DriveFileManager.constants.openInPlaceDirectoryURL,
FileManager.default.fileExists(atPath: folderURL.path) else {
return
}
let group = DispatchGroup()
var shouldCleanFolder = false
let driveFolders = (try? FileManager.default.contentsOfDirectory(atPath: folderURL.path)) ?? []
// Hierarchy inside folderURL should be /driveId/fileId/fileName.extension
for driveFolder in driveFolders {
let driveFolderURL = folderURL.appendingPathComponent(driveFolder)
guard let driveId = Int(driveFolder),
let drive = driveInfosManager.getDrive(id: driveId, userId: accountManager.currentUserId),
let fileFolders = try? FileManager.default.contentsOfDirectory(atPath: driveFolderURL.path) else {
Log.sceneDelegate("[OPEN-IN-PLACE UPLOAD] Could not infer drive from \(driveFolderURL)")
continue
}
for fileFolder in fileFolders {
let fileFolderURL = driveFolderURL.appendingPathComponent(fileFolder)
guard let fileId = Int(fileFolder),
let driveFileManager = accountManager.getDriveFileManager(for: drive.id, userId: drive.userId),
let file = driveFileManager.getCachedFile(id: fileId) else {
Log.sceneDelegate("[OPEN-IN-PLACE UPLOAD] Could not infer file from \(fileFolderURL)")
continue
}
let fileURL = fileFolderURL.appendingPathComponent(file.name)
guard FileManager.default.fileExists(atPath: fileURL.path) else {
continue
}
let attributes = try? FileManager.default.attributesOfItem(atPath: fileURL.path)
let modificationDate = attributes?[.modificationDate] as? Date ?? Date(timeIntervalSince1970: 0)
guard modificationDate > file.revisedAt else {
continue
}
let uploadFile = UploadFile(parentDirectoryId: file.parentId,
userId: accountManager.currentUserId,
driveId: file.driveId,
url: fileURL,
name: file.name,
conflictOption: .version,
shouldRemoveAfterUpload: false)
group.enter()
shouldCleanFolder = true
@InjectService var uploadQueue: UploadQueue
var observationToken: ObservationToken?
observationToken = uploadQueue
.observeFileUploaded(self, fileId: uploadFile.id) { [fileId = file.id] uploadFile, _ in
observationToken?.cancel()
if let error = uploadFile.error {
shouldCleanFolder = false
Log.sceneDelegate("[OPEN-IN-PLACE UPLOAD] Error while uploading: \(error)", level: .error)
} else {
// Update file to get the new modification date
Task {
let file = try await driveFileManager.file(id: fileId, forceRefresh: true)
try? FileManager.default.setAttributes([.modificationDate: file.revisedAt],
ofItemAtPath: file.localUrl.path)
driveFileManager.notifyObserversWith(file: file)
}
}
group.leave()
}
uploadQueue.saveToRealm(uploadFile, itemIdentifier: nil)
}
}
group.notify(queue: DispatchQueue.global(qos: .utility)) {
if shouldCleanFolder {
Log.sceneDelegate("[OPEN-IN-PLACE UPLOAD] Cleaning folder")
try? FileManager.default.removeItem(at: folderURL)
}
}
}
private func setGlobalWindowTint() {
window?.tintColor = KDriveResourcesAsset.infomaniakColor.color
UITabBar.appearance().unselectedItemTintColor = KDriveResourcesAsset.iconColor.color
// Migration from old UserDefaults
if UserDefaults.shared.legacyIsFirstLaunch {
UserDefaults.shared.legacyIsFirstLaunch = UserDefaults.standard.legacyIsFirstLaunch
}
}
}
extension SceneDelegate {
func stateRestorationActivity(for scene: UIScene) -> NSUserActivity? {
Log.sceneDelegate("stateRestorationActivity for:\(scene)")
guard appRestorationService.shouldRestoreApplicationState else {
return nil
}
return scene.userActivity
}
}