-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathAppDelegate.swift
353 lines (288 loc) · 12.7 KB
/
AppDelegate.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
import FBSDKCoreKit
import Foundation
import HockeySDK
#if DEBUG
@testable import KsApi
#else
import KsApi
#endif
import Kickstarter_Framework
import Library
import LiveStream
import Prelude
import ReactiveExtensions
import ReactiveSwift
import Result
import SafariServices
import UIKit
import UserNotifications
@UIApplicationMain
internal final class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
fileprivate let viewModel: AppDelegateViewModelType = AppDelegateViewModel()
internal var rootTabBarController: RootTabBarViewController? {
return self.window?.rootViewController as? RootTabBarViewController
}
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIView.doBadSwizzleStuff()
UIViewController.doBadSwizzleStuff()
AppEnvironment.replaceCurrentEnvironment(
AppEnvironment.fromStorage(
ubiquitousStore: NSUbiquitousKeyValueStore.default(),
userDefaults: UserDefaults.standard
)
)
// NB: We have to push this shared instance directly because somehow we get two different shared
// instances if we use the one from `Environment.init`.
AppEnvironment.replaceCurrentEnvironment(facebookAppDelegate: FBSDKApplicationDelegate.sharedInstance())
#if DEBUG
if KsApi.Secrets.isOSS {
AppEnvironment.replaceCurrentEnvironment(apiService: MockService())
}
#endif
self.viewModel.outputs.updateCurrentUserInEnvironment
.observeForUI()
.observeValues { [weak self] user in
AppEnvironment.updateCurrentUser(user)
self?.viewModel.inputs.currentUserUpdatedInEnvironment()
}
self.viewModel.outputs.forceLogout
.observeForUI()
.observeValues {
AppEnvironment.logout()
NotificationCenter.default.post(.init(name: .ksr_sessionEnded, object: nil))
}
self.viewModel.outputs.updateConfigInEnvironment
.observeForUI()
.observeValues { AppEnvironment.updateConfig($0) }
self.viewModel.outputs.postNotification
.observeForUI()
.observeValues(NotificationCenter.default.post)
self.viewModel.outputs.presentViewController
.observeForUI()
.observeValues { [weak self] in
self?.rootTabBarController?.dismiss(animated: true, completion: nil)
self?.rootTabBarController?.present($0, animated: true, completion: nil)
}
self.viewModel.outputs.goToDiscovery
.observeForUI()
.observeValues { [weak self] in self?.rootTabBarController?.switchToDiscovery(params: $0) }
self.viewModel.outputs.goToActivity
.observeForUI()
.observeValues { [weak self] in self?.rootTabBarController?.switchToActivities() }
self.viewModel.outputs.goToDashboard
.observeForUI()
.observeValues { [weak self] in self?.rootTabBarController?.switchToDashboard(project: $0) }
self.viewModel.outputs.goToLiveStream
.observeForControllerAction()
.observeValues { [weak self] in
self?.goToLiveStream(project: $0, liveStreamEvent: $1, refTag: $2)
}
self.viewModel.outputs.goToCreatorMessageThread
.observeForUI()
.observeValues { [weak self] in
self?.goToCreatorMessageThread($0, $1)
}
self.viewModel.outputs.goToMessageThread
.observeForUI()
.observeValues { [weak self] in self?.goToMessageThread($0) }
self.viewModel.outputs.goToProjectActivities
.observeForUI()
.observeValues { [weak self] in
self?.goToProjectActivities($0)
}
self.viewModel.outputs.goToSearch
.observeForUI()
.observeValues { [weak self] in self?.rootTabBarController?.switchToSearch() }
self.viewModel.outputs.goToMobileSafari
.observeForUI()
.observeValues { UIApplication.shared.openURL($0) }
self.viewModel.outputs.registerForRemoteNotifications
.observeForUI()
.observeValues {
if #available(iOS 10.0, *) {
UIApplication.shared.registerForRemoteNotifications()
} else {
UIApplication.shared.registerUserNotificationSettings(
UIUserNotificationSettings(types: .alert, categories: [])
)
UIApplication.shared.registerForRemoteNotifications()
}
}
if #available(iOS 10.0, *) {
self.viewModel.outputs.getNotificationAuthorizationStatus
.observeForUI()
.observeValues { [weak self] in
UNUserNotificationCenter.current().getNotificationSettings { settings in
self?.viewModel.inputs.notificationAuthorizationStatusReceived(settings.authorizationStatus)
}
}
self.viewModel.outputs.authorizeForRemoteNotifications
.observeForUI()
.observeValues { [weak self] in
UNUserNotificationCenter.current().requestAuthorization(options: [.alert]) { (isGranted, _) in
self?.viewModel.inputs.notificationAuthorizationCompleted(isGranted: isGranted)
}
}
}
self.viewModel.outputs.unregisterForRemoteNotifications
.observeForUI()
.observeValues(UIApplication.shared.unregisterForRemoteNotifications)
self.viewModel.outputs.presentRemoteNotificationAlert
.observeForUI()
.observeValues { [weak self] in
self?.presentRemoteNotificationAlert($0)
}
self.viewModel.outputs.configureHockey
.observeForUI()
.observeValues { [weak self] data in
guard let _self = self else { return }
let manager = BITHockeyManager.shared()
manager.delegate = _self
manager.configure(withIdentifier: data.appIdentifier)
manager.crashManager.crashManagerStatus = .autoSend
manager.isUpdateManagerDisabled = data.disableUpdates
manager.userID = data.userId
manager.userName = data.userName
manager.start()
manager.authenticator.authenticateInstallation()
}
self.viewModel.outputs.synchronizeUbiquitousStore
.observeForUI()
.observeValues {
_ = AppEnvironment.current.ubiquitousStore.synchronize()
}
self.viewModel.outputs.setApplicationShortcutItems
.observeForUI()
.observeValues { shortcutItems in
UIApplication.shared.shortcutItems = shortcutItems.map { $0.applicationShortcutItem }
}
self.viewModel.outputs.findRedirectUrl
.observeForUI()
.observeValues { [weak self] in self?.findRedirectUrl($0) }
NotificationCenter.default
.addObserver(forName: Notification.Name.ksr_sessionStarted, object: nil, queue: nil) { [weak self] _ in
self?.viewModel.inputs.userSessionStarted()
}
NotificationCenter.default
.addObserver(forName: Notification.Name.ksr_sessionEnded, object: nil, queue: nil) { [weak self] _ in
self?.viewModel.inputs.userSessionEnded()
}
self.window?.tintColor = .ksr_dark_grey_500
self.viewModel.inputs.applicationDidFinishLaunching(application: application,
launchOptions: launchOptions)
return self.viewModel.outputs.applicationDidFinishLaunchingReturnValue
}
func applicationWillEnterForeground(_ application: UIApplication) {
self.viewModel.inputs.applicationWillEnterForeground()
}
func applicationDidEnterBackground(_ application: UIApplication) {
self.viewModel.inputs.applicationDidEnterBackground()
}
func application(_ application: UIApplication,
continue userActivity: NSUserActivity,
restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
return self.viewModel.inputs.applicationContinueUserActivity(userActivity)
}
func application(_ application: UIApplication,
open url: URL,
sourceApplication: String?,
annotation: Any) -> Bool {
return self.viewModel.inputs.applicationOpenUrl(application: application,
url: url,
sourceApplication: sourceApplication,
annotation: annotation)
}
internal func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
self.viewModel.inputs.didRegisterForRemoteNotifications(withDeviceTokenData: deviceToken)
}
internal func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
self.viewModel.inputs.didReceive(remoteNotification: userInfo,
applicationIsActive: application.applicationState == .active)
}
internal func application(_ application: UIApplication,
didReceive notification: UILocalNotification) {
if let userInfo = notification.userInfo, userInfo["aps"] != nil {
self.viewModel.inputs.didReceive(remoteNotification: userInfo,
applicationIsActive: application.applicationState == .active)
}
}
internal func applicationDidReceiveMemoryWarning(_ application: UIApplication) {
self.viewModel.inputs.applicationDidReceiveMemoryWarning()
}
internal func application(_ application: UIApplication,
performActionFor shortcutItem: UIApplicationShortcutItem,
completionHandler: @escaping (Bool) -> Void) {
self.viewModel.inputs.applicationPerformActionForShortcutItem(shortcutItem)
completionHandler(true)
}
fileprivate func presentRemoteNotificationAlert(_ message: String) {
let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
alert.addAction(
UIAlertAction(title: Strings.View(), style: .default) { [weak self] _ in
self?.viewModel.inputs.openRemoteNotificationTappedOk()
}
)
alert.addAction(
UIAlertAction(title: Strings.Dismiss(), style: .cancel, handler: nil)
)
self.rootTabBarController?.present(alert, animated: true, completion: nil)
}
private func goToLiveStream(project: Project,
liveStreamEvent: LiveStreamEvent,
refTag: RefTag?) {
let projectVc = ProjectPamphletViewController.configuredWith(projectOrParam: .left(project),
refTag: refTag)
let liveVc: UIViewController
if liveStreamEvent.startDate < AppEnvironment.current.dateType.init().date {
liveVc = LiveStreamContainerViewController.configuredWith(project: project,
liveStreamEvent: liveStreamEvent,
refTag: .push,
presentedFromProject: false)
} else {
liveVc = LiveStreamCountdownViewController.configuredWith(project: project,
liveStreamEvent: liveStreamEvent,
refTag: .push,
presentedFromProject: false)
}
let nav = UINavigationController(navigationBarClass: ClearNavigationBar.self, toolbarClass: nil)
nav.viewControllers = [liveVc]
self.rootTabBarController?.present(projectVc, animated: true) {
projectVc.present(nav, animated: true)
}
}
private func goToMessageThread(_ messageThread: MessageThread) {
self.rootTabBarController?.switchToMessageThread(messageThread)
}
private func goToCreatorMessageThread(_ projectId: Param, _ messageThread: MessageThread) {
self.rootTabBarController?
.switchToCreatorMessageThread(projectId: projectId, messageThread: messageThread)
}
private func goToProjectActivities(_ projectId: Param) {
self.rootTabBarController?.switchToProjectActivities(projectId: projectId)
}
private func findRedirectUrl(_ url: URL) {
let session = URLSession(configuration: .default, delegate: self, delegateQueue: nil)
let task = session.dataTask(with: url)
task.resume()
}
}
extension AppDelegate: BITHockeyManagerDelegate {
func crashManagerDidFinishSendingCrashReport(_ crashManager: BITCrashManager!) {
self.viewModel.inputs.crashManagerDidFinishSendingCrashReport()
}
}
extension AppDelegate: URLSessionTaskDelegate {
public func urlSession(_ session: URLSession,
task: URLSessionTask,
willPerformHTTPRedirection response: HTTPURLResponse,
newRequest request: URLRequest,
completionHandler: @escaping (URLRequest?) -> Void) {
request.url.doIfSome(self.viewModel.inputs.foundRedirectUrl)
completionHandler(nil)
}
}