summaryrefslogtreecommitdiffstats
path: root/Solsnu.Widget/Solsnu_Widget.swift
blob: bc6fc0ed33a318a69fcbe094f1feb4992e731d1e (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
//
//  Solsnu_Widget.swift
//  Solsnu.Widget
//
//  Created by Ivar Løvlie on 15/12/2025.
//

import WidgetKit
import SwiftUI

// MARK: - Provider

struct Provider: TimelineProvider {
    func placeholder(in context: Context) -> SolvervEntry {
        .placeholder
    }

    func getSnapshot(in context: Context, completion: @escaping (SolvervEntry) -> Void) {
        completion(makeEntry(for: Date()))
    }

    func getTimeline(in context: Context, completion: @escaping (Timeline<SolvervEntry>) -> Void) {
        let entry = makeEntry(for: Date())
        let nextMidnight = Calendar.current.nextDate(
            after: Date(),
            matching: DateComponents(hour: 0, minute: 0, second: 0),
            matchingPolicy: .nextTime
        ) ?? Date().addingTimeInterval(86_400)
        completion(Timeline(entries: [entry], policy: .after(nextMidnight)))
    }

    private func makeEntry(for date: Date) -> SolvervEntry {
        let progress = SolsticeData.shared.progressToNextEvent(from: date)
        let ratio = progress.map {
            max(0, min(1, Double($0.elapsed) / Double($0.total)))
        } ?? 0

        let sunTimes = AppGroupManager.shared.getLocation().map {
            SunTimes(latitude: $0.latitude, longitude: $0.longitude, date: date)
        }

        return SolvervEntry(
            date: date,
            nextEvent: SolsticeData.shared.nextEvent(from: date),
            currentEvent: SolsticeData.shared.currentEvent(from: date),
            progressRatio: ratio,
            sunriseTime: sunTimes?.sunrise(),
            sunsetTime: sunTimes?.sunset()
        )
    }
}

// MARK: - Entry

struct SolvervEntry: TimelineEntry {
    let date: Date
    let nextEvent: SolsticeEvent?
    let currentEvent: SolsticeEvent?
    let progressRatio: Double
    let sunriseTime: Date?
    let sunsetTime: Date?

    static let placeholder = SolvervEntry(
        date: Date(),
        nextEvent: nil,
        currentEvent: nil,
        progressRatio: 0.4,
        sunriseTime: nil,
        sunsetTime: nil
    )
}

// MARK: - Family dispatch

struct SolsticeWidgetView: View {
    let entry: SolvervEntry
    @Environment(\.widgetFamily) var widgetFamily

    var body: some View {
        switch widgetFamily {
        case .systemMedium:
            MediumWidgetView(entry: entry)
        default:
            SmallWidgetView(entry: entry)
        }
    }
}

// MARK: - Widget

struct Solsnu_Widget: Widget {
    let kind = "Solsnu_Widget"

    var body: some WidgetConfiguration {
        StaticConfiguration(kind: kind, provider: Provider()) { entry in
            SolsticeWidgetView(entry: entry)
        }
        .contentMarginsDisabled()
        .configurationDisplayName("Solstice Countdown")
        .description("Days until next solstice or equinox")
        .supportedFamilies([.systemSmall, .systemMedium])
    }
}

// MARK: - Previews

#Preview(as: .systemSmall) {
    Solsnu_Widget()
} timeline: {
    let now = Date()
    SolvervEntry(
        date: now,
        nextEvent: SolsticeData.shared.nextEvent(from: now),
        currentEvent: SolsticeData.shared.currentEvent(from: now),
        progressRatio: 0.4,
        sunriseTime: Calendar.current.date(bySettingHour: 6, minute: 28, second: 0, of: now),
        sunsetTime: Calendar.current.date(bySettingHour: 21, minute: 15, second: 0, of: now)
    )
    SolvervEntry.placeholder
}

#Preview(as: .systemMedium) {
    Solsnu_Widget()
} timeline: {
    let now = Date()
    SolvervEntry(
        date: now,
        nextEvent: SolsticeData.shared.nextEvent(from: now),
        currentEvent: SolsticeData.shared.currentEvent(from: now),
        progressRatio: 0.4,
        sunriseTime: Calendar.current.date(bySettingHour: 6, minute: 28, second: 0, of: now),
        sunsetTime: Calendar.current.date(bySettingHour: 21, minute: 15, second: 0, of: now)
    )
    SolvervEntry.placeholder
}