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