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

persistent notification option, watch calendar customization option

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

+ 45 - 2
LoopFollow/Controllers/MainViewController.swift

@@ -1050,7 +1050,6 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
         if !granted { return }
             
         // Create Event info
-           // eventTitle = BGText.text + " " + DirectionText.text + " " + DeltaText.text + "\nC:" + tableData[1].value + "g I:" + tableData[0].value + "u"
             let deltaBG = self.bgData[self.bgData.count - 1].sgv -  self.bgData[self.bgData.count - 2].sgv as Int
             let deltaTime = (TimeInterval(Date().timeIntervalSince1970) - self.bgData[self.bgData.count - 1].date) / 60
             var deltaString = ""
@@ -1062,9 +1061,39 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
                 deltaString = "+" + String(deltaBG)
             }
             let direction = self.bgDirectionGraphic(self.bgData[self.bgData.count - 1].direction ?? "")
+            
+            
             var eventStartDate = Date(timeIntervalSince1970: self.bgData[self.bgData.count - 1].date)
             var eventEndDate = eventStartDate.addingTimeInterval(60 * 10)
-            var eventTitle = ""
+            var  eventTitle = UserDefaultsRepository.watchLine1.value + "\n" + UserDefaultsRepository.watchLine2.value
+            eventTitle = eventTitle.replacingOccurrences(of: "%BG%", with: String(self.bgData[self.bgData.count - 1].sgv))
+            eventTitle = eventTitle.replacingOccurrences(of: "%DIRECTION%", with: direction)
+            eventTitle = eventTitle.replacingOccurrences(of: "%DELTA%", with: deltaString)
+            var minAgo = ""
+            if deltaTime > 5 {
+                // write old BG reading and continue pushing out end date to show last entry
+                minAgo = String(Int(deltaTime)) + " min"
+                eventEndDate = eventStartDate.addingTimeInterval((60 * 10) + (deltaTime * 60))
+            }
+            var cob = "0"
+            if self.tableData[1].value != "" {
+                cob = self.tableData[1].value
+            }
+            var basal = "~"
+            if self.tableData[2].value != "" {
+                basal = self.tableData[2].value
+            }
+            var iob = "0"
+            if self.tableData[0].value != "" {
+                iob = self.tableData[0].value
+            }
+            eventTitle = eventTitle.replacingOccurrences(of: "%MINAGO%", with: minAgo)
+            eventTitle = eventTitle.replacingOccurrences(of: "%IOB%", with: self.tableData[0].value)
+            eventTitle = eventTitle.replacingOccurrences(of: "%COB%", with: cob)
+            eventTitle = eventTitle.replacingOccurrences(of: "%BASAL%", with: basal)
+            
+            // old
+            /*
             eventTitle += String(self.bgData[self.bgData.count - 1].sgv) + " "
             eventTitle += direction + " "
             eventTitle += deltaString + " "
@@ -1081,6 +1110,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
             if self.tableData[0].value != "" {
                 eventTitle += "I: " + self.tableData[0].value + "u"
             }
+            */
             
             
             
@@ -1129,7 +1159,9 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
         clearOldSnoozes()
         
         // Exit if all is snoozed
+        // still send persistent notification with all snoozed
         if UserDefaultsRepository.alertSnoozeAllIsSnoozed.value {
+            persistentNotification(bgTime: currentBGTime)
             return
         }
         
@@ -1299,6 +1331,9 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
             }
         }
         
+        // still send persistent notification if no alarms trigger and persistent notification is on
+        persistentNotification(bgTime: currentBGTime)
+        
     }
     
     func triggerAlarm(sound: String, snooozedBGReadingTime: TimeInterval?)
@@ -1314,6 +1349,14 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
         AlarmSound.play()
     }
     
