1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
//
// Solsnu_Widget.swift
// Solsnu.Widget
//
// Created by Ivar Løvlie on 15/12/2025.
//
import WidgetKit
import SwiftUI
import Foundation
struct Provider: TimelineProvider {
func placeholder(in context: Context) -> SolvervEntry {
SolvervEntry(def: SolvervDef(date: Date()))
}
func getSnapshot(in context: Context, completion: @escaping (SolvervEntry) -> ()) {
let entry = SolvervEntry(def: SolvervDef(date: Date()))
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<SolvervEntry>) -> ()) {
var entries: [SolvervEntry] = []
let currentDate = Date()
// 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
components.minute = 0
components.second = 0
let todayMidnight = calendar.date(from: components)!
let nextMidnight = calendar.date(byAdding: .day, value: 1, to: todayMidnight)!
let timeline = Timeline(entries: entries, policy: .after(nextMidnight))
completion(timeline)
}
}
struct SolvervEntry: TimelineEntry {
let date: Date
let def: SolvervDef
init(def: SolvervDef, date: Date? = nil) {
self.date = date ?? def.date
self.def = def
}
}
struct Solsnu_Widget: Widget {
let kind: String = "Solsnu_Widget_Small"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
SmallWidgetView(entry: entry)
}
.contentMarginsDisabled()
.configurationDisplayName("Solstice Countdown")
.description("Days until next solstice or equinox")
.supportedFamilies([.systemSmall,.systemMedium])
}
}
#Preview(as: .systemSmall) {
Solsnu_Widget()
} timeline: {
// Test with valid sunrise/sunset times (spring equinox, Oslo approx)
let springDate = Calendar.current.date(from: DateComponents(year: 2026, month: 3, day: 20))!
let sunriseTime = Calendar.current.date(bySettingHour: 7, minute: 30, second: 0, of: springDate)!
let sunsetTime = Calendar.current.date(bySettingHour: 19, minute: 45, second: 0, of: springDate)!
SolvervEntry(def: SolvervDef(date: springDate, sunriseTime: sunriseTime, sunsetTime: sunsetTime))
// Test with nil times (no location)
SolvervEntry(def: SolvervDef(date: springDate, sunriseTime: nil, sunsetTime: nil))
// Original equinox dates
SolvervEntry(def: SolvervDef(utcString: "2026-03-20 14:46:00"))
SolvervEntry(def: SolvervDef(utcString: "2026-06-21 08:25:00"))
SolvervEntry(def: SolvervDef(utcString: "2026-09-23 00:06:00"))
SolvervEntry(def: SolvervDef(utcString: "2026-12-21 20:50:00"))
}
#Preview(as: .systemMedium) {
Solsnu_Widget()
} timeline: {
// Test medium widget with valid sunrise/sunset times
let springDate = Calendar.current.date(from: DateComponents(year: 2026, month: 3, day: 20))!
let sunriseTime = Calendar.current.date(bySettingHour: 7, minute: 30, second: 0, of: springDate)!
let sunsetTime = Calendar.current.date(bySettingHour: 19, minute: 45, second: 0, of: springDate)!
SolvervEntry(def: SolvervDef(date: springDate, sunriseTime: sunriseTime, sunsetTime: sunsetTime))
// Test medium widget with nil times
SolvervEntry(def: SolvervDef(date: springDate, sunriseTime: nil, sunsetTime: nil))
// Original equinox dates
SolvervEntry(def: SolvervDef(utcString: "2026-03-20 14:46:00"))
SolvervEntry(def: SolvervDef(utcString: "2026-06-21 08:25:00"))
}
struct SolvervDef {
let date: Date
let bg: String
let sunriseTime: Date?
let sunsetTime: Date?
// Cached formatter for performance
private static let timeFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
formatter.timeZone = TimeZone.current
return formatter
}()
init(date: Date, sunriseTime: Date? = nil, sunsetTime: Date? = nil) {
self.date = date
self.sunriseTime = sunriseTime
self.sunsetTime = sunsetTime
self.bg = "smallbg"
}
init(utcString: String, sunriseTime: Date? = nil, sunsetTime: Date? = nil) {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
formatter.timeZone = TimeZone(abbreviation: "UTC")
// Should probably guard this, but not now
let date = formatter.date(from: utcString)!
self.date = date
self.sunriseTime = sunriseTime
self.sunsetTime = sunsetTime
self.bg = "smallbg"
}
var sunriseFormatted: String {
guard let time = sunriseTime else { return "" }
return Self.timeFormatter.string(from: time)
}
var sunsetFormatted: String {
guard let time = sunsetTime else { return "" }
return Self.timeFormatter.string(from: time)
}
}
extension SolvervDef {
var season: Season {
guard let next = nextEvent else { return .winter }
return next.season
}
var nextEvent: SolsticeEvent? {
SolsticeData.shared.nextEvent(from: date)
}
func daysUntilNext() -> Int {
guard let next = nextEvent else { return 0 }
return next.daysUntil(from: date)
}
func progressRatio() -> Double {
guard let progress = SolsticeData.shared.progressToNextEvent(from: date) else { return 0.0 }
let ratio = Double(progress.elapsed) / Double(progress.total)
return max(0, min(1.0, ratio))
}
func upcomingEventsPreview(count: Int) -> [SolsticeEvent] {
SolsticeData.shared.upcomingEvents(count: count, from: date)
}
static var preview: SolvervDef {
SolvervDef(date: Date())
}
}
|