TheNoiseClock/TheNoiseClock/Features/Alarms/Views/Components/TimeUntilAlarmSection.swift
Matt Bruce 2f59f2aaf8 modern ios26
Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
2026-02-07 10:58:16 -06:00

78 lines
2.4 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")
.foregroundStyle(AppAccent.primary)
Text(timeUntilAlarm)
.font(.subheadline)
.foregroundStyle(.secondary)
}
Text(dayText)
.font(.caption)
.foregroundStyle(.secondary)
}
.frame(maxWidth: .infinity)
.padding(.vertical, 12)
.background(AppSurface.primary)
}
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 {
return alarmTime.formatted(.dateTime.weekday(.wide))
}
}
}