Sources/OrgstarMobile/OrgstarMobileApp.swift
244 lines · 10000 bytes
1#if os(iOS)
2import CoreSpotlight
3import OrgApp
4import OrgCore
5import SwiftUI
6import UserNotifications
7
8/// Orgstar on iPhone and iPad: the agenda, capture, and the folders they read.
9@main
10struct OrgstarMobileApp: App {
11 @UIApplicationDelegateAdaptor private var delegate: AppDelegate
12 @State private var model = MobileModel.shared
13
14 var body: some Scene {
15 WindowGroup {
16 if let workspace = model.workspace, let agenda = model.agenda {
17 RootView(model: model, workspace: workspace, agenda: agenda)
18 .environment(model)
19 .environment(model.clock)
20 .onAppear { delegate.model = model }
21 } else {
22 ContentUnavailableView("Can't open the index", systemImage: "exclamationmark.triangle", description: Text(model.failure ?? ""))
23 }
24 }
25 }
26}
27
28/// Reminders show while the app is open too, and open their entry when tapped.
29final class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
30 @MainActor var model: MobileModel?
31
32 func application(_ application: UIApplication, didFinishLaunchingWithOptions options: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
33 UNUserNotificationCenter.current().delegate = self
34 return true
35 }
36
37 nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions {
38 [.banner, .sound, .list]
39 }
40
41 nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {
42 let info = response.notification.request.content.userInfo
43 guard let path = info["path"] as? String, let offset = info["offset"] as? Int else { return }
44 await MainActor.run { model?.openRequest = .offset(path, offset) }
45 }
46}
47
48enum AppTab: Hashable { case agenda, folders, settings, search }
49
50extension View {
51 /// Readers pushed for `ReaderRoute`s.
52 func readerDestination(workspace: WorkspaceModel, session: DocumentSession) -> some View {
53 navigationDestination(for: ReaderRoute.self) { route in
54 ReaderScreen(route: route, workspace: workspace, session: session)
55 }
56 .navigationDestination(for: EditorRoute.self) { route in
57 EditorScreen(path: route.path, offset: route.offset, workspace: workspace, session: session)
58 }
59 }
60}
61
62/// An org-protocol capture, or a plain one.
63struct CaptureRequest: Identifiable {
64 let id = UUID()
65 var template: String?
66 var properties: [String: String] = [:]
67}
68
69extension CaptureRequest {
70 /// An `org-protocol://capture` link's request.
71 init?(link: String) {
72 guard case .capture(let template, let properties)? = OrgProtocol.parse(link) else { return nil }
73 self.init(template: template, properties: properties)
74 }
75}
76
77struct RootView: View {
78 let model: MobileModel
79 let workspace: WorkspaceModel
80 let agenda: AgendaModel
81 @State private var capture: CaptureRequest?
82 /// Captures from the share extension after the one showing.
83 @State private var waiting: [CaptureRequest] = []
84 @State private var tab = AppTab.agenda
85 @State private var agendaPath = NavigationPath()
86 @State private var hour = 0
87 @State private var showingClockReport = false
88 @State private var message: String?
89 @Environment(\.scenePhase) private var scenePhase
90
91 private var session: DocumentSession { model.session }
92
93 private struct SpotlightKey: Equatable {
94 var files: Int
95 var changed: Int?
96 var saved: Int
97 }
98
99 private struct ReminderKey: Equatable {
100 var files: Int
101 var changed: Int?
102 var edits: Int
103 var settings: Int
104 var active: Bool
105 var hour: Int
106 }
107
108 var body: some View {
109 TabView(selection: $tab) {
110 Tab("Agenda", systemImage: "calendar", value: .agenda) {
111 NavigationStack(path: $agendaPath) {
112 AgendaScreen(workspace: workspace, session: session, agenda: agenda, scheduler: model.scheduler)
113 .toolbar { captureButton }
114 .readerDestination(workspace: workspace, session: session)
115 }
116 }
117 Tab("Folders", systemImage: "folder", value: .folders) {
118 NavigationStack {
119 FoldersScreen(workspace: workspace)
120 .toolbar { captureButton }
121 .readerDestination(workspace: workspace, session: session)
122 }
123 }
124 Tab("Settings", systemImage: "gearshape", value: .settings) {
125 NavigationStack { SettingsScreen(model: model) }
126 }
127 Tab(value: .search, role: .search) {
128 NavigationStack {
129 SearchScreen(workspace: workspace, session: session)
130 .readerDestination(workspace: workspace, session: session)
131 }
132 }
133 }
134 .clockAccessory(model.clock.running, perform: clock)
135 .overlay(alignment: .top) {
136 if let message {
137 Text(message).font(.callout).padding(8).frame(maxWidth: .infinity).background(.bar)
138 .onTapGesture { self.message = nil }
139 .task(id: message) {
140 try? await Task.sleep(for: .seconds(4))
141 if self.message == message { self.message = nil }
142 }
143 }
144 }
145 .sheet(isPresented: $showingClockReport) { ClockReportScreen(workspace: workspace) }
146 .onChange(of: model.openRequest) {
147 guard let route = model.openRequest else { return }
148 model.openRequest = nil
149 tab = .agenda
150 agendaPath.append(route)
151 }
152 .onChange(of: model.agendaRequest) {
153 guard let request = model.agendaRequest else { return }
154 model.agendaRequest = nil
155 agenda.mode = .agenda(span: request.count, startOffset: nil)
156 agenda.start = request.start
157 tab = .agenda
158 agendaPath = NavigationPath()
159 }
160 .onChange(of: workspace.changedFile?.version) {
161 // An open file takes in what changed on disk.
162 if let path = workspace.changedFile?.path { session.fileChanged(path) }
163 }
164 .sheet(item: $capture, onDismiss: showWaiting) { request in
165 CaptureScreen(workspace: workspace, session: session, request: request)
166 }
167 .task {
168 takeShared()
169 // Development: `ORGSTAR_ROOT` adds a folder at launch, as on the Mac.
170 if let root = ProcessInfo.processInfo.environment["ORGSTAR_ROOT"] { try? await workspace.addRoot(URL(fileURLWithPath: root)) }
171 workspace.startWatching()
172 await workspace.refresh()
173 }
174 .task {
175 while !Task.isCancelled {
176 try? await Task.sleep(for: .seconds(3600))
177 hour += 1
178 }
179 }
180 // Reminders follow the files, the settings, coming back to the app and the hour.
181 .task(id: ReminderKey(files: workspace.files.count, changed: workspace.changedFile?.version, edits: session.editVersion,
182 settings: model.settingsVersion, active: scenePhase == .active, hour: hour)) {
183 guard scenePhase == .active else { return }
184 try? await Task.sleep(for: .seconds(1))
185 guard !Task.isCancelled else { return }
186 await model.updateReminders()
187 }
188 // Spotlight follows the files and saved edits.
189 .task(id: SpotlightKey(files: workspace.files.count, changed: workspace.changedFile?.version, saved: session.dirtyPaths.count)) {
190 await model.spotlight.update(workspace.files)
191 }
192 .onContinueUserActivity(CSSearchableItemActionType) { activity in
193 if let path = activity.userInfo?[CSSearchableItemActivityIdentifier] as? String { model.openRequest = .file(path) }
194 }
195 .onChange(of: scenePhase) { _, phase in
196 // Unsaved edits are written before the app may be stopped.
197 if phase == .background {
198 for path in session.dirtyPaths { _ = session.save(path) }
199 }
200 // Files and settings synced while away; not every sync tool tells file presenters.
201 guard phase == .active else { return }
202 takeShared()
203 model.reloadConfig()
204 Task { await workspace.refresh() }
205 }
206 .onOpenURL { url in
207 if let request = CaptureRequest(link: url.absoluteString) { capture = request }
208 }
209 }
210
211 /// What the share extension left, one capture sheet at a time.
212 private func takeShared() {
213 waiting += (model.inbox?.take() ?? []).compactMap(CaptureRequest.init(link:))
214 if capture == nil { showWaiting() }
215 }
216
217 private func showWaiting() {
218 if !waiting.isEmpty { capture = waiting.removeFirst() }
219 }
220
221 private func clock(_ id: String) {
222 switch id {
223 case "app.clock.out": message = model.clock.clockOut(session)
224 case "app.clock.cancel": message = model.clock.clockOut(session, cancel: true)
225 case "app.clock.goto":
226 if let location = model.clock.location(session) { model.openRequest = .offset(location.path, location.offset) }
227 case "app.clock.report": showingClockReport = true
228 default: break
229 }
230 }
231
232 private var captureButton: some ToolbarContent {
233 ToolbarItem(placement: .primaryAction) {
234 Button("Capture", systemImage: "square.and.pencil") { capture = CaptureRequest() }
235 }
236 }
237}
238#else
239/// The iOS app has nothing to run elsewhere.
240@main
241enum OrgstarMobileUnavailable {
242 static func main() { print("Orgstar for iOS runs on iPhone and iPad.") }
243}
244#endif