Sources/OrgstarMobile/OnboardingScreen.swift
187 lines · 7679 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
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("Continue") {
94 Task {
95 let granted = (try? await UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge])) ?? false
96 // Off when declined, so the agenda doesn't ask later; Settings turns it back on.
97 if !granted { model.set("reminders", to: .bool(false)) }
98 next()
99 }
100 }
101 }
102 }
103
104 private var calendar: some View {
105 page(symbol: "calendar.badge.clock", title: "Calendar events",
106 text: "Show events from Calendar among your agenda's entries. You can choose which calendars in Settings.") {
107 EmptyView()
108 } actions: {
109 primary("Continue") {
110 Task {
111 if await model.calendarEvents.requestAccess() { model.set("calendarEvents", to: .bool(true)) }
112 next()
113 }
114 }
115 }
116 }
117
118 private var reminders: some View {
119 page(symbol: "checklist", title: "Reminders app",
120 text: "Show incomplete reminders with a due date from the Reminders app in your agenda, read-only.") {
121 EmptyView()
122 } actions: {
123 primary("Continue") {
124 Task {
125 if await model.reminderLists.requestAccess() { model.set("agendaReminders", to: .bool(true)) }
126 next()
127 }
128 }
129 }
130 }
131
132 private func next() {
133 guard let following = Step(rawValue: step.rawValue + 1) else {
134 model.reloadConfig()
135 finish()
136 return
137 }
138 withAnimation(.snappy) { step = following }
139 }
140
141 // MARK: - Layout
142
143 private func page<Content: View, Actions: View>(
144 symbol: String, title: LocalizedStringKey, text: LocalizedStringKey,
145 @ViewBuilder content: () -> Content, @ViewBuilder actions: () -> Actions
146 ) -> some View {
147 VStack(spacing: 20) {
148 Spacer(minLength: 24)
149 Image(systemName: symbol)
150 .font(.system(size: 44, weight: .semibold))
151 .foregroundStyle(Color.accentColor)
152 .frame(width: 96, height: 96)
153 .background(Color.accentColor.opacity(0.12), in: Circle())
154 .accessibilityHidden(true)
155 Text(title).font(.title.bold()).multilineTextAlignment(.center)
156 Text(text).font(.body).foregroundStyle(.secondary).multilineTextAlignment(.center)
157 content()
158 Spacer(minLength: 24)
159 VStack(spacing: 12) { actions() }
160 }
161 }
162
163 private func feature(_ symbol: String, _ title: LocalizedStringKey, _ text: LocalizedStringKey) -> some View {
164 HStack(alignment: .top, spacing: 14) {
165 Image(systemName: symbol).font(.title2).foregroundStyle(Color.accentColor).frame(width: 32)
166 VStack(alignment: .leading, spacing: 2) {
167 Text(title).font(.headline)
168 Text(text).font(.subheadline).foregroundStyle(.secondary)
169 }
170 }
171 }
172
173 private func primary(_ title: LocalizedStringKey, action: @escaping () -> Void) -> some View {
174 Button(action: action) {
175 Text(title).font(.body.weight(.semibold)).frame(maxWidth: .infinity, minHeight: 50)
176 }
177 .buttonStyle(.borderedProminent)
178 .buttonBorderShape(.capsule)
179 }
180
181 private func secondary(_ title: LocalizedStringKey, action: @escaping () -> Void) -> some View {
182 Button(title, action: action)
183 .font(.body.weight(.medium))
184 .frame(minHeight: 44)
185 }
186}
187#endif