@@ -12,15 +12,28 @@ import SwiftUI
1212import SwiftUICharts
1313
1414struct StepWeeklyChart : View {
15- @ObservedObject var bleManager = BLEManager . shared
15+ @ObservedObject var bleManager = BLEManager . shared
1616
1717 @Environment ( \. managedObjectContext) var viewContext
18- @Environment ( \. colorScheme) var colorScheme
18+ @Environment ( \. colorScheme) var colorScheme
1919
20- @FetchRequest ( sortDescriptors: [ NSSortDescriptor ( keyPath: \StepCounts . timestamp, ascending: true ) ] ) private var chartPoints : FetchedResults < StepCounts >
20+ @FetchRequest ( sortDescriptors: [ NSSortDescriptor ( keyPath: \StepCounts . timestamp, ascending: true ) ] ) private var chartPoints : FetchedResults < StepCounts >
2121
22- @Binding var stepCountGoal : Int
22+ @Binding var stepCountGoal : Int
2323 @State var displayDate = Date ( )
24+
25+ var weekTitle : String {
26+ let calendar = Calendar . current
27+ if calendar. isDateInToday ( displayDate) {
28+ return " This Week "
29+ } else if calendar. isDate ( displayDate, equalTo: Date ( ) . addingTimeInterval ( - 7 * 24 * 60 * 60 ) , toGranularity: . weekOfYear) {
30+ return " Last Week "
31+ } else {
32+ let dateFormatter = DateFormatter ( )
33+ dateFormatter. dateFormat = " MM/dd/yyyy "
34+ return dateFormatter. string ( from: displayDate)
35+ }
36+ }
2437
2538 func getStepCounts( displayWeek: DateInterval ? ) -> [ BarChartDataPoint ] {
2639 var dataPoints = [ BarChartDataPoint] ( )
@@ -84,34 +97,34 @@ struct StepWeeklyChart: View {
8497
8598 VStack {
8699 HStack {
87- Button {
88- // Update date here...
89- } label : {
100+ Button ( action : {
101+ self . displayDate = Calendar . current . date ( byAdding : . weekOfYear , value : - 1 , to : self . displayDate ) !
102+ } ) {
90103 Image ( systemName: " chevron.left " )
91104 . imageScale ( . medium)
92- . padding ( 14 )
93105 . font ( . body. weight ( . semibold) )
94106 . foregroundColor ( colorScheme == . dark ? . white : . darkGray)
95- . background ( Material . thin)
96- . clipShape ( Circle ( ) )
97107 }
98108 Spacer ( )
99- Text ( " Weekly Steps " )
109+ Text ( weekTitle )
100110 . font ( . title2. weight ( . semibold) )
101111 Spacer ( )
102- Button {
103- // Update date here...
104- } label: {
112+ Button ( action: {
113+ let newDate = Calendar . current. date ( byAdding: . weekOfYear, value: 1 , to: self . displayDate) !
114+ if newDate <= Date ( ) {
115+ self . displayDate = newDate
116+ }
117+ } ) {
105118 Image ( systemName: " chevron.right " )
106119 . imageScale ( . medium)
107- . padding ( 14 )
108120 . font ( . body. weight ( . semibold) )
109121 . foregroundColor ( colorScheme == . dark ? . white : . darkGray)
110- . background ( Material . thin)
111- . clipShape ( Circle ( ) )
112122 }
123+ . disabled ( Calendar . current. date ( byAdding: . weekOfYear, value: 1 , to: self . displayDate) ! > Date ( ) )
124+ . opacity ( Calendar . current. date ( byAdding: . weekOfYear, value: 1 , to: self . displayDate) ! > Date ( ) ? 0.5 : 1.0 )
113125 }
114126 . padding ( . bottom, 25 )
127+ . padding ( . horizontal, 5 )
115128 BarChart ( chartData: chartData)
116129 . yAxisPOI ( chartData: chartData, markerName: " Step Goal " , markerValue: Double ( stepCountGoal) , labelColour: Color ( . lightGray) . opacity ( 0.25 ) , lineColour: Color ( . lightGray) . opacity ( 0.25 ) , strokeStyle: StrokeStyle . init ( dash: [ 5 ] ) )
117130 . floatingInfoBox ( chartData: chartData)
0 commit comments