Newer
Older
import UIKit
import OSLog
import BackgroundTasks
import Theme
import XXLogger
import Defaults
import Integration
import CrashReporting
import PushNotifications
import DependencyInjection
import OnboardingFeature
let logger = Logger(subsystem: "logs_xxmessenger", category: "AppDelegate.swift")
public class AppDelegate: UIResponder, UIApplicationDelegate {
@Dependency private var pushHandler: PushHandling
@Dependency private var crashReporter: CrashReporter
@Dependency private var dropboxService: DropboxInterface
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
@KeyObject(.hideAppList, defaultValue: false) var hideAppList: Bool
@KeyObject(.recordingLogs, defaultValue: true) var recordingLogs: Bool
@KeyObject(.crashReporting, defaultValue: true) var isCrashReportingEnabled: Bool
var calledStopNetwork = false
var forceFailedPendingMessages = false
var coverView: UIView?
var backgroundTimer: Timer?
public var window: UIWindow?
public func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
#if DEBUG
DependencyRegistrator.registerForMock()
#else
DependencyRegistrator.registerForLive()
#endif
if recordingLogs {
XXLogger.start()
}
crashReporter.configure()
crashReporter.setEnabled(isCrashReportingEnabled)
UNUserNotificationCenter.current().delegate = self
let rootScreen = StatusBarViewController(
UINavigationController(rootViewController: OnboardingLaunchController())
)
window = Window()
window?.rootViewController = rootScreen
window?.backgroundColor = UIColor.white
window?.makeKeyAndVisible()
UserDefaults.standard.set(false, forKey: "_UIConstraintBasedLayoutLogUnsatisfiable")
return true
}
public func application(application: UIApplication, shouldAllowExtensionPointIdentifier: String) -> Bool {
false
}
public func applicationDidEnterBackground(_ application: UIApplication) {
if let session = try? DependencyInjection.Container.shared.resolve() as SessionType {
let backgroundTask = application.beginBackgroundTask(withName: "xx.stop.network") {
logger.log("Background task will expire")
}
// An option here would be: create async completion closure
backgroundTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in
logger.log("Background time remaining: \(UIApplication.shared.backgroundTimeRemaining)")
guard UIApplication.shared.backgroundTimeRemaining > 8 else {
if !self.calledStopNetwork {
self.calledStopNetwork = true
session.stop()
logger.log("Stopping client threads...")
} else {
if session.hasRunningTasks == false {
application.endBackgroundTask(backgroundTask)
timer.invalidate()
logger.log("Finished background processes")
}
}
return
}
guard UIApplication.shared.backgroundTimeRemaining > 9 else {
if !self.forceFailedPendingMessages {
self.forceFailedPendingMessages = true
logger.log("Background time is running out. Will force-fail all pending messages")
session.forceFailMessages()
} else {
logger.log("Background time is running out without pending messages")
}
return
}
}
}
}
public func applicationWillResignActive(_ application: UIApplication) {
if hideAppList {
coverView?.removeFromSuperview()
coverView = UIVisualEffectView(effect: UIBlurEffect(style: .regular))
coverView?.frame = window?.bounds ?? .zero
window?.addSubview(coverView!)
}
}
public func applicationWillTerminate(_ application: UIApplication) {
if let session = try? DependencyInjection.Container.shared.resolve() as SessionType {
logger.log("applicationWillTerminate but has an ongoing session. Calling stopNetwork...")
session.stop()
} else {
logger.log("applicationWillTerminate without any session")
}
}
public func applicationWillEnterForeground(_ application: UIApplication) {
if backgroundTimer != nil {
logger.log("Invalidating background timer...")
backgroundTimer?.invalidate()
backgroundTimer = nil
}
if let session = try? DependencyInjection.Container.shared.resolve() as SessionType {
guard self.calledStopNetwork == true else {
logger.log("A client instance is already running. Moving on...")
return
}
logger.log("A client instance is stopped. Starting network...")
session.start()
self.calledStopNetwork = false
}
}
public func applicationDidBecomeActive(_ application: UIApplication) {
application.applicationIconBadgeNumber = 0
coverView?.removeFromSuperview()
}
public func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
dropboxService.handleOpenUrl(url)
}
}
// MARK: Notifications
extension AppDelegate: UNUserNotificationCenterDelegate {
public func application(
_: UIApplication,
didReceiveRemoteNotification notification: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
) {
pushHandler.didReceiveRemote(notification, completionHandler)
}
public func application(
_: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
pushHandler.didRegisterWith(deviceToken)
}
}