192 lines
7.7 KiB
Swift
192 lines
7.7 KiB
Swift
//
|
|
// EditAlarmView.swift
|
|
// TheNoiseClock
|
|
//
|
|
// Created by Matt Bruce on 9/7/25.
|
|
//
|
|
|
|
import SwiftUI
|
|
import Bedrock
|
|
import AudioPlaybackKit
|
|
import Foundation
|
|
|
|
/// View for editing existing alarms
|
|
struct EditAlarmView: View {
|
|
|
|
// MARK: - Properties
|
|
let viewModel: AlarmViewModel
|
|
let alarm: Alarm
|
|
@Environment(\.dismiss) private var dismiss
|
|
@AppStorage(ClockStyle.appStorageKey) private var clockStyleData: Data = Data()
|
|
|
|
@State private var alarmTime: Date
|
|
@State private var selectedSoundName: String
|
|
@State private var alarmLabel: String
|
|
@State private var notificationMessage: String
|
|
@State private var snoozeDuration: Int
|
|
@State private var isVibrationEnabled: Bool
|
|
@State private var isLightFlashEnabled: Bool
|
|
@State private var volume: Float
|
|
|
|
// MARK: - Initialization
|
|
init(viewModel: AlarmViewModel, alarm: Alarm) {
|
|
self.viewModel = viewModel
|
|
self.alarm = alarm
|
|
|
|
// Initialize state with current alarm values
|
|
self._alarmTime = State(initialValue: alarm.time)
|
|
self._selectedSoundName = State(initialValue: alarm.soundName)
|
|
self._alarmLabel = State(initialValue: alarm.label)
|
|
self._notificationMessage = State(initialValue: alarm.notificationMessage)
|
|
self._snoozeDuration = State(initialValue: alarm.snoozeDuration)
|
|
self._isVibrationEnabled = State(initialValue: alarm.isVibrationEnabled)
|
|
self._isLightFlashEnabled = State(initialValue: alarm.isLightFlashEnabled)
|
|
self._volume = State(initialValue: alarm.volume)
|
|
}
|
|
|
|
// MARK: - Body
|
|
var body: some View {
|
|
NavigationView {
|
|
VStack(spacing: 0) {
|
|
// Time picker section at top
|
|
TimePickerSection(selectedTime: $alarmTime)
|
|
TimeUntilAlarmSection(alarmTime: alarmTime)
|
|
|
|
// List for settings below
|
|
List {
|
|
if !isKeepAwakeEnabled {
|
|
Section {
|
|
AlarmLimitationsBanner()
|
|
.listRowInsets(EdgeInsets())
|
|
.listRowBackground(Color.clear)
|
|
.listRowSeparator(.hidden)
|
|
}
|
|
}
|
|
|
|
// Label Section
|
|
NavigationLink(destination: LabelEditView(label: $alarmLabel)) {
|
|
HStack {
|
|
Image(systemName: "textformat")
|
|
.foregroundColor(AppAccent.primary)
|
|
.frame(width: 24)
|
|
Text("Label")
|
|
.foregroundStyle(AppTextColors.primary)
|
|
Spacer()
|
|
Text(alarmLabel)
|
|
.foregroundColor(AppTextColors.secondary)
|
|
}
|
|
}
|
|
.listRowBackground(AppSurface.card)
|
|
|
|
// Notification Message Section
|
|
NavigationLink(destination: NotificationMessageEditView(message: $notificationMessage)) {
|
|
HStack {
|
|
Image(systemName: "message")
|
|
.foregroundColor(AppAccent.primary)
|
|
.frame(width: 24)
|
|
Text("Message")
|
|
.foregroundStyle(AppTextColors.primary)
|
|
Spacer()
|
|
Text(notificationMessage)
|
|
.foregroundColor(AppTextColors.secondary)
|
|
.lineLimit(1)
|
|
}
|
|
}
|
|
.listRowBackground(AppSurface.card)
|
|
|
|
// Sound Section
|
|
NavigationLink(destination: SoundSelectionView(selectedSound: $selectedSoundName)) {
|
|
HStack {
|
|
Image(systemName: "music.note")
|
|
.foregroundColor(AppAccent.primary)
|
|
.frame(width: 24)
|
|
Text("Sound")
|
|
.foregroundStyle(AppTextColors.primary)
|
|
Spacer()
|
|
Text(getSoundDisplayName(selectedSoundName))
|
|
.foregroundColor(AppTextColors.secondary)
|
|
}
|
|
}
|
|
.listRowBackground(AppSurface.card)
|
|
|
|
// Snooze Section
|
|
NavigationLink(destination: SnoozeSelectionView(snoozeDuration: $snoozeDuration)) {
|
|
HStack {
|
|
Image(systemName: "clock.arrow.circlepath")
|
|
.foregroundColor(AppAccent.primary)
|
|
.frame(width: 24)
|
|
Text("Snooze")
|
|
.foregroundStyle(AppTextColors.primary)
|
|
Spacer()
|
|
Text("for \(snoozeDuration) min")
|
|
.foregroundColor(AppTextColors.secondary)
|
|
}
|
|
}
|
|
.listRowBackground(AppSurface.card)
|
|
}
|
|
.listStyle(.insetGrouped)
|
|
.scrollContentBackground(.hidden)
|
|
.background(AppSurface.primary.ignoresSafeArea())
|
|
}
|
|
.navigationTitle("Edit Alarm")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.navigationBarBackButtonHidden(true)
|
|
.toolbar {
|
|
ToolbarItem(placement: .navigationBarLeading) {
|
|
Button("Cancel") {
|
|
dismiss()
|
|
}
|
|
.foregroundColor(AppAccent.primary)
|
|
}
|
|
|
|
ToolbarItem(placement: .navigationBarTrailing) {
|
|
Button("Save") {
|
|
Task {
|
|
let updatedAlarm = Alarm(
|
|
id: alarm.id, // Keep the same ID
|
|
time: alarmTime,
|
|
isEnabled: alarm.isEnabled, // Keep the same enabled state
|
|
soundName: selectedSoundName,
|
|
label: alarmLabel,
|
|
notificationMessage: notificationMessage,
|
|
snoozeDuration: snoozeDuration,
|
|
isVibrationEnabled: isVibrationEnabled,
|
|
isLightFlashEnabled: isLightFlashEnabled,
|
|
volume: volume
|
|
)
|
|
await viewModel.updateAlarm(updatedAlarm)
|
|
dismiss()
|
|
}
|
|
}
|
|
.foregroundColor(AppAccent.primary)
|
|
.fontWeight(.semibold)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Helper Methods
|
|
private func getSoundDisplayName(_ fileName: String) -> String {
|
|
return AlarmSoundService.shared.getSoundDisplayName(fileName)
|
|
}
|
|
|
|
private var isKeepAwakeEnabled: Bool {
|
|
guard let decoded = try? JSONDecoder().decode(ClockStyle.self, from: clockStyleData) else {
|
|
return ClockStyle().keepAwake
|
|
}
|
|
return decoded.keepAwake
|
|
}
|
|
}
|
|
|
|
// MARK: - Preview
|
|
#Preview {
|
|
EditAlarmView(
|
|
viewModel: AlarmViewModel(),
|
|
alarm: Alarm(
|
|
time: Date(),
|
|
label: "Morning Alarm",
|
|
notificationMessage: "Time to wake up!"
|
|
)
|
|
)
|
|
}
|