Skip to content

Commit

Permalink
watchOS 3 support (LoopKit#185)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
loudnate committed Sep 29, 2016
1 parent 1c8b1c6 commit 5806ace
Show file tree
Hide file tree
Showing 19 changed files with 393 additions and 375 deletions.
39 changes: 23 additions & 16 deletions Loop.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

File renamed without changes.
30 changes: 0 additions & 30 deletions Loop/Extensions/NSData.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"class" : "CLKComplicationTemplateModularSmallStackText",
"highlightLine2" : false,
"line2TextProvider" : {
"class" : "CLKLocalizableSimpleTextProvider",
"text" : "mg\/dL"
},
"line1TextProvider" : {
"shortText" : "--",
"class" : "CLKSimpleTextProvider",
"text" : "--",
"accessibilityLabel" : "No glucose value available"
},
"version" : 30000
}
10 changes: 10 additions & 0 deletions Loop/gallery.ckcomplication/Base.lproj/ckcomplication.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
ckcomplication.strings
Loop

Created by Nate Racklyeft on 9/18/16.
Copyright © 2016 Nathan Racklyeft. All rights reserved.
*/

/* The complication template example unit string */
"mg/dL" = "mg/dL"
6 changes: 6 additions & 0 deletions Loop/gallery.ckcomplication/complicationManifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"supported complication families" : {
"0" : "A307227B-6EFF-4242-A538-2C9AC617E041.json"
},
"client ID" : "com.loudnate.Loop.watchkitapp.watchkitextension"
}
10 changes: 10 additions & 0 deletions WatchApp Extension/Base.lproj/ckcomplication.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
ckcomplication.strings
Loop

Created by Nate Racklyeft on 9/18/16.
Copyright © 2016 Nathan Racklyeft. All rights reserved.
*/

/* The complication template example unit string */
"mg/dL" = "mg/dL"
31 changes: 8 additions & 23 deletions WatchApp Extension/ComplicationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import ClockKit
import WatchKit


final class ComplicationController: NSObject, CLKComplicationDataSource {
Expand All @@ -18,15 +19,15 @@ final class ComplicationController: NSObject, CLKComplicationDataSource {
}

func getTimelineStartDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
if let date = DeviceDataManager.sharedManager.lastContextData?.glucoseDate {
if let date = ExtensionDelegate.shared().lastContext?.glucoseDate {
handler(date as Date)
} else {
handler(nil)
}
}

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

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

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

func requestedUpdateDidBegin() {
DeviceDataManager.sharedManager.updateComplicationDataIfNeeded()
}

func requestedUpdateBudgetExhausted() {
// TODO: os_log_info in iOS 10
}

// MARK: - Update Scheduling

func getNextRequestedUpdateDate(handler: @escaping (Date?) -> Void) {
// Call the handler with the date when you would next like to be given the opportunity to update your complication content
handler(Date(timeIntervalSinceNow: TimeInterval(2 * 60 * 60)))
}

// MARK: - Placeholder Templates
func getPlaceholderTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {

func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
switch complication.family {
case .modularSmall:
let template = CLKComplicationTemplateModularSmallStackText()

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

handler(template)
default:
handler(nil)
}
}

}
20 changes: 18 additions & 2 deletions WatchApp Extension/Controllers/AddCarbsInterfaceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import WatchKit
import Foundation
import WatchConnectivity


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

DeviceDataManager.sharedManager.sendCarbEntry(entry)
do {
try WCSession.default().sendCarbEntryMessage(entry,
replyHandler: { (suggestion) in
WKExtension.shared().rootInterfaceController?.presentController(withName: BolusInterfaceController.className, context: suggestion)
},
errorHandler: { (error) in
ExtensionDelegate.shared().present(error)
}
)
} catch {
presentAlert(withTitle: NSLocalizedString("Send Failed", comment: "The title of the alert controller displayed after a carb entry send attempt fails"),
message: NSLocalizedString("Make sure your iPhone is nearby and try again", comment: "The recovery message displayed after a carb entry send attempt fails"),
preferredStyle: .alert,
actions: [WKAlertAction.dismissAction()]
)
return
}
}

dismiss()
Expand Down
12 changes: 8 additions & 4 deletions WatchApp Extension/Controllers/BolusInterfaceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import WatchKit
import Foundation
import WatchConnectivity


final class BolusInterfaceController: WKInterfaceController, IdentifiableClass {
Expand Down Expand Up @@ -130,16 +131,19 @@ final class BolusInterfaceController: WKInterfaceController, IdentifiableClass {
@IBAction func deliver() {
if bolusValue > 0 {
let bolus = SetBolusUserInfo(value: bolusValue, startDate: Date())

do {
try DeviceDataManager.sharedManager.sendSetBolus(bolus)
} catch DeviceDataManagerError.reachabilityError {
presentAlert(withTitle: NSLocalizedString("Bolus Failed", comment: "The title of the alert controller displayed after a bolus attempt fails"),
try WCSession.default().sendBolusMessage(bolus) { (error) in
ExtensionDelegate.shared().present(error)
}
} catch {
presentAlert(
withTitle: NSLocalizedString("Bolus Failed", comment: "The title of the alert controller displayed after a bolus attempt fails"),
message: NSLocalizedString("Make sure your iPhone is nearby and try again", comment: "The recovery message displayed after a bolus attempt fails"),
preferredStyle: .alert,
actions: [WKAlertAction.dismissAction()]
)
return
} catch {
}
}

Expand Down
56 changes: 0 additions & 56 deletions WatchApp Extension/Controllers/ContextInterfaceController.swift

This file was deleted.

14 changes: 14 additions & 0 deletions WatchApp Extension/Controllers/ContextUpdatable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// ContextUpdatable.swift
// Loop
//
// Created by Nate Racklyeft on 9/19/16.
// Copyright © 2016 Nathan Racklyeft. All rights reserved.
//

import Foundation


protocol ContextUpdatable {
func update(with context: WatchContext?)
}
2 changes: 0 additions & 2 deletions WatchApp Extension/Controllers/NotificationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ final class NotificationController: WKUserNotificationInterfaceController {
super.didDeactivate()
}

/*
override func didReceive(_ notification: UNNotification, withCompletion completionHandler: @escaping (WKUserNotificationInterfaceType) -> Void) {
completionHandler(.default)
}
*/

}
Loading

0 comments on commit 5806ace

Please sign in to comment.