Skip to content

Commit ee5dab7

Browse files
committed
watchOS 3 support (#185)
* watchOS 3 background tasks WIP * Adding complication gallery bundle, and more background task comments * More rearchitecture of the watch app, WIP * Refactor bolus / carb sending * Changes to WCSession activation and observation * Fixing bolus, debugging complication * Additional comments, and a TODO * Removing unused code, and more comments
1 parent 1d0c67c commit ee5dab7

19 files changed

+393
-375
lines changed

Loop.xcodeproj/project.pbxproj

Lines changed: 23 additions & 16 deletions
Large diffs are not rendered by default.
File renamed without changes.

Loop/Extensions/NSData.swift

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"class" : "CLKComplicationTemplateModularSmallStackText",
3+
"highlightLine2" : false,
4+
"line2TextProvider" : {
5+
"class" : "CLKLocalizableSimpleTextProvider",
6+
"text" : "mg\/dL"
7+
},
8+
"line1TextProvider" : {
9+
"shortText" : "--",
10+
"class" : "CLKSimpleTextProvider",
11+
"text" : "--",
12+
"accessibilityLabel" : "No glucose value available"
13+
},
14+
"version" : 30000
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
ckcomplication.strings
3+
Loop
4+
5+
Created by Nate Racklyeft on 9/18/16.
6+
Copyright © 2016 Nathan Racklyeft. All rights reserved.
7+
*/
8+
9+
/* The complication template example unit string */
10+
"mg/dL" = "mg/dL"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"supported complication families" : {
3+
"0" : "A307227B-6EFF-4242-A538-2C9AC617E041.json"
4+
},
5+
"client ID" : "com.loudnate.Loop.watchkitapp.watchkitextension"
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
ckcomplication.strings
3+
Loop
4+
5+
Created by Nate Racklyeft on 9/18/16.
6+
Copyright © 2016 Nathan Racklyeft. All rights reserved.
7+
*/
8+
9+
/* The complication template example unit string */
10+
"mg/dL" = "mg/dL"

WatchApp Extension/ComplicationController.swift

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
import ClockKit
10+
import WatchKit
1011

1112

1213
final class ComplicationController: NSObject, CLKComplicationDataSource {
@@ -18,15 +19,15 @@ final class ComplicationController: NSObject, CLKComplicationDataSource {
1819
}
1920

2021
func getTimelineStartDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
21-
if let date = DeviceDataManager.sharedManager.lastContextData?.glucoseDate {
22+
if let date = ExtensionDelegate.shared().lastContext?.glucoseDate {
2223
handler(date as Date)
2324
} else {
2425
handler(nil)
2526
}
2627
}
2728

2829
func getTimelineEndDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
29-
if let date = DeviceDataManager.sharedManager.lastContextData?.glucoseDate {
30+
if let date = ExtensionDelegate.shared().lastContext?.glucoseDate {
3031
handler(date as Date)
3132
} else {
3233
handler(nil)
@@ -45,7 +46,7 @@ final class ComplicationController: NSObject, CLKComplicationDataSource {
4546

4647
switch complication.family {
4748
case .modularSmall:
48-
if let context = DeviceDataManager.sharedManager.lastContextData,
49+
if let context = ExtensionDelegate.shared().lastContext,
4950
let glucose = context.glucose,
5051
let unit = context.preferredGlucoseUnit,
5152
let glucoseString = formatter.string(from: NSNumber(value: glucose.doubleValue(for: unit))),
@@ -68,7 +69,7 @@ final class ComplicationController: NSObject, CLKComplicationDataSource {
6869

6970
func getTimelineEntries(for complication: CLKComplication, after date: Date, limit: Int, withHandler handler: (@escaping ([CLKComplicationTimelineEntry]?) -> Void)) {
7071
// Call the handler with the timeline entries after to the given date
71-
if let context = DeviceDataManager.sharedManager.lastContextData,
72+
if let context = ExtensionDelegate.shared().lastContext,
7273
let glucose = context.glucose,
7374
let unit = context.preferredGlucoseUnit,
7475
let glucoseString = formatter.string(from: NSNumber(value: glucose.doubleValue(for: unit))),
@@ -81,35 +82,19 @@ final class ComplicationController: NSObject, CLKComplicationDataSource {
8182
}
8283
}
8384

84-
func requestedUpdateDidBegin() {
85-
DeviceDataManager.sharedManager.updateComplicationDataIfNeeded()
86-
}
87-
88-
func requestedUpdateBudgetExhausted() {
89-
// TODO: os_log_info in iOS 10
90-
}
91-
92-
// MARK: - Update Scheduling
93-
94-
func getNextRequestedUpdateDate(handler: @escaping (Date?) -> Void) {
95-
// Call the handler with the date when you would next like to be given the opportunity to update your complication content
96-
handler(Date(timeIntervalSinceNow: TimeInterval(2 * 60 * 60)))
97-
}
98-
9985
// MARK: - Placeholder Templates
100-
101-
func getPlaceholderTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
86+
87+
func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
10288
switch complication.family {
10389
case .modularSmall:
10490
let template = CLKComplicationTemplateModularSmallStackText()
10591

10692
template.line1TextProvider = CLKSimpleTextProvider(text: "--", shortText: "--", accessibilityLabel: "No glucose value available")
107-
template.line2TextProvider = CLKSimpleTextProvider(text: "mg/dL")
93+
template.line2TextProvider = CLKSimpleTextProvider.localizableTextProvider(withStringsFileTextKey: "mg/dL")
10894

10995
handler(template)
11096
default:
11197
handler(nil)
11298
}
11399
}
114-
115100
}

WatchApp Extension/Controllers/AddCarbsInterfaceController.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
import WatchKit
10-
import Foundation
10+
import WatchConnectivity
1111

1212

1313
final class AddCarbsInterfaceController: WKInterfaceController, IdentifiableClass {
@@ -98,7 +98,23 @@ final class AddCarbsInterfaceController: WKInterfaceController, IdentifiableClas
9898
if carbValue > 0 {
9999
let entry = CarbEntryUserInfo(value: Double(carbValue), absorptionTimeType: absorptionTime, startDate: Date())
100100

101-
DeviceDataManager.sharedManager.sendCarbEntry(entry)
101+
do {
102+
try WCSession.default().sendCarbEntryMessage(entry,
103+
replyHandler: { (suggestion) in
104+
WKExtension.shared().rootInterfaceController?.presentController(withName: BolusInterfaceController.className, context: suggestion)
105+
},
106+
errorHandler: { (error) in
107+
ExtensionDelegate.shared().present(error)
108+
}
109+
)
110+
} catch {
111+
presentAlert(withTitle: NSLocalizedString("Send Failed", comment: "The title of the alert controller displayed after a carb entry send attempt fails"),
112+
message: NSLocalizedString("Make sure your iPhone is nearby and try again", comment: "The recovery message displayed after a carb entry send attempt fails"),
113+
preferredStyle: .alert,
114+
actions: [WKAlertAction.dismissAction()]
115+
)
116+
return
117+
}
102118
}
103119

104120
dismiss()

WatchApp Extension/Controllers/BolusInterfaceController.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import WatchKit
1010
import Foundation
11+
import WatchConnectivity
1112

1213

1314
final class BolusInterfaceController: WKInterfaceController, IdentifiableClass {
@@ -130,16 +131,19 @@ final class BolusInterfaceController: WKInterfaceController, IdentifiableClass {
130131
@IBAction func deliver() {
131132
if bolusValue > 0 {
132133
let bolus = SetBolusUserInfo(value: bolusValue, startDate: Date())
134+
133135
do {
134-
try DeviceDataManager.sharedManager.sendSetBolus(bolus)
135-
} catch DeviceDataManagerError.reachabilityError {
136-
presentAlert(withTitle: NSLocalizedString("Bolus Failed", comment: "The title of the alert controller displayed after a bolus attempt fails"),
136+
try WCSession.default().sendBolusMessage(bolus) { (error) in
137+
ExtensionDelegate.shared().present(error)
138+
}
139+
} catch {
140+
presentAlert(
141+
withTitle: NSLocalizedString("Bolus Failed", comment: "The title of the alert controller displayed after a bolus attempt fails"),
137142
message: NSLocalizedString("Make sure your iPhone is nearby and try again", comment: "The recovery message displayed after a bolus attempt fails"),
138143
preferredStyle: .alert,
139144
actions: [WKAlertAction.dismissAction()]
140145
)
141146
return
142-
} catch {
143147
}
144148
}
145149

0 commit comments

Comments
 (0)