Sources/OrgstarMobile/FoldersScreen.swift

9a7439fd01e4ce5041bc5fab90542f083143ae10
orgstar/Sources/OrgstarMobile/FoldersScreen.swift history · blame · raw

92 lines · 3861 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                    FolderContents(workspace: workspace, root: root, folder: "")
16                    Button("Remove Folder", role: .destructive) { try? workspace.removeRoot(path: root) }
17                }
18            }
19            if let error = workspace.lastError {
20                Text(error).foregroundStyle(.red)
21            }
22        }
23        .navigationTitle("Folders")
24        .overlay {
25            if workspace.rootPaths.isEmpty {
26                ContentUnavailableView {
27                    Label("No folders", systemImage: "folder")
28                } description: {
29                    Text("Add the folder your org files are in, from iCloud Drive or another app's storage.")
30                } actions: {
31                    Button("Add Folder") { importing = true }
32                }
33            }
34        }
35        .toolbar {
36            ToolbarItem(placement: .topBarLeading) {
37                Button("Add Folder", systemImage: "folder.badge.plus") { importing = true }
38            }
39        }
40        .fileImporter(isPresented: $importing, allowedContentTypes: [.folder]) { result in
41            guard case .success(let url) = result else { return }
42            // Kept for the bookmark; WorkspaceRoot resolves it with access again at launch.
43            _ = url.startAccessingSecurityScopedResource()
44            Task {
45                do {
46                    try await workspace.addRoot(url)
47                } catch {
48                    workspace.lastError = "Can't add \(url.lastPathComponent): \(error.localizedDescription)"
49                }
50            }
51        }
52    }
53}
54/// One folder's subfolders, which open their own contents, then its org files and the files
55/// still downloading into it. `folder` is relative to `root`, empty for the root itself.
56private struct FolderContents: View {
57    let workspace: WorkspaceModel
58    let root: String
59    let folder: String
60
61    private var prefix: String { folder.isEmpty ? "" : folder + "/" }
62
63    /// Paths below this folder, relative to it.
64    private func below(_ relativePaths: [String]) -> [String] {
65        relativePaths.filter { $0.hasPrefix(prefix) }.map { String($0.dropFirst(prefix.count)) }
66    }
67
68    var body: some View {
69        let files = workspace.files.filter { $0.root == root && $0.kind == .org && $0.relativePath.hasPrefix(prefix) }
70        let pending = below(workspace.downloading.filter { $0.hasPrefix(root + "/") }.map { String($0.dropFirst(root.count + 1)) })
71        let subfolders = Set((below(files.map(\.relativePath)) + pending).compactMap { path in
72            path.firstIndex(of: "/").map { String(path[..<$0]) }
73        }).sorted { $0.localizedStandardCompare($1) == .orderedAscending }
74        ForEach(subfolders, id: \.self) { name in
75            NavigationLink {
76                List { FolderContents(workspace: workspace, root: root, folder: prefix + name) }
77                    .navigationTitle(name)
78            } label: {
79                Label(name, systemImage: "folder")
80            }
81        }
82        ForEach(files.filter { !$0.relativePath.dropFirst(prefix.count).contains("/") }) { file in
83            NavigationLink(value: ReaderRoute.file(file.path)) {
84                Label(file.name, systemImage: "doc.text")
85            }
86        }
87        ForEach(pending.filter { !$0.contains("/") }, id: \.self) { name in
88            Label(name, systemImage: "icloud.and.arrow.down").foregroundStyle(.secondary)
89        }
90    }
91}
92#endif