Skip to content

Commit 3044cf8

Browse files
committed
Added options sheet for Steps page, which allows you to set the step goal and also arbitrarily change the step count for a given date, if e.g. you didn't sync one day but remember how many steps you got
1 parent 37e2f5a commit 3044cf8

8 files changed

Lines changed: 164 additions & 33 deletions

File tree

InfiniLink.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
266F53EA26FA91F3007481A6 /* DeviceInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 266F53E926FA91F3007481A6 /* DeviceInfo.swift */; };
5050
2675CA5A26DC81AA00967E4D /* DFUComplete.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2675CA5926DC81AA00967E4D /* DFUComplete.swift */; };
5151
267C2EF8273C8AB300F464B0 /* StepWeeklyGraph.swift in Sources */ = {isa = PBXBuildFile; fileRef = 267C2EF7273C8AB300F464B0 /* StepWeeklyGraph.swift */; };
52+
267C2EFA273DB62800F464B0 /* StepsSettingsSheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 267C2EF9273DB62800F464B0 /* StepsSettingsSheet.swift */; };
5253
268E773C27174A2B000DF90D /* ChartSettingsSheetMain.swift in Sources */ = {isa = PBXBuildFile; fileRef = 268E773B27174A2B000DF90D /* ChartSettingsSheetMain.swift */; };
5354
268E773F27174B8B000DF90D /* ChartSettingsSheetSliders.swift in Sources */ = {isa = PBXBuildFile; fileRef = 268E773E27174B8B000DF90D /* ChartSettingsSheetSliders.swift */; };
5455
268E774127175830000DF90D /* ChartSettingsSheetDatePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 268E774027175830000DF90D /* ChartSettingsSheetDatePicker.swift */; };
@@ -149,6 +150,7 @@
149150
266F53E926FA91F3007481A6 /* DeviceInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceInfo.swift; sourceTree = "<group>"; };
150151
2675CA5926DC81AA00967E4D /* DFUComplete.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DFUComplete.swift; sourceTree = "<group>"; };
151152
267C2EF7273C8AB300F464B0 /* StepWeeklyGraph.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StepWeeklyGraph.swift; sourceTree = "<group>"; };
153+
267C2EF9273DB62800F464B0 /* StepsSettingsSheet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StepsSettingsSheet.swift; sourceTree = "<group>"; };
152154
268E773B27174A2B000DF90D /* ChartSettingsSheetMain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChartSettingsSheetMain.swift; sourceTree = "<group>"; };
153155
268E773E27174B8B000DF90D /* ChartSettingsSheetSliders.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChartSettingsSheetSliders.swift; sourceTree = "<group>"; };
154156
268E774027175830000DF90D /* ChartSettingsSheetDatePicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChartSettingsSheetDatePicker.swift; sourceTree = "<group>"; };
@@ -449,6 +451,7 @@
449451
26A40E842704E6F3007966F6 /* SheetManager.swift */,
450452
268E773D27174B6D000DF90D /* ChartSettingsSheet */,
451453
26C5FAD726FC129400921207 /* ArbitraryNotificationView.swift */,
454+
267C2EF9273DB62800F464B0 /* StepsSettingsSheet.swift */,
452455
26C5FAE226FEB0A400921207 /* DFUDownloadView.swift */,
453456
26C5FADB26FE7EB000921207 /* SheetCloseButton.swift */,
454457
2617EF3A27036D9100FE6F48 /* What's New */,
@@ -632,6 +635,7 @@
632635
26A40E892704E97C007966F6 /* DebugViewBLE.swift in Sources */,
633636
2632389026F2980A00D72B43 /* OnboardingDismissButton.swift in Sources */,
634637
264AE04627026BD6001504A7 /* UptimeManager.swift in Sources */,
638+
267C2EFA273DB62800F464B0 /* StepsSettingsSheet.swift in Sources */,
635639
2632388C26F265B100D72B43 /* MainView.swift in Sources */,
636640
2632388826F2576D00D72B43 /* DFUFileSelectButton.swift in Sources */,
637641
26F426EF26C72D7D00D0866B /* ChartView.swift in Sources */,

