summaryrefslogtreecommitdiffstats
path: root/Solverv/Views/InfoScreenView.swift
blob: d876a456302cb6cda96d0ae105c91724b174204c (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
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
196
197
198
199
200
//
//  InfoScreenView.swift
//  Solverv
//
//  Created by Ivar Løvlie on 23/03/2026.
//

import SwiftUI
import Combine

struct InfoScreenView: View {
    @State private var upcomingEvents: [SolsticeEvent] = []
    @State private var nextEvent: SolsticeEvent?
    @State private var progressData: (elapsed: Int, total: Int)? = nil
    @State private var sunriseTime: Date?
    @State private var sunsetTime: Date?

    var body: some View {
        ScrollView {
            VStack(spacing: 24) {
                // Top section: Image + countdown
                VStack(spacing: 16) {
                    if let next = nextEvent {
                        Image(next.season.assetName)
                            .resizable()
                            .scaledToFit()
                            .frame(height: 200)
                            .clipped()

                        VStack(spacing: 8) {
                            Text(next.name)
                                .font(.title3)

                            Text("\(next.daysUntil())")
                                .font(.system(.title, design: .default).weight(.bold))
                                .foregroundColor(next.season.colorLight)

                            Text("days remaining")
                                .font(.caption)
                                .foregroundColor(.secondary)
                        }

                        if let progress = progressData {
                            ProgressView(value: Double(progress.elapsed) / Double(progress.total))
                                .tint(next.season.colorLight)
                        }
                    }
                }
                .padding()
                .background(Color(.systemGray6))
                .cornerRadius(12)

                // Middle section: Sun times + season info
                VStack(spacing: 16) {
                    VStack(alignment: .leading, spacing: 12) {
                        Text("Today's Sun Times")
                            .font(.headline)

                        HStack {
                            Label("Sunrise", systemImage: "sunrise.fill")
                            Spacer()
                            if let sunrise = sunriseTime {
                                Text(sunrise, style: .time)
                            } else {
                                Text("—").foregroundColor(.secondary)
                            }
                        }

                        HStack {
                            Label("Sunset", systemImage: "sunset.fill")
                            Spacer()
                            if let sunset = sunsetTime {
                                Text(sunset, style: .time)
                            } else {
                                Text("—").foregroundColor(.secondary)
                            }
                        }
                    }

                    Divider()

                    if let next = nextEvent {
                        VStack(alignment: .leading, spacing: 8) {
                            Text("Current Season")
                                .font(.headline)

                            HStack(spacing: 8) {
                                Circle()
                                    .fill(next.season.colorLight)
                                    .frame(width: 12, height: 12)

                                VStack(alignment: .leading, spacing: 4) {
                                    Text(next.season.displayName)
                                        .font(.subheadline)

                                    Text(next.season.description)
                                        .font(.caption)
                                        .foregroundColor(.secondary)
                                }
                            }
                        }
                    }
                }
                .padding()
                .background(Color(.systemGray6))
                .cornerRadius(12)

                // Bottom section: Upcoming events list
                eventsListSection
            }
            .padding()
        }
        .navigationTitle("Solstices & Equinoxes")
        .onAppear(perform: loadData)
        .onReceive(Timer.publish(every: 60, on: .main, in: .common).autoconnect()) { _ in
            loadData()
        }
    }

    private var eventsListSection: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text("Upcoming Events")
                .font(.headline)

            if upcomingEvents.isEmpty {
                Text("No upcoming events")
                    .foregroundColor(.secondary)
                    .font(.caption)
            } else {
                VStack(spacing: 0) {
                    ForEach(Array(upcomingEvents.enumerated()), id: \.element.id) { index, event in
                        VStack(spacing: 0) {
                            HStack {
                                VStack(alignment: .leading, spacing: 2) {
                                    Text(event.name)
                                        .font(.subheadline)
                                        .lineLimit(1)

                                    Text(formatEventDate(event.localDateTime()))
                                        .font(.caption)
                                        .foregroundColor(.secondary)
                                }

                                Spacer()

                                VStack(alignment: .trailing, spacing: 2) {
                                    Text("\(event.daysUntil())d")
                                        .font(.subheadline)

                                    Circle()
                                        .fill(event.season.colorLight)
                                        .frame(width: 12, height: 12)
                                }
                            }
                            .padding(.vertical, 8)

                            if index < upcomingEvents.count - 1 {
                                Divider()
                            }
                        }
                    }
                }
            }
        }
        .padding()
        .background(Color(.systemGray6))
        .cornerRadius(12)
    }

    private func loadData() {
        nextEvent = SolsticeData.shared.nextEvent()
        upcomingEvents = SolsticeData.shared.upcomingEvents(count: 12)
        progressData = SolsticeData.shared.progressToNextEvent()

        // Load sunrise/sunset from location if available
        if let location = AppGroupManager.shared.getLocation() {
            let sunTimes = SunTimes(
                latitude: location.latitude,
                longitude: location.longitude,
                date: Date()
            )
            sunriseTime = sunTimes.sunrise()
            sunsetTime = sunTimes.sunset()
        }
    }

    private func formatEventDate(_ date: Date) -> String {
        let formatter = DateFormatter()
        formatter.dateStyle = .short
        formatter.timeStyle = .short
        formatter.timeZone = .current
        return formatter.string(from: date)
    }
}

#Preview {
    NavigationStack {
        InfoScreenView()
    }
}