iOS Folders: list and delete empty folders !215

merged merged by cmc on 2026-10-10 20:03 UTC · krz/orgstar:delete-empty-folders into main

2 files changed, +42 −9

Layout: unified · split

Sources/OrgstarMobile/FoldersScreen.swift +41 −8
@@ -10,12 +10,14 @@ struct FoldersScreen: View {
1010 @State private var importing = false
1111 /// The file whose deletion is being confirmed.
1212 @State private var deleting: String?
13 /// Counts deletions, so the list reads the empty folders on disk again.
14 @State private var deletions = 0
1315
1416 var body: some View {
1517 List {
1618 ForEach(workspace.rootPaths, id: \.self) { root in
1719 Section((root as NSString).lastPathComponent) {
18 FolderContents(workspace: workspace, root: root, folder: "", deleting: $deleting)
20 FolderContents(workspace: workspace, root: root, folder: "", deleting: $deleting, deletions: deletions)
1921 Button("Remove Folder", role: .destructive) { try? workspace.removeRoot(path: root) }
2022 }
2123 }
@@ -27,7 +29,7 @@ struct FoldersScreen: View {
2729 .navigationDestination(for: FolderRoute.self) { route in
2830 FolderPage(workspace: workspace, session: session, route: route)
2931 }
30 .confirmDeletion($deleting, workspace: workspace, session: session)
32 .confirmDeletion($deleting, workspace: workspace, session: session) { deletions += 1 }
3133 .overlay {
3234 if workspace.rootPaths.isEmpty {
3335 ContentUnavailableView {
@@ -64,18 +66,21 @@ private struct FolderPage: View {
6466 let session: DocumentSession
6567 let route: FolderRoute
6668 @State private var deleting: String?
69 @State private var deletions = 0
6770
6871 var body: some View {
69 List { FolderContents(workspace: workspace, root: route.root, folder: route.folder, deleting: $deleting) }
72 List { FolderContents(workspace: workspace, root: route.root, folder: route.folder, deleting: $deleting, deletions: deletions) }
7073 .navigationTitle((route.folder as NSString).lastPathComponent)
71 .confirmDeletion($deleting, workspace: workspace, session: session)
74 .confirmDeletion($deleting, workspace: workspace, session: session) { deletions += 1 }
7275 }
7376}
7477
7578private extension View {
76 /// Asks before deleting the file in `deleting`, closing it first.
77 func confirmDeletion(_ deleting: Binding<String?>, workspace: WorkspaceModel, session: DocumentSession) -> some View {
79 /// Asks before deleting the file or empty folder in `deleting`, closing a file first.
80 func confirmDeletion(_ deleting: Binding<String?>, workspace: WorkspaceModel, session: DocumentSession, deleted: @escaping () -> Void) -> some View {
7881 let name = deleting.wrappedValue.map { ($0 as NSString).lastPathComponent } ?? ""
82 var isFolder: ObjCBool = false
83 let folder = deleting.wrappedValue.map { FileManager.default.fileExists(atPath: $0, isDirectory: &isFolder) && isFolder.boolValue } ?? false
7984 return confirmationDialog("Delete “\(name)”?", isPresented: Binding { deleting.wrappedValue != nil } set: { if !$0 { deleting.wrappedValue = nil } },
8085 titleVisibility: .visible) {
8186 Button("Delete", role: .destructive) {
@@ -84,10 +89,11 @@ private extension View {
8489 session.kill(path)
8590 Task {
8691 do { try await workspace.trash(path) } catch { workspace.lastError = "Can't delete \(name): \(error.localizedDescription)" }
92 deleted()
8793 }
8894 }
8995 } message: {
90 Text("The whole file is removed. Files in iCloud Drive go to Recently Deleted in the Files app.")
96 Text(folder ? "The empty folder is removed." : "The whole file is removed. Files in iCloud Drive go to Recently Deleted in the Files app.")
9197 }
9298 }
9399}
@@ -105,9 +111,25 @@ private struct FolderContents: View {
105111 let root: String
106112 let folder: String
107113 @Binding var deleting: String?
114 /// Changes after each deletion, which the index may not show, so empty folders are read again.
115 let deletions: Int
108116
109117 private var prefix: String { folder.isEmpty ? "" : folder + "/" }
110118
119 /// Subfolders on disk with nothing in them but dotfiles, which the index, built from
120 /// files, doesn't know of.
121 private func emptySubfolders() -> [String] {
122 let fm = FileManager.default
123 let url = URL(fileURLWithPath: root).appendingPathComponent(folder)
124 guard let names = try? fm.contentsOfDirectory(atPath: url.path) else { return [] }
125 return names.filter { name in
126 var isFolder: ObjCBool = false
127 guard !name.hasPrefix("."), fm.fileExists(atPath: url.appendingPathComponent(name).path, isDirectory: &isFolder), isFolder.boolValue,
128 let inside = try? fm.contentsOfDirectory(atPath: url.appendingPathComponent(name).path) else { return false }
129 return inside.allSatisfy { $0.hasPrefix(".") }
130 }
131 }
132
111133 /// Paths below this folder, relative to it.
112134 private func below(_ relativePaths: [String]) -> [String] {
113135 relativePaths.filter { $0.hasPrefix(prefix) }.map { String($0.dropFirst(prefix.count)) }
@@ -116,14 +138,25 @@ private struct FolderContents: View {
116138 var body: some View {
117139 let files = workspace.files.filter { $0.root == root && $0.kind == .org && $0.relativePath.hasPrefix(prefix) }
118140 let pending = below(workspace.downloading.filter { $0.hasPrefix(root + "/") }.map { String($0.dropFirst(root.count + 1)) })
141 let empty = Set(emptySubfolders())
119142 let subfolders = Set((below(files.map(\.relativePath)) + pending).compactMap { path in
120143 path.firstIndex(of: "/").map { String(path[..<$0]) }
121 }).sorted { $0.localizedStandardCompare($1) == .orderedAscending }
144 }).union(empty).sorted { $0.localizedStandardCompare($1) == .orderedAscending }
122145 ForEach(subfolders, id: \.self) { name in
123146 // A value, as the files' links are: a stack mixing the two kinds pushes wrongly.
124147 NavigationLink(value: FolderRoute(root: root, folder: prefix + name)) {
125148 Label(name, systemImage: "folder")
126149 }
150 .swipeActions(edge: .trailing) {
151 if empty.contains(name) {
152 Button("Delete", systemImage: "trash") { deleting = root + "/" + prefix + name }.tint(.red)
153 }
154 }
155 .contextMenu {
156 if empty.contains(name) {
157 Button("Delete…", systemImage: "trash", role: .destructive) { deleting = root + "/" + prefix + name }
158 }
159 }
127160 }
128161 ForEach(files.filter { !$0.relativePath.dropFirst(prefix.count).contains("/") }) { file in
129162 NavigationLink(value: ReaderRoute.file(file.path)) {
docs/manual/guide/14-ios.org +1 −1
@@ -22,7 +22,7 @@ orgstar on iOS works on folders you choose, in iCloud Drive or in another app's
2222
2323orgstar keeps access to the folder across launches. Each folder appears as a section listing its =.org= files by their path inside the folder. Remove Folder at the end of a section removes it from orgstar; the files stay where they are.
2424
25Swipe left on a file, or touch and hold it, to delete it. orgstar asks first. A file in iCloud Drive goes to Recently Deleted in the Files app; elsewhere it is removed.
25Swipe left on a file, or touch and hold it, to delete it. orgstar asks first. A file in iCloud Drive goes to Recently Deleted in the Files app; elsewhere it is removed. Empty folders are listed too and delete the same way; a folder with anything in it other than hidden files can't be deleted from orgstar.
2626
2727Files that iCloud hasn't downloaded to the device yet appear in grey with a cloud icon. orgstar asks iCloud to download them and lists them as files once they arrive.
2828