120 lines
3.9 KiB
Swift
120 lines
3.9 KiB
Swift
//
|
|
// ApplicationDelegate.swift
|
|
// Cyberlock
|
|
//
|
|
// Created by Jura on 8/13/19.
|
|
// Copyright © 2019 Omicronmedia. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import SwiftyStoreKit
|
|
|
|
protocol ApplicationDelegateInput {
|
|
func openRoute(_ route: Route)
|
|
}
|
|
|
|
class ApplicationDelegate: UIResponder, UIApplicationDelegate, ApplicationDelegateInput {
|
|
|
|
internal var window: UIWindow?
|
|
private var appCoordinator: ApplicationCoordinator?
|
|
private var urlRoute: Route?
|
|
|
|
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
|
|
|
|
self.setupLoggers()
|
|
self.completePurchaseTransactions()
|
|
|
|
// Start application coordinator
|
|
let appWindow = UIWindow(frame: UIScreen.main.bounds)
|
|
self.window = appWindow
|
|
|
|
self.appCoordinator = ApplicationCoordinator(window: appWindow)
|
|
self.appCoordinator?.start()
|
|
|
|
let environments: [String: Any] = ["environment": Environment.environment,
|
|
"arguments": Environment.arguments,
|
|
"operationSystem": Environment.operationSystem,
|
|
"currentUUID": Environment.currentUUID,
|
|
"shortVersion": Environment.shortVersion,
|
|
"bundleVersion": Environment.bundleVersion]
|
|
CometLogger.shared.event(name: "Environment", attributes: environments, secured: nil)
|
|
|
|
LocalNotificationsManager.shared().register(for: application)
|
|
|
|
if let urlRoute = self.urlRoute {
|
|
self.appCoordinator?.handleRoute(urlRoute)
|
|
self.urlRoute = nil
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func applicationDidReceiveMemoryWarning(_ application: UIApplication) {
|
|
print("\n-----MEMORY WARNING-----\n")
|
|
}
|
|
|
|
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
|
|
|
|
guard let route = Inception.handle(url: url) else {
|
|
return true
|
|
}
|
|
|
|
if let appCoordinator = self.appCoordinator {
|
|
appCoordinator.handleRoute(route)
|
|
} else {
|
|
self.urlRoute = route
|
|
}
|
|
return true
|
|
}
|
|
|
|
func applicationDidBecomeActive(_ application: UIApplication) {
|
|
NotificationCenter.default.post(name: Notification.Name(PrivadoConstants.Application.didBecomeActive), object: nil)
|
|
}
|
|
|
|
// MARK: - Utilities methods
|
|
|
|
private func setupLoggers() {
|
|
let loggers: [CometLoggerProtocol] = [
|
|
SentryLogger(),
|
|
SnowplowLogger(),
|
|
NetworkLogger(),
|
|
VPNLogger(),
|
|
EventLogger()]
|
|
CometLogger.shared.add(loggers: loggers)
|
|
|
|
let plugin = NetworkLoggerPlugin(logger: CometLogger.shared)
|
|
NetworkRouter.addPlugin(plugin)
|
|
|
|
#if PRODUCTION
|
|
CometLogger.shared.level = .info
|
|
#else
|
|
CometLogger.shared.level = .debug
|
|
#endif
|
|
}
|
|
|
|
private func completePurchaseTransactions() {
|
|
SwiftyStoreKit.completeTransactions(atomically: true) { purchases in
|
|
for purchase in purchases {
|
|
switch purchase.transaction.transactionState {
|
|
case .purchased, .restored:
|
|
if purchase.needsFinishTransaction {
|
|
// Deliver content from server, then:
|
|
SwiftyStoreKit.finishTransaction(purchase.transaction)
|
|
}
|
|
// Unlock content
|
|
case .failed, .purchasing, .deferred:
|
|
break // do nothing
|
|
@unknown default:
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - ApplicationDelegateInput
|
|
|
|
func openRoute(_ route: Route) {
|
|
self.appCoordinator?.handleRoute(route)
|
|
}
|
|
}
|