Skip to content

Commit 24cdff5

Browse files
committed
Added previous weight to Watch, Speed improvements
1 parent 1c45337 commit 24cdff5

File tree

9 files changed

+84
-25
lines changed

9 files changed

+84
-25
lines changed

Shared/DateExtension.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,26 @@
99
import Foundation
1010

1111
extension Date {
12-
func string(dateFormat format: String) -> String {
12+
func string(dateFormat format: DateFormatter.Style) -> String {
13+
let dateFormatter = DateFormatter()
14+
dateFormatter.dateStyle = format
15+
return dateFormatter.string(from: self)
16+
}
17+
18+
func string(timeFormat format: DateFormatter.Style) -> String {
19+
let dateFormatter = DateFormatter()
20+
dateFormatter.timeStyle = format
21+
return dateFormatter.string(from: self)
22+
}
23+
24+
private func string(dateFormat format: String) -> String {
1325
let dateFormatter = DateFormatter()
1426
dateFormatter.dateFormat = format
1527
return dateFormatter.string(from: self)
1628
}
1729

1830
func isSameDay(as date: Date) -> Bool {
31+
// Likely a better way to do this than string comparison, TODO later.
1932
return self.string(dateFormat: "dd/MM") == date.string(dateFormat: "dd/MM")
2033
}
2134
}

Shared/WeightLogic.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ class WeightLogic {
4444
}
4545
}
4646

47+
public var weightLbs: Double? {
48+
get {
49+
if let weightUnwrapped = weight {
50+
return (weightUnwrapped / 100).rounded() / 10 / 0.45359237
51+
} else {
52+
return nil
53+
}
54+
}
55+
}
56+
4757
public var bmi: Double? {
4858
get {
4959
// Compute BMI when requested

Weight WatchKit App/Assets.xcassets/rectangularButton.imageset/Contents.json renamed to Weight WatchKit App/Assets.xcassets/Button.imageset/Contents.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"author" : "xcode"
1111
},
1212
"properties" : {
13-
"preserves-vector-representation" : true
13+
"auto-scaling" : "auto"
1414
}
1515
}

Weight WatchKit App/Assets.xcassets/rectangularButton.imageset/Rectangular Button.pdf renamed to Weight WatchKit App/Assets.xcassets/Button.imageset/Rectangular Button.pdf

File renamed without changes.

Weight WatchKit App/Assets.xcassets/Minus.imageset/Contents.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"author" : "xcode"
1111
},
1212
"properties" : {
13-
"preserves-vector-representation" : true,
1413
"auto-scaling" : "auto"
1514
}
1615
}

Weight WatchKit App/Assets.xcassets/Plus.imageset/Contents.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"author" : "xcode"
1111
},
1212
"properties" : {
13-
"preserves-vector-representation" : true,
1413
"auto-scaling" : "auto"
1514
}
1615
}

