Commit 54e0925047

54e09250478e8521ef93134df6a5391de368efcb

parent: 3e8222e0d8

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-10 20:08 UTC

iOS Folders: New File and New Folder

Each added folder's section ends with New File… and New Folder…; a subfolder has them under +. Names come from an alert; a file without an extension gets .org.

Layout: unified · split

Sources/OrgApp/WorkspaceModel.swift +1
@@ -294,6 +294,7 @@ public final class WorkspaceModel {
294 public struct FileError: LocalizedError { 294 public struct FileError: LocalizedError {
295 public let message: String 295 public let message: String
296 public var errorDescription: String? { message } 296 public var errorDescription: String? { message }
297 public init(message: String) { self.message = message }
297 } 298 }
298 299
299 /// Creates an empty file at `name` (a name or a relative path; `.org` is added when it has 300 /// Creates an empty file at `name` (a name or a relative path; `.org` is added when it has
Sources/OrgstarMobile/FoldersScreen.swift +77 −9
@@ -10,14 +10,17 @@ 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. 13 /// Counts deletions and new folders, so the list reads the empty folders on disk again.
14 @State private var deletions = 0 14 @State private var changes = 0
15 @State private var creating: NewItem?
15 16
16 var body: some View { 17 var body: some View {
17 List { 18 List {
18 ForEach(workspace.rootPaths, id: \.self) { root in 19 ForEach(workspace.rootPaths, id: \.self) { root in
19 Section((root as NSString).lastPathComponent) { 20 Section((root as NSString).lastPathComponent) {
20 FolderContents(workspace: workspace, root: root, folder: "", deleting: $deleting, deletions: deletions) 21 FolderContents(workspace: workspace, root: root, folder: "", deleting: $deleting, changes: changes)
22 Button("New File…", systemImage: "doc.badge.plus") { creating = NewItem(folder: root, isFolder: false) }
23 Button("New Folder…", systemImage: "folder.badge.plus") { creating = NewItem(folder: root, isFolder: true) }
21 Button("Remove Folder", role: .destructive) { try? workspace.removeRoot(path: root) } 24 Button("Remove Folder", role: .destructive) { try? workspace.removeRoot(path: root) }
22 } 25 }
23 } 26 }
@@ -29,7 +32,8 @@ struct FoldersScreen: View {
29 .navigationDestination(for: FolderRoute.self) { route in 32 .navigationDestination(for: FolderRoute.self) { route in
30 FolderPage(workspace: workspace, session: session, route: route) 33 FolderPage(workspace: workspace, session: session, route: route)
31 } 34 }
32 .confirmDeletion($deleting, workspace: workspace, session: session) { deletions += 1 } 35 .confirmDeletion($deleting, workspace: workspace, session: session) { changes += 1 }
36 .createItem($creating, workspace: workspace) { changes += 1 }
33 .overlay { 37 .overlay {
34 if workspace.rootPaths.isEmpty { 38 if workspace.rootPaths.isEmpty {
35 ContentUnavailableView { 39 ContentUnavailableView {
@@ -60,22 +64,86 @@ struct FoldersScreen: View {
60 } 64 }
61 } 65 }
62} 66}
63/// A subfolder's own list, asking about its own deletions. 67/// A subfolder's own list, asking about its own changes.
64private struct FolderPage: View { 68private struct FolderPage: View {
65 let workspace: WorkspaceModel 69 let workspace: WorkspaceModel
66 let session: DocumentSession 70 let session: DocumentSession
67 let route: FolderRoute 71 let route: FolderRoute
68 @State private var deleting: String? 72 @State private var deleting: String?
69 @State private var deletions = 0 73 @State private var changes = 0
74 @State private var creating: NewItem?
70 75
71 var body: some View { 76 var body: some View {
72 List { FolderContents(workspace: workspace, root: route.root, folder: route.folder, deleting: $deleting, deletions: deletions) } 77 let folder = route.root + "/" + route.folder
78 List { FolderContents(workspace: workspace, root: route.root, folder: route.folder, deleting: $deleting, changes: changes) }
73 .navigationTitle((route.folder as NSString).lastPathComponent) 79 .navigationTitle((route.folder as NSString).lastPathComponent)
74 .confirmDeletion($deleting, workspace: workspace, session: session) { deletions += 1 } 80 .confirmDeletion($deleting, workspace: workspace, session: session) { changes += 1 }
81 .createItem($creating, workspace: workspace) { changes += 1 }
82 .toolbar {
83 ToolbarItem(placement: .primaryAction) {
84 Menu("New", systemImage: "plus") {
85 Button("New File…", systemImage: "doc.badge.plus") { creating = NewItem(folder: folder, isFolder: false) }
86 Button("New Folder…", systemImage: "folder.badge.plus") { creating = NewItem(folder: folder, isFolder: true) }
87 }
88 }
89 }
90 }
91}
92
93/// A file or folder to create in `folder`, named in an alert.
94private struct NewItem: Identifiable {
95 let folder: String
96 let isFolder: Bool
97 var id: String { folder + (isFolder ? "/" : "") }
98}
99
100/// The alert naming a new file or folder.
101private struct NewItemAlert: ViewModifier {
102 @Binding var creating: NewItem?
103 let workspace: WorkspaceModel
104 let created: () -> Void
105 @State private var name = ""
106
107 func body(content: Content) -> some View {
108 content.alert(creating?.isFolder == true ? "New Folder" : "New File", isPresented: Binding { creating != nil } set: { if !$0 { creating = nil } }) {
109 TextField("Name", text: $name)
110 .autocorrectionDisabled()
111 .textInputAutocapitalization(.never)
112 Button("Create") {
113 guard let item = creating else { return }
114 let typed = name
115 name = ""
116 Task {
117 do {
118 if item.isFolder { try Self.makeFolder(typed, in: item.folder) } else { _ = try await workspace.createFile(named: typed, in: item.folder) }
119 created()
120 } catch {
121 workspace.lastError = error.localizedDescription
122 }
123 }
124 }
125 Button("Cancel", role: .cancel) { name = "" }
126 } message: {
127 if creating?.isFolder != true { Text("A name without an extension gets .org.") }
128 }
129 }
130
131 static func makeFolder(_ name: String, in folder: String) throws {
132 let trimmed = name.trimmingCharacters(in: .whitespaces)
133 guard !trimmed.isEmpty, !trimmed.contains("/"), !trimmed.hasPrefix(".") else {
134 throw WorkspaceModel.FileError(message: "Not a folder name: \(name)")
135 }
136 let url = URL(fileURLWithPath: folder).appendingPathComponent(trimmed)
137 guard !FileManager.default.fileExists(atPath: url.path) else { throw WorkspaceModel.FileError(message: "\(trimmed) already exists.") }
138 try FileManager.default.createDirectory(at: url, withIntermediateDirectories: false)
75 } 139 }
76} 140}
77 141
78private extension View { 142private extension View {
143 func createItem(_ creating: Binding<NewItem?>, workspace: WorkspaceModel, created: @escaping () -> Void) -> some View {
144 modifier(NewItemAlert(creating: creating, workspace: workspace, created: created))
145 }
146
79 /// Asks before deleting the file or empty folder in `deleting`, closing a file first. 147 /// 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 { 148 func confirmDeletion(_ deleting: Binding<String?>, workspace: WorkspaceModel, session: DocumentSession, deleted: @escaping () -> Void) -> some View {
81 let name = deleting.wrappedValue.map { ($0 as NSString).lastPathComponent } ?? "" 149 let name = deleting.wrappedValue.map { ($0 as NSString).lastPathComponent } ?? ""
@@ -112,7 +180,7 @@ private struct FolderContents: View {
112 let folder: String 180 let folder: String
113 @Binding var deleting: String? 181 @Binding var deleting: String?
114 /// Changes after each deletion, which the index may not show, so empty folders are read again. 182 /// Changes after each deletion, which the index may not show, so empty folders are read again.
115 let deletions: Int 183 let changes: Int
116 184
117 private var prefix: String { folder.isEmpty ? "" : folder + "/" } 185 private var prefix: String { folder.isEmpty ? "" : folder + "/" }
118 186
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. 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. 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. New File… and New Folder… at the end of a folder's section, or under + in a subfolder, make one there; a file name without an extension gets =.org=. 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