+    func persistentNotification(bgTime: TimeInterval)
+    {
+        if UserDefaultsRepository.persistentNotification.value && bgTime > UserDefaultsRepository.persistentNotificationLastBGTime.value {
+            guard let snoozer = self.tabBarController!.viewControllers?[2] as? SnoozeViewController else { return }
+            snoozer.sendNotification(self, bgVal: BGText.text ?? "", directionVal: DirectionText.text ?? "", deltaVal: DeltaText.text ?? "", minAgoVal: MinAgoText.text ?? "", alertLabelVal: "Latest BG")
+        }
+    }
+    
     func clearOldSnoozes(){
         let date = Date()
         let now = date.timeIntervalSince1970

+ 25 - 2
LoopFollow/Controllers/SettingsViewController.swift

@@ -70,13 +70,20 @@ class SettingsViewController: FormViewController {
             guard let mainScreen = self?.tabBarController!.viewControllers?[0] as? MainViewController else { return }
             mainScreen.setBGTextColor()
         }
-            <<< SwitchRow("forceDarkMode") { row in
+        <<< SwitchRow("forceDarkMode") { row in
         row.title = "Force Dark Mode (Restart App)"
         row.value = UserDefaultsRepository.forceDarkMode.value
         }.onChange { [weak self] row in
             guard let value = row.value else { return }
                 UserDefaultsRepository.forceDarkMode.value = value
         }
+        <<< SwitchRow("persistentNotification") { row in
+        row.title = "Persistent Notification"
+        row.value = UserDefaultsRepository.persistentNotification.value
+        }.onChange { [weak self] row in
+            guard let value = row.value else { return }
+                UserDefaultsRepository.persistentNotification.value = value
+        }
         <<< SwitchRow("screenlockSwitchState") { row in
             row.title = "Keep Screen Active"
             row.value = UserDefaultsRepository.screenlockSwitchState.value
@@ -235,7 +242,7 @@ class SettingsViewController: FormViewController {
         }
         
         form
-        +++ Section(header: "Watch Settings", footer: "Add the Apple calendar complication to your watch face for BG, Trend, Delta, COB, and IOB updated every 5 minutes. It is recommended to create a new calendar called 'Loop' and modify the calendar settings in the iPhone Watch App to only display the Loop calendar on your watch")
+        +++ Section(header: "Watch Settings", footer: "Add the Apple calendar complication to your watch face for BG, Trend, Delta, COB, and IOB updated every 5 minutes. It is recommended to create a new calendar called 'Loop' and modify the calendar settings in the iPhone Watch App to only display the Loop calendar on your watch. Available variables are: %BG%, %DIRECTION%, %DELTA%, %MINAGO%, %IOB%, %COB%, %BASAL%. ** %MINAGO% only displays if it is an old reading")
         <<< SwitchRow("writeCalendarEvent"){ row in
             row.title = "BG to Calendar"
             row.value = UserDefaultsRepository.writeCalendarEvent.value
@@ -264,6 +271,22 @@ class SettingsViewController: FormViewController {
                 guard let value = row.value else { return }
                 UserDefaultsRepository.calendarIdentifier.value = value
         }
+        <<< TextRow("watchLine1"){ row in
+            row.title = "Line 1"
+            row.hidden = "$writeCalendarEvent == false"
+            row.value = UserDefaultsRepository.watchLine1.value
+        }.onChange { row in
+            guard let value = row.value else { return }
+            UserDefaultsRepository.watchLine1.value = value.lowercased()
+        }
+        <<< TextRow("watchLine2"){ row in
+            row.title = "Line 2"
+            row.hidden = "$writeCalendarEvent == false"
+            row.value = UserDefaultsRepository.watchLine2.value
+        }.onChange { row in
+            guard let value = row.value else { return }
+            UserDefaultsRepository.watchLine2.value = value.lowercased()
+        }
     }
  
 

+ 4 - 0
LoopFollow/repository/UserDefaults.swift

@@ -29,6 +29,8 @@ class UserDefaultsRepository {
     static let appBadge = UserDefaultsValue<Bool>(key: "appBadge", default: true)
     static let dimScreenWhenIdle = UserDefaultsValue<Int>(key: "dimScreenWhenIdle", default: 0)
     static let forceDarkMode = UserDefaultsValue<Bool>(key: "forceDarkMode", default: true)
+    static let persistentNotification = UserDefaultsValue<Bool>(key: "persistentNotification", default: false)
+    static let persistentNotificationLastBGTime = UserDefaultsValue<TimeInterval>(key: "persistentNotificationLastBGTime", default: 0)
     static let screenlockSwitchState = UserDefaultsValue<Bool>(
         key: "screenlockSwitchState",
         default: UIApplication.shared.isIdleTimerDisabled,
@@ -42,6 +44,8 @@ class UserDefaultsRepository {
     static let savedEventID = UserDefaultsValue<String>(key: "savedEventID", default: "")
     static let lastCalendarStartDate = UserDefaultsValue<Date?>(key: "lastCalendarStartDate", default: nil)
     static let writeCalendarEvent = UserDefaultsValue<Bool>(key: "writeCalendarEvent", default: false)
+    static let watchLine1 = UserDefaultsValue<String>(key: "watchLine1", default: "%BG% %DIRECTION% %DELTA% %MINAGO%")
+    static let watchLine2 = UserDefaultsValue<String>(key: "watchLine2", default: "C:%COB% I:%IOB% B:%BASAL%")
     
     // Alarm Settings
     static let systemOutputVolume = UserDefaultsValue<Float>(key: "systemOutputVolume", default: 0.5)