-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
570 lines (494 loc) · 23.1 KB
/
Copy pathAppDelegate.swift
File metadata and controls
570 lines (494 loc) · 23.1 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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//
// AppDelegate.swift
// ProtonMail
//
//
// Copyright (c) 2019 Proton Technologies AG
//
// This file is part of ProtonMail.
//
// ProtonMail 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.
//
// ProtonMail 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 ProtonMail. If not, see <https://www.gnu.org/licenses/>.
import UIKit
import SWRevealViewController
import AFNetworking
import PMKeymaker
import UserNotifications
import Intents
import PMCommon
import PMPayments
let sharedUserDataService = UserDataService(api: PMAPIService.unauthorized)
@UIApplicationMain
class AppDelegate: UIResponder {
var window: UIWindow? { // this property is important for State Restoration of modally presented viewControllers
return self.coordinator.currentWindow
}
lazy var coordinator: WindowsCoordinator = WindowsCoordinator(services: sharedServices)
private var currentState: UIApplication.State = .active
}
// MARK: - this is workaround to track when the SWRevealViewController first time load
extension SWRevealViewController {
open override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "sw_rear") {
if let menuViewController = segue.destination as? MenuViewController {
let usersManager = sharedServices.get(by: UsersManager.self)
let viewModel = MenuViewModelImpl(usersManager: usersManager)
let menu = MenuCoordinatorNew(vc: menuViewController, vm: viewModel, services: sharedServices)
menu.start()
}
} else if (segue.identifier == "sw_front") {
if let navigation = segue.destination as? UINavigationController {
if let mailboxViewController: MailboxViewController = navigation.firstViewController() as? MailboxViewController {
///TODO::fixme AppDelegate.coordinator.serviceHolder is bad
sharedVMService.mailbox(fromMenu: mailboxViewController)
let usersManager = sharedServices.get(by: UsersManager.self)
let user = usersManager.firstUser!
let viewModel = MailboxViewModelImpl(label: .inbox,
userManager: user,
usersManager: usersManager,
pushService: sharedServices.get(),
coreDataService: sharedServices.get())
let mailbox = MailboxCoordinator(vc: mailboxViewController, vm: viewModel, services: sharedServices)
mailbox.start()
}
}
}
}
}
// MARK: - consider move this to coordinator
extension AppDelegate: UserDataServiceDelegate {
func onLogout(animated: Bool) {
if #available(iOS 13.0, *) {
let sessions = Array(UIApplication.shared.openSessions)
let oneToStay = sessions.first(where: { $0.scene?.delegate as? WindowSceneDelegate != nil })
(oneToStay?.scene?.delegate as? WindowSceneDelegate)?.coordinator.go(dest: .signInWindow)
for session in sessions where session != oneToStay {
UIApplication.shared.requestSceneSessionDestruction(session, options: nil) { error in
PMLog.D(error.localizedDescription)
}
}
} else {
self.coordinator.go(dest: .signInWindow)
}
}
}
extension AppDelegate: APIServiceDelegate {
func isReachable() -> Bool {
#if !APP_EXTENSION
return sharedInternetReachability.currentReachabilityStatus() != NetworkStatus.NotReachable
#else
return sharedInternetReachability.currentReachabilityStatus() != NetworkStatus.NotReachable
#endif
}
func onUpdate(serverTime: Int64) {
Crypto.updateTime(serverTime)
}
var appVersion: String {
get {
return "iOS_\(Bundle.main.majorVersion)"
}
}
var userAgent: String? {
UserAgent.default.ua
}
func onDohTroubleshot() { }
}
extension AppDelegate: TrustKitUIDelegate {
func onTrustKitValidationError(_ alert: UIAlertController) {
let currentWindow: UIWindow? = {
if #available(iOS 13.0, *) {
let session = UIApplication.shared.openSessions.first { $0.scene?.activationState == UIScene.ActivationState.foregroundActive }
let scene = session?.scene as? UIWindowScene
let window = scene?.windows.first
return window
} else {
return self.window
}
}()
guard let top = currentWindow?.topmostViewController(), !(top is UIAlertController) else { return }
top.present(alert, animated: true, completion: nil)
}
}
//move to a manager class later
let sharedInternetReachability : Reachability = Reachability.forInternetConnection()
//let sharedRemoteReachability : Reachability = Reachability(hostName: AppConstants.API_HOST_URL)
// MARK: - UIApplicationDelegate
extension AppDelegate: UIApplicationDelegate {
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
sharedServices.get(by: AppCacheService.self).restoreCacheWhenAppStart()
let usersManager = UsersManager(doh: DoHMail.default, delegate: self)
sharedServices.add(UnlockManager.self, for: UnlockManager(cacheStatus: userCachedStatus, delegate: self))
sharedServices.add(UsersManager.self, for: usersManager)
sharedServices.add(SignInManager.self, for: SignInManager(usersManager: usersManager))
sharedServices.add(SpringboardShortcutsService.self, for: SpringboardShortcutsService())
sharedServices.add(StoreKitManagerImpl.self, for: StoreKitManagerImpl())
return true
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
#if DEBUG
PMLog.D("App group directory: " + FileManager.default.appGroupsDirectoryURL.absoluteString)
PMLog.D("App directory: " + FileManager.default.applicationSupportDirectoryURL.absoluteString)
PMLog.D("Tmp directory: " + FileManager.default.temporaryDirectoryUrl.absoluteString)
PMAPIService.noTrustKit = true
#endif
TrustKitWrapper.start(delegate: self)
Analytics.shared.setup()
UIApplication.shared.setMinimumBackgroundFetchInterval(300)
///TODO::fixme refactor
shareViewModelFactoy = ViewModelFactoryProduction()
sharedVMService.cleanLegacy()
AFNetworkActivityIndicatorManager.shared().isEnabled = true
//start network notifier
sharedInternetReachability.startNotifier()
// setup language: iOS 13 allows setting language per-app in Settings.app, so we trust that value
// we still use LanguageManager because Bundle.main of Share extension will take the value from host application :(
if #available(iOS 13.0, *), let code = Bundle.main.preferredLocalizations.first {
LanguageManager.saveLanguage(byCode: code)
}
//setup language
LanguageManager.setupCurrentLanguage()
let pushService : PushNotificationService = sharedServices.get()
UNUserNotificationCenter.current().delegate = pushService
pushService.registerForRemoteNotifications()
pushService.setLaunchOptions(launchOptions)
StoreKitManager.default.delegate = sharedServices.get(by: StoreKitManagerImpl.self)
StoreKitManager.default.subscribeToPaymentQueue()
StoreKitManager.default.updateAvailableProductsList()
#if DEBUG
NotificationCenter.default.addObserver(forName: Keymaker.Const.errorObtainingMainKey, object: nil, queue: .main) { notification in
(notification.userInfo?["error"] as? Error)?.localizedDescription.alertToast()
}
NotificationCenter.default.addObserver(forName: Keymaker.Const.removedMainKeyFromMemory, object: nil, queue: .main) { notification in
"Removed main key from memory".alertToastBottom()
}
#endif
NotificationCenter.default.addObserver(forName: Keymaker.Const.obtainedMainKey, object: nil, queue: .main) { notification in
#if DEBUG
"Obtained main key".alertToastBottom()
#endif
if self.currentState != .active {
keymaker.updateAutolockCountdownStart()
}
}
NotificationCenter.default.addObserver(self,
selector: #selector(didSignOutNotification(_:)),
name: NSNotification.Name.didSignOut,
object: nil)
if #available(iOS 12.0, *) {
// let intent = WipeMainKeyIntent()
// let suggestions = [INShortcut(intent: intent)!]
// INVoiceShortcutCenter.shared.setShortcutSuggestions(suggestions)
}
if #available(iOS 15.0, *) {
setupNavigationBarAppearance()
setupTableViewHeader()
}
if #available(iOS 13.0, *) {
// multiwindow support managed by UISessionDelegate, not UIApplicationDelegate
} else {
self.coordinator.start()
}
#if DEBUG
setupUITestsMocks()
#endif
return true
}
@objc fileprivate func didSignOutNotification(_ notification: Notification) {
self.onLogout(animated: false)
}
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return self.checkOrientation(window?.rootViewController)
}
func checkOrientation (_ viewController: UIViewController?) -> UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .pad || viewController == nil {
return UIInterfaceOrientationMask.all
} else if let nav = viewController as? UINavigationController {
if (nav.topViewController!.isKind(of: PinCodeViewController.self)) {
return UIInterfaceOrientationMask.portrait
}
return UIInterfaceOrientationMask.all
} else {
if let sw = viewController as? SWRevealViewController {
if let nav = sw.frontViewController as? UINavigationController {
if (nav.topViewController!.isKind(of: PinCodeViewController.self)) {
return UIInterfaceOrientationMask.portrait
}
}
}
return .all
}
}
@available(iOS, deprecated: 13, message: "This method will not get called on iOS 13, move the code to WindowSceneDelegate.scene(_:openURLContexts:)" )
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
return self.application(app, handleOpen: url)
}
@available(iOS, deprecated: 13, message: "This method will not get called on iOS 13, move the code to WindowSceneDelegate.scene(_:openURLContexts:)" )
func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
guard let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
return false
}
if ["protonmail", "mailto"].contains(urlComponents.scheme) || "mailto".caseInsensitiveCompare(urlComponents.scheme ?? "") == .orderedSame {
var path = url.absoluteString
if urlComponents.scheme == "protonmail" {
path = path.preg_replace("protonmail://", replaceto: "")
}
let deeplink = DeepLink(String(describing: MailboxViewController.self), sender: Message.Location.inbox.rawValue)
deeplink.append(DeepLink.Node(name: "toMailboxSegue", value: Message.Location.inbox))
deeplink.append(DeepLink.Node(name: "toComposeMailto", value: path))
self.coordinator.followDeeplink(deeplink)
return true
}
guard urlComponents.host == "signup" else {
return false
}
guard let queryItems = urlComponents.queryItems, let verifyObject = queryItems.filter({$0.name == "verifyCode"}).first else {
return false
}
guard let code = verifyObject.value else {
return false
}
///TODO::fixme change to deeplink
let info : [String:String] = ["verifyCode" : code]
let notification = Notification(name: .customUrlSchema,
object: nil,
userInfo: info)
NotificationCenter.default.post(notification)
return true
}
@available(iOS, deprecated: 13, message: "This method will not get called on iOS 13, move the code to WindowSceneDelegate.sceneDidEnterBackground()" )
func applicationDidEnterBackground(_ application: UIApplication) {
self.currentState = .background
keymaker.updateAutolockCountdownStart()
let users: UsersManager = sharedServices.get()
var taskID = UIBackgroundTaskIdentifier(rawValue: 0)
taskID = application.beginBackgroundTask {
PMLog.D("Background Task Timed Out")
application.endBackgroundTask(taskID)
taskID = .invalid
}
let delayedCompletion: ()->Void = {
delay(3) {
PMLog.D("End Background Task")
application.endBackgroundTask(taskID)
taskID = .invalid
}
}
if let user = users.firstUser {
user.messageService.backgroundTimeRemaining = {
application.backgroundTimeRemaining
}
user.messageService.purgeOldMessages()
user.messageService.cleanOldAttachment()
user.messageService.updateMessageCount()
user.messageService.backgroundFetch {
delayedCompletion()
}
} else {
delayedCompletion()
}
PMLog.D("Enter Background")
}
@available(iOS, deprecated: 13, message: "This method will not get called on iOS 13, deprecated in favor of similar method in WindowSceneDelegate" )
func application(_ application: UIApplication,
continue userActivity: NSUserActivity,
restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
{
if let data = userActivity.userInfo?["deeplink"] as? Data,
let deeplink = try? JSONDecoder().decode(DeepLink.self, from: data)
{
self.coordinator.followDeeplink(deeplink)
}
return true
}
func applicationWillTerminate(_ application: UIApplication) {
//TODO::here need change to notify composer to save editing draft
let coreDataService = sharedServices.get(by: CoreDataService.self)
let mainContext = coreDataService.mainManagedObjectContext
mainContext.performAndWait {
let _ = mainContext.saveUpstreamIfNeeded()
}
let backgroundContext = coreDataService.backgroundManagedObjectContext
backgroundContext.performAndWait {
let _ = backgroundContext.saveUpstreamIfNeeded()
}
}
func applicationWillEnterForeground(_ application: UIApplication) {
self.currentState = .active
let users: UsersManager = sharedServices.get()
users.users.forEach { (user) in
user.messageService.unBlockQueueAction()
user.messageService.backgroundTimeRemaining = nil
}
}
// MARK: Background methods
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// this feature can only work if user did not lock the app
let signInManager = SignInManagerProvider()
let unlockManager = UnlockManagerProvider()
guard signInManager.isSignedIn, unlockManager.isUnlocked else {
completionHandler(.noData)
return
}
let usersManager: UsersManager = sharedServices.get()
usersManager.firstUser?.messageService.backgroundFetch(notify: {
completionHandler(.newData)
})
//HACK: Call this after n seconds to prevent app got killed.
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(5)) {
completionHandler(.newData)
}
}
// MARK: Notification methods
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
Analytics.shared.error(message: .notificationError, error: error)
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
PMLog.D(deviceToken.stringFromToken())
let pushService: PushNotificationService = sharedServices.get()
pushService.didRegisterForRemoteNotifications(withDeviceToken: deviceToken.stringFromToken())
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let point = touch.location(in: UIApplication.shared.keyWindow)
let statusBarFrame = UIApplication.shared.statusBarFrame
if (statusBarFrame.contains(point)) {
self.touchStatusBar()
}
}
}
func touchStatusBar() {
let notification = Notification(name: .touchStatusBar, object: nil, userInfo: nil)
NotificationCenter.default.post(notification)
}
// MARK: - State restoration
func application(_ application: UIApplication, shouldSaveApplicationState coder: NSCoder) -> Bool {
if UIDevice.current.stateRestorationPolicy == .multiwindow {
return false
} else {
self.coordinator.saveForRestoration(coder)
return true
}
}
func application(_ application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
if UIDevice.current.stateRestorationPolicy == .multiwindow {
// everything is handled by a window scene delegate
} else if UIDevice.current.stateRestorationPolicy == .deeplink {
self.coordinator.restoreState(coder)
}
return false
}
// MARK: - Multiwindow iOS 13
@available(iOS 13.0, *)
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration
{
let scene = Scenes.fullApp // TODO: add more scenes
let config = UISceneConfiguration(name: scene.rawValue, sessionRole: connectingSceneSession.role)
config.delegateClass = scene.delegateClass
return config
}
@available(iOS 13.0, *)
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
sceneSessions.forEach { session in
// TODO: check that this discards state restoration for scenes explicitely closed by user
// up to at least iOS 13.3 beta 2 this does not work properly
session.stateRestorationActivity = nil
session.scene?.userActivity = nil
}
}
// MARK: Shortcuts
@available(iOS, deprecated: 13, message: "This method will not get called on iOS 13, deprecated in favor of similar method in WindowSceneDelegate" )
func application(_ application: UIApplication,
performActionFor shortcutItem: UIApplicationShortcutItem,
completionHandler: @escaping (Bool) -> Void)
{
if let data = shortcutItem.userInfo?["deeplink"] as? Data,
let deeplink = try? JSONDecoder().decode(DeepLink.self, from: data)
{
self.coordinator.followDeeplink(deeplink)
}
completionHandler(true)
}
}
extension AppDelegate : UsersManagerDelegate {
func migrating() {
}
func session() {
}
}
extension AppDelegate : UnlockManagerDelegate {
var isUserCredentialStored: Bool {
get {
let users = sharedServices.get(by: UsersManager.self)
if users.isMailboxPasswordStored || users.hasUsers() {
return true
}
return false
}
}
func isUserStored() -> Bool {
let users = sharedServices.get(by: UsersManager.self)
if users.hasUserName() || users.hasUsers() {
return true
}
return false
}
func isMailboxPasswordStored(forUser uid: String?) -> Bool {
let users = sharedServices.get(by: UsersManager.self)
guard let _ = uid else {
return users.isPasswordStored || users.hasUserName() //|| users.isMailboxPasswordStored
}
return !(sharedServices.get(by: UsersManager.self).users.last?.mailboxPassword ?? "").isEmpty
}
func cleanAll() {
///
sharedServices.get(by: UsersManager.self).clean()
keymaker.wipeMainKey()
keymaker.mainKeyExists()
}
func unlocked() {
// should work via messages
}
}
extension AppDelegate {
@available(iOS 15.0, *)
private func setupNavigationBarAppearance() {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor.ProtonMail.Nav_Bar_Background
let navigationBarTitleFont = Fonts.h2.light
appearance.titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: navigationBarTitleFont
]
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().compactScrollEdgeAppearance = appearance
}
@available(iOS 15.0, *)
private func setupTableViewHeader() {
UITableView.appearance().sectionHeaderTopPadding = 0
}
}
#if DEBUG
extension AppDelegate {
private func setupUITestsMocks() {
let environment = ProcessInfo.processInfo.environment
if let _ = environment["HumanVerificationStubs"] {
HumanVerificationManager.shared.setupUITestsMocks()
} else if let _ = environment["ForceUpgradeStubs"] {
ForceUpgradeManager.shared.setupUITestsMocks()
}
}
}
#endif