diff options
| author | ivar <i@oiee.no> | 2026-03-24 12:29:47 +0100 |
|---|---|---|
| committer | ivar <i@oiee.no> | 2026-03-24 12:29:47 +0100 |
| commit | c0f84ad32693afed9d53a5962303e91172e95a3d (patch) | |
| tree | 4b6f4d3546cf66d686b3d85bbd68df5c62a5a7c5 /Solsnu.Widget/Solsnu_Widget.swift | |
| parent | def2bdee1007f05d0fb6f1a69b2817d2e03b2c54 (diff) | |
| download | solverv-c0f84ad32693afed9d53a5962303e91172e95a3d.tar.xz solverv-c0f84ad32693afed9d53a5962303e91172e95a3d.zip | |
feat: add location fetching and sun time calculation to widget provider
- Fetch cached user location from AppGroupManager (via App Group storage)
- Check if location is fresh (< 24 hours old)
- Calculate sunrise/sunset times using SunTimes utility if location is fresh
- Pass sun times to SolvervDef for widget display
- Create widget-local copies of AppGroupManager and SunTimes utilities
- Widget maintains midnight refresh policy
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
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 |
