Commit ef89d935d4

ef89d935d420c67a68025e240043169d5f9c0abe

parent: 4e8f923e9d

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-22 12:25 UTC

Keep shared captures in the inbox until their sheet is saved or discarded

CaptureInbox.links() leaves each link in place and remove() deletes it once
its capture sheet is dismissed. A link that can't be read is kept.

Closes #191

Layout: unified · split

Sources/OrgApp/CaptureInbox.swift +14 −4
@@ -36,16 +36,26 @@ public struct CaptureInbox: Sendable {
36 try Data(link.utf8).write(to: folder.appendingPathComponent(name), options: .atomic) 36 try Data(link.utf8).write(to: folder.appendingPathComponent(name), options: .atomic)
37 } 37 }
38 38
39 /// The waiting links, oldest first, each removed as it's taken. 39 /// A waiting link and the file that holds it.
40 public func take() -> [String] { 40 public struct Link: Sendable, Hashable {
41 public let file: URL
42 public let text: String
43 }
44
45 /// The waiting links, oldest first. Each stays until `remove`; a file that can't be read
46 /// is left out and kept.
47 public func links() -> [Link] {
41 let names = ((try? FileManager.default.contentsOfDirectory(atPath: folder.path)) ?? []).filter { $0.hasSuffix(".link") }.sorted() 48 let names = ((try? FileManager.default.contentsOfDirectory(atPath: folder.path)) ?? []).filter { $0.hasSuffix(".link") }.sorted()
42 return names.compactMap { name in 49 return names.compactMap { name in
43 let file = folder.appendingPathComponent(name) 50 let file = folder.appendingPathComponent(name)
44 defer { try? FileManager.default.removeItem(at: file) } 51 return (try? Data(contentsOf: file)).flatMap { String(data: $0, encoding: .utf8) }.map { Link(file: file, text: $0) }
45 return (try? Data(contentsOf: file)).flatMap { String(data: $0, encoding: .utf8) }
46 } 52 }
47 } 53 }
48 54
55 public func remove(_ link: Link) {
56 try? FileManager.default.removeItem(at: link.file)
57 }
58
49 public func setTemplates(_ templates: [Template]) throws { 59 public func setTemplates(_ templates: [Template]) throws {
50 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) 60 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
51 try JSONEncoder().encode(templates).write(to: templatesFile, options: .atomic) 61 try JSONEncoder().encode(templates).write(to: templatesFile, options: .atomic)
Sources/OrgstarMobile/OrgstarMobileApp.swift +27 −6
@@ -120,8 +120,9 @@ struct RootView: View {
120 let workspace: WorkspaceModel 120 let workspace: WorkspaceModel
121 let agenda: AgendaModel 121 let agenda: AgendaModel
122 @State private var capture: CaptureRequest? 122 @State private var capture: CaptureRequest?
123 /// Captures from the share extension after the one showing. 123 /// The share extension's link whose capture sheet is showing, with its request's id. The link
124 @State private var waiting: [CaptureRequest] = [] 124 /// stays in the inbox until the sheet is dismissed.
125 @State private var shared: (link: CaptureInbox.Link, request: UUID)?
125 /// Files from the share extension, waiting for the entry to attach them to. 126 /// Files from the share extension, waiting for the entry to attach them to.
126 @State private var sharedFiles: SharedFiles? 127 @State private var sharedFiles: SharedFiles?
127 @State private var tab = AppTab.agenda 128 @State private var tab = AppTab.agenda
@@ -290,7 +291,11 @@ struct RootView: View {
290 capture = request 291 capture = request
291 } 292 }
292 293
293 .sheet(item: $capture, onDismiss: showWaiting) { request in 294 // Another capture took the sheet from a shared one, which stays in the inbox for later.
295 .onChange(of: capture?.id) { _, id in
296 if let id, id != shared?.request { shared = nil }
297 }
298 .sheet(item: $capture, onDismiss: captureDismissed) { request in
294 CaptureScreen(workspace: workspace, session: session, request: request) 299 CaptureScreen(workspace: workspace, session: session, request: request)
295 } 300 }
296 .sheet(item: $sharedFiles) { shared in 301 .sheet(item: $sharedFiles) { shared in
@@ -371,13 +376,29 @@ struct RootView: View {
371 376
372 /// What the share extension left, one capture sheet at a time. 377 /// What the share extension left, one capture sheet at a time.
373 private func takeShared() { 378 private func takeShared() {
374 waiting += (model.inbox?.take() ?? []).compactMap(CaptureRequest.init(link:)) 379 showWaiting()
375 if capture == nil { showWaiting() }
376 if sharedFiles == nil, let batch = AttachmentInbox()?.batches().first { sharedFiles = SharedFiles(batch: batch) } 380 if sharedFiles == nil, let batch = AttachmentInbox()?.batches().first { sharedFiles = SharedFiles(batch: batch) }
377 } 381 }
378 382
383 /// The inbox's oldest capture, unless a capture sheet is showing or being dismissed.
379 private func showWaiting() { 384 private func showWaiting() {
380 if !waiting.isEmpty { capture = waiting.removeFirst() } 385 guard capture == nil, shared == nil, let inbox = model.inbox else { return }
386 for link in inbox.links() {
387 guard let request = CaptureRequest(link: link.text) else {
388 inbox.remove(link)
389 continue
390 }
391 shared = (link, request.id)
392 capture = request
393 return
394 }
395 }
396
397 /// The shared capture was saved or discarded, so its link goes; then the next one shows.
398 private func captureDismissed() {
399 if let shared { model.inbox?.remove(shared.link) }
400 shared = nil
401 showWaiting()
381 } 402 }
382 403
383 /// A widget's or the Live Activity's link. 404 /// A widget's or the Live Activity's link.
Tests/OrgAppTests/CaptureInboxTests.swift +23 −4
@@ -5,14 +5,33 @@ import Testing
5@testable import OrgApp 5@testable import OrgApp
6 6
7struct CaptureInboxTests { 7struct CaptureInboxTests {
8 @Test func takesLinksOldestFirstOnce() throws { 8 @Test func keepsLinksOldestFirstUntilRemoved() throws {
9 let folder = try Folder() 9 let folder = try Folder()
10 let inbox = CaptureInbox(folder: folder.url.appendingPathComponent("Captures")) 10 let inbox = CaptureInbox(folder: folder.url.appendingPathComponent("Captures"))
11 #expect(inbox.take() == []) 11 #expect(inbox.links() == [])
12 try inbox.add("org-protocol://capture?body=one") 12 try inbox.add("org-protocol://capture?body=one")
13 try inbox.add("org-protocol://capture?body=two") 13 try inbox.add("org-protocol://capture?body=two")
14 #expect(inbox.take() == ["org-protocol://capture?body=one", "org-protocol://capture?body=two"]) 14 let links = inbox.links()
15 #expect(inbox.take() == []) 15 #expect(links.map(\.text) == ["org-protocol://capture?body=one", "org-protocol://capture?body=two"])
16 // Another instance, as after the app was ended with both queued.
17 #expect(CaptureInbox(folder: inbox.folder).links() == links)
18 inbox.remove(links[0])
19 #expect(inbox.links().map(\.text) == ["org-protocol://capture?body=two"])
20 inbox.remove(links[1])
21 #expect(inbox.links() == [])
22 }
23
24 @Test func keepsALinkItCannotRead() throws {
25 let folder = try Folder()
26 let inbox = CaptureInbox(folder: folder.url.appendingPathComponent("Captures"))
27 try inbox.add("org-protocol://capture?body=one")
28 let notText = inbox.folder.appendingPathComponent("0-not-text.link")
29 try Data([0xFF, 0xFE, 0xFD]).write(to: notText)
30 let directory = inbox.folder.appendingPathComponent("1-directory.link")
31 try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
32 #expect(inbox.links().map(\.text) == ["org-protocol://capture?body=one"])
33 #expect(FileManager.default.fileExists(atPath: notText.path))
34 #expect(FileManager.default.fileExists(atPath: directory.path))
16 } 35 }
17 36
18 @Test func keepsTheTemplateList() throws { 37 @Test func keepsTheTemplateList() throws {
docs/manual/guide/08-capture.org +1 −1
@@ -353,7 +353,7 @@ In another app, share a web page or text and choose orgstar. The share form has:
353- for a link, its title (editable) and address; 353- for a link, its title (editable) and address;
354- a Text field, filled with shared text. 354- a Text field, filled with shared text.
355 355
356Capture saves the item in the app group's capture inbox. The next time you open orgstar, it opens the capture sheet with the item as an org-protocol capture: the link becomes =%a= and =%:link=, the title =%:description=, and the text =%i=. Several waiting items open one after another, oldest first. If the extension says orgstar's shared folder isn't available, the app group is missing from the build and nothing can be saved. 356Capture saves the item in the app group's capture inbox. The next time you open orgstar, it opens the capture sheet with the item as an org-protocol capture: the link becomes =%a= and =%:link=, the title =%:description=, and the text =%i=. Several waiting items open one after another, oldest first. An item stays in the inbox until its sheet is saved or cancelled, so items still waiting when orgstar is ended open the next time. If the extension says orgstar's shared folder isn't available, the app group is missing from the build and nothing can be saved.
357 357
358The share form lists no templates until orgstar has run once with your configuration folder; the capture sheet then starts on the first template. 358The share form lists no templates until orgstar has run once with your configuration folder; the capture sheet then starts on the first template.
359 359