Jose Paredes 6 лет назад
Родитель
Сommit
a7d3836ebf

+ 12 - 0
LoopFollow.xcodeproj/project.pbxproj

@@ -8,6 +8,7 @@
 
 /* Begin PBXBuildFile section */
 		3F1335F351590E573D8E6962 /* Pods_LoopFollow.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A7D55B42A22051DAD69E89D0 /* Pods_LoopFollow.framework */; };
+		DD98F54424BCEFEE0007425A /* ShareClientExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD98F54324BCEFEE0007425A /* ShareClientExtension.swift */; };
 		FC16A97A24996673003D6245 /* NightScout.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC16A97924996673003D6245 /* NightScout.swift */; };
 		FC16A97B249966A3003D6245 /* AlarmSound.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC7CE589248ABEA3001F83B8 /* AlarmSound.swift */; };
 		FC16A97D24996747003D6245 /* Alarms.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC16A97C24996747003D6245 /* Alarms.swift */; };
@@ -162,6 +163,7 @@
 /* Begin PBXFileReference section */
 		059B0FA59AABFE72FE13DDDA /* Pods-LoopFollow.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-LoopFollow.release.xcconfig"; path = "Target Support Files/Pods-LoopFollow/Pods-LoopFollow.release.xcconfig"; sourceTree = "<group>"; };
 		A7D55B42A22051DAD69E89D0 /* Pods_LoopFollow.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_LoopFollow.framework; sourceTree = BUILT_PRODUCTS_DIR; };
+		DD98F54324BCEFEE0007425A /* ShareClientExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareClientExtension.swift; sourceTree = "<group>"; };
 		ECA3EFB4037410B4973BB632 /* Pods-LoopFollow.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-LoopFollow.debug.xcconfig"; path = "Target Support Files/Pods-LoopFollow/Pods-LoopFollow.debug.xcconfig"; sourceTree = "<group>"; };
 		FC16A97924996673003D6245 /* NightScout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NightScout.swift; sourceTree = "<group>"; };
 		FC16A97C24996747003D6245 /* Alarms.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Alarms.swift; sourceTree = "<group>"; };
@@ -350,6 +352,14 @@
 			path = Pods;
 			sourceTree = "<group>";
 		};
+		DD98F54224BCEF190007425A /* Extensions */ = {
+			isa = PBXGroup;
+			children = (
+				DD98F54324BCEFEE0007425A /* ShareClientExtension.swift */,
+			);
+			path = Extensions;
+			sourceTree = "<group>";
+		};
 		FC16A97624995FEE003D6245 /* Application */ = {
 			isa = PBXGroup;
 			children = (
@@ -513,6 +523,7 @@
 				FC8DEEE62485D1ED0075863F /* Info.plist */,
 				FC7CE59A248D334B001F83B8 /* Resources */,
 				FCC68871248A736700A0279D /* ViewControllers */,
+				DD98F54224BCEF190007425A /* Extensions */,
 				FC16A9782499657E003D6245 /* Controllers */,
 				FCC688542489367300A0279D /* helpers */,
 				FCC68872248A748A00A0279D /* Models */,
@@ -841,6 +852,7 @@
 				FCC0FAC224922A22003E610E /* DictionaryKeyPath.swift in Sources */,
 				FC9788182485969B00A7906C /* AppDelegate.swift in Sources */,
 				FCC6886B24898FD800A0279D /* ObservationToken.swift in Sources */,
+				DD98F54424BCEFEE0007425A /* ShareClientExtension.swift in Sources */,
 				FCC6886D2489909D00A0279D /* AnyConvertible.swift in Sources */,
 				FC97881A2485969B00A7906C /* SceneDelegate.swift in Sources */,
 				FCD49B6C24AA536E007879DC /* DebugViewController.swift in Sources */,

+ 58 - 0
LoopFollow/Extensions/ShareClientExtension.swift

@@ -0,0 +1,58 @@
+//
+//  ShareClientExtension.swift
+//  LoopFollow
+//
+//  Created by Jose Paredes on 7/13/20.
+//  Copyright © 2020 Jon Fawcett. All rights reserved.
+//
+
+import Foundation
+import ShareClient
+
+public struct ShareGlucoseData: Codable {
+   var sgv: Int
+   var date: TimeInterval
+   var direction: String?
+}
+
+private var TrendTable: [String] = [
+   "NONE",            // 0
+   "DoubleUp",        // 1
+   "SingleUp",        // 2
+   "FourtyFiveUp",    // 3
+   "Flat",            // 4
+   "FourtyFiveDown",  // 5
+   "SingleDown",      // 6
+   "DoubleDown"       // 7
+]
+
+// TODO: probably better to make this an inherited class rather than an extension
+extension ShareClient {
+
+    public func fetchData(_ entries: Int, callback: @escaping (ShareError?, [ShareGlucoseData]?) -> Void) {
+        
+        self.fetchLast(entries) { (error, result) -> () in
+            guard error == nil || result != nil else {
+                return callback(error, nil)
+            }
+            
+            // parse data to conanical form
+            var shareData = [ShareGlucoseData]()
+            for i in 0..<result!.count {
+                
+                var trend = Int(result![i].trend)
+                if(trend < 0 || trend > TrendTable.count-1) {
+                    trend = 0
+                }
+            
+                let newShareData = ShareGlucoseData(
+                    sgv: Int(result![i].glucose),
+                    date: result![i].timestamp.timeIntervalSince1970,
+                    direction: TrendTable[trend]
+                )
+                shareData.append(newShareData)
+            }
+            callback(nil,shareData)
+         }
+    }
+}

+ 19 - 6
LoopFollow/ViewControllers/MainViewController.swift

@@ -111,13 +111,15 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
     var lastOverrideStartTime: TimeInterval = 0
     var lastOverrideEndTime: TimeInterval = 0
     
+    // share
+    var bgDataShare: [ShareGlucoseData] = []
+    var dexShare: ShareClient?;
+    
     // calendar setup
     let store = EKEventStore()
     
     var snoozeTabItem: UITabBarItem = UITabBarItem()
     
-    var dexShare: ShareClient?;
-    
     override func viewDidLoad() {
         super.viewDidLoad()
         
@@ -174,11 +176,15 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
         // Load Data
         if UserDefaultsRepository.url.value != "" && firstGraphLoad {
             nightscoutLoader()
-            dexShare?.fetchLast(150) { (err, result) -> () in
-               print("Share: \(result)")
+          
+            // TODO: move this to MainViewController extension ?
+            dexShare?.fetchData(262) { (err, result) -> () in
+                
+                // TODO: add error checking
+                self.bgDataShare = result!
+                // print("\(self.bgDataShare)")
             }
         }
-        
     }
     
     override func viewWillAppear(_ animated: Bool) {
@@ -187,7 +193,6 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
         
     }
     
-    
     // Info Table Functions
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return tableData.count
@@ -302,6 +307,14 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
 //        if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Main timer ended") }
         updateMinAgo()
         nightscoutLoader()
+        
+        // TODO: move this to MainViewController extension ?
+        dexShare?.fetchData(262) { (err, result) -> () in
+            
+            // TODO: add error checking
+            self.bgDataShare = result!
+            // print("\(self.bgDataShare)")
+        }
     }
 
     //update Min Ago Text. We need to call this separately because it updates between readings