Просмотр исходного кода

fixed screen lock switch
moved bg loader out of view appear so it's not triggering unnecessarily
renamed backgroundtask file to backgroundtaskaudio to add location tasks separately
stored snoozebgtime to not trigger another BG alert on same reading

Jon Fawcett 6 лет назад
Родитель
Сommit
c0e7e4de56

+ 4 - 4
LoopFollow.xcodeproj/project.pbxproj

@@ -8,6 +8,7 @@
 
 /* Begin PBXBuildFile section */
 		3F1335F351590E573D8E6962 /* Pods_LoopFollow.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A7D55B42A22051DAD69E89D0 /* Pods_LoopFollow.framework */; };
+		FC3CAB022493B6220068A152 /* BackgroundTaskAudio.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCC688592489554800A0279D /* BackgroundTaskAudio.swift */; };
 		FC7CE518248ABE37001F83B8 /* Siri_Alert_Calibration_Needed.caf in Resources */ = {isa = PBXBuildFile; fileRef = FC7CE4A9248ABE2B001F83B8 /* Siri_Alert_Calibration_Needed.caf */; };
 		FC7CE519248ABE37001F83B8 /* Rise_And_Shine.caf in Resources */ = {isa = PBXBuildFile; fileRef = FC7CE4AA248ABE2B001F83B8 /* Rise_And_Shine.caf */; };
 		FC7CE51A248ABE37001F83B8 /* Jingle_All_The_Way.caf in Resources */ = {isa = PBXBuildFile; fileRef = FC7CE4AB248ABE2B001F83B8 /* Jingle_All_The_Way.caf */; };
@@ -136,7 +137,6 @@
 		FC9788292485969C00A7906C /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FC9788272485969C00A7906C /* LaunchScreen.storyboard */; };
 		FCC0FAC224922A22003E610E /* DictionaryKeyPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCC0FAC124922A22003E610E /* DictionaryKeyPath.swift */; };
 		FCC68850248935D800A0279D /* AlarmViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCC6884F248935D800A0279D /* AlarmViewController.swift */; };
-		FCC6885A2489554800A0279D /* BackgroundTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCC688592489554800A0279D /* BackgroundTask.swift */; };
 		FCC6885C2489559400A0279D /* blank.wav in Resources */ = {isa = PBXBuildFile; fileRef = FCC6885B2489559400A0279D /* blank.wav */; };
 		FCC6885E24896A6C00A0279D /* silence.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = FCC6885D24896A6C00A0279D /* silence.mp3 */; };
 		FCC6886524898EEE00A0279D /* UserDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCC6886424898EEE00A0279D /* UserDefaults.swift */; };
@@ -285,7 +285,7 @@
 		FC9788282485969C00A7906C /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
 		FCC0FAC124922A22003E610E /* DictionaryKeyPath.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DictionaryKeyPath.swift; sourceTree = "<group>"; };
 		FCC6884F248935D800A0279D /* AlarmViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlarmViewController.swift; sourceTree = "<group>"; };
-		FCC688592489554800A0279D /* BackgroundTask.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BackgroundTask.swift; sourceTree = "<group>"; };
+		FCC688592489554800A0279D /* BackgroundTaskAudio.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BackgroundTaskAudio.swift; sourceTree = "<group>"; };
 		FCC6885B2489559400A0279D /* blank.wav */ = {isa = PBXFileReference; lastKnownFileType = audio.wav; path = blank.wav; sourceTree = "<group>"; };
 		FCC6885D24896A6C00A0279D /* silence.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = silence.mp3; sourceTree = "<group>"; };
 		FCC6886424898EEE00A0279D /* UserDefaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserDefaults.swift; sourceTree = "<group>"; };
@@ -517,7 +517,7 @@
 				FCC6886C2489909D00A0279D /* AnyConvertible.swift */,
 				FCC6885D24896A6C00A0279D /* silence.mp3 */,
 				FCC6885B2489559400A0279D /* blank.wav */,
-				FCC688592489554800A0279D /* BackgroundTask.swift */,
+				FCC688592489554800A0279D /* BackgroundTaskAudio.swift */,
 				FCFEEC9F2488157B00402A7F /* Chart.swift */,
 				FCC0FAC124922A22003E610E /* DictionaryKeyPath.swift */,
 			);
