Skip to content
99 changes: 83 additions & 16 deletions WatchApp Extension/Controllers/AddCarbsInterfaceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,45 @@ import WatchConnectivity

final class AddCarbsInterfaceController: WKInterfaceController, IdentifiableClass {

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

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

valueLabel.setText(String(carbValue))
}
}

private var absorptionTime = AbsorptionTimeType.medium {
private let maximumDatePastInterval = TimeInterval(hours: 8)
private let maximumDateFutureInterval = TimeInterval(hours: 4)

private var date = Date() {
didSet {
let now = Date()
let minimumDate = now - maximumDatePastInterval
let maximumDate = now + maximumDateFutureInterval

if date < minimumDate {
date = minimumDate
} else if date > maximumDate {
date = maximumDate
}

dateLabel.setText(dateFormatter.string(from: date))
}
}

private let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .none
formatter.timeStyle = .short
return formatter
}()

private var absorptionTime: AbsorptionTimeType = .medium {
didSet {
absorptionButtonA.setBackgroundColor(UIColor.darkCarbsColor)
absorptionButtonB.setBackgroundColor(UIColor.darkCarbsColor)
Expand All @@ -45,20 +67,41 @@ final class AddCarbsInterfaceController: WKInterfaceController, IdentifiableClas
}
}

@IBOutlet weak var valueLabel: WKInterfaceLabel!
@IBOutlet var valueLabel: WKInterfaceLabel!

@IBOutlet var dateLabel: WKInterfaceLabel!

@IBOutlet weak var absorptionButtonA: WKInterfaceButton!

@IBOutlet weak var absorptionButtonB: WKInterfaceButton!

@IBOutlet weak var absorptionButtonC: WKInterfaceButton!

private enum InputMode {
case value
case date
}

private var inputMode: InputMode = .value {
didSet {
switch inputMode {
case .value:
valueLabel.setTextColor(UIColor.carbsColor)
dateLabel.setTextColor(UIColor.lightGray)
case .date:
valueLabel.setTextColor(UIColor.lightGray)
dateLabel.setTextColor(UIColor.carbsColor)
}
}
}

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

// Configure interface objects here.
crownSequencer.delegate = self

date = Date()
absorptionTime = .medium
}

Expand All @@ -76,12 +119,29 @@ final class AddCarbsInterfaceController: WKInterfaceController, IdentifiableClas

// MARK: - Actions

private let valueIncrement = 5
private let dateIncrement = TimeInterval(minutes: 15)

@IBAction func decrement() {
carbValue -= 5
switch inputMode {
case .value:
carbValue -= valueIncrement
case .date:
date -= dateIncrement
}
}

@IBAction func increment() {
carbValue += 5
switch inputMode {
case .value:
carbValue += valueIncrement
case .date:
date += dateIncrement
}
}

@IBAction func toggleInputMode() {
inputMode = (inputMode == .value) ? .date : .value
}

