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