79 lines
2.5 KiB
Swift
79 lines
2.5 KiB
Swift
//
|
|
// TimeUntilAlarmSection.swift
|
|
// TheNoiseClock
|
|
//
|
|
// Created by Matt Bruce on 9/8/25.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
/// Component showing time until alarm and day information
|
|
struct TimeUntilAlarmSection: View {
|
|
let alarmTime: Date
|
|
|
|
var body: some View {
|
|
VStack(spacing: 4) {
|
|
HStack {
|
|
Image(systemName: "calendar")
|
|
.foregroundColor(.orange)
|
|
Text(timeUntilAlarm)
|
|
.font(.subheadline)
|
|
.foregroundColor(.secondary)
|
|
}
|
|
|
|
Text(dayText)
|
|
.font(.caption)
|
|
.foregroundColor(.secondary)
|
|
}
|
|
.padding(.vertical, 12)
|
|
.background(Color(.systemGroupedBackground))
|
|
}
|
|
|
|
private var timeUntilAlarm: String {
|
|
let now = Date()
|
|
let calendar = Calendar.current
|
|
|
|
// Calculate the next occurrence of the alarm time
|
|
let nextAlarmTime: Date
|
|
if calendar.isDateInToday(alarmTime) && alarmTime < now {
|
|
// If alarm time has passed today, calculate for tomorrow
|
|
nextAlarmTime = calendar.date(byAdding: .day, value: 1, to: alarmTime) ?? alarmTime
|
|
} else {
|
|
// Use the alarm time as-is (either future today or already tomorrow+)
|
|
nextAlarmTime = alarmTime
|
|
}
|
|
|
|
// Calculate time difference from now to next alarm
|
|
let components = calendar.dateComponents([.hour, .minute], from: now, to: nextAlarmTime)
|
|
if let hours = components.hour, let minutes = components.minute {
|
|
if hours > 0 {
|
|
return "Will turn on in \(hours)h \(minutes)m"
|
|
} else if minutes > 0 {
|
|
return "Will turn on in \(minutes)m"
|
|
} else {
|
|
return "Will turn on now"
|
|
}
|
|
}
|
|
|
|
return "Will turn on tomorrow"
|
|
}
|
|
|
|
private var dayText: String {
|
|
let calendar = Calendar.current
|
|
let now = Date()
|
|
|
|
// If alarm time is in the past today, show tomorrow
|
|
if calendar.isDateInToday(alarmTime) && alarmTime < now {
|
|
return "Tomorrow"
|
|
} else if calendar.isDateInToday(alarmTime) {
|
|
return "Today"
|
|
} else if calendar.isDateInTomorrow(alarmTime) {
|
|
return "Tomorrow"
|
|
} else {
|
|
let formatter = DateFormatter()
|
|
formatter.dateFormat = "EEEE"
|
|
return formatter.string(from: alarmTime)
|
|
}
|
|
}
|
|
}
|