From 9eaf30042c8dfd65844874036ade889260ba57a3 Mon Sep 17 00:00:00 2001 From: Daniel Eden Date: Sat, 22 Jun 2024 08:35:32 +0100 Subject: [PATCH] Avoid recomputing solar events in chart --- Localizable.xcstrings | 3 ++ Solstice/Charts/DaylightChart.swift | 35 +++---------------- Solstice/List View/DaylightSummaryRow.swift | 2 +- ...rviewWidgetView+AccessoryWidgetViews.swift | 2 +- .../Overview Widget/OverviewWidgetView.swift | 2 +- .../SolarChartWidgetView.swift | 2 +- 6 files changed, 11 insertions(+), 35 deletions(-) diff --git a/Localizable.xcstrings b/Localizable.xcstrings index ec748c0c..7112e81f 100644 --- a/Localizable.xcstrings +++ b/Localizable.xcstrings @@ -545,6 +545,9 @@ }, "Current Location" : { + }, + "Current Location %@" : { + }, "Customise notification content" : { "localizations" : { diff --git a/Solstice/Charts/DaylightChart.swift b/Solstice/Charts/DaylightChart.swift index ef064150..0d2b28d8 100644 --- a/Solstice/Charts/DaylightChart.swift +++ b/Solstice/Charts/DaylightChart.swift @@ -18,7 +18,7 @@ struct DaylightChart: View { var solar: Solar var timeZone: TimeZone - var eventTypes: [Solar.Phase] = Solar.Phase.allCases + var showEventTypes = true var appearance = Appearance.simple var includesSummaryTitle = true @@ -27,8 +27,6 @@ struct DaylightChart: View { var markSize: CGFloat = 6 var yScale = -1.5...1.5 - @State private var solarEvents: [Solar.Event] = [] - var markForegroundColor: Color { if appearance == .graphical { return .white @@ -61,7 +59,7 @@ struct DaylightChart: View { } Chart { - ForEach(solarEvents.filter { range.contains($0.date) }) { solarEvent in + ForEach(solar.events.filter { range.contains($0.date) }) { solarEvent in PointMark( x: .value("Event Time", solarEvent.date), y: .value("Event", yValue(for: solarEvent.date )) @@ -246,33 +244,8 @@ struct DaylightChart: View { .blendMode(.plusLighter) .environment(\.colorScheme, .dark) } - .task(id: solar.date, priority: .background) { - solarEvents = [] - do { - try await Task.sleep(nanoseconds: 250_000_000) - if solarEvents.isEmpty { resetSolarEvents() } - } catch { - print(error) - } - } - .onAppear { - resetSolarEvents() - } .environment(\.timeZone, timeZone) } - - func resetSolarEvents() { - let events = solar.events.filter { event in - eventTypes.contains { phase in - event.phase == phase - } - } - .compactMap { $0 } - - withAnimation { - solarEvents = events - } - } } extension DaylightChart { @@ -312,7 +285,7 @@ extension DaylightChart { } func resetSelectedEvent() { - selectedEvent = solarEvents.filter { + selectedEvent = solar.events.filter { $0.phase == .sunset || $0.phase == .sunrise }.sorted(by: { a, b in a.date.compare(.now) == .orderedDescending @@ -343,7 +316,7 @@ extension DaylightChart { currentX = proxy.value(atX: xCurrent) if let currentX, - let nearestEvent = solarEvents.sorted(by: { lhs, rhs in + let nearestEvent = solar.events.sorted(by: { lhs, rhs in abs(lhs.date.distance(to: currentX)) <= abs(rhs.date.distance(to: currentX)) }).first { selectedEvent = nearestEvent diff --git a/Solstice/List View/DaylightSummaryRow.swift b/Solstice/List View/DaylightSummaryRow.swift index aab927c1..a9842b61 100644 --- a/Solstice/List View/DaylightSummaryRow.swift +++ b/Solstice/List View/DaylightSummaryRow.swift @@ -49,7 +49,7 @@ struct DaylightSummaryRow: View { DaylightChart( solar: solar, timeZone: location.timeZone, - eventTypes: [.sunrise, .sunset], + showEventTypes: false, includesSummaryTitle: false, hideXAxis: true, markSize: 2 diff --git a/Widget/Overview Widget/OverviewWidgetView+AccessoryWidgetViews.swift b/Widget/Overview Widget/OverviewWidgetView+AccessoryWidgetViews.swift index f269c0d9..201be433 100644 --- a/Widget/Overview Widget/OverviewWidgetView+AccessoryWidgetViews.swift +++ b/Widget/Overview Widget/OverviewWidgetView+AccessoryWidgetViews.swift @@ -19,7 +19,7 @@ extension OverviewWidgetView { DaylightChart( solar: solar, timeZone: location.timeZone, - eventTypes: [.sunset, .sunrise], + showEventTypes: false, appearance: renderingMode == .fullColor ? .graphical : .simple, includesSummaryTitle: false, hideXAxis: true, diff --git a/Widget/Overview Widget/OverviewWidgetView.swift b/Widget/Overview Widget/OverviewWidgetView.swift index 85a04bfd..9517ec9a 100644 --- a/Widget/Overview Widget/OverviewWidgetView.swift +++ b/Widget/Overview Widget/OverviewWidgetView.swift @@ -75,7 +75,7 @@ struct OverviewWidgetView: View { DaylightChart( solar: solar, timeZone: location.timeZone, - eventTypes: [.sunrise, .sunset], + showEventTypes: false, includesSummaryTitle: false, hideXAxis: true, markSize: family == .systemSmall ? 3 : 5 diff --git a/Widget/Solar Chart Widget/SolarChartWidgetView.swift b/Widget/Solar Chart Widget/SolarChartWidgetView.swift index 8e81ad14..7045ea38 100644 --- a/Widget/Solar Chart Widget/SolarChartWidgetView.swift +++ b/Widget/Solar Chart Widget/SolarChartWidgetView.swift @@ -52,7 +52,7 @@ struct SolarChartWidgetView: View { DaylightChart( solar: solar, timeZone: location.timeZone, - eventTypes: [], + showEventTypes: false, includesSummaryTitle: false, markSize: 3, yScale: -1.0...1.5