Skip to content

Commit 983702c

Browse files
Steve TreaseSteve Trease
authored andcommitted
Restricted Today Extension to show today's steps and seven day average
1 parent 92e7526 commit 983702c

File tree

10 files changed

+123
-23
lines changed

10 files changed

+123
-23
lines changed

build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
557
1+
562

iOS Steps Today/Base.lproj/MainInterface.storyboard

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16F71b" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="M4Y-Lb-cyx">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.17" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="M4Y-Lb-cyx">
33
<device id="retina3_5" orientation="landscape">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
77
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.14"/>
9+
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
910
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1011
</dependencies>
1112
<scenes>
@@ -21,17 +22,34 @@
2122
<rect key="frame" x="0.0" y="0.0" width="320" height="37"/>
2223
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2324
<subviews>
24-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="ZdE-90-rhb" customClass="UICountingLabel">
25-
<rect key="frame" x="0.0" y="0.0" width="320" height="37"/>
26-
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
27-
</view>
25+
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillProportionally" alignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="COV-QY-SJN">
26+
<rect key="frame" x="16" y="0.0" width="288" height="37"/>
27+
<subviews>
28+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="ZdE-90-rhb" customClass="UICountingLabel">
29+
<rect key="frame" x="0.0" y="0.0" width="288" height="13.5"/>
30+
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
31+
</view>
32+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="63c-KM-sb8">
33+
<rect key="frame" x="106.5" y="13.5" width="75.5" height="10"/>
34+
<fontDescription key="fontDescription" type="system" pointSize="32"/>
35+
<nil key="textColor"/>
36+
<nil key="highlightedColor"/>
37+
</label>
38+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Fte-7B-W9C">
39+
<rect key="frame" x="0.0" y="23.5" width="288" height="13.5"/>
40+
<fontDescription key="fontDescription" type="system" pointSize="16"/>
41+
<nil key="textColor"/>
42+
<nil key="highlightedColor"/>
43+
</label>
44+
</subviews>
45+
</stackView>
2846
</subviews>
2947
<gestureRecognizers/>
3048
<constraints>
31-
<constraint firstItem="ZdE-90-rhb" firstAttribute="leading" secondItem="S3S-Oj-5AN" secondAttribute="leading" id="1aI-ce-Z56"/>
32-
<constraint firstItem="ZdE-90-rhb" firstAttribute="top" secondItem="S3S-Oj-5AN" secondAttribute="top" id="4ly-Xj-iBF"/>
33-
<constraint firstAttribute="trailing" secondItem="ZdE-90-rhb" secondAttribute="trailing" id="uS4-1t-hzH"/>
34-
<constraint firstAttribute="bottom" secondItem="ZdE-90-rhb" secondAttribute="bottom" id="xZp-1f-4I0"/>
49+
<constraint firstItem="COV-QY-SJN" firstAttribute="top" secondItem="Ft6-oW-KC0" secondAttribute="bottom" id="1hz-G3-POS"/>
50+
<constraint firstAttribute="bottomMargin" secondItem="COV-QY-SJN" secondAttribute="bottom" id="4A0-lp-QJH"/>
51+
<constraint firstAttribute="trailingMargin" secondItem="COV-QY-SJN" secondAttribute="trailing" id="RR4-Dq-fuk"/>
52+
<constraint firstItem="COV-QY-SJN" firstAttribute="leading" secondItem="S3S-Oj-5AN" secondAttribute="leadingMargin" id="q3C-ih-xHY"/>
3553
</constraints>
3654
<connections>
3755
<outletCollection property="gestureRecognizers" destination="O2w-ql-fca" appends="YES" id="HSH-06-eog"/>
@@ -44,6 +62,8 @@
4462
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
4563
<size key="freeformSize" width="320" height="37"/>
4664
<connections>
65+
<outlet property="averageLabel" destination="Fte-7B-W9C" id="TWd-vi-sCy"/>
66+
<outlet property="stepsLabel" destination="63c-KM-sb8" id="zMz-FZ-NKr"/>
4767
<outlet property="stepsLabel2" destination="ZdE-90-rhb" id="CPF-TR-c26"/>
4868
</connections>
4969
</viewController>

