Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 62 additions & 11 deletions Loop/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,92 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

private(set) lazy var deviceManager = DeviceDataManager()
private var deviceManager: DeviceDataManager?

private var rootViewController: RootNavigationController! {
return window?.rootViewController as? RootNavigationController
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

private var isAfterFirstUnlock: Bool {
let fileManager = FileManager.default
do {
let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
let fileURL = documentDirectory.appendingPathComponent("protection.test")
guard fileManager.fileExists(atPath: fileURL.path) else {
let contents = Data("unimportant".utf8)
try? contents.write(to: fileURL, options: .completeFileProtectionUntilFirstUserAuthentication)
// If file doesn't exist, we're at first start, which will be user directed.
return true
}
let contents = try? Data(contentsOf: fileURL)
return contents != nil
} catch {
log.error(error)
}
return false
}

private func finishLaunch() {
log.default("Finishing launching")

deviceManager = DeviceDataManager()

NotificationManager.authorize(delegate: self)

let mainStatusViewControllers = UIStoryboard(name: "Main", bundle: Bundle(for: AppDelegate.self)).instantiateViewController(withIdentifier: "MainStatusViewController") as! StatusTableViewController

log.info(#function)

AnalyticsManager.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
rootViewController.pushViewController(mainStatusViewControllers, animated: false)

rootViewController.rootViewController.deviceManager = deviceManager

}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

log.default("didFinishLaunchingWithOptions \(String(describing: launchOptions))")

AnalyticsManager.shared.application(application, didFinishLaunchingWithOptions: launchOptions)

guard isAfterFirstUnlock else {
log.default("Launching before first unlock; pausing launch...")
return false
}

finishLaunch()

let notificationOption = launchOptions?[.remoteNotification]

if let notification = notificationOption as? [String: AnyObject] {
deviceManager.handleRemoteNotification(notification)
deviceManager?.handleRemoteNotification(notification)
}

return true
}

func applicationWillResignActive(_ application: UIApplication) {
log.default(#function)
}

func applicationDidEnterBackground(_ application: UIApplication) {
log.default(#function)
}

func applicationWillEnterForeground(_ application: UIApplication) {
log.default(#function)
}

func applicationDidBecomeActive(_ application: UIApplication) {
deviceManager.updatePumpManagerBLEHeartbeatPreference()
deviceManager?.updatePumpManagerBLEHeartbeatPreference()
}

func applicationWillTerminate(_ application: UIApplication) {
log.default(#function)
}

// MARK: - Continuity

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
log.default(#function)

if #available(iOS 12.0, *) {
if userActivity.activityType == NewCarbEntryIntent.className {
Expand All @@ -86,7 +129,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
let token = tokenParts.joined()
log.default("RemoteNotifications device token: \(token)")
deviceManager.loopManager.settings.deviceToken = deviceToken
deviceManager?.loopManager.settings.deviceToken = deviceToken
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
Expand All @@ -102,9 +145,17 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
return
}

deviceManager.handleRemoteNotification(notification)
deviceManager?.handleRemoteNotification(notification)
completionHandler(.noData)
}

func applicationProtectedDataDidBecomeAvailable(_ application: UIApplication) {
log.default("applicationProtectedDataDidBecomeAvailable")

if deviceManager == nil {
finishLaunch()
}
}

}

Expand All @@ -119,7 +170,7 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
{
AnalyticsManager.shared.didRetryBolus()

deviceManager.enactBolus(units: units, at: startDate) { (_) in
deviceManager?.enactBolus(units: units, at: startDate) { (_) in
completionHandler()
}
return
Expand Down
Loading