Skip to content

Commit aad52cc

Browse files
committed
ui: disable next month button for future months
1 parent 39e9157 commit aad52cc

3 files changed

Lines changed: 58 additions & 18 deletions

File tree

InfiniLink/Core/Components/Charts/Steps/StepChartView.swift

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct StepChartView: View {
3030

3131
let fitnessCalculator = FitnessCalculator()
3232

33-
func steps() -> [StepChartDataPoint] {
33+
func stepChartPoints() -> [StepChartDataPoint] {
3434
let calendar = Calendar.current
3535
let now = Date()
3636
let startOfWeek = calendar.startOfDay(for: calendar.date(from: calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: now))!)
@@ -58,11 +58,25 @@ struct StepChartView: View {
5858
return filledData
5959
}
6060

61+
var streak: Int {
62+
var streak = 0
63+
64+
// Add limit to week or month to avoid iterating through thousands of points?
65+
for point in chartManager.stepPoints().reversed().prefix(7) {
66+
if point.steps >= deviceManager.settings.stepsGoal {
67+
streak += 1
68+
} else {
69+
break
70+
}
71+
}
72+
73+
return streak
74+
}
6175
var earliestDate: Date {
62-
steps().compactMap({ $0.date }).min() ?? Date()
76+
stepChartPoints().compactMap({ $0.date }).min() ?? Date()
6377
}
6478
var latestDate: Date {
65-
steps().compactMap({ $0.date }).max() ?? Date()
79+
stepChartPoints().compactMap({ $0.date }).max() ?? Date()
6680
}
6781

6882
let columns = Array(repeating: GridItem(.flexible()), count: 7)
@@ -74,7 +88,7 @@ struct StepChartView: View {
7488
RuleMark(y: .value("Daily Goal", deviceManager.settings.stepsGoal))
7589
.foregroundStyle(.green)
7690
.lineStyle(StrokeStyle(lineWidth: 2, dash: [4]))
77-
ForEach(steps(), id: \.date) {
91+
ForEach(stepChartPoints(), id: \.date) {
7892
BarMark(
7993
x: .value("Date", $0.date, unit: .weekday),
8094
y: .value("Steps", $0.steps)
@@ -105,7 +119,7 @@ struct StepChartView: View {
105119

106120
let (day, _) = proxy.value(at: location, as: (Date, Int).self) ?? (Date(), 0)
107121
// We compare the formatted dates because the dates are too specific otherwise
108-
let steps = steps().first(where: { $0.date.comparable() == day.comparable() })?.steps ?? 0
122+
let steps = stepChartPoints().first(where: { $0.date.comparable() == day.comparable() })?.steps ?? 0
109123

110124
selectedDate = day
111125
selectedSteps = steps
@@ -117,20 +131,20 @@ struct StepChartView: View {
117131
}
118132
}
119133
.chartXAxis {
120-
AxisMarks(values: steps().map({ $0.date })) {
134+
AxisMarks(values: stepChartPoints().map({ $0.date })) {
121135
AxisGridLine()
122136
AxisValueLabel(format: .dateTime.weekday(.abbreviated))
123137
}
124138
}
125-
.frame(height: 250)
139+
.frame(height: 280)
126140
} header: {
127141
VStack(alignment: .leading) {
128-
Text(steps().count > 1 ? showSelectionBar ? "Total" : "Average" : " ")
142+
Text(stepChartPoints().count > 1 ? showSelectionBar ? "Total" : "Average" : " ")
129143
Text({
130144
if showSelectionBar {
131145
return "\(selectedSteps) "
132-
} else if !steps().isEmpty {
133-
return "\(steps().reduce(0) { $0 + $1.steps } / steps().count) "
146+
} else if !stepChartPoints().isEmpty {
147+
return "\(stepChartPoints().reduce(0) { $0 + $1.steps } / stepChartPoints().count) "
134148
}
135149
return "0 "
136150
}())
@@ -139,6 +153,21 @@ struct StepChartView: View {
139153
.fontWeight(.bold)
140154
+ Text("steps")
141155
Text(showSelectionBar ? "\(selectedDate.formatted(date: .abbreviated, time: .omitted))" : "\(earliestDate.formatted(date: .abbreviated, time: .omitted)) - \(latestDate.formatted(date: .abbreviated, time: .omitted))")
156+
if streak > 0 {
157+
HStack(spacing: 5) {
158+
Image(systemName: "flame.fill")
159+
.imageScale(.large)
160+
.foregroundStyle(.orange)
161+
Text("This week you have a ") +
162+
Text("\(streak)-day")
163+
.foregroundColor(.orange)
164+
.fontWeight(.bold) +
165+
Text(" step goal streak!")
166+
}
167+
.foregroundStyle(Color.primary)
168+
.font(.system(size: 15))
169+
.padding(.vertical, 9)
170+
}
142171
}
143172
.fontWeight(.semibold)
144173
}
@@ -148,15 +177,15 @@ struct StepChartView: View {
148177
let steps = Int(chartManager.stepPoints().last?.steps ?? 0)
149178

150179
if stepCountManager.hasReachedStepGoal {
151-
Text("Great job, you reached your daily step goal today! You've walked \(fitnessCalculator.calculateDistance(steps: steps)) \(personalizationController.units == .imperial ? "miles" : "kilometers") and burned around \(fitnessCalculator.calculateCaloriesBurned(steps: steps)) kcal.")
180+
Text("Great job, you reached your daily step goal today! You've walked \(String(format: "%.2f", fitnessCalculator.calculateDistance(steps: steps))) \(personalizationController.units == .imperial ? "miles" : "kilometers") and burned around \(fitnessCalculator.calculateCaloriesBurned(steps: steps)) kcal.")
152181
} else {
153182
let stepsRemaining = stepCountManager.stepGoal - steps
154183
let distanceRemaining = fitnessCalculator.calculateDistance(steps: stepsRemaining)
155184
let caloriesRemaining = fitnessCalculator.calculateCaloriesBurned(steps: stepsRemaining)
156185
let timeRemaining = fitnessCalculator.secondsFormatted(seconds: Int(fitnessCalculator.secondsForDistance(distance: distanceRemaining)), full: true)
157186

158187
if stepsRemaining <= 1000 {
159-
Text("You're almost there! A quick \(String(format: "%.1f", distanceRemaining)) \(personalizationController.units == .imperial ? "mile" : "km") walk will get you to your goal. It should only take you about \(timeRemaining).")
188+
Text("You're almost there! A quick \(String(format: "%.1f", distanceRemaining)) \(personalizationController.units == .imperial ? "mile" : "km") walk should get you to your goal. It should only take you about \(timeRemaining).")
160189
} else if stepsRemaining <= 2500 {
161190
Text("You're making great progress! You have about \(String(format: "%.1f", distanceRemaining)) \(personalizationController.units == .imperial ? "miles" : "kilometers") to walk. At your current pace, you'll hit your goal in \(timeRemaining).")
162191
} else {

InfiniLink/Core/Components/Charts/Steps/StepMonthlyChartView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ struct StepCalendarView: View {
9191
.background(background)
9292
.clipShape(Circle())
9393
}
94+
.disabled(fetchSelectedMonth(selectedMonth + 1) > Date())
9495
}
9596
}
9697
}
@@ -112,7 +113,8 @@ struct StepCalendarView: View {
112113
return dates
113114
}
114115

115-
func fetchSelectedMonth() -> Date {
116+
func fetchSelectedMonth(_ month: Int? = nil) -> Date {
117+
let selectedMonth = month ?? selectedMonth
116118
let calendar = Calendar.current
117119
let month = calendar.date(byAdding: .month, value: selectedMonth, to: Date())!
118120

InfiniLink/Localizable.xcstrings

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
},
1616
" Recovery" : {
1717

18+
},
19+
" step goal streak!" : {
20+
1821
},
1922
"-" : {
2023

@@ -143,6 +146,9 @@
143146
}
144147
}
145148
}
149+
},
150+
"%lld-day" : {
151+
146152
},
147153
"%lld%%" : {
148154

@@ -436,12 +442,12 @@
436442
"Good morning" : {
437443

438444
},
439-
"Great job, you reached your daily step goal today! You've walked %lf %@ and burned around %lld kcal." : {
445+
"Great job, you reached your daily step goal today! You've walked %@ %@ and burned around %lld kcal." : {
440446
"localizations" : {
441447
"en" : {
442448
"stringUnit" : {
443449
"state" : "new",
444-
"value" : "Great job, you reached your daily step goal today! You've walked %1$lf %2$@ and burned around %3$lld kcal."
450+
"value" : "Great job, you reached your daily step goal today! You've walked %1$@ %2$@ and burned around %3$lld kcal."
445451
}
446452
}
447453
}
@@ -770,6 +776,9 @@
770776
},
771777
"This new version of InfiniLink has not been localized yet, so if you can help translate, visit the InfiniLink GitHub repo to open a pull request!" : {
772778

779+
},
780+
"This week you have a " : {
781+
773782
},
774783
"Time" : {
775784

@@ -888,12 +897,12 @@
888897
}
889898
}
890899
},
891-
"You're almost there! A quick %@ %@ walk will get you to your goal. It should only take you about %@." : {
900+
"You're almost there! A quick %@ %@ walk should get you to your goal. It should only take you about %@." : {
892901
"localizations" : {
893902
"en" : {
894903
"stringUnit" : {
895904
"state" : "new",
896-
"value" : "You're almost there! A quick %1$@ %2$@ walk will get you to your goal. It should only take you about %3$@."
905+
"value" : "You're almost there! A quick %1$@ %2$@ walk should get you to your goal. It should only take you about %3$@."
897906
}
898907
}
899908
}
@@ -919,4 +928,4 @@
919928
}
920929
},
921930
"version" : "1.0"
922-
}
931+
}

0 commit comments

Comments
 (0)