@@ -11,128 +11,12 @@ import WatchKit
1111import WatchConnectivity
1212
1313// This class handles values that are passed from the ios app.
14- class AppMessageService : NSObject , WCSessionDelegate {
14+ class AppMessageService {
1515
1616 static let singleton = AppMessageService ( )
17-
18- // request the baseUri from the iosApp and stores the result in the UserDefaultsRepository
19- func requestBaseUri( ) {
2017
21- if WCSession . isSupported ( ) {
22-
23- let session = WCSession . default
24-
25- if session. isReachable {
26- session. sendMessage ( [ " requestBaseUri " : " " ] , replyHandler: { ( response) -> Void in
27-
28- if let baseUri = response. first? . 1 {
29- UserDefaultsRepository . saveBaseUri ( String ( describing: baseUri) )
30- }
31- } , errorHandler: { ( error) -> Void in
32- print ( error)
33- } )
34- }
35- }
36- }
37-
18+ // send a dummy message to keep the phone app awake (start the app if is not started)
3819 func keepAwakePhoneApp( ) {
39-
40- // send a dummy message to keep the phone app awake (start the app if is not started)
41- if WCSession . isSupported ( ) {
42-
43- let session = WCSession . default
44- if session. isReachable {
45- session. sendMessage ( [ " keepAwake " : " " ] , replyHandler: nil ) { error in
46- print ( error)
47- BackgroundRefreshLogger . info ( " Error received while trying to awake phone app: \( error) " )
48- }
49- }
50- }
51- }
52-
53- func updateValuesFromApplicationContext( _ applicationContext: [ String : AnyObject ] ) {
54- if let units = applicationContext [ " units " ] as? String {
55- UserDefaultsRepository . saveUnits ( Units ( rawValue: units) !)
56- }
57-
58- if let hostUri = applicationContext [ " hostUri " ] as? String {
59- UserDefaultsRepository . saveBaseUri ( hostUri)
60- }
61-
62- if let showRawBG = applicationContext [ " showRawBG " ] as? Bool {
63- UserDefaultsRepository . saveShowRawBG ( showRawBG)
64- }
65-
66- var shouldRepaintCharts = false
67- if let alertIfAboveValue = applicationContext [ " alertIfAboveValue " ] as? Float {
68- let defaults = UserDefaults ( suiteName: AppConstants . APP_GROUP_ID)
69- defaults!. setValue ( alertIfAboveValue, forKey: " alertIfAboveValue " )
70- AlarmRule . alertIfAboveValue = alertIfAboveValue
71-
72- shouldRepaintCharts = true
73- }
74-
75- if let alertIfBelowValue = applicationContext [ " alertIfBelowValue " ] as? Float {
76- let defaults = UserDefaults ( suiteName: AppConstants . APP_GROUP_ID)
77- defaults!. setValue ( alertIfBelowValue, forKey: " alertIfBelowValue " )
78- AlarmRule . alertIfBelowValue = alertIfBelowValue
79-
80- shouldRepaintCharts = true
81- }
82-
83- if shouldRepaintCharts {
84- if #available( watchOSApplicationExtension 3 . 0 , * ) {
85- if let interfaceController = WKExtension . shared ( ) . rootInterfaceController as? InterfaceController {
86- if WKExtension . shared ( ) . applicationState == . active {
87- DispatchQueue . main. async {
88- interfaceController. loadAndPaintChartData ( forceRepaint: true )
89- }
90- } else {
91- interfaceController. shouldRepaintChartsOnActivation = true
92- }
93- }
94- }
95- }
96-
97- if let _ = applicationContext [ " nightscoutData " ] {
98- if #available( watchOSApplicationExtension 3 . 0 , * ) {
99- if let extensionDelegate = WKExtension . shared ( ) . delegate as? ExtensionDelegate {
100- DispatchQueue . main. async {
101- extensionDelegate. handleNightscoutDataMessage ( applicationContext)
102- }
103- }
104- }
105- }
106- }
107-
108- @available ( watchOSApplicationExtension 2 . 2 , * )
109- func session( _ session: WCSession , activationDidCompleteWith activationState: WCSessionActivationState , error: Error ? ) {
110-
111- DispatchQueue . main. async { ( ) -> Void in
112-
113- self . updateValuesFromApplicationContext ( session. receivedApplicationContext as [ String : AnyObject ] )
114- }
115- }
116-
117- /** Called on the delegate of the receiver. Will be called on startup if an applicationContext is available. */
118- @available ( watchOS 2 . 0 , * )
119- func session( _ session: WCSession , didReceiveApplicationContext applicationContext: [ String : Any ] ) {
120- updateValuesFromApplicationContext ( applicationContext as [ String : AnyObject ] )
121- }
122-
123- /** Called on the delegate of the receiver. Will be called on startup if the user info finished transferring when the receiver was not running. */
124- @available ( watchOS 2 . 0 , * )
125- func session( _ session: WCSession , didReceiveUserInfo userInfo: [ String : Any ] ) {
126- updateValuesFromApplicationContext ( userInfo as [ String : AnyObject ] )
127- }
128-
129- @available ( watchOS 2 . 0 , * )
130- func session( _ session: WCSession , didReceiveMessage message: [ String : Any ] ) {
131- updateValuesFromApplicationContext ( message as [ String : AnyObject ] )
132- }
133-
134- @available ( watchOS 2 . 0 , * )
135- func session( _ session: WCSession , didReceiveMessage message: [ String : Any ] , replyHandler: @escaping ( [ String : Any ] ) -> Swift . Void ) {
136- updateValuesFromApplicationContext ( message as [ String : AnyObject ] )
20+ KeepAwakeMessage ( ) . send ( )
13721 }
13822}
0 commit comments