Sources/OrgstarMobile/FoldersScreen.swift
62 lines · 2515 bytes
1#if os(iOS)
2import OrgApp
3import SwiftUI
4import UniformTypeIdentifiers
5
6/// The folders of org files the agenda reads, and their files.
7struct FoldersScreen: View {
8 let workspace: WorkspaceModel
9 @State private var importing = false
10
11 var body: some View {
12 List {
13 ForEach(workspace.rootPaths, id: \.self) { root in
14 Section((root as NSString).lastPathComponent) {
15 let files = workspace.files.filter { $0.root == root && $0.kind == .org }
16 ForEach(files) { file in
17 Label(file.relativePath, systemImage: "doc.text")
18 }
19 let pending = workspace.downloading.filter { $0.hasPrefix(root + "/") }.sorted()
20 ForEach(pending, id: \.self) { path in
21 Label(String(path.dropFirst(root.count + 1)), systemImage: "icloud.and.arrow.down")
22 .foregroundStyle(.secondary)
23 }
24 Button("Remove Folder", role: .destructive) { try? workspace.removeRoot(path: root) }
25 }
26 }
27 if let error = workspace.lastError {
28 Text(error).foregroundStyle(.red)
29 }
30 }
31 .navigationTitle("Folders")
32 .overlay {
33 if workspace.rootPaths.isEmpty {
34 ContentUnavailableView {
35 Label("No folders", systemImage: "folder")
36 } description: {
37 Text("Add the folder your org files are in, from iCloud Drive or another app's storage.")
38 } actions: {
39 Button("Add Folder") { importing = true }
40 }
41 }
42 }
43 .toolbar {
44 ToolbarItem(placement: .topBarLeading) {
45 Button("Add Folder", systemImage: "folder.badge.plus") { importing = true }
46 }
47 }
48 .fileImporter(isPresented: $importing, allowedContentTypes: [.folder]) { result in
49 guard case .success(let url) = result else { return }
50 // Kept for the bookmark; WorkspaceRoot resolves it with access again at launch.
51 _ = url.startAccessingSecurityScopedResource()
52 Task {
53 do {
54 try await workspace.addRoot(url)
55 } catch {
56 workspace.lastError = "Can't add \(url.lastPathComponent): \(error.localizedDescription)"
57 }
58 }
59 }
60 }
61}
62#endif