77//
88
99import UIKit
10- import xDripG5
10+ import CGMBLEKit
1111import CoreBluetooth
1212
1313@UIApplicationMain
14- class AppDelegate : UIResponder , UIApplicationDelegate , TransmitterDelegate {
14+ class AppDelegate : UIResponder , UIApplicationDelegate , TransmitterDelegate , TransmitterCommandSource {
1515
1616 var window : UIWindow ?
1717
1818 static var sharedDelegate : AppDelegate {
1919 return UIApplication . shared. delegate as! AppDelegate
2020 }
2121
22+ var transmitterID : String ? {
23+ didSet {
24+ if let id = transmitterID {
25+ transmitter = Transmitter (
26+ id: id,
27+ passiveModeEnabled: UserDefaults . standard. passiveModeEnabled
28+ )
29+ transmitter? . stayConnected = UserDefaults . standard. stayConnected
30+ transmitter? . delegate = self
31+ transmitter? . commandSource = self
32+
33+ UserDefaults . standard. transmitterID = id
34+ }
35+ glucose = nil
36+ }
37+ }
38+
2239 var transmitter : Transmitter ?
2340
41+ let commandQueue = CommandQueue ( )
42+
43+ var glucose : Glucose ?
44+
2445 func application( _ application: UIApplication , didFinishLaunchingWithOptions launchOptions: [ UIApplicationLaunchOptionsKey : Any ] ? ) -> Bool {
2546
26- transmitter = Transmitter (
27- ID: UserDefaults . standard. transmitterID,
28- passiveModeEnabled: UserDefaults . standard. passiveModeEnabled
29- )
30- transmitter? . stayConnected = UserDefaults . standard. stayConnected
31- transmitter? . delegate = self
47+ transmitterID = UserDefaults . standard. transmitterID
3248
3349 return true
3450 }
@@ -42,7 +58,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, TransmitterDelegate {
4258 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
4359 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
4460
45- if let transmitter = transmitter , !transmitter. stayConnected {
61+ if let transmitter = transmitter, !transmitter. stayConnected {
4662 transmitter. stopScanning ( )
4763 }
4864 }
@@ -72,27 +88,48 @@ class AppDelegate: UIResponder, UIApplicationDelegate, TransmitterDelegate {
7288 return dateFormatter
7389 } ( )
7490
91+ func dequeuePendingCommand( for transmitter: Transmitter ) -> Command ? {
92+ return commandQueue. dequeue ( )
93+ }
94+
95+ func transmitter( _ transmitter: Transmitter , didFail command: Command , with error: Error ) {
96+ // TODO: implement
97+ }
98+
99+ func transmitter( _ transmitter: Transmitter , didComplete command: Command ) {
100+ // TODO: implement
101+ }
102+
75103 func transmitter( _ transmitter: Transmitter , didError error: Error ) {
76- if let vc = window ? . rootViewController as? TransmitterDelegate {
77- DispatchQueue . main . async {
104+ DispatchQueue . main . async {
105+ if let vc = self . window ? . rootViewController as? TransmitterDelegate {
78106 vc. transmitter ( transmitter, didError: error)
79107 }
80108 }
81109 }
82110
83111 func transmitter( _ transmitter: Transmitter , didRead glucose: Glucose ) {
84- if let vc = window? . rootViewController as? TransmitterDelegate {
85- DispatchQueue . main. async {
112+ self . glucose = glucose
113+ DispatchQueue . main. async {
114+ if let vc = self . window? . rootViewController as? TransmitterDelegate {
86115 vc. transmitter ( transmitter, didRead: glucose)
87116 }
88117 }
89118 }
90119
91120 func transmitter( _ transmitter: Transmitter , didReadUnknownData data: Data ) {
92- if let vc = window ? . rootViewController as? TransmitterDelegate {
93- DispatchQueue . main . async {
121+ DispatchQueue . main . async {
122+ if let vc = self . window ? . rootViewController as? TransmitterDelegate {
94123 vc. transmitter ( transmitter, didReadUnknownData: data)
95124 }
96125 }
97126 }
127+
128+ func transmitter( _ transmitter: Transmitter , didReadBackfill glucose: [ Glucose ] ) {
129+ DispatchQueue . main. async {
130+ if let vc = self . window? . rootViewController as? TransmitterDelegate {
131+ vc. transmitter ( transmitter, didReadBackfill: glucose)
132+ }
133+ }
134+ }
98135}
0 commit comments