Sources/OrgstarMobile/OnboardingScreen.swift

192 lines · 7810 bytes

  1#if os(iOS)
  2import OrgApp
  3import OrgCore
  4import SwiftUI
  5import UserNotifications
  6
  7/// First launch: what the app does, the folder of org files, and the permissions each
  8/// optional feature needs, asked for one at a time and only when the user chooses to.
  9struct OnboardingScreen: View {
 10    /// Set once onboarding is done or skipped for a workspace that had folders already.
 11    static let doneKey = "onboarded"
 12
 13    let model: MobileModel
 14    let workspace: WorkspaceModel
 15    let finish: () -> Void
 16    @State private var step = Step.welcome
 17    @State private var importing = false
 18    @State private var problem: String?
 19
 20    private enum Step: Int, CaseIterable {
 21        case welcome, folder, notifications, calendar, reminders
 22    }
 23
 24    var body: some View {
 25        VStack(spacing: 0) {
 26            Group {
 27                switch step {
 28                case .welcome: welcome
 29                case .folder: folder
 30                case .notifications: notifications
 31                case .calendar: calendar
 32                case .reminders: reminders
 33                }
 34            }
 35            .frame(maxWidth: 500, maxHeight: .infinity)
 36            .padding(.horizontal, 28)
 37            .transition(.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)).combined(with: .opacity))
 38            .id(step)
 39            HStack(spacing: 8) {
 40                ForEach(Step.allCases, id: \.self) { s in
 41                    Circle().fill(s == step ? Color.accentColor : Color.secondary.opacity(0.3)).frame(width: 7, height: 7)
 42                }
 43            }
 44            .accessibilityElement(children: .ignore)
 45            .accessibilityLabel("Step \(step.rawValue + 1) of \(Step.allCases.count)")
 46            .padding(.bottom, 12)
 47        }
 48        .tint(model.theme.accentColor)
 49        .fileImporter(isPresented: $importing, allowedContentTypes: [.folder]) { result in
 50            guard case .success(let url) = result else { return }
 51            // Kept for the bookmark; WorkspaceRoot resolves it with access again at launch.
 52            _ = url.startAccessingSecurityScopedResource()
 53            Task {
 54                do {
 55                    try await workspace.addRoot(url)
 56                    problem = nil
 57                    next()
 58                } catch {
 59                    problem = "Can't add \(url.lastPathComponent): \(error.localizedDescription)"
 60                }
 61            }
 62        }
 63    }
 64
 65    private var welcome: some View {
 66        page(symbol: "star.fill", title: "Welcome to orgstar", text: "Your org files on iPhone, in the folders where they already are.") {
 67            VStack(alignment: .leading, spacing: 18) {
 68                feature("calendar", "The agenda", "Scheduled entries, deadlines and TODOs from all your files.")
 69                feature("hand.tap", "Tap to edit", "Change an entry's state, dates, tags and notes in place. Swipe to finish or reschedule.")
 70                feature("square.and.pencil", "Capture", "Your capture templates, from the app, the share sheet, widgets and the watch.")
 71            }
 72            .padding(.top, 8)
 73        } actions: {
 74            primary("Continue") { next() }
 75        }
 76    }
 77
 78    private var folder: some View {
 79        page(symbol: "folder.fill", title: "Choose your org folder",
 80             text: "Pick the folder that holds your org files, in iCloud Drive or another provider. orgstar reads and writes them there; nothing is copied.") {
 81            if let problem { Text(problem).font(.footnote).foregroundStyle(.red).multilineTextAlignment(.center) }
 82        } actions: {
 83            primary("Choose Folder") { importing = true }
 84            secondary("Later") { next() }
 85        }
 86    }
 87
 88    private var notifications: some View {
 89        page(symbol: "bell.badge.fill", title: "Notifications",
 90             text: "orgstar can notify you before timed entries, as Org's appointment reminders do, and show today's overdue count on its icon.") {
 91            EmptyView()
 92        } actions: {
 93            primary("Allow Notifications") {
 94                Task {
 95                    _ = try? await UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge])
 96                    next()
 97                }
 98            }
 99            secondary("Not Now") {
100                // Off, so the agenda doesn't ask later; Settings turns it back on.
101                model.set("reminders", to: .bool(false))
102                next()
103            }
104        }
105    }
106
107    private var calendar: some View {
108        page(symbol: "calendar.badge.clock", title: "Calendar events",
109             text: "Show events from Calendar among your agenda's entries. You can choose which calendars in Settings.") {
110            EmptyView()
111        } actions: {
112            primary("Show Calendar Events") {
113                Task {
114                    if await model.calendarEvents.requestAccess() { model.set("calendarEvents", to: .bool(true)) }
115                    next()
116                }
117            }
118            secondary("Not Now") { next() }
119        }
120    }
121
122    private var reminders: some View {
123        page(symbol: "checklist", title: "Reminders app",
124             text: "Show incomplete reminders with a due date from the Reminders app in your agenda, read-only.") {
125            EmptyView()
126        } actions: {
127            primary("Show Reminders") {
128                Task {
129                    if await model.reminderLists.requestAccess() { model.set("agendaReminders", to: .bool(true)) }
130                    next()
131                }
132            }
133            secondary("Not Now") { next() }
134        }
135    }
136
137    private func next() {
138        guard let following = Step(rawValue: step.rawValue + 1) else {
139            model.reloadConfig()
140            finish()
141            return
142        }
143        withAnimation(.snappy) { step = following }
144    }
145
146    // MARK: - Layout
147
148    private func page<Content: View, Actions: View>(
149        symbol: String, title: LocalizedStringKey, text: LocalizedStringKey,
150        @ViewBuilder content: () -> Content, @ViewBuilder actions: () -> Actions
151    ) -> some View {
152        VStack(spacing: 20) {
153            Spacer(minLength: 24)
154            Image(systemName: symbol)
155                .font(.system(size: 44, weight: .semibold))
156                .foregroundStyle(Color.accentColor)
157                .frame(width: 96, height: 96)
158                .background(Color.accentColor.opacity(0.12), in: Circle())
159                .accessibilityHidden(true)
160            Text(title).font(.title.bold()).multilineTextAlignment(.center)
161            Text(text).font(.body).foregroundStyle(.secondary).multilineTextAlignment(.center)
162            content()
163            Spacer(minLength: 24)
164            VStack(spacing: 12) { actions() }
165        }
166    }
167
168    private func feature(_ symbol: String, _ title: LocalizedStringKey, _ text: LocalizedStringKey) -> some View {
169        HStack(alignment: .top, spacing: 14) {
170            Image(systemName: symbol).font(.title2).foregroundStyle(Color.accentColor).frame(width: 32)
171            VStack(alignment: .leading, spacing: 2) {
172                Text(title).font(.headline)
173                Text(text).font(.subheadline).foregroundStyle(.secondary)
174            }
175        }
176    }
177
178    private func primary(_ title: LocalizedStringKey, action: @escaping () -> Void) -> some View {
179        Button(action: action) {
180            Text(title).font(.body.weight(.semibold)).frame(maxWidth: .infinity, minHeight: 50)
181        }
182        .buttonStyle(.borderedProminent)
183        .buttonBorderShape(.capsule)
184    }
185
186    private func secondary(_ title: LocalizedStringKey, action: @escaping () -> Void) -> some View {
187        Button(title, action: action)
188            .font(.body.weight(.medium))
189            .frame(minHeight: 44)
190    }
191}
192#endif