@@ -92,6 +92,11 @@ struct HeartChartView: View {
9292 point. min == point. max
9393 }
9494
95+ func updateYScale( ) {
96+ displayedMin = visibleMin
97+ displayedMax = visibleMax
98+ }
99+
95100 @ChartContentBuilder
96101 func chartContent( for point: HeartChartDataPoint ) -> some ChartContent {
97102 if isSingleReading ( point) {
@@ -122,6 +127,7 @@ struct HeartChartView: View {
122127 }
123128 }
124129
130+ // fixed graph
125131 func updateDisplayed( ) {
126132 displayedDate = windowStart
127133 displayedMin = Int ( windowPoints. map ( { $0. min } ) . min ( ) ?? 0 )
@@ -258,30 +264,36 @@ struct HeartChartView: View {
258264 }
259265 . onAppear {
260266 points = heartPoints ( )
261- scrollPositionDate = windowStart
267+ scrollPositionDate = Date ( timeInterval: - 86400 , since: latestDate)
268+ displayedDate = scrollPositionDate
262269 updateDisplayed ( )
263270 }
264- . onChange ( of: dayOffset) { _ in
271+ . onChange ( of: bleManager. heartRate) { _ in
272+ let previousLatest = latestDate
273+ let wasAtLatest = scrollPositionDate >= Date ( timeInterval: - 86400 , since: previousLatest)
274+ points = heartPoints ( )
275+ if Calendar . current. component ( . hour, from: latestDate) > Calendar . current. component ( . hour, from: previousLatest) {
276+ if wasAtLatest {
277+ scrollPositionDate = Date ( timeInterval: - 86400 , since: latestDate)
278+ }
279+ }
265280 updateDisplayed ( )
281+ updateYScale ( ) // scrollable chart
266282 }
283+ // scrollable graph
267284 . onChange ( of: scrollPositionDate) { newValue in
268285 displayedDate = newValue
269286 }
270287 . onChange ( of: scrollPositionDate) { newValue in
271288 Task {
272289 try ? await Task . sleep ( nanoseconds: 300_000_000 )
273290 if scrollPositionDate == newValue {
274- let clamped = min ( max ( newValue, Calendar . current. startOfDay ( for: earliestDate) ) , Calendar . current. startOfDay ( for: latestDate) )
275- if clamped != newValue {
276- scrollPositionDate = clamped
277- }
278- displayedMin = visibleMin
279- displayedMax = visibleMax
291+ updateYScale ( )
280292 }
281293 }
282294 }
283- . onChange ( of : bleManager . heartRate ) { _ in
284- points = heartPoints ( )
295+ // fixed graph
296+ . onChange ( of : dayOffset ) { _ in
285297 updateDisplayed ( )
286298 }
287299 }
0 commit comments