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 {
10 @State private var importing = false 10 @State private var importing = false
11 /// The file whose deletion is being confirmed. 11 /// The file whose deletion is being confirmed.
12 @State private var deleting: String? 12 @State private var deleting: String?
13 /// Counts deletions, so the list reads the empty folders on disk again.
14 @State private var deletions = 0
13 15
14 var body: some View { 16 var body: some View {
15 List { 17 List {
16 ForEach(workspace.rootPaths, id: \.self) { root in 18 ForEach(workspace.rootPaths, id: \.self) { root in
17 Section((root as NSString).lastPathComponent) { 19 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)
19 Button("Remove Folder", role: .destructive) { try? workspace.removeRoot(path: root) } 21 Button("Remove Folder", role: .destructive) { try? workspace.removeRoot(path: root) }
20 } 22 }
21 } 23 }
@@ -27,7 +29,7 @@ struct FoldersScreen: View {
27 .navigationDestination(for: FolderRoute.self) { route in 29 .navigationDestination(for: FolderRoute.self) { route in
28 FolderPage(workspace: workspace, session: session, route: route) 30 FolderPage(workspace: workspace, session: session, route: route)
29 } 31 }
30 .confirmDeletion($deleting, workspace: workspace, session: session) 32 .confirmDeletion($deleting, workspace: workspace, session: session) { deletions += 1 }
31 .overlay { 33 .overlay {
32 if workspace.rootPaths.isEmpty { 34 if workspace.rootPaths.isEmpty {
33 ContentUnavailableView { 35 ContentUnavailableView {
@@ -64,18 +66,21 @@ private struct FolderPage: View {
64 let session: DocumentSession 66 let session: DocumentSession
65 let route: FolderRoute 67 let route: FolderRoute
66 @State private var deleting: String? 68 @State private var deleting: String?
69 @State private var deletions = 0
67 70
68 var body: some View { 71 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) }
70 .navigationTitle((route.folder as NSString).lastPathComponent) 73 .navigationTitle((route.folder as NSString).lastPathComponent)
71 .confirmDeletion($deleting, workspace: workspace, session: session) 74 .confirmDeletion($deleting, workspace: workspace, session: session) { deletions += 1 }
72 } 75 }
73} 76}
74 77
75private extension View { 78private extension View {
76 /// Asks before deleting the file in `deleting`, closing it first. 79 /// Asks before deleting the file or empty folder in `deleting`, closing a file first.
77 func confirmDeletion(_ deleting: Binding<String?>, workspace: WorkspaceModel, session: DocumentSession) -> some View { 80 func confirmDeletion(_ deleting: Binding<String?>, workspace: WorkspaceModel, session: DocumentSession, deleted: @escaping () -> Void) -> some View {
78 let name = deleting.wrappedValue.map { ($0 as NSString).lastPathComponent } ?? "" 81 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
79 return confirmationDialog("Delete “\(name)”?", isPresented: Binding { deleting.wrappedValue != nil } set: { if !$0 { deleting.wrappedValue = nil } }, 84 return confirmationDialog("Delete “\(name)”?", isPresented: Binding { deleting.wrappedValue != nil } set: { if !$0 { deleting.wrappedValue = nil } },
80 titleVisibility: .visible) { 85 titleVisibility: .visible) {
81 Button("Delete", role: .destructive) { 86 Button("Delete", role: .destructive) {
@@ -84,10 +89,11 @@ private extension View {
84 session.kill(path) 89 session.kill(path)
85 Task { 90 Task {
86 do { try await workspace.trash(path) } catch { workspace.lastError = "Can't delete \(name): \(error.localizedDescription)" } 91 do { try await workspace.trash(path) } catch { workspace.lastError = "Can't delete \(name): \(error.localizedDescription)" }
92 deleted()
87 } 93 }
88 } 94 }
89 } message: { 95 } 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.")
91 } 97 }
92 } 98 }
93} 99}
@@ -105,9 +111,25 @@ private struct FolderContents: View {
105 let root: String 111 let root: String
106 let folder: String 112 let folder: String
107 @Binding var deleting: String? 113 @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
108 116
109 private var prefix: String { folder.isEmpty ? "" : folder + "/" } 117 private var prefix: String { folder.isEmpty ? "" : folder + "/" }
110 118
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
111 /// Paths below this folder, relative to it. 133 /// Paths below this folder, relative to it.
112 private func below(_ relativePaths: [String]) -> [String] { 134 private func below(_ relativePaths: [String]) -> [String] {
113 relativePaths.filter { $0.hasPrefix(prefix) }.map { String($0.dropFirst(prefix.count)) } 135 relativePaths.filter { $0.hasPrefix(prefix) }.map { String($0.dropFirst(prefix.count)) }
@@ -116,14 +138,25 @@ private struct FolderContents: View {
116 var body: some View { 138 var body: some View {
117 let files = workspace.files.filter { $0.root == root && $0.kind == .org && $0.relativePath.hasPrefix(prefix) } 139 let files = workspace.files.filter { $0.root == root && $0.kind == .org && $0.relativePath.hasPrefix(prefix) }
118 let pending = below(workspace.downloading.filter { $0.hasPrefix(root + "/") }.map { String($0.dropFirst(root.count + 1)) }) 140 let pending = below(workspace.downloading.filter { $0.hasPrefix(root + "/") }.map { String($0.dropFirst(root.count + 1)) })
141 let empty = Set(emptySubfolders())
119 let subfolders = Set((below(files.map(\.relativePath)) + pending).compactMap { path in 142 let subfolders = Set((below(files.map(\.relativePath)) + pending).compactMap { path in
120 path.firstIndex(of: "/").map { String(path[..<$0]) } 143 path.firstIndex(of: "/").map { String(path[..<$0]) }
121 }).sorted { $0.localizedStandardCompare($1) == .orderedAscending } 144 }).union(empty).sorted { $0.localizedStandardCompare($1) == .orderedAscending }
122 ForEach(subfolders, id: \.self) { name in 145 ForEach(subfolders, id: \.self) { name in
123 // A value, as the files' links are: a stack mixing the two kinds pushes wrongly. 146 // A value, as the files' links are: a stack mixing the two kinds pushes wrongly.
124 NavigationLink(value: FolderRoute(root: root, folder: prefix + name)) { 147 NavigationLink(value: FolderRoute(root: root, folder: prefix + name)) {
125 Label(name, systemImage: "folder") 148 Label(name, systemImage: "folder")
126 } 149 }
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 }
127 } 160 }
128 ForEach(files.filter { !$0.relativePath.dropFirst(prefix.count).contains("/") }) { file in 161 ForEach(files.filter { !$0.relativePath.dropFirst(prefix.count).contains("/") }) { file in
129 NavigationLink(value: ReaderRoute.file(file.path)) { 162 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
22 22
23orgstar 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. 23orgstar 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.
24 24
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.
26 26
27Files 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. 27Files 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.
28 28