Sources/OrgstarMobile/OrgstarMobileApp.swift
86 lines · 2941 bytes
1#if os(iOS)
2import OrgApp
3import OrgCore
4import SwiftUI
5
6/// Orgstar on iPhone and iPad: the agenda, capture, and the folders they read.
7@main
8struct OrgstarMobileApp: App {
9 @State private var workspace = Result { try WorkspaceModel(directory: WorkspaceModel.defaultDirectory) }
10 @State private var session = DocumentSession()
11
12 var body: some Scene {
13 WindowGroup {
14 switch workspace {
15 case .success(let model):
16 RootView(workspace: model, session: session)
17 case .failure(let error):
18 ContentUnavailableView("Can't open the index", systemImage: "exclamationmark.triangle", description: Text(error.localizedDescription))
19 }
20 }
21 }
22}
23
24/// An org-protocol capture, or a plain one.
25struct CaptureRequest: Identifiable {
26 let id = UUID()
27 var template: String?
28 var properties: [String: String] = [:]
29}
30
31struct RootView: View {
32 let workspace: WorkspaceModel
33 let session: DocumentSession
34 @State private var agenda: AgendaModel
35 @State private var capture: CaptureRequest?
36
37 init(workspace: WorkspaceModel, session: DocumentSession) {
38 self.workspace = workspace
39 self.session = session
40 _agenda = State(initialValue: AgendaModel(workspace: workspace))
41 }
42
43 var body: some View {
44 TabView {
45 Tab("Agenda", systemImage: "calendar") {
46 NavigationStack {
47 AgendaScreen(workspace: workspace, session: session, agenda: agenda)
48 .toolbar { captureButton }
49 }
50 }
51 Tab("Folders", systemImage: "folder") {
52 NavigationStack {
53 FoldersScreen(workspace: workspace)
54 .toolbar { captureButton }
55 }
56 }
57 }
58 .sheet(item: $capture) { request in
59 CaptureScreen(workspace: workspace, session: session, request: request)
60 }
61 .task {
62 // Development: `ORGSTAR_ROOT` adds a folder at launch, as on the Mac.
63 if let root = ProcessInfo.processInfo.environment["ORGSTAR_ROOT"] { try? await workspace.addRoot(URL(fileURLWithPath: root)) }
64 workspace.startWatching()
65 await workspace.refresh()
66 }
67 .onOpenURL { url in
68 if case .capture(let template, let properties)? = OrgProtocol.parse(url.absoluteString) {
69 capture = CaptureRequest(template: template, properties: properties)
70 }
71 }
72 }
73
74 private var captureButton: some ToolbarContent {
75 ToolbarItem(placement: .primaryAction) {
76 Button("Capture", systemImage: "square.and.pencil") { capture = CaptureRequest() }
77 }
78 }
79}
80#else
81/// The iOS app has nothing to run elsewhere.
82@main
83enum OrgstarMobileUnavailable {
84 static func main() { print("Orgstar for iOS runs on iPhone and iPad.") }
85}
86#endif