Sources/OrgstarMobile/OnboardingScreen.swift

7d137d1b02ef553e996f782b56fafc59026c759f
orgstar/Sources/OrgstarMobile/OnboardingScreen.swift

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

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form