Weight WatchKit App/Base.lproj/Interface.storyboard

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<document type="com.apple.InterfaceBuilder.WatchKit.Storyboard" version="3.0" toolsVersion="14460.31" targetRuntime="watchKit" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="AgC-eL-Hgc">
3-
<device id="watch42" orientation="portrait">
3+
<device id="watch38" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
@@ -13,7 +13,7 @@
1313
<objects>
1414
<controller identifier="mainInterface" title="Weight" id="AgC-eL-Hgc" customClass="EntryInterfaceController" customModule="Weight_WatchKit_App" customModuleProvider="target">
1515
<items>
16-
<group width="1" alignment="left" layout="vertical" id="9QU-Lu-NwZ" userLabel="Weight Information">
16+
<group width="1" alignment="center" layout="vertical" id="9QU-Lu-NwZ" userLabel="Weight Information">
1717
<items>
1818
<group width="1" alignment="left" layout="vertical" spacing="0.0" id="prs-FM-Kzg">
1919
<items>
@@ -46,12 +46,22 @@
4646
</label>
4747
</items>
4848
</group>
49-
<label height="0.29999999999999999" alignment="center" verticalAlignment="bottom" text="Previous: Weight" textAlignment="justified" baselineAdjustment="alignCenters" id="L4J-3m-WwV">
50-
<fontDescription key="font" style="UICTFontTextStyleFootnote"/>
51-
</label>
49+
<separator alignment="center" verticalAlignment="center" id="cMd-VM-iYF"/>
50+
<group alignment="center" verticalAlignment="center" layout="vertical" id="aBw-w4-Sb5">
51+
<items>
52+
<label width="1" alignment="center" verticalAlignment="center" text="Previous" textAlignment="center" id="L4J-3m-WwV">
53+
<color key="textColor" red="0.77706670759999996" green="0.97428315880000005" blue="0.42329245809999999" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
54+
<fontDescription key="font" style="UICTFontTextStyleFootnote"/>
55+
</label>
56+
<label width="1" alignment="center" verticalAlignment="center" text="Weight (Date)" textAlignment="center" numberOfLines="2" id="e62-j5-QFa">
57+
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
58+
<fontDescription key="font" style="UICTFontTextStyleFootnote"/>
59+
</label>
60+
</items>
61+
</group>
5262
</items>
5363
</group>
54-
<button alignment="center" verticalAlignment="bottom" semanticContentAttribute="playback" title="Update" backgroundImage="rectangularButton" id="v0e-Sp-vdp">
64+
<button alignment="center" verticalAlignment="bottom" semanticContentAttribute="playback" title="Update" backgroundImage="Button" id="v0e-Sp-vdp">
5565
<color key="titleColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
5666
<fontDescription key="font" style="UICTFontTextStyleBody"/>
5767
<connections>
@@ -69,6 +79,7 @@
6979
</items>
7080
</menu>
7181
<connections>
82+
<outlet property="previousWeightDateLabel" destination="e62-j5-QFa" id="GJR-Wb-bMS"/>
7283
<outlet property="previousWeightLabel" destination="L4J-3m-WwV" id="cTf-2h-WFS"/>
7384
<outlet property="unitLabel" destination="ydA-RM-0iY" id="LhP-Qr-ejN"/>
7485
<outlet property="weightLabel" destination="zSO-BU-G9J" id="HiH-H1-oq9"/>
@@ -95,9 +106,7 @@
95106
</group>
96107
</items>
97108
</group>
98-
<separator alignment="center" verticalAlignment="center" id="Awu-FH-JFX">
99-
<color key="color" red="0.77706670759999996" green="0.97428315880000005" blue="0.42329245809999999" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
100-
</separator>
109+
<separator alignment="center" verticalAlignment="center" id="Awu-FH-JFX"/>
101110
<group width="1" alignment="left" verticalAlignment="center" id="wkD-yC-IeU" userLabel="BMI">
102111
<items>
103112
<label alignment="left" verticalAlignment="center" text="BMI:" textAlignment="left" id="biV-0p-4qS">

Weight WatchKit Extension/EntryInterfaceController.swift

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class EntryInterfaceController: WKInterfaceController, WKCrownDelegate {
1414
@IBOutlet weak var weightLabel: WKInterfaceLabel!
1515
@IBOutlet weak var unitLabel: WKInterfaceLabel!
1616
@IBOutlet weak var previousWeightLabel: WKInterfaceLabel!
17+
@IBOutlet weak var previousWeightDateLabel: WKInterfaceLabel!
1718

1819
var selectedUnit: UnitType = .Metric
1920
let weightLogic = WeightLogic()
@@ -31,18 +32,19 @@ class EntryInterfaceController: WKInterfaceController, WKCrownDelegate {
3132
if let deactivationTime = self.deactivationTime {
3233
// Check if more than 2 minutes have passed since last run.
3334
if Date(timeIntervalSinceNow: 0).timeIntervalSince(deactivationTime) > TimeInterval(exactly: 120)! {
34-
weightLogic.updateWeight(updateWeightLabel)
35+
weightLogic.updateWeight(updateLabels)
3536
}
3637
} else {
3738
// If this is the first run
38-
weightLogic.updateWeight(updateWeightLabel)
39+
weightLogic.updateWeight(updateLabels)
3940
}
4041

4142
crownSequencer.focus()
4243
super.willActivate()
4344
}
4445

4546
override func didAppear() {
47+
updateLabels()
4648
crownSequencer.focus()
4749
}
4850

@@ -95,7 +97,7 @@ class EntryInterfaceController: WKInterfaceController, WKCrownDelegate {
9597
self.selectedUnit = .Imperial
9698
}
9799

98-
updateWeightLabel()
100+
updateLabels()
99101
}
100102

