Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 34 additions & 17 deletions WatchApp Extension/Controllers/AddCarbsInterfaceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,21 @@ import WatchConnectivity

final class AddCarbsInterfaceController: WKInterfaceController, IdentifiableClass {

private var carbValue: Int = 15
fileprivate var carbValue: Int = 15 {
didSet {
guard carbValue >= 0 else {
carbValue = 0
return
}

guard carbValue <= 100 else {
carbValue = 100
return
}

valueLabel.setText(String(carbValue))
}
}

private var absorptionTime = AbsorptionTimeType.medium {
didSet {
Expand All @@ -33,8 +47,6 @@ final class AddCarbsInterfaceController: WKInterfaceController, IdentifiableClas

@IBOutlet var valueLabel: WKInterfaceLabel!

@IBOutlet var valuePicker: WKInterfacePicker!

@IBOutlet var absorptionButtonA: WKInterfaceButton!

@IBOutlet var absorptionButtonB: WKInterfaceButton!
Expand All @@ -45,12 +57,7 @@ final class AddCarbsInterfaceController: WKInterfaceController, IdentifiableClas
super.awake(withContext: context)

// Configure interface objects here.

let items = (0...100).map { _ in WKPickerItem() }

valuePicker.setItems(items)

valuePicker.setSelectedItemIndex(carbValue)
crownSequencer.delegate = self

absorptionTime = .medium
}
Expand All @@ -59,7 +66,7 @@ final class AddCarbsInterfaceController: WKInterfaceController, IdentifiableClas
// This method is called when watch view controller is about to be visible to user
super.willActivate()

valuePicker.focus()
crownSequencer.focus()
}

override func didDeactivate() {
Expand All @@ -69,17 +76,12 @@ final class AddCarbsInterfaceController: WKInterfaceController, IdentifiableClas

// MARK: - Actions

@IBAction func pickerValueUpdated(_ value: Int) {
carbValue = value
valueLabel.setText(String(value))
}

@IBAction func decrement() {
valuePicker.setSelectedItemIndex(carbValue - 5)
carbValue -= 5
}

@IBAction func increment() {
valuePicker.setSelectedItemIndex(carbValue + 5)
carbValue += 5
}

@IBAction func setAbsorptionTimeFast() {
Expand Down Expand Up @@ -120,4 +122,19 @@ final class AddCarbsInterfaceController: WKInterfaceController, IdentifiableClas
dismiss()
}

// MARK: - Crown Sequencer

fileprivate var accumulatedRotation: Double = 0
}

fileprivate let rotationsPerCarb: Double = 1/24

extension AddCarbsInterfaceController: WKCrownDelegate {
func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double) {
accumulatedRotation += rotationalDelta

let remainder = accumulatedRotation.truncatingRemainder(dividingBy: rotationsPerCarb)
carbValue += Int((accumulatedRotation - remainder).divided(by: rotationsPerCarb))
accumulatedRotation = remainder
}
}
67 changes: 44 additions & 23 deletions WatchApp Extension/Controllers/BolusInterfaceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,20 @@ import WatchConnectivity

