diff options
Diffstat (limited to 'Solsnu.Widget/Solsnu_Widget.swift')
| -rw-r--r-- | Solsnu.Widget/Solsnu_Widget.swift | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/Solsnu.Widget/Solsnu_Widget.swift b/Solsnu.Widget/Solsnu_Widget.swift index 5da5621..a7a5764 100644 --- a/Solsnu.Widget/Solsnu_Widget.swift +++ b/Solsnu.Widget/Solsnu_Widget.swift @@ -7,6 +7,7 @@ import WidgetKit import SwiftUI +import Foundation struct Provider: TimelineProvider { func placeholder(in context: Context) -> SolvervEntry { @@ -21,8 +22,29 @@ struct Provider: TimelineProvider { func getTimeline(in context: Context, completion: @escaping (Timeline<SolvervEntry>) -> ()) { var entries: [SolvervEntry] = [] let currentDate = Date() - let entry = SolvervEntry(def: SolvervDef(date: currentDate)) + + // Fetch location from AppGroupManager + var sunriseTime: Date? = nil + var sunsetTime: Date? = nil + + if let location = AppGroupManager.shared.getLocation() { + // Check if location is fresh (< 24 hours old) + let isoFormatter = ISO8601DateFormatter() + if let locationTimestamp = isoFormatter.date(from: location.timestamp) { + let hoursSinceCache = currentDate.timeIntervalSince(locationTimestamp) / 3600.0 + + if hoursSinceCache < 24.0 { + // Location is fresh, calculate sun times + let calculator = SunTimes(latitude: location.latitude, longitude: location.longitude, date: currentDate) + sunriseTime = calculator.sunrise() + sunsetTime = calculator.sunset() + } + } + } + + let entry = SolvervEntry(def: SolvervDef(date: currentDate, sunriseTime: sunriseTime, sunsetTime: sunsetTime)) entries.append(entry) + let calendar = Calendar.current var components = calendar.dateComponents([.year, .month, .day], from: currentDate) components.hour = 0 |