101103
func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double) {
@@ -113,28 +115,55 @@ class EntryInterfaceController: WKInterfaceController, WKCrownDelegate {
113115
updateWeightLabel()
114116
}
115117

116-
func updateWeightLabel() {
118+
private func lastWeighDateText() -> String {
119+
let previousWeightDate = weightLogic.lastWeightDate ?? Date(timeIntervalSinceNow: 0)
120+
121+
let previousTimeString = previousWeightDate.string(timeFormat: DateFormatter.Style.short)
122+
let nonBreakingPreviousTimeString = previousTimeString.replacingOccurrences(of: " ", with: " ")
123+
124+
if previousWeightDate.isSameDay(as: Date(timeIntervalSinceNow: 0)) {
125+
// Same day, only needs to display time
126+
return "Today \(nonBreakingPreviousTimeString)"
127+
} else if previousWeightDate.isSameDay(as: Date(timeIntervalSinceNow: -86_400)) {
128+
return "Yesterday \(nonBreakingPreviousTimeString)"
129+
} else {
130+
let durationPassed = DateInterval(start: previousWeightDate, end: Date(timeIntervalSinceNow: 0)).duration
131+
let daysPassed = ceil(durationPassed / 60 / 60 / 24)
132+
return "\(Int(daysPassed)) days ago at \(nonBreakingPreviousTimeString)"
133+
}
134+
}
135+
136+
private func updateLabels() {
117137
var unitText: String
118-
var weightInUnit: Double
138+
var shortUnitText: String
119139
var previousWeightInUnit: Double
120140

121141
switch selectedUnit {
122142
case .Metric:
123-
weightInUnit = weightLogic.weightKG ?? 0
124143
previousWeightInUnit = weightLogic.lastWeightKG ?? 0
125144
unitText = "Kilograms"
145+
shortUnitText = "Kg"
126146
case .Imperial:
127-
weightInUnit = (weightLogic.weightKG ?? 0) / 0.45359237
128147
previousWeightInUnit = (weightLogic.lastWeightKG ?? 0) / 0.45359237
129148
unitText = "Pounds"
149+
shortUnitText = "lbs"
130150
}
131151

132-
let weightLabelText = String(format: "%.1f", weightInUnit)
133-
let previousWeightLabelText = String(format: "Previous: %.1f", previousWeightInUnit)
152+
let previousWeightTruncated = String(format: "%.1f", previousWeightInUnit)
153+
let previousWeightLabelText = "\(previousWeightTruncated) \(shortUnitText) \(lastWeighDateText())"
134154

135-
weightLabel.setText(weightLabelText)
136155
unitLabel.setText(unitText)
137-
previousWeightLabel.setText(previousWeightLabelText)
156+
previousWeightDateLabel.setText(previousWeightLabelText)
157+
updateWeightLabel()
158+
}
159+
160+
private func updateWeightLabel() {
161+
switch selectedUnit {
162+
case .Metric:
163+
weightLabel.setText(String(format: "%.1f", weightLogic.weightKG ?? 0))
164+
case .Imperial:
165+
weightLabel.setText(String(format: "%.1f", weightLogic.weightLbs ?? 0))
166+
}
138167
}
139168

140169
}

Weight/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class ViewController: UIViewController {
6565
let lastWeightText = String(format: "Previous: %.1f KG", previousWeight)
6666

6767
var lastWeightDateText = ""
68-
let previousDateString = previousWeightDate.string(dateFormat: "HH:mm")
68+
let previousDateString = previousWeightDate.string(dateFormat: DateFormatter.Style.short)
6969
if previousWeightDate.isSameDay(as: Date(timeIntervalSinceNow: 0)) {
7070
// Same day, only needs to display time
7171
lastWeightDateText = "Today at \(previousDateString)"

0 commit comments

Comments
 (0)