Procházet zdrojové kódy

Merge branch 'master' into Dev

jonfawcett před 6 roky
rodič
revize
ac5d859410

+ 67 - 25
LoopFollow/Controllers/NightScout.swift

@@ -81,6 +81,7 @@ extension MainViewController {
     
     // NS BG Data Web call
     func webLoadNSBGData(onlyPullLastRecord: Bool = false) {
+
         // Set the count= in the url either to pull 24 hours or only the last record
         var points = "1"
         if !onlyPullLastRecord {
@@ -103,15 +104,18 @@ extension MainViewController {
         request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalCacheData
 
         // Downloader
+        chartDispatch.enter()
         let getBGTask = URLSession.shared.dataTask(with: request) { data, response, error in
             if self.consoleLogging == true {print("start bg url")}
             guard error == nil else {
-
+                self.chartDispatch.leave()
                 return
+                
             }
             guard let data = data else {
-
+                self.chartDispatch.leave()
                 return
+                
             }
 
             let decoder = JSONDecoder()
@@ -120,9 +124,12 @@ extension MainViewController {
                 DispatchQueue.main.async {
                     // trigger the processor for the data after downloading.
                     self.ProcessNSBGData(data: entriesResponse, onlyPullLastRecord: onlyPullLastRecord)
+                    self.chartDispatch.leave()
                 }
             } else {
+                self.chartDispatch.leave()
                 return
+                
             }
         }
         getBGTask.resume()
@@ -226,27 +233,36 @@ extension MainViewController {
             return
           }
           if consoleLogging == true {print("entered device status task.")}
-          var requestDeviceStatus = URLRequest(url: urlDeviceStatus)
-          requestDeviceStatus.cachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalCacheData
-          let deviceStatusTask = URLSession.shared.dataTask(with: requestDeviceStatus) { data, response, error in
-          if self.consoleLogging == true {print("in device status loop.")}
-          guard error == nil else {
-            return
-          }
-          guard let data = data else {
-            return
-          }
-              
-              let json = try? (JSONSerialization.jsonObject(with: data) as! [[String:AnyObject]])
-          if let json = json {
-              DispatchQueue.main.async {
-                  self.updateDeviceStatusDisplay(jsonDeviceStatus: json)
-              }
-          } else {
-            return
-          }
-          if self.consoleLogging == true {print("finish pump update")}}
-          deviceStatusTask.resume()
+          
+            
+            var requestDeviceStatus = URLRequest(url: urlDeviceStatus)
+            requestDeviceStatus.cachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalCacheData
+            
+            self.chartDispatch.enter()
+            let deviceStatusTask = URLSession.shared.dataTask(with: requestDeviceStatus) { data, response, error in
+            if self.consoleLogging == true {print("in device status loop.")}
+            guard error == nil else {
+                self.chartDispatch.leave()
+                return
+            }
+            guard let data = data else {
+                self.chartDispatch.leave()
+                return
+            }
+
+
+            let json = try? (JSONSerialization.jsonObject(with: data) as! [[String:AnyObject]])
+            if let json = json {
+                DispatchQueue.main.async {
+                    self.updateDeviceStatusDisplay(jsonDeviceStatus: json)
+                    self.chartDispatch.leave()
+                }
+            } else {
+                self.chartDispatch.leave()
+                return
+            }
+            if self.consoleLogging == true {print("finish pump update")}}
+            deviceStatusTask.resume()
       }
       
       // NS Device Status Response Processor
@@ -514,6 +530,7 @@ extension MainViewController {
        
       // NS Profile Web Call
       func webLoadNSProfile() {
+
         let urlString = UserDefaultsRepository.url.value + "/api/v1/profile/current.json"
           let escapedAddress = urlString.addingPercentEncoding(withAllowedCharacters:NSCharacterSet.urlQueryAllowed)
           guard let url = URL(string: escapedAddress!) else {
@@ -522,11 +539,14 @@ extension MainViewController {
           
           var request = URLRequest(url: url)
           request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalCacheData
-          let task = URLSession.shared.dataTask(with: request) { data, response, error in
+            self.chartDispatch.enter()
+            let task = URLSession.shared.dataTask(with: request) { data, response, error in
               guard error == nil else {
+                self.chartDispatch.leave()
                 return
               }
               guard let data = data else {
+                self.chartDispatch.leave()
                 return
               }
               
@@ -536,8 +556,10 @@ extension MainViewController {
               if let json = json {
                   DispatchQueue.main.async {
                       self.updateProfile(jsonDeviceStatus: json)
+                    self.chartDispatch.leave()
                   }
               } else {
+                self.chartDispatch.leave()
                 return
               }
           }
@@ -563,6 +585,7 @@ extension MainViewController {
       
         // NS Temp Basal Web Call
       func WebLoadNSTempBasals() {
+
         let yesterdayString = dateTimeUtils.nowMinus24HoursTimeInterval()
 
         var urlString = UserDefaultsRepository.url.value + "/api/v1/treatments.json?find[eventType][$eq]=Temp%20Basal&find[created_at][$gte]=" + yesterdayString
@@ -577,12 +600,15 @@ extension MainViewController {
           
             var request = URLRequest(url: urlData)
             request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalCacheData
+                self.chartDispatch.enter()
             let task = URLSession.shared.dataTask(with: request) { data, response, error in
            
                 guard error == nil else {
+                   self.chartDispatch.leave()
                     return
                 }
                 guard let data = data else {
+                    self.chartDispatch.leave()
                     return
                 }
                     
@@ -590,8 +616,10 @@ extension MainViewController {
                 if let json = json {
                     DispatchQueue.main.async {
                         self.updateBasals(entries: json)
+                        self.chartDispatch.leave()
                     }
                 } else {
+                   self.chartDispatch.leave()
                     return
                 }
             }
@@ -723,6 +751,7 @@ extension MainViewController {
     
     // NS Bolus Web Call
       func webLoadNSBoluses(){
+
         let yesterdayString = dateTimeUtils.nowMinus24HoursTimeInterval()
         let urlUser = UserDefaultsRepository.url.value
           var searchString = "find[eventType]=Correction%20Bolus&find[created_at][$gte]=" + yesterdayString
@@ -739,12 +768,15 @@ extension MainViewController {
           }
           var request = URLRequest(url: urlData)
            request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalCacheData
-           let task = URLSession.shared.dataTask(with: request) { data, response, error in
+        self.chartDispatch.enter()
+        let task = URLSession.shared.dataTask(with: request) { data, response, error in
           
                guard error == nil else {
+                self.chartDispatch.leave()
                 return
                }
                guard let data = data else {
+                self.chartDispatch.leave()
                 return
                }
                    
@@ -752,8 +784,10 @@ extension MainViewController {
                if let json = json {
                    DispatchQueue.main.async {
                        self.processNSBolus(entries: json)
+                    self.chartDispatch.leave()
                    }
                } else {
+                self.chartDispatch.leave()
                 return
                }
            }
@@ -788,6 +822,7 @@ extension MainViewController {
                 if dateTimeStamp < (dateTimeUtils.getNowTimeIntervalUTC() + (60 * 60)) {
                     bolusData.append(dot)
                 }
+
             }
 
            
@@ -796,6 +831,7 @@ extension MainViewController {
     
     // NS Carb Web Call
       func webLoadNSCarbs(){
+
         let yesterdayString = dateTimeUtils.nowMinus24HoursTimeInterval()
         let urlUser = UserDefaultsRepository.url.value
           var searchString = "find[eventType]=Meal%20Bolus&find[created_at][$gte]=" + yesterdayString
@@ -812,12 +848,15 @@ extension MainViewController {
           }
           var request = URLRequest(url: urlData)
            request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalCacheData
+        self.chartDispatch.enter()
            let task = URLSession.shared.dataTask(with: request) { data, response, error in
           
                guard error == nil else {
+                self.chartDispatch.leave()
                 return
                }
                guard let data = data else {
+                self.chartDispatch.leave()
                 return
                }
                    
@@ -825,8 +864,10 @@ extension MainViewController {
                if let json = json {
                    DispatchQueue.main.async {
                        self.processNSCarbs(entries: json)
+                    self.chartDispatch.leave()
                    }
                } else {
+                self.chartDispatch.leave()
                 return
                }
            }
@@ -861,6 +902,7 @@ extension MainViewController {
                  
                 if dateTimeStamp < (dateTimeUtils.getNowTimeIntervalUTC() + (60 * 60)) {
                  carbData.append(dot)
+
                 }
             }
 

+ 3 - 3
LoopFollow/ViewControllers/MainViewController.swift

@@ -152,9 +152,9 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
         infoTable.dataSource = self
         
         // Load Data
-        nightscoutLoader()
-        
-        
+        if UserDefaultsRepository.url.value != "" {
+            nightscoutLoader()
+        } 
     }
     
     override func viewWillAppear(_ animated: Bool) {

+ 2 - 1
LoopFollow/ViewControllers/SettingsViewController.swift

@@ -34,7 +34,8 @@ class SettingsViewController: FormViewController {
                 row.value = UserDefaultsRepository.url.value
             }.onChange { row in
                 guard let value = row.value else { return }
-                UserDefaultsRepository.url.value = value.lowercased()
+                let urlNSInput = value.replacingOccurrences(of: "\\s+$", with: "", options: .regularExpression)
+                UserDefaultsRepository.url.value = urlNSInput.lowercased()
                 }
             <<< TextRow(){ row in
                 row.title = "NS Token"

+ 8 - 2
readme.md

@@ -18,7 +18,7 @@ Special thanks to Spike-App, NSApple, and Nightguard for helping me figure out h
 If you want to contribute, the biggest needs today are to make the code cleaner and more efficient, get some major details like Bolus/Basal graph and mg/DL vs mmol added, and create the basis for a watch app.
 
 ### General feature list
-- scrollable/scalable graph display with standard Bg details plus Loop status, Loop Prediction, and the General NS Care portal info.
+- scrollable/scalable graph display with BG, basal, bolus, and carb details plus Loop status, Loop Prediction, and the General NS Care portal info.
 - option to override DND and system volume for all alerts.
 - snoozes per alert, presnooze, edit existing snooze, and snooze all alert settings.
 - the standard Low/High, Urgent Low/High, and missed reading alerts. High will have a persistence option that’s still to do. Eg high for x minutes.
@@ -29,8 +29,14 @@ If you want to contribute, the biggest needs today are to make the code cleaner
 - background silent audio to keep iOS from killing the app. It has a selectable refresh rate that should help reduce battery usage. This is why it can’t go in the App Store for just a simple download.
 
 ### New functionality under consideration
-- basal, carb, and insulin entries to the graph.
 - missed bolus alert.
 - watch app.
 - mmol support.
 - ability to pull from dex share for Bg. So it can automatically switch to dex BG display for those times when NS has delayed readings.
+
+### Contributing, Building, and Branches
+- New code will be pushed to the Dev branch as soon as it has been added. It might be very rough around the edges. Once it has been thoroughly tested, it will be merged to Master. If you are even remotely adventurous, please build Dev to help test the new features as they are added.
+- If you want to contribute, please PR on Dev unless it is an important bug fix to address in Master
+
+### Open Source DIY
+- This is a DIY open source project that may or may not function as you expect. You take full responsibility for building and running this app and do so at your own risk.