iOS Steps Today/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<key>CFBundleShortVersionString</key>
2020
<string>1.0</string>
2121
<key>CFBundleVersion</key>
22-
<string>557</string>
22+
<string>562</string>
2323
<key>NSExtension</key>
2424
<dict>
2525
<key>NSExtensionMainStoryboard</key>

iOS Steps Today/TodayViewController.swift

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,22 @@ import HealthKit
1212

1313
import UICountingLabel
1414

15-
1615
class TodayViewController: UIViewController, NCWidgetProviding {
1716

17+
@IBOutlet weak var stepsLabel2: UICountingLabel!
18+
@IBOutlet weak var stepsLabel: UILabel!
19+
@IBOutlet weak var averageLabel: UILabel!
20+
21+
1822
let healthStore = HKHealthStore()
1923
let cal = Calendar.current
2024
let numberFormatter = NumberFormatter()
2125

22-
@IBOutlet weak var stepsLabel2: UICountingLabel!
26+
var stepsToday: Double = 0.0
27+
var sevenDayStepAverage: Double = 100000.0
28+
var sevenDayStepAverageLastUpdated: Date = Date.distantPast
29+
30+
2331

2432
override func viewDidLoad() {
2533
super.viewDidLoad()
@@ -37,19 +45,23 @@ class TodayViewController: UIViewController, NCWidgetProviding {
3745
}
3846
}
3947
stepsLabel2.textAlignment = .center
48+
49+
stepsLabel2.isEnabled = false
4050

4151
checkHealthKitAuthorization()
4252

4353
updateView ()
4454
}
4555

56+
4657
override func viewDidAppear(_ animated: Bool) {
4758
super.viewDidAppear(animated)
4859
print ("viewDidAppear")
4960

5061
updateView()
5162
}
5263

64+
5365
override func didReceiveMemoryWarning() {
5466
super.didReceiveMemoryWarning()
5567
// Dispose of any resources that can be recreated.
@@ -84,20 +96,54 @@ class TodayViewController: UIViewController, NCWidgetProviding {
8496

8597

8698
func updateView () {
87-
let energyFormatter = EnergyFormatter()
88-
energyFormatter.numberFormatter.maximumFractionDigits = 0
89-
9099
let numberFormatter = NumberFormatter()
91100
numberFormatter.maximumFractionDigits = 0
92101
numberFormatter.numberStyle = NumberFormatter.Style.decimal
93102

103+
/*
94104
getTodayStepCount (completion: { (steps) in
95105
if steps != -1.0 {
96106
OperationQueue.main.addOperation {
97107
self.stepsLabel2.countFromCurrentValue(to: CGFloat(steps!), withDuration: 0.5)
98108
}
99109
}
100110
})
111+
*/
112+
113+
getTodayStepCount (completion: { (steps) in
114+
if steps != -1.0 {
115+
OperationQueue.main.addOperation {
116+
let numberString = self.numberFormatter.string(from: steps! as NSNumber)
117+
self.stepsLabel.text = numberString!
118+
119+
if (self.stepsToday > self.sevenDayStepAverage) {
120+
self.stepsLabel.textColor = UIColor(red: 0, green: 0.25, blue: 0, alpha: 1.0) // pale green
121+
} else {
122+
self.stepsLabel.textColor = .black
123+
}
124+
}
125+
}
126+
})
127+
128+
// only update if not updated already today
129+
if (!cal.isDateInToday(sevenDayStepAverageLastUpdated)) {
130+
getSevenDayStepAverage (completion: { (averageSteps) in
131+
if averageSteps != -1.0 {
132+
self.sevenDayStepAverage = averageSteps!
133+
self.sevenDayStepAverageLastUpdated = Date()
134+
OperationQueue.main.addOperation {
135+
let numberString = self.numberFormatter.string(from: averageSteps! as NSNumber)
136+
self.averageLabel.text = numberString!
137+
138+
if (self.stepsToday > self.sevenDayStepAverage) {
139+
self.stepsLabel.textColor = UIColor(red: 0, green: 0.25, blue: 0, alpha: 1.0) // pale green
140+
} else {
141+
self.stepsLabel.textColor = .black
142+
}
143+
}
144+
}
145+
})
146+
}
101147
}
102148

103149

@@ -154,5 +200,33 @@ class TodayViewController: UIViewController, NCWidgetProviding {
154200
}
155201
healthStore.execute(query)
156202
}
203+
204+
205+
func getSevenDayStepAverage(completion:@escaping (Double?)->())
206+
{
207+
// Define the sample type
208+
let type = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)
209+
210+
let cal = Calendar.current
211+
let endDate = cal.startOfDay(for: Date())
212+
let startDate = cal.date(byAdding: .day, value: -7, to: endDate)
213+
214+
// Set the predicate
215+
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [])
216+
217+
let query = HKStatisticsQuery(quantityType: type!, quantitySamplePredicate: predicate, options: .cumulativeSum) { query, results, error in
218+
let quantity = results?.sumQuantity()
219+
let unit = HKUnit.count()
220+
let steps = quantity?.doubleValue(for: unit)
221+
222+
if steps != nil {
223+
completion(steps! / 7.0)
224+
} else {
225+
print("getStepsAverage: results are nil - returning zero steps")
226+
completion(-1.0)
227+
}
228+
}
229+
healthStore.execute(query)
230+
}
157231

