SPB Git

Repository Public License

RootView.swift 6.7 KB 129 lines swift

1// ORIX Alarm2// File: App/Sources/RootView.swift3// Description: Root navigation: five tabs, onboarding, alarm editor sheet, full-screen wake mission and Nightstand mode.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import AlarmCore8import SwiftUI910struct RootView: View {11    @Environment(AppModel.self) private var model1213    var body: some View {14        @Bindable var model = model15        TabView(selection: $model.selectedTab) {16            Tab("Today", systemImage: "sun.horizon.fill", value: AppTab.today) { DashboardView() }17            Tab("Alarms", systemImage: "alarm.fill", value: AppTab.alarms) { AlarmListView() }18            Tab("Studio", systemImage: "wand.and.stars", value: AppTab.studio) { StudioView() }19            Tab("Sleep", systemImage: "moon.stars.fill", value: AppTab.sleep) { SleepView() }20            Tab("Insights", systemImage: "chart.xyaxis.line", value: AppTab.insights) { InsightsView() }21        }22        .preferredColorScheme(.dark)23        .sheet(item: $model.editingAlarm) { alarm in24            AlarmEditorView(alarm: alarm, isNew: model.alarm(alarm.id) == nil).tint(Theme.sunrise)25        }26        .fullScreenCover(item: $model.missionRun) { run in27            MissionRunnerView(run: run)28        }29        .fullScreenCover(isPresented: Binding(get: { model.nightstandAlarmID != nil }, set: { if !$0 { model.nightstandAlarmID = nil } })) {30            if let id = model.nightstandAlarmID, let alarm = model.alarm(id) { NightstandView(alarm: alarm) }31        }32        .fullScreenCover(isPresented: Binding(get: { !model.settings.hasCompletedOnboarding }, set: { _ in })) {33            OnboardingView()34        }35        .alert("Your data was recovered", isPresented: Binding(get: { model.storageNotice != nil }, set: { if !$0 { model.storageNotice = nil } })) {36            Button("OK") { model.storageNotice = nil }37        } message: {38            Text(model.storageNotice == .resetAfterCorruption39                 ? "The saved data could not be read and was set aside. ORIX Alarm started with empty data: please create your alarms again."40                 : "The saved data was damaged. ORIX Alarm restored the previous copy; check that your alarms are as you expect.")41        }42        .tint(Theme.sunrise)43    }44}4546/// First launch: what the app does, what it cannot do, and the alarm permission.47struct OnboardingView: View {48    @Environment(AppModel.self) private var model49    @State private var isRequesting = false5051    var body: some View {52        ZStack {53            AuroraBackground(warmth: 0.55)54            VStack(spacing: 28) {55                Spacer()56                Image(systemName: "sun.horizon.fill").font(.system(size: 72)).foregroundStyle(Theme.sunriseGradient)57                    .accessibilityHidden(true)58                VStack(spacing: 10) {59                    Text("ORIX Alarm").font(.system(size: 40, weight: .bold, design: .rounded))60                    Text("Wake smarter. Rise better.").font(.orixTitle).foregroundStyle(Theme.textSecondary)61                }62                VStack(alignment: .leading, spacing: 16) {63                    row("alarm.fill", "Real system alarms", "Alarms are scheduled with iOS, so they ring when the app is closed or the iPhone is locked.")64                    row("puzzlepiece.extension.fill", "Wake missions", "Maths, memory, QR codes, steps: prove you are up. iOS always lets you stop an alarm; missions are your own commitment.")65                    row("lock.shield.fill", "Private by design", "No account, no tracking, no server. Everything stays on this iPhone.")66                }67                .glassCard()68                Spacer()69                VStack(spacing: 12) {70                    Button {71                        isRequesting = true72                        Task {73                            await model.requestAuthorization()74                            model.updateSettings { $0.hasCompletedOnboarding = true }75                            isRequesting = false76                        }77                    } label: { Text(isRequesting ? "Requesting…" : "Allow alarms and continue") }78                    .buttonStyle(PrimaryButtonStyle()).disabled(isRequesting)79                    .accessibilityIdentifier("onboarding.continue")80                    Button("Not now") { model.updateSettings { $0.hasCompletedOnboarding = true } }81                        .font(.orixCaption).foregroundStyle(Theme.textSecondary).frame(minHeight: 44)82                        .accessibilityIdentifier("onboarding.skip")83                }84            }85            .padding(24)86        }87        .preferredColorScheme(.dark)88        .interactiveDismissDisabled()89    }9091    private func row(_ symbol: String, _ title: LocalizedStringKey, _ text: LocalizedStringKey) -> some View {92        HStack(alignment: .top, spacing: 14) {93            Image(systemName: symbol).font(.title3).foregroundStyle(Theme.gold).frame(width: 30).accessibilityHidden(true)94            VStack(alignment: .leading, spacing: 3) {95                Text(title).font(.orixHeadline)96                Text(text).font(.orixCaption).foregroundStyle(Theme.textSecondary)97            }98        }99        .accessibilityElement(children: .combine)100    }101}102103/// Banner shown wherever alarms are listed when the app cannot schedule them.104struct AuthorizationBanner: View {105    @Environment(AppModel.self) private var model106    @Environment(\.openURL) private var openURL107    var body: some View {108        if model.authorization != .authorized {109            VStack(alignment: .leading, spacing: 10) {110                Label(model.authorization == .denied ? "Alarms are not allowed" : "Alarm permission needed", systemImage: "exclamationmark.triangle.fill")111                    .font(.orixHeadline).foregroundStyle(Theme.gold)112                Text(model.authorization == .denied113                     ? "ORIX Alarm cannot ring until you allow alarms for it in Settings. Your alarms are saved but not scheduled."114                     : "Allow ORIX Alarm to schedule alarms so they can ring when the app is closed.")115                    .font(.orixCaption).foregroundStyle(Theme.textSecondary)116                Button(model.authorization == .denied ? "Open Settings" : "Allow alarms") {117                    if model.authorization == .denied, let url = URL(string: UIApplication.openSettingsURLString) { openURL(url) }118                    else { Task { await model.requestAuthorization() } }119                }120                .buttonStyle(SecondaryButtonStyle())121                .accessibilityIdentifier("authorization.action")122            }123            .glassCard(tint: Theme.gold)124            .accessibilityElement(children: .contain)125            .accessibilityIdentifier("authorization.banner")126        }127    }128}129
Files main
Menu

Theme