SPB Git

Repository Public License

AlarmLiveActivity.swift 5.6 KB 117 lines swift

1// ORIX Alarm2// File: Widgets/AlarmLiveActivity.swift3// Description: Live Activity and Dynamic Island for AlarmKit alarms: snooze and focus-timer countdowns, paused timer and alerting state.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import ActivityKit8import AlarmKit9import AppIntents10import SwiftUI11import WidgetKit1213struct AlarmLiveActivity: Widget {14    var body: some WidgetConfiguration {15        ActivityConfiguration(for: AlarmAttributes<ORIXAlarmMetadata>.self) { context in16            LockScreenAlarmView(attributes: context.attributes, state: context.state)17                .padding(16)18                .activityBackgroundTint(WidgetTheme.midnight.opacity(0.85))19                .activitySystemActionForegroundColor(.white)20        } dynamicIsland: { context in21            DynamicIsland {22                DynamicIslandExpandedRegion(.leading) {23                    Label(title(context.attributes, context.state), systemImage: symbol(context.attributes))24                        .font(.system(.caption, design: .rounded, weight: .semibold)).foregroundStyle(context.attributes.tintColor).lineLimit(1)25                }26                DynamicIslandExpandedRegion(.trailing) {27                    AlarmCountdownText(state: context.state).font(.system(.title3, design: .rounded, weight: .semibold)).monospacedDigit()28                        .frame(maxWidth: 96, alignment: .trailing)29                }30                DynamicIslandExpandedRegion(.bottom) {31                    AlarmControls(attributes: context.attributes, state: context.state)32                }33            } compactLeading: {34                Image(systemName: symbol(context.attributes)).foregroundStyle(context.attributes.tintColor)35            } compactTrailing: {36                AlarmCountdownText(state: context.state).monospacedDigit().frame(maxWidth: 52)37            } minimal: {38                Image(systemName: symbol(context.attributes)).foregroundStyle(context.attributes.tintColor)39            }40            .keylineTint(context.attributes.tintColor)41        }42    }43}4445private func symbol(_ attributes: AlarmAttributes<ORIXAlarmMetadata>) -> String { attributes.metadata?.symbol ?? "alarm.fill" }4647private func title(_ attributes: AlarmAttributes<ORIXAlarmMetadata>, _ state: AlarmPresentationState) -> LocalizedStringResource {48    switch state.mode {49    case .countdown: attributes.presentation.countdown?.title ?? "Alarm"50    case .paused: attributes.presentation.paused?.title ?? "Paused"51    case .alert: attributes.presentation.alert.title52    @unknown default: "Alarm"53    }54}5556/// Remaining time, rendered by the system so it keeps counting without updates from the app.57struct AlarmCountdownText: View {58    let state: AlarmPresentationState59    var body: some View {60        switch state.mode {61        case .countdown(let countdown):62            Text(timerInterval: Date.now...max(countdown.fireDate, Date.now), countsDown: true)63        case .paused(let paused):64            let remaining = Duration.seconds(max(0, paused.totalCountdownDuration - paused.previouslyElapsedDuration))65            Text(remaining.formatted(.time(pattern: .minuteSecond)))66        case .alert(let alert):67            Text(String(format: "%d:%02d", alert.time.hour, alert.time.minute))68        @unknown default:69            EmptyView()70        }71    }72}7374struct LockScreenAlarmView: View {75    let attributes: AlarmAttributes<ORIXAlarmMetadata>76    let state: AlarmPresentationState77    var body: some View {78        VStack(alignment: .leading, spacing: 10) {79            HStack(alignment: .center) {80                Image(systemName: symbol(attributes)).font(.title2).foregroundStyle(attributes.tintColor).accessibilityHidden(true)81                VStack(alignment: .leading, spacing: 2) {82                    Text(title(attributes, state)).font(.system(.subheadline, design: .rounded, weight: .semibold)).foregroundStyle(.white.opacity(0.7))83                    if let name = attributes.metadata?.name, !name.isEmpty, attributes.metadata?.role != .focusTimer {84                        Text(name).font(.system(.headline, design: .rounded)).foregroundStyle(.white).lineLimit(1)85                    }86                }87                Spacer()88                AlarmCountdownText(state: state).font(.system(size: 36, weight: .light, design: .rounded)).monospacedDigit().foregroundStyle(.white)89                    .frame(maxWidth: 140, alignment: .trailing)90            }91            AlarmControls(attributes: attributes, state: state)92        }93    }94}9596/// Pause / resume for the focus timer; a hint for snoozes that end with a wake mission.97struct AlarmControls: View {98    let attributes: AlarmAttributes<ORIXAlarmMetadata>99    let state: AlarmPresentationState100    var body: some View {101        if let metadata = attributes.metadata {102            switch (metadata.role, state.mode) {103            case (.focusTimer, .countdown):104                Button(intent: PauseTimerIntent(systemID: state.alarmID)) { Label("Pause", systemImage: "pause.fill").frame(maxWidth: .infinity) }105                    .buttonStyle(.bordered).tint(attributes.tintColor)106            case (.focusTimer, .paused):107                Button(intent: ResumeTimerIntent(systemID: state.alarmID)) { Label("Resume", systemImage: "play.fill").frame(maxWidth: .infinity) }108                    .buttonStyle(.bordered).tint(attributes.tintColor)109            case (.snooze, .countdown) where metadata.hasMission:110                Label("A wake mission follows the next alarm", systemImage: "puzzlepiece.extension.fill")111                    .font(.caption2).foregroundStyle(.white.opacity(0.6))112            default: EmptyView()113            }114        }115    }116}117
Files main
Menu

Theme