Sources/OrgstarMobile/OrgstarMobileApp.swift
128 lines · 4760 bytes
1#if os(iOS)
2import OrgApp
3import OrgCore
4import SwiftUI
5import UserNotifications
6
7/// Orgstar on iPhone and iPad: the agenda, capture, and the folders they read.
8@main
9struct OrgstarMobileApp: App {
10 @UIApplicationDelegateAdaptor private var delegate: AppDelegate
11 @State private var model = MobileModel()
12
13 var body: some Scene {
14 WindowGroup {
15 if let workspace = model.workspace, let agenda = model.agenda {
16 RootView(model: model, workspace: workspace, agenda: agenda)
17 } else {
18 ContentUnavailableView("Can't open the index", systemImage: "exclamationmark.triangle", description: Text(model.failure ?? ""))
19 }
20 }
21 }
22}
23
24/// Reminders show while the app is open too.
25final class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
26 func application(_ application: UIApplication, didFinishLaunchingWithOptions options: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
27 UNUserNotificationCenter.current().delegate = self
28 return true
29 }
30
31 nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions {
32 [.banner, .sound, .list]
33 }
34}
35
36/// An org-protocol capture, or a plain one.
37struct CaptureRequest: Identifiable {
38 let id = UUID()
39 var template: String?
40 var properties: [String: String] = [:]
41}
42
43struct RootView: View {
44 let model: MobileModel
45 let workspace: WorkspaceModel
46 let agenda: AgendaModel
47 @State private var capture: CaptureRequest?
48 @State private var hour = 0
49 @Environment(\.scenePhase) private var scenePhase
50
51 private var session: DocumentSession { model.session }
52
53 private struct ReminderKey: Equatable {
54 var files: Int
55 var changed: Int?
56 var edits: Int
57 var settings: Int
58 var active: Bool
59 var hour: Int
60 }
61
62 var body: some View {
63 TabView {
64 Tab("Agenda", systemImage: "calendar") {
65 NavigationStack {
66 AgendaScreen(workspace: workspace, session: session, agenda: agenda, scheduler: model.scheduler)
67 .toolbar { captureButton }
68 }
69 }
70 Tab("Folders", systemImage: "folder") {
71 NavigationStack {
72 FoldersScreen(workspace: workspace)
73 .toolbar { captureButton }
74 }
75 }
76 Tab("Settings", systemImage: "gearshape") {
77 NavigationStack { SettingsScreen(model: model) }
78 }
79 }
80 .sheet(item: $capture) { request in
81 CaptureScreen(workspace: workspace, session: session, request: request)
82 }
83 .task {
84 // Development: `ORGSTAR_ROOT` adds a folder at launch, as on the Mac.
85 if let root = ProcessInfo.processInfo.environment["ORGSTAR_ROOT"] { try? await workspace.addRoot(URL(fileURLWithPath: root)) }
86 workspace.startWatching()
87 await workspace.refresh()
88 }
89 .task {
90 while !Task.isCancelled {
91 try? await Task.sleep(for: .seconds(3600))
92 hour += 1
93 }
94 }
95 // Reminders follow the files, the settings, coming back to the app and the hour.
96 .task(id: ReminderKey(files: workspace.files.count, changed: workspace.changedFile?.version, edits: session.editVersion,
97 settings: model.settingsVersion, active: scenePhase == .active, hour: hour)) {
98 guard scenePhase == .active else { return }
99 try? await Task.sleep(for: .seconds(1))
100 guard !Task.isCancelled else { return }
101 await model.updateReminders()
102 }
103 .onChange(of: scenePhase) { _, phase in
104 // Files and settings synced while away; not every sync tool tells file presenters.
105 guard phase == .active else { return }
106 model.reloadConfig()
107 Task { await workspace.refresh() }
108 }
109 .onOpenURL { url in
110 if case .capture(let template, let properties)? = OrgProtocol.parse(url.absoluteString) {
111 capture = CaptureRequest(template: template, properties: properties)
112 }
113 }
114 }
115
116 private var captureButton: some ToolbarContent {
117 ToolbarItem(placement: .primaryAction) {
118 Button("Capture", systemImage: "square.and.pencil") { capture = CaptureRequest() }
119 }
120 }
121}
122#else
123/// The iOS app has nothing to run elsewhere.
124@main
125enum OrgstarMobileUnavailable {
126 static func main() { print("Orgstar for iOS runs on iPhone and iPad.") }
127}
128#endif