Skip to content

Commit

Permalink
Avoid recomputing solar events in chart
Browse files Browse the repository at this point in the history
  • Loading branch information
daneden committed Jun 22, 2024
1 parent 28ec28d commit 9eaf300
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 35 deletions.
3 changes: 3 additions & 0 deletions Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,9 @@
},
"Current Location" : {

},
"Current Location %@" : {

},
"Customise notification content" : {
"localizations" : {
Expand Down
35 changes: 4 additions & 31 deletions Solstice/Charts/DaylightChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 ))
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Solstice/List View/DaylightSummaryRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct DaylightSummaryRow<Location: ObservableLocation>: View {
DaylightChart(
solar: solar,
timeZone: location.timeZone,
eventTypes: [.sunrise, .sunset],
showEventTypes: false,
includesSummaryTitle: false,
hideXAxis: true,
markSize: 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion Widget/Overview Widget/OverviewWidgetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Widget/Solar Chart Widget/SolarChartWidgetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9eaf300

Please sign in to comment.