Ver código fonte

Merge pull request #285 from loopandlearn/indicate-stale-badge-data2

Clear badge counter if bg data is stale
Marion Barker 2 anos atrás
pai
commit
5a2d88ab13

+ 5 - 2
LoopFollow/Controllers/Nightscout/BGData.swift

@@ -176,8 +176,9 @@ extension MainViewController {
                 self.startBGTimer(time: 300 - secondsAgo + Double(UserDefaultsRepository.bgUpdateDelay.value))
                 self.startBGTimer(time: 300 - secondsAgo + Double(UserDefaultsRepository.bgUpdateDelay.value))
                 let timerVal = 310 - secondsAgo
                 let timerVal = 310 - secondsAgo
                 print("##### started 5:10 bg timer: \(timerVal)")
                 print("##### started 5:10 bg timer: \(timerVal)")
-                self.updateBadge(val: data[0].sgv)
-                self.evaluateSpeakConditions(currentValue: data[0].sgv, previousValue: data[1].sgv)
+                if data.count > 1 {
+                    self.evaluateSpeakConditions(currentValue: data[0].sgv, previousValue: data[1].sgv)
+                }
             }
             }
         }
         }
         
         
@@ -276,8 +277,10 @@ extension MainViewController {
             attributeString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: attributeString.length))
             attributeString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: attributeString.length))
             if deltaTime >= 12 { // Data is stale
             if deltaTime >= 12 { // Data is stale
                 attributeString.addAttribute(.strikethroughColor, value: UIColor.systemRed, range: NSRange(location: 0, length: attributeString.length))
                 attributeString.addAttribute(.strikethroughColor, value: UIColor.systemRed, range: NSRange(location: 0, length: attributeString.length))
+                self.updateBadge(val: 0)
             } else { // Data is fresh
             } else { // Data is fresh
                 attributeString.addAttribute(.strikethroughColor, value: UIColor.clear, range: NSRange(location: 0, length: attributeString.length))
                 attributeString.addAttribute(.strikethroughColor, value: UIColor.clear, range: NSRange(location: 0, length: attributeString.length))
+                self.updateBadge(val: latestBG)
             }
             }
             self.BGText.attributedText = attributeString
             self.BGText.attributedText = attributeString
             
             

+ 2 - 4
LoopFollow/ViewControllers/MainViewController.swift

@@ -473,13 +473,11 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
     }
     }
     
     
     func updateBadge(val: Int) {
     func updateBadge(val: Int) {
-        DispatchQueue.main.async {
         if UserDefaultsRepository.appBadge.value {
         if UserDefaultsRepository.appBadge.value {
             let latestBG = String(val)
             let latestBG = String(val)
             UIApplication.shared.applicationIconBadgeNumber = Int(bgUnits.removePeriodAndCommaForBadge(bgUnits.toDisplayUnits(latestBG))) ?? val
             UIApplication.shared.applicationIconBadgeNumber = Int(bgUnits.removePeriodAndCommaForBadge(bgUnits.toDisplayUnits(latestBG))) ?? val
-            } else {
-                UIApplication.shared.applicationIconBadgeNumber = 0
-            }
+        } else {
+            UIApplication.shared.applicationIconBadgeNumber = 0
         }
         }
     }
     }