@IBAction func setAbsorptionTimeFast() {
Expand All @@ -98,7 +158,7 @@ final class AddCarbsInterfaceController: WKInterfaceController, IdentifiableClas

@IBAction func save() {
if carbValue > 0 {
let entry = CarbEntryUserInfo(value: Double(carbValue), absorptionTimeType: absorptionTime, startDate: Date())
let entry = CarbEntryUserInfo(value: Double(carbValue), absorptionTimeType: absorptionTime, startDate: date)

do {
try WCSession.default().sendCarbEntryMessage(entry,
Expand All @@ -124,17 +184,24 @@ final class AddCarbsInterfaceController: WKInterfaceController, IdentifiableClas

// MARK: - Crown Sequencer

fileprivate var accumulatedRotation: Double = 0
private var accumulatedRotation: Double = 0
}

fileprivate let rotationsPerCarb: Double = 1/24
private let rotationsPerIncrement: Double = 1/24

extension AddCarbsInterfaceController: WKCrownDelegate {
func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double) {
accumulatedRotation += rotationalDelta
let remainder = accumulatedRotation.truncatingRemainder(dividingBy: rotationsPerIncrement)
let delta = (accumulatedRotation - remainder) / rotationsPerIncrement

switch inputMode {
case .value:
carbValue += Int(delta)
case .date:
date += TimeInterval(minutes: delta)
}

let remainder = accumulatedRotation.truncatingRemainder(dividingBy: rotationsPerCarb)
carbValue += Int((accumulatedRotation - remainder) / rotationsPerCarb)
accumulatedRotation = remainder
}
}
98 changes: 58 additions & 40 deletions WatchApp/Base.lproj/Interface.storyboard
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder.WatchKit.Storyboard" version="3.0" toolsVersion="12120" systemVersion="16F73" targetRuntime="watchKit" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="rNf-Mh-tID">
<document type="com.apple.InterfaceBuilder.WatchKit.Storyboard" version="3.0" toolsVersion="13529" targetRuntime="watchKit" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="rNf-Mh-tID">
<device id="watch42" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="watchOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBWatchKitPlugin" version="12029"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13527"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBWatchKitPlugin" version="13507"/>
</dependencies>
<scenes>
<!--AddCarbsInterfaceController-->
Expand All @@ -16,68 +16,85 @@
<items>
<group width="1" height="1" alignment="left" layout="vertical" spacing="1" id="uPt-Z4-ngs">
<items>
<group width="1" alignment="left" spacing="0.0" id="WLN-cX-0wl">
<group width="1" alignment="left" layout="vertical" id="hwe-wE-KAr">
<items>
<group width="22" height="22" alignment="left" verticalAlignment="center" radius="11" id="J90-Xm-Znp">
<group width="1" height="0.25" alignment="left" spacing="0.0" id="WLN-cX-0wl">
<items>
<button width="22" height="22" alignment="left" verticalAlignment="center" accessibilityLabel="Subtract" title="−" id="Dh9-HV-fXy">
<color key="titleColor" red="0.38823529410000002" green="0.85490196080000003" blue="0.21960784310000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="backgroundColor" red="0.38823529410000002" green="0.85490196080000003" blue="0.21960784310000001" alpha="0.14000000000000001" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="font" type="system" weight="semibold" pointSize="20"/>
<connections>
<action selector="decrement" destination="zh0-gB-D44" id="6k1-iq-yYz"/>
</connections>
</button>
<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" accessibilityLabel="Subtract" title="−" id="Dh9-HV-fXy">
<color key="titleColor" red="0.38823529410000002" green="0.85490196080000003" blue="0.21960784310000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="backgroundColor" red="0.38823529410000002" green="0.85490196080000003" blue="0.21960784310000001" alpha="0.14000000000000001" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="font" type="system" weight="semibold" pointSize="20"/>
<connections>
<action selector="decrement" destination="zh0-gB-D44" id="FdO-GD-e1m"/>
</connections>
</button>
</items>
</group>
<label height="37" alignment="center" text="15" id="CWt-7U-cnK">
<color key="textColor" red="0.38985729217529297" green="0.85640043020248413" blue="0.21795386075973511" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="font" type="system" pointSize="40"/>
</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" accessibilityLabel="Add" title="+" id="eu3-pj-GH3">
<color key="titleColor" red="0.38823529410000002" green="0.85490196080000003" blue="0.21960784310000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="backgroundColor" red="0.38823529410000002" green="0.85490196080000003" blue="0.21960784310000001" alpha="0.14000000000000001" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="font" type="system" weight="semibold" pointSize="20"/>
<connections>
<action selector="increment" destination="zh0-gB-D44" id="6yb-Y8-oKu"/>
</connections>
</button>
</items>
</group>
</items>
</group>
<label height="37" alignment="center" text="15" textAlignment="center" id="E5r-2c-UZm">
<color key="textColor" red="0.38823529410000002" green="0.85490196080000003" blue="0.21960784310000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="font" type="system" weight="light" pointSize="40"/>
<label alignment="center" text="10:09 AM" id="Ury-of-vQg">
<color key="textColor" cocoaTouchSystemColor="lightTextColor"/>
<fontDescription key="font" type="system" pointSize="15"/>
</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" accessibilityLabel="Add" title="+" id="eu3-pj-GH3">
<color key="titleColor" red="0.38823529410000002" green="0.85490196080000003" blue="0.21960784310000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="backgroundColor" red="0.38823529410000002" green="0.85490196080000003" blue="0.21960784310000001" alpha="0.14000000000000001" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="font" type="system" weight="semibold" pointSize="20"/>
<connections>
<action selector="increment" destination="zh0-gB-D44" id="itV-Ss-2Cc"/>
</connections>
</button>
</items>
</group>
<separator alignment="left" id="psX-Zk-dTM">
<color key="color" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</separator>
</items>
<gestureRecognizers>
<tapGestureRecognizer id="ZKu-em-Fme">
<connections>
<action selector="toggleInputMode" destination="zh0-gB-D44" id="iNJ-Rq-8w7"/>
</connections>
</tapGestureRecognizer>
</gestureRecognizers>
</group>
<label alignment="center" text="GRAMS" textAlignment="center" id="18G-mT-92P">
<color key="textColor" red="0.38823529410000002" green="0.85490196080000003" blue="0.21960784310000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="font" style="UICTFontTextStyleCaption2"/>
</label>
<separator alignment="left" id="psX-Zk-dTM">
<color key="color" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</separator>
<group width="1" alignment="left" spacing="0.0" id="XMa-yL-87D">
<group width="1" height="0.25" alignment="center" verticalAlignment="center" spacing="0.0" id="XMa-yL-87D">
<items>
<button width="0.33000000000000002" alignment="left" title="🍭" id="gAn-qe-OvX">
<button width="0.33000000000000002" height="1" alignment="left" title="🍭" id="gAn-qe-OvX">
<fontDescription key="font" type="system" pointSize="25"/>
<connections>
<action selector="setAbsorptionTimeFast" destination="zh0-gB-D44" id="Eoj-WC-Fod"/>
</connections>
</button>
<button width="0.33000000000000002" alignment="left" title="🌮" id="0fo-Z3-hTi">
<button width="0.33000000000000002" height="1" alignment="left" title="🌮" id="0fo-Z3-hTi">
<color key="backgroundColor" red="0.38823529410000002" green="0.85490196080000003" blue="0.21960784310000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="font" type="system" pointSize="25"/>
<connections>
<action selector="setAbsorptionTimeMedium" destination="zh0-gB-D44" id="GXY-aa-sz8"/>
</connections>
</button>
<button width="0.33000000000000002" alignment="left" title="🍕" id="dPF-QZ-sh6">
<button width="0.33000000000000002" height="1" alignment="left" title="🍕" id="dPF-QZ-sh6">
<fontDescription key="font" type="system" pointSize="25"/>
<connections>
<action selector="setAbsorptionTimeSlow" destination="zh0-gB-D44" id="qa3-FG-c9F"/>
</connections>
</button>
</items>
</group>
<separator alignment="left" id="arf-ic-MQs">
<color key="color" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</separator>
<separator alignment="left" id="EVD-AC-w2R">
<color key="color" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</separator>
<button width="1" alignment="left" verticalAlignment="bottom" title="Add Carbs" id="b6f-3I-jki">
<color key="backgroundColor" red="0.38823529410000002" green="0.85490196080000003" blue="0.21960784310000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="font" type="system" weight="semibold" pointSize="15"/>
Expand All @@ -92,7 +109,8 @@
<outlet property="absorptionButtonA" destination="gAn-qe-OvX" id="mMN-LG-P3Q"/>
<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="dateLabel" destination="Ury-of-vQg" id="tRT-Eb-SVK"/>
<outlet property="valueLabel" destination="CWt-7U-cnK" id="hJE-ER-FQy"/>
</connections>
</controller>
</objects>
Expand Down