summaryrefslogtreecommitdiffstats
path: root/Solsnu.Widget/Solsnu_Widget.swift
blob: a7a5764b94076f201ecc38a8000253b7d9425002 (plain) (blame)
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
//
//  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: {
    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"))
}

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()
    }

    func daysUntilNext() -> Int {
        guard let next = nextEvent else { return 0 }
        return next.daysUntil()
    }

    func progressRatio() -> Double {
        guard let progress = SolsticeData.shared.progressToNextEvent() 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)
    }

    static var preview: SolvervDef {
        SolvervDef(date: Date())
    }
}