88
99
1010import 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}
0 commit comments