InfiniLink/BLE/BLEUpdateHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct BLEUpdatedCharacteristicHandler {
5656
}
5757
let stepData = [UInt8](value)
5858
bleManager.stepCount = Int(stepData[0]) + (Int(stepData[1]) * 256) + (Int(stepData[2]) * 65536) + (Int(stepData[3]) * 16777216)
59-
StepCountPersistenceManager().setStepCount(steps: bleManager.stepCount)
59+
StepCountPersistenceManager().setStepCount(steps: bleManager.stepCount, arbitrary: false, date: Date())
6060
default:
6161
break
6262
}

InfiniLink/Utilities/StepCountPersistenceManager.swift

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@
88

99

1010
import CoreData
11+
import SwiftUI
1112

12-
struct StepCountPersistenceManager {
13+
class StepCountPersistenceManager: ObservableObject {
14+
static let shared = StepCountPersistenceManager()
15+
@Published var currentCount: Int32 = 0
1316
let viewContext = PersistenceController.shared.container.viewContext
1417

18+
init() {
19+
retrieveStepCount(date: Date())
20+
}
21+
1522
func lookupStepCounts(write: Bool) -> [StepCounts] {
1623
var existingCounts: [StepCounts] = []
1724
let request = NSFetchRequest<StepCounts>(entityName: "StepCounts")
@@ -23,11 +30,20 @@ struct StepCountPersistenceManager {
2330
return existingCounts
2431
}
2532

26-
func setStepCount(steps: Int) {
33+
func retrieveStepCount(date: Date) {
34+
let existingCounts = lookupStepCounts(write: true)
35+
for i in existingCounts {
36+
if Calendar.current.isDate(i.timestamp!, inSameDayAs: date) {
37+
currentCount = i.steps
38+
}
39+
}
40+
}
41+
42+
func setStepCount(steps: Int, arbitrary: Bool, date: Date) {
2743
let existingCounts = lookupStepCounts(write: true)
2844

2945
let countExists = existingCounts.contains { count in
30-
if Calendar.current.isDateInToday(count.timestamp!) {
46+
if Calendar.current.isDate(date, inSameDayAs: count.timestamp!) {
3147
return true
3248
} else {
3349
return false
@@ -36,28 +52,42 @@ struct StepCountPersistenceManager {
3652

3753
if countExists {
3854
for i in existingCounts {
39-
if Calendar.current.isDateInToday(i.timestamp!) {
40-
if steps > i.steps {
41-
i.timestamp = Date()
42-
i.steps = Int32(steps)
43-
do {
44-
try viewContext.save()
45-
} catch {
46-
DebugLogManager.shared.debug(error: "Couldn't save step count: \(error)", log: .app, date: Date())
55+
if Calendar.current.isDate(i.timestamp!, inSameDayAs: date) {
56+
if arbitrary {
57+
overwriteStepCount(oldStepCount: i, newSteps: steps, date)
58+
} else {
59+
if steps > i.steps {
60+
overwriteStepCount(oldStepCount: i, newSteps: steps, date)
61+
return
4762
}
48-
return
4963
}
5064
}
5165
}
5266
} else {
53-
let newCount = StepCounts(context: viewContext)
54-
newCount.steps = Int32(steps)
55-
newCount.timestamp = Date()
56-
do {
57-
try viewContext.save()
58-
} catch {
59-
DebugLogManager.shared.debug(error: "Couldn't save step count: \(error)", log: .app, date: Date())
60-
}
67+
saveNewStepCount(steps: steps, date: date)
68+
}
69+
}
70+
71+
func overwriteStepCount(oldStepCount: StepCounts, newSteps: Int, _ newDate: Date) {
72+
oldStepCount.timestamp = newDate
73+
oldStepCount.steps = Int32(newSteps)
74+
do {
75+
try viewContext.save()
76+
} catch {
77+
DebugLogManager.shared.debug(error: "Couldn't save step count: \(error)", log: .app, date: Date())
78+
}
79+
retrieveStepCount(date: Date())
80+
}
81+
82+
func saveNewStepCount(steps: Int, date: Date) {
83+
let newCount = StepCounts(context: viewContext)
84+
newCount.steps = Int32(steps)
85+
newCount.timestamp = date
86+
do {
87+
try viewContext.save()
88+
} catch {
89+
DebugLogManager.shared.debug(error: "Couldn't save step count: \(error)", log: .app, date: Date())
6190
}
91+
retrieveStepCount(date: Date())
6292
}
6393
}

InfiniLink/View Components/Sheets/SheetManager.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ enum SheetSelection {
1717
case downloadUpdate
1818
case whatsNew
1919
case chartSettings
20+
case stepSettings
2021
}
2122

2223
class SheetManager: ObservableObject {
@@ -80,6 +81,8 @@ class SheetManager: ObservableObject {
8081
WhatsNew()
8182
case .chartSettings:
8283
ChartSettingsSheet()
84+
case .stepSettings:
85+
StepSettingsSheet()
8386
}
8487
}
8588
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// StepsSettingsSheet.swift
3+
// InfiniLink
4+
//
5+
// Created by Alex Emry on 11/11/21.
6+
//
7+
//
8+
9+
10+
import SwiftUI
11+
12+
struct StepSettingsSheet: View {
13+
14+
@ObservedObject var addDateValue = NumbersOnly()
15+
@ObservedObject var setStepGoal = NumbersOnly()
16+
let persistenceManager = StepCountPersistenceManager()
17+
@AppStorage("stepCountGoal") var stepCountGoal = 10000
18+
@State var selectedDate: Date = Date()
19+
20+
func readyToSubmit(value: String) -> Bool {
21+
if value == "" {
22+
return true
23+
} else {
24+
return false
25+
}
26+
}
27+
28+
var body: some View {
29+
VStack {
30+
SheetCloseButton()
31+
Text("Step Count Settings")
32+
.font(.largeTitle)
33+
.padding()
34+
Text("Change Step Count Goal")
35+
TextField("Step Goal", text: $setStepGoal.value)
36+
.padding()
37+
.keyboardType(.decimalPad)
38+
Button {
39+
stepCountGoal = Int(setStepGoal.value)!
40+
} label: {
41+
Text("Submit New Step Goal")
42+
}.disabled(readyToSubmit(value: setStepGoal.value))
43+
44+
Text("Manually Add Step Count")
45+
.font(.title)
46+
.padding()
47+
DatePicker("Select Date", selection: $selectedDate, displayedComponents: [.date])
48+
TextField("Number of Steps", text: $addDateValue.value)
49+
.padding()
50+
.keyboardType(.decimalPad)
51+
Button {
52+
persistenceManager.setStepCount(steps: Int(addDateValue.value)!, arbitrary: true, date: selectedDate)
53+
} label: {
54+
Text("Submit Count")
55+
}.disabled(readyToSubmit(value: addDateValue.value))
56+
}
57+
}
58+
}
59+
60+
class NumbersOnly: ObservableObject {
61+
@Published var value = "" {
62+
didSet {
63+
let filtered = value.filter { $0.isNumber }
64+
65+
if value != filtered {
66+
value = filtered
67+
}
68+
}
69+
}
70+
}

InfiniLink/View Components/Steps View Components/StepProgressGauge.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,22 @@ import SwiftUI
1111

1212
struct StepProgressGauge: View {
1313
@ObservedObject var bleManager = BLEManager.shared
14+
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \StepCounts.timestamp, ascending: true)])
15+
private var existingStepCounts: FetchedResults<StepCounts>
1416
@Binding var currentPercentage: Float
1517
@Binding var stepCountGoal: Int
1618
var calendar: Bool
1719
@State var backgroundColor = Color.clear
1820

21+
func getStepHistory(date: Date) -> Int32 {
22+
for stepCount in existingStepCounts {
23+
if Calendar.current.isDate(stepCount.timestamp!, inSameDayAs: date) { //(stepCount.timestamp!, to: date, toGranularity: .day) == .orderedSame {
24+
return stepCount.steps
25+
}
26+
}
27+
return 0
28+
}
29+
1930
var body: some View {
2031
VStack {
2132
ZStack {
@@ -24,14 +35,14 @@ struct StepProgressGauge: View {
2435
.opacity(0.3)
2536
.foregroundColor(Color.gray)
2637
Circle()
27-
.trim(from: 0.0, to: CGFloat(min(currentPercentage, 1.0)))
38+
.trim(from: 0.0, to: CGFloat(min((Float(getStepHistory(date: Date()))/Float(stepCountGoal)), 1.0)))
2839
.stroke(style: StrokeStyle(lineWidth: calendar ? 5.0 : 20.0, lineCap: .round, lineJoin: .round))
2940
.foregroundColor(Color.blue)
3041
.rotationEffect(Angle(degrees: 270.0))
3142
//.animation(.linear)
3243
if !calendar {
3344
VStack {
34-
Text(String(bleManager.stepCount))
45+
Text(String(getStepHistory(date: Date())))
3546
.font(.largeTitle)
3647
.bold()
3748
Text("Step Goal: \(stepCountGoal)")

InfiniLink/View Components/Steps View Components/StepWeeklyGraph.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,19 @@ struct StepWeeklyChart: View {
2525
let today = calendar.startOfDay(for: Date())
2626
var week = [Date]()
2727
if let thisWeek = calendar.dateInterval(of: .weekOfYear, for: today) {
28-
for i in 0...6 {
29-
if let day = calendar.date(byAdding: .day, value: i, to: thisWeek.start) {
28+
for n in 0...6 {
29+
if let day = calendar.date(byAdding: .day, value: n, to: thisWeek.start) {
3030
week += [day]
3131
let shortFormatter = DateFormatter()
3232
shortFormatter.dateFormat = "EEEEE"
3333
let longFormatter = DateFormatter()
3434
longFormatter.dateFormat = "EEEE"
3535
let color = ColourStyle(colour: .blue)
36+
dataPoints.append(BarChartDataPoint(value: 0, xAxisLabel: shortFormatter.string(from: day), description: longFormatter.string(from: day), date: day, colour: color))
3637

3738
for i in chartPoints {
3839
if calendar.isDate(i.timestamp!, inSameDayAs: day) {
39-
dataPoints.append(BarChartDataPoint(value: Double(i.steps), xAxisLabel: shortFormatter.string(from: day), description: longFormatter.string(from: day), date: i.timestamp!, colour: color))
40-
} else {
41-
dataPoints.append(BarChartDataPoint(value: 0, xAxisLabel: shortFormatter.string(from: day), description: longFormatter.string(from: day), date: i.timestamp!, colour: color))
40+
dataPoints[n] = BarChartDataPoint(value: Double(i.steps), xAxisLabel: shortFormatter.string(from: day), description: longFormatter.string(from: day), date: i.timestamp!, colour: color)
4241
}
4342
}
4443
}
@@ -85,6 +84,6 @@ struct StepWeeklyChart: View {
8584
.touchOverlay(chartData: chartData)
8685
.xAxisLabels(chartData: chartData)
8786
.yAxisLabels(chartData: chartData)
88-
.id(chartData.id)
87+
// .id(chartData.id)
8988
}
9089
}

InfiniLink/View Components/StepsView.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ import SwiftUI
1212
struct StepsView: View {
1313

1414
@ObservedObject var bleManager = BLEManager.shared
15+
@ObservedObject var stepManager = StepCountPersistenceManager.shared
1516
@Environment(\.colorScheme) var colorScheme
16-
@State var stepCountGoal = 10000
17+
@AppStorage("stepCountGoal") var stepCountGoal = 10000
18+
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \StepCounts.timestamp, ascending: true)])
19+
private var chartPoints: FetchedResults<StepCounts>
1720
@State var stepGoalPercentage: Float = 0.0
1821
@State var selection: Int = 2
1922

@@ -26,6 +29,14 @@ struct StepsView: View {
2629
.padding(.leading)
2730
.padding(.vertical)
2831
.frame(alignment: .leading)
32+
Button {
33+
SheetManager.shared.sheetSelection = .stepSettings
34+
SheetManager.shared.showSheet = true
35+
} label: {
36+
Image(systemName: "gear")
37+
.imageScale(.large)
38+
.padding(.vertical)
39+
}
2940
Spacer()
3041
}
3142
TabView(selection: $selection) {
@@ -56,11 +67,14 @@ struct StepsView: View {
5667
.padding(.top)
5768
.tag(3)
5869
}
59-
.onChange(of: bleManager.stepCount) { _ in
60-
stepGoalPercentage = (Float(bleManager.stepCount)/Float(stepCountGoal))
70+
.onChange(of: stepManager.currentCount) { _ in
71+
stepGoalPercentage = (Float(stepManager.currentCount)/Float(stepCountGoal))
72+
}
73+
.onChange(of: stepCountGoal) { _ in
74+
stepGoalPercentage = (Float(stepManager.currentCount)/Float(stepCountGoal))
6175
}
6276
.onAppear {
63-
stepGoalPercentage = (Float(bleManager.stepCount)/Float(stepCountGoal))
77+
stepGoalPercentage = (Float(stepManager.currentCount)/Float(stepCountGoal))
6478
}
6579
}
6680
}

0 commit comments

Comments
 (0)