final class BolusInterfaceController: WKInterfaceController, IdentifiableClass {

private var bolusValue: Double = 0 {
fileprivate var pickerValue: Int = 0 {
didSet {
guard pickerValue >= 0 else {
pickerValue = 0
return
}

guard pickerValue <= maxPickerValue else {
pickerValue = maxPickerValue
return
}

let bolusValue = bolusValueFromPickerValue(pickerValue)

switch bolusValue {
case let x where x < 1:
formatter.minimumFractionDigits = 3
Expand Down Expand Up @@ -58,55 +70,51 @@ final class BolusInterfaceController: WKInterfaceController, IdentifiableClass {
return formatter
}()

private var maxPickerValue = 0

/// 1.25
@IBOutlet var valueLabel: WKInterfaceLabel!

@IBOutlet var valuePicker: WKInterfacePicker!

/// REC: 2.25 U
@IBOutlet var recommendedValueLabel: WKInterfaceLabel!

override func awake(withContext context: Any?) {
super.awake(withContext: context)

let maxPickerValue: Int
var maxBolusValue: Double = 15
let pickerValue: Int
var pickerValue = 0

let info: BolusSuggestionUserInfo? = BolusSuggestionUserInfo(recommendedBolus: 3.5, maxBolus: 10)

if let context = context as? BolusSuggestionUserInfo {
let recommendedBolus = context.recommendedBolus
if let context = info {
let recommendedBolus = 3.5

if let maxBolus = context.maxBolus {
maxBolusValue = maxBolus
} else if recommendedBolus > 0 {
maxBolusValue = recommendedBolus
}

maxPickerValue = pickerValueFromBolusValue(maxBolusValue)
let recommendedPickerValue = pickerValueFromBolusValue(recommendedBolus)
maxBolusValue = bolusValueFromPickerValue(maxPickerValue)
pickerValue = Int(Double(recommendedPickerValue) * 0.75)
bolusValue = bolusValueFromPickerValue(pickerValue)

if let valueString = formatter.string(from: NSNumber(value: recommendedBolus)) {
recommendedValueLabel.setText(String(format: NSLocalizedString("Rec: %@ U", comment: "The label and value showing the recommended bolus"), valueString).localizedUppercase)
}
} else {
maxPickerValue = pickerValueFromBolusValue(maxBolusValue)
pickerValue = pickerValueFromBolusValue(bolusValue)
bolusValue = 0
}

let items = (0...maxPickerValue).map { _ in WKPickerItem() }
valuePicker.setItems(items)
valuePicker.setSelectedItemIndex(pickerValue)
self.maxPickerValue = pickerValueFromBolusValue(maxBolusValue)
self.pickerValue = pickerValue

crownSequencer.delegate = self
}

override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()

valuePicker.focus()
crownSequencer.focus()
}

override func didDeactivate() {
Expand All @@ -116,19 +124,17 @@ final class BolusInterfaceController: WKInterfaceController, IdentifiableClass {

// MARK: - Actions

@IBAction func pickerValueUpdated(_ value: Int) {
bolusValue = bolusValueFromPickerValue(value)
}

@IBAction func decrement() {
valuePicker.setSelectedItemIndex(pickerValueFromBolusValue(bolusValue) - 10)
pickerValue -= 10
}

@IBAction func increment() {
valuePicker.setSelectedItemIndex(pickerValueFromBolusValue(bolusValue) + 10)
pickerValue += 10
}

@IBAction func deliver() {
let bolusValue = bolusValueFromPickerValue(pickerValue)

if bolusValue > 0 {
let bolus = SetBolusUserInfo(value: bolusValue, startDate: Date())

Expand All @@ -150,4 +156,19 @@ final class BolusInterfaceController: WKInterfaceController, IdentifiableClass {
dismiss()
}

// MARK: - Crown Sequencer

fileprivate var accumulatedRotation: Double = 0
}

fileprivate let rotationsPerValue: Double = 1/24

extension BolusInterfaceController: WKCrownDelegate {
func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double) {
accumulatedRotation += rotationalDelta

let remainder = accumulatedRotation.truncatingRemainder(dividingBy: rotationsPerValue)
pickerValue += Int((accumulatedRotation - remainder).divided(by: rotationsPerValue))
accumulatedRotation = remainder
}
}
5 changes: 2 additions & 3 deletions WatchApp Extension/ExtensionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,12 @@ final class ExtensionDelegate: NSObject, WKExtensionDelegate {
for complication in server.activeComplications ?? [] {
// In watchOS 2, we forced a timeline reload every 8 hours because attempting to extend it indefinitely seemed to lead to the complication "freezing".
if UserDefaults.standard.complicationDataLastRefreshed.timeIntervalSinceNow < TimeInterval(hours: -8) {
UserDefaults.standard.complicationDataLastRefreshed = Date()
os_log("Reloading complication timeline")
server.reloadTimeline(for: complication)
} else {
os_log("Extending complication timeline")
// TODO: Switch this back to extendTimeline if things are working correctly.
// Time Travel appears to be disabled by default in watchOS 3 anyway
server.reloadTimeline(for: complication)
server.extendTimeline(for: complication)
}
}
}
Expand Down
24 changes: 6 additions & 18 deletions WatchApp/Base.lproj/Interface.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder.WatchKit.Storyboard" version="3.0" toolsVersion="11201" systemVersion="15G1004" targetRuntime="watchKit" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="rNf-Mh-tID">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder.WatchKit.Storyboard" version="3.0" toolsVersion="11201" systemVersion="16A323" targetRuntime="watchKit" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="rNf-Mh-tID">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11161"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBWatchKitPlugin" version="11077"/>
Expand All @@ -12,16 +12,11 @@
<items>
<group width="1" height="1" alignment="left" layout="vertical" spacing="1" id="uPt-Z4-ngs">
<items>
<picker height="2" alignment="left" style="sequence" id="brP-46-ycV">
<connections>
<action selector="pickerValueUpdated:" destination="zh0-gB-D44" id="cSR-oa-odP"/>
</connections>
</picker>
<group width="1" alignment="left" spacing="0.0" id="WLN-cX-0wl">
<items>
<group width="22" height="22" alignment="left" verticalAlignment="center" radius="11" id="J90-Xm-Znp">
<items>
<button width="22" height="22" alignment="left" verticalAlignment="center" title="−" id="Dh9-HV-fXy">
<button width="22" height="22" alignment="left" verticalAlignment="center" accessibilityLabel="Subtract" title="−" id="Dh9-HV-fXy">
<color key="titleColor" red="1" green="0.58431372550000005" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="backgroundColor" red="1" green="0.58431372549019611" blue="0.0" alpha="0.14999999999999999" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="font" type="system" weight="semibold" pointSize="20"/>
Expand All @@ -37,7 +32,7 @@
</label>
<group width="22" height="22" alignment="right" verticalAlignment="center" radius="11" id="6ip-X6-F2J">
<items>
<button width="22" height="22" alignment="right" verticalAlignment="center" title="+" id="eu3-pj-GH3">
<button width="22" height="22" alignment="right" verticalAlignment="center" accessibilityLabel="Add" title="+" id="eu3-pj-GH3">
<color key="titleColor" red="1" green="0.58431372550000005" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="backgroundColor" red="1" green="0.58431372550000005" blue="0.0" alpha="0.14999999999999999" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="font" type="system" weight="semibold" pointSize="20"/>
Expand Down Expand Up @@ -94,7 +89,6 @@
<outlet property="absorptionButtonB" destination="0fo-Z3-hTi" id="bPH-HR-eTF"/>
<outlet property="absorptionButtonC" destination="dPF-QZ-sh6" id="KfE-KO-HcS"/>
<outlet property="valueLabel" destination="E5r-2c-UZm" id="oll-hK-YCN"/>
<outlet property="valuePicker" destination="brP-46-ycV" id="9rB-uA-Dg4"/>
</connections>
</controller>
</objects>
Expand Down Expand Up @@ -172,16 +166,11 @@
<items>
<group width="1" height="1" alignment="left" layout="vertical" spacing="1" id="PJO-Ol-ya8">
<items>
<picker height="2" alignment="left" style="sequence" id="d95-7v-5ZX">
<connections>
<action selector="pickerValueUpdated:" destination="zTm-yH-n4A" id="pa6-22-dFb"/>
</connections>
</picker>
<group width="1" alignment="left" spacing="0.0" id="xP1-tZ-vSS">
<items>
<group width="22" height="22" alignment="left" verticalAlignment="center" radius="11" id="d24-et-2S4">
<items>
<button width="22" height="22" alignment="left" verticalAlignment="center" title="−" id="hjF-xr-cwO">
<button width="22" height="22" alignment="left" verticalAlignment="center" accessibilityLabel="Subtract" title="−" id="hjF-xr-cwO">
<color key="titleColor" red="1" green="0.58431372550000005" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="backgroundColor" red="1" green="0.58431372550000005" blue="0.0" alpha="0.14999999999999999" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="font" type="system" weight="semibold" pointSize="20"/>
Expand All @@ -197,7 +186,7 @@
</label>
<group width="22" height="22" alignment="right" verticalAlignment="center" radius="11" id="JXe-b7-7Ef">
<items>
<button width="22" height="22" alignment="right" verticalAlignment="center" title="+" id="DZc-Gn-RLu">
<button width="22" height="22" alignment="right" verticalAlignment="center" accessibilityLabel="Add" title="+" id="DZc-Gn-RLu">
<color key="titleColor" red="1" green="0.58431372550000005" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="backgroundColor" red="1" green="0.58431372550000005" blue="0.0" alpha="0.14999999999999999" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="font" type="system" weight="semibold" pointSize="20"/>
Expand Down Expand Up @@ -230,7 +219,6 @@
<connections>
<outlet property="recommendedValueLabel" destination="lDp-Dk-msn" id="B0r-C8-lMN"/>
<outlet property="valueLabel" destination="mpK-zY-UvA" id="pM7-ih-eRu"/>
<outlet property="valuePicker" destination="d95-7v-5ZX" id="AIU-bs-k8X"/>
</connections>
</controller>
</objects>
Expand Down