158232
}

iOS Steps Watch Extension/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<key>CFBundleShortVersionString</key>
2020
<string>1.0</string>
2121
<key>CFBundleVersion</key>
22-
<string>557</string>
22+
<string>562</string>
2323
<key>NSExtension</key>
2424
<dict>
2525
<key>NSExtensionAttributes</key>

iOS Steps Watch Extension/InterfaceController.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ class InterfaceController: WKInterfaceController {
8383
let numberString = self.numberFormatter.string(from: steps! as NSNumber)
8484
self.stepsLabel.setText(numberString!)
8585

86-
WKInterfaceDevice.current().play(.click)
8786
if (self.stepsToday > self.sevenDayStepAverage) {
8887
self.stepsLabel.setTextColor(UIColor(red: 0.8, green: 1.0, blue: 0.8, alpha: 1.0)) // pale green
8988
} else {
@@ -100,8 +99,15 @@ class InterfaceController: WKInterfaceController {
10099
self.sevenDayStepAverage = averageSteps!
101100
self.sevenDayStepAverageLastUpdated = Date()
102101
OperationQueue.main.addOperation {
102+
WKInterfaceDevice.current().play(.click)
103103
let numberString = self.numberFormatter.string(from: averageSteps! as NSNumber)
104104
self.averageLabel.setText(numberString!)
105+
106+
if (self.stepsToday > self.sevenDayStepAverage) {
107+
self.stepsLabel.setTextColor(UIColor(red: 0.8, green: 1.0, blue: 0.8, alpha: 1.0)) // pale green
108+
} else {
109+
self.stepsLabel.setTextColor(.white)
110+
}
105111
}
106112
}
107113
})

iOS Steps Watch/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<key>CFBundleShortVersionString</key>
2020
<string>1.0</string>
2121
<key>CFBundleVersion</key>
22-
<string>557</string>
22+
<string>562</string>
2323
<key>UIBackgroundModes</key>
2424
<array/>
2525
<key>UISupportedInterfaceOrientations</key>

iOS Steps/Base.lproj/LaunchScreen.storyboard

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
2222
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2323
<subviews>
24-
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Build: 557 (241c3ce)" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="yhA-lX-4Of" userLabel="APP_VERSION">
24+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Build: 562 (92e7526)" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="yhA-lX-4Of" userLabel="APP_VERSION">
2525
<rect key="frame" x="132.5" y="527" width="55.5" height="21"/>
2626
<fontDescription key="fontDescription" type="system" pointSize="17"/>
2727
<nil key="textColor"/>

iOS Steps/Base.lproj/LaunchScreen.storyboardbak

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
2222
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2323
<subviews>
24-
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Build: 556 (241c3ce)" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="yhA-lX-4Of" userLabel="APP_VERSION">
24+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Build: 561 (92e7526)" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="yhA-lX-4Of" userLabel="APP_VERSION">
2525
<rect key="frame" x="132.5" y="527" width="55.5" height="21"/>
2626
<fontDescription key="fontDescription" type="system" pointSize="17"/>
2727
<nil key="textColor"/>

iOS Steps/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</dict>
2929
</array>
3030
<key>CFBundleVersion</key>
31-
<string>557</string>
31+
<string>562</string>
3232
<key>LSRequiresIPhoneOS</key>
3333
<true/>
3434
<key>NSHealthShareUsageDescription</key>

0 commit comments

Comments
 (0)