From 1458d4b713cad5999845b098af8895716648d5e6 Mon Sep 17 00:00:00 2001 From: Nathan Racklyeft Date: Wed, 21 Sep 2016 20:34:42 -0700 Subject: [PATCH] Fixing the removal of Pump and G5 Transmitter ID Fixes #181 --- Loop/AppDelegate.swift | 11 +++++++++-- Loop/Managers/DeviceDataManager.swift | 11 ++++++----- .../SettingsTableViewController.swift | 4 +--- Loop/View Controllers/StatusTableViewController.swift | 4 +++- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Loop/AppDelegate.swift b/Loop/AppDelegate.swift index 45b1404ba5..990268eb00 100644 --- a/Loop/AppDelegate.swift +++ b/Loop/AppDelegate.swift @@ -15,6 +15,8 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? + private(set) lazy var dataManager = DeviceDataManager() + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { window?.tintColor = UIColor.tintColor @@ -22,6 +24,11 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { AnalyticsManager.sharedManager.application(application, didFinishLaunchingWithOptions: launchOptions) + if let navVC = window?.rootViewController as? UINavigationController, + let statusVC = navVC.viewControllers.first as? StatusTableViewController { + statusVC.dataManager = dataManager + } + return true } @@ -42,7 +49,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. - DeviceDataManager.sharedManager.transmitter?.resumeScanning() + dataManager.transmitter?.resumeScanning() } func applicationWillTerminate(_ application: UIApplication) { @@ -73,7 +80,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { { AnalyticsManager.sharedManager.didRetryBolus() - DeviceDataManager.sharedManager.enactBolus(units) { (error) in + dataManager.enactBolus(units) { (error) in if error != nil { NotificationManager.sendBolusFailureNotificationForAmount(units, atDate: startDate) } diff --git a/Loop/Managers/DeviceDataManager.swift b/Loop/Managers/DeviceDataManager.swift index 66911687bf..a5182582f2 100644 --- a/Loop/Managers/DeviceDataManager.swift +++ b/Loop/Managers/DeviceDataManager.swift @@ -606,11 +606,13 @@ final class DeviceDataManager: CarbStoreDelegate, DoseStoreDelegate, Transmitter return pumpState?.pumpID } set { - guard newValue?.characters.count == 6 && newValue != pumpState?.pumpID else { + guard newValue != pumpState?.pumpID else { return } - if let pumpID = newValue { + var pumpID = newValue + + if let pumpID = pumpID, pumpID.characters.count == 6 { let pumpState = PumpState(pumpID: pumpID, pumpRegion: .northAmerica) if let timeZone = self.pumpState?.timeZone { @@ -619,6 +621,7 @@ final class DeviceDataManager: CarbStoreDelegate, DoseStoreDelegate, Transmitter self.pumpState = pumpState } else { + pumpID = nil self.pumpState = nil } @@ -868,8 +871,6 @@ final class DeviceDataManager: CarbStoreDelegate, DoseStoreDelegate, Transmitter // MARK: - Initialization - static let sharedManager = DeviceDataManager() - private(set) var loopManager: LoopDataManager! init() { @@ -933,7 +934,7 @@ final class DeviceDataManager: CarbStoreDelegate, DoseStoreDelegate, Transmitter receiver?.delegate = self } - if let transmitterID = UserDefaults.standard.transmitterID { + if let transmitterID = UserDefaults.standard.transmitterID, transmitterID.characters.count == 6 { transmitter = Transmitter(ID: transmitterID, passiveModeEnabled: true) transmitter?.delegate = self } diff --git a/Loop/View Controllers/SettingsTableViewController.swift b/Loop/View Controllers/SettingsTableViewController.swift index cd29118cbd..b95f220dc0 100644 --- a/Loop/View Controllers/SettingsTableViewController.swift +++ b/Loop/View Controllers/SettingsTableViewController.swift @@ -65,9 +65,7 @@ final class SettingsTableViewController: UITableViewController, DailyValueSchedu dataManagerObserver = nil } - fileprivate var dataManager: DeviceDataManager { - return DeviceDataManager.sharedManager - } + var dataManager: DeviceDataManager! private var dataManagerObserver: Any? { willSet { diff --git a/Loop/View Controllers/StatusTableViewController.swift b/Loop/View Controllers/StatusTableViewController.swift index ee0be4ce7b..1d35d177c5 100644 --- a/Loop/View Controllers/StatusTableViewController.swift +++ b/Loop/View Controllers/StatusTableViewController.swift @@ -106,7 +106,7 @@ final class StatusTableViewController: UITableViewController, UIGestureRecognize // References to registered notification center observers private var notificationObservers: [Any] = [] - unowned let dataManager = DeviceDataManager.sharedManager + weak var dataManager: DeviceDataManager! private var active = true { didSet { @@ -559,6 +559,8 @@ final class StatusTableViewController: UITableViewController, UIGestureRecognize } case let vc as PredictionTableViewController: vc.dataManager = dataManager + case let vc as SettingsTableViewController: + vc.dataManager = dataManager default: break }