iOS Folders: list and delete empty folders !215
2 files changed, +42 −9
Layout: unified · split
Sources/OrgstarMobile/FoldersScreen.swift +41 −8
| @@ -10,12 +10,14 @@ struct FoldersScreen: View { | ||
| 10 | 10 | @State private var importing = false |
| 11 | 11 | /// The file whose deletion is being confirmed. |
| 12 | 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 | 16 | var body: some View { |
| 15 | 17 | List { |
| 16 | 18 | ForEach(workspace.rootPaths, id: \.self) { root in |
| 17 | 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 | 21 | Button("Remove Folder", role: .destructive) { try? workspace.removeRoot(path: root) } |
| 20 | 22 | } |
| 21 | 23 | } |
| @@ -27,7 +29,7 @@ struct FoldersScreen: View { | ||
| 27 | 29 | .navigationDestination(for: FolderRoute.self) { route in |
| 28 | 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 | 33 | .overlay { |
| 32 | 34 | if workspace.rootPaths.isEmpty { |
| 33 | 35 | ContentUnavailableView { |
| @@ -64,18 +66,21 @@ private struct FolderPage: View { | ||
| 64 | 66 | let session: DocumentSession |
| 65 | 67 | let route: FolderRoute |
| 66 | 68 | @State private var deleting: String? |
| 69 | @State private var deletions = 0 | |
| 67 | 70 | |
| 68 | 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 | 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 | |
| 75 | 78 | private 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 { | |
| 78 | 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 | 84 | return confirmationDialog("Delete “\(name)”?", isPresented: Binding { deleting.wrappedValue != nil } set: { if !$0 { deleting.wrappedValue = nil } }, |
| 80 | 85 | titleVisibility: .visible) { |
| 81 | 86 | Button("Delete", role: .destructive) { |
| @@ -84,10 +89,11 @@ private extension View { | ||
| 84 | 89 | session.kill(path) |
| 85 | 90 | Task { |
| 86 | 91 | do { try await workspace.trash(path) } catch { workspace.lastError = "Can't delete \(name): \(error.localizedDescription)" } |
| 92 | deleted() | |
| 87 | 93 | } |
| 88 | 94 | } |
| 89 | 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 | 111 | let root: String |
| 106 | 112 | let folder: String |
| 107 | 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 | 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 | 133 | /// Paths below this folder, relative to it. |
| 112 | 134 | private func below(_ relativePaths: [String]) -> [String] { |
| 113 | 135 | relativePaths.filter { $0.hasPrefix(prefix) }.map { String($0.dropFirst(prefix.count)) } |
| @@ -116,14 +138,25 @@ private struct FolderContents: View { | ||
| 116 | 138 | var body: some View { |
| 117 | 139 | let files = workspace.files.filter { $0.root == root && $0.kind == .org && $0.relativePath.hasPrefix(prefix) } |
| 118 | 140 | let pending = below(workspace.downloading.filter { $0.hasPrefix(root + "/") }.map { String($0.dropFirst(root.count + 1)) }) |
| 141 | let empty = Set(emptySubfolders()) | |
| 119 | 142 | let subfolders = Set((below(files.map(\.relativePath)) + pending).compactMap { path in |
| 120 | 143 | path.firstIndex(of: "/").map { String(path[..<$0]) } |
| 121 | }).sorted { $0.localizedStandardCompare($1) == .orderedAscending } | |
| 144 | }).union(empty).sorted { $0.localizedStandardCompare($1) == .orderedAscending } | |
| 122 | 145 | ForEach(subfolders, id: \.self) { name in |
| 123 | 146 | // A value, as the files' links are: a stack mixing the two kinds pushes wrongly. |
| 124 | 147 | NavigationLink(value: FolderRoute(root: root, folder: prefix + name)) { |
| 125 | 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 | 161 | ForEach(files.filter { !$0.relativePath.dropFirst(prefix.count).contains("/") }) { file in |
| 129 | 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 | |
| 23 | 23 | orgstar 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 | |
| 25 | Swipe 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. | |
| 25 | Swipe 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 | |
| 27 | 27 | Files 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 | |