@@ -778,7 +778,6 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
-				FCC6885A2489554800A0279D /* BackgroundTask.swift in Sources */,
 				FCC68850248935D800A0279D /* AlarmViewController.swift in Sources */,
 				FC7CE59F248D8D23001F83B8 /* SnoozeViewController.swift in Sources */,
 				FCC6886724898F8000A0279D /* UserDefaultsValue.swift in Sources */,
@@ -799,6 +798,7 @@
 				FCC6886D2489909D00A0279D /* AnyConvertible.swift in Sources */,
 				FC7CE588248ABE5A001F83B8 /* Alarms.swift in Sources */,
 				FC97881A2485969B00A7906C /* SceneDelegate.swift in Sources */,
+				FC3CAB022493B6220068A152 /* BackgroundTaskAudio.swift in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 21 - 1
LoopFollow/AppDelegate.swift

@@ -12,7 +12,7 @@ import CoreData
 @UIApplicationMain
 class AppDelegate: UIResponder, UIApplicationDelegate {
 
-
+    var window: UIWindow?
 
     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
         // Override point for customization after application launch.
@@ -21,6 +21,26 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
     }
 
     // MARK: UISceneSession Lifecycle
+    
+    func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
+        
+
+        // This application should be called in background every X Minutes
+        UIApplication.shared.setMinimumBackgroundFetchInterval(
+            TimeInterval(UserDefaultsRepository.backgroundRefreshFrequency.value * 60)
+        )
+        
+        // set "prevent screen lock" to ON when the app is started for the first time
+        if !UserDefaultsRepository.screenlockSwitchState.exists {
+            UserDefaultsRepository.screenlockSwitchState.value = true
+        }
+        
+        // set the "prevent screen lock" option when the app is started
+        // This method doesn't seem to be working anymore. Added to view controllers as solution offered on SO
+        UIApplication.shared.isIdleTimerDisabled = UserDefaultsRepository.screenlockSwitchState.value
+        
+        return true
+    }
 
     func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
         // Called when a new scene session is being created.

+ 33 - 17
LoopFollow/Controllers/MainViewController.swift

@@ -133,11 +133,18 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
         infoTable.rowHeight = 25
         infoTable.dataSource = self
         
+        // Load Data
+        nightscoutLoader()
+        
     }
     
     override func viewWillAppear(_ animated: Bool) {
+        // set screen lock
+        UIApplication.shared.isIdleTimerDisabled = UserDefaultsRepository.screenlockSwitchState.value;
+        
         // Pull fresh data when view appears
-        nightscoutLoader()
+        // moved this to didload, timer end, and foreground
+        //nightscoutLoader()
     }
     
     
@@ -178,6 +185,9 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
     }
     
     @objc func appMovedToBackground() {
+        // Allow screen to turn off
+        UIApplication.shared.isIdleTimerDisabled = false;
+        
         // We want to always come back to the home screen
         tabBarController?.selectedIndex = 0
         
@@ -190,6 +200,9 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
     }
 
     @objc func appCameToForeground() {
+        // reset screenlock state if needed
+        UIApplication.shared.isIdleTimerDisabled = UserDefaultsRepository.screenlockSwitchState.value;
+        
         // Cancel the background tasks, start a fresh timer, and immediately check for new data
         if UserDefaultsRepository.backgroundRefresh.value {
             backgroundTask.stopBackgroundTask()
@@ -1067,7 +1080,6 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
         let currentBGTime = bgs[bgs.count - 1].date
         var alarmTriggered = false
         
-        
         clearOldSnoozes()
         
         // Exit if all is snoozed
@@ -1075,24 +1087,25 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
             return
         }
         
+        
         // BG Based Alarms
-        // Check to make sure it is a current reading
-        if now - currentBGTime <= (5*60) {
+        // Check to make sure it is a current reading and has not already triggered alarm from this reading
+        if now - currentBGTime <= (5*60) && currentBGTime > UserDefaultsRepository.snoozedBGReadingTime.value as! TimeInterval {
             
             // trigger temporary alert first
             if UserDefaultsRepository.alertTemporaryActive.value {
                 if UserDefaultsRepository.alertTemporaryBelow.value {
                     if currentBG < UserDefaultsRepository.alertTemporaryBG.value {
                         UserDefaultsRepository.alertTemporaryActive.value = false
-                        AlarmSound.whichAlarm = "Temporary"
-                        triggerAlarm(sound: UserDefaultsRepository.alertTemporarySound.value)
+                        AlarmSound.whichAlarm = "Temporary Alert"
+                        triggerAlarm(sound: UserDefaultsRepository.alertTemporarySound.value, snooozedBGReadingTime: currentBGTime)
                         return
                     }
                 } else{
                     if currentBG > UserDefaultsRepository.alertTemporaryBG.value {
                       tabBarController?.selectedIndex = 2
                         AlarmSound.whichAlarm = "Temporary Alert"
-                        triggerAlarm(sound: UserDefaultsRepository.alertTemporarySound.value)
+                        triggerAlarm(sound: UserDefaultsRepository.alertTemporarySound.value, snooozedBGReadingTime: currentBGTime)
                         return
                    }
                 }
@@ -1102,7 +1115,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
             if UserDefaultsRepository.alertUrgentLowActive.value && !UserDefaultsRepository.alertUrgentLowIsSnoozed.value &&
             currentBG <= UserDefaultsRepository.alertUrgentLowBG.value && !UserDefaultsRepository.alertUrgentLowIsSnoozed.value {
                 AlarmSound.whichAlarm = "Urgent Low Alert"
-                triggerAlarm(sound: UserDefaultsRepository.alertUrgentLowSound.value)
+                triggerAlarm(sound: UserDefaultsRepository.alertUrgentLowSound.value, snooozedBGReadingTime: currentBGTime)
                 return
             }
             
@@ -1110,7 +1123,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
             if UserDefaultsRepository.alertLowActive.value && !UserDefaultsRepository.alertUrgentLowIsSnoozed.value &&
             currentBG <= UserDefaultsRepository.alertLowBG.value && !UserDefaultsRepository.alertLowIsSnoozed.value {
                 AlarmSound.whichAlarm = "Low Alert"
-                triggerAlarm(sound: UserDefaultsRepository.alertLowSound.value)
+                triggerAlarm(sound: UserDefaultsRepository.alertLowSound.value, snooozedBGReadingTime: currentBGTime)
                 return
             }
             
@@ -1118,7 +1131,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
             if UserDefaultsRepository.alertHighActive.value && !UserDefaultsRepository.alertHighIsSnoozed.value &&
             currentBG >= UserDefaultsRepository.alertHighBG.value && !UserDefaultsRepository.alertHighIsSnoozed.value {
                 AlarmSound.whichAlarm = "High Alert"
-                triggerAlarm(sound: UserDefaultsRepository.alertHighSound.value)
+                triggerAlarm(sound: UserDefaultsRepository.alertHighSound.value, snooozedBGReadingTime: currentBGTime)
                 return
             }
             
@@ -1126,7 +1139,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
             if UserDefaultsRepository.alertUrgentHighActive.value && !UserDefaultsRepository.alertUrgentHighIsSnoozed.value &&
             currentBG >= UserDefaultsRepository.alertUrgentHighBG.value && !UserDefaultsRepository.alertUrgentHighIsSnoozed.value {
                 AlarmSound.whichAlarm = "Urgent High Alert"
-                triggerAlarm(sound: UserDefaultsRepository.alertUrgentHighSound.value)
+                triggerAlarm(sound: UserDefaultsRepository.alertUrgentHighSound.value, snooozedBGReadingTime: currentBGTime)
                 return
             }
             
@@ -1141,7 +1154,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
                     || (UserDefaultsRepository.alertFastDropReadings.value == 3 && deltas[0] <= compare && deltas[1] <= compare)
                     || (UserDefaultsRepository.alertFastDropReadings.value == 4 && deltas[0] <= compare && deltas[1] <= compare && deltas[2] <= compare) {
                         AlarmSound.whichAlarm = "Fast Drop Alert"
-                        triggerAlarm(sound: UserDefaultsRepository.alertFastDropSound.value)
+                        triggerAlarm(sound: UserDefaultsRepository.alertFastDropSound.value, snooozedBGReadingTime: currentBGTime)
                         return
                     }
                 }
@@ -1158,7 +1171,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
                     || (UserDefaultsRepository.alertFastRiseReadings.value == 3 && deltas[0] >= compare && deltas[1] >= compare)
                     || (UserDefaultsRepository.alertFastRiseReadings.value == 4 && deltas[0] >= compare && deltas[1] >= compare && deltas[2] >= compare) {
                         AlarmSound.whichAlarm = "Fast Rise Alert"
-                        triggerAlarm(sound: UserDefaultsRepository.alertFastRiseSound.value)
+                        triggerAlarm(sound: UserDefaultsRepository.alertFastRiseSound.value, snooozedBGReadingTime: currentBGTime)
                         return
                     }
                 }
@@ -1172,7 +1185,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
         //check for missed reading alert
         if UserDefaultsRepository.alertMissedReadingActive.value && !UserDefaultsRepository.alertMissedReadingIsSnoozed.value && (Double(now - currentBGTime) >= Double(UserDefaultsRepository.alertMissedReading.value * 60)) {
             AlarmSound.whichAlarm = "Missed Reading Alert"
-                triggerAlarm(sound: UserDefaultsRepository.alertMissedReadingSound.value)
+                triggerAlarm(sound: UserDefaultsRepository.alertMissedReadingSound.value, snooozedBGReadingTime: nil)
                 return
         }
         
@@ -1184,7 +1197,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
             var tenDays = 10 * 24 * 60 * 60
             if Double(tenDays) - Double(delta) <= alertDistance {
                 AlarmSound.whichAlarm = "Sensor Change Alert"
-                triggerAlarm(sound: UserDefaultsRepository.alertSAGESound.value)
+                triggerAlarm(sound: UserDefaultsRepository.alertSAGESound.value, snooozedBGReadingTime: nil)
                 return
             }
         }
@@ -1197,19 +1210,22 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
             var tenDays = 3 * 24 * 60 * 60
             if Double(tenDays) - Double(delta) <= alertDistance {
                 AlarmSound.whichAlarm = "Pump Change Alert"
-                triggerAlarm(sound: UserDefaultsRepository.alertCAGESound.value)
+                triggerAlarm(sound: UserDefaultsRepository.alertCAGESound.value, snooozedBGReadingTime: nil)
                 return
             }
         }
         
     }
     
-    func triggerAlarm(sound: String)
+    func triggerAlarm(sound: String, snooozedBGReadingTime: TimeInterval?)
     {
         guard let snoozer = self.tabBarController!.viewControllers?[2] as? SnoozeViewController else { return }
         snoozer.updateDisplayWhenTriggered(bgVal: BGText.text ?? "", directionVal: DirectionText.text ?? "", deltaVal: DeltaText.text ?? "", minAgoVal: MinAgoText.text ?? "", alertLabelVal: AlarmSound.whichAlarm)
         snoozeTabItem.isEnabled = true;
         tabBarController?.selectedIndex = 2
+        if snooozedBGReadingTime != nil {
+            UserDefaultsRepository.snoozedBGReadingTime.value = snooozedBGReadingTime
+        }
         AlarmSound.setSoundFile(str: sound)
         AlarmSound.play()
     }

LoopFollow/helpers/BackgroundTask.swift → LoopFollow/helpers/BackgroundTaskAudio.swift


+ 2 - 0
LoopFollow/repository/UserDefaults.swift

@@ -50,6 +50,8 @@ class UserDefaultsRepository {
     
     
     // Alerts
+    static let snoozedBGReadingTime = UserDefaultsValue<TimeInterval?>(key: "snoozedBGReadingTime", default: 0)
+    
     static let alertCageInsertTime = UserDefaultsValue<TimeInterval>(key: "alertCageInsertTime", default: 0)
     static let alertSageInsertTime = UserDefaultsValue<TimeInterval>(key: "alertSageInsertTime", default: 0)