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 {
3636 try Data(link.utf8).write(to: folder.appendingPathComponent(name), options: .atomic)
3737 }
3838
39 /// The waiting links, oldest first, each removed as it's taken.
40 public func take() -> [String] {
39 /// A waiting link and the file that holds it.
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] {
4148 let names = ((try? FileManager.default.contentsOfDirectory(atPath: folder.path)) ?? []).filter { $0.hasSuffix(".link") }.sorted()
4249 return names.compactMap { name in
4350 let file = folder.appendingPathComponent(name)
44 defer { try? FileManager.default.removeItem(at: file) }
45 return (try? Data(contentsOf: file)).flatMap { String(data: $0, encoding: .utf8) }
51 return (try? Data(contentsOf: file)).flatMap { String(data: $0, encoding: .utf8) }.map { Link(file: file, text: $0) }
4652 }
4753 }
4854
55 public func remove(_ link: Link) {
56 try? FileManager.default.removeItem(at: link.file)
57 }
58
4959 public func setTemplates(_ templates: [Template]) throws {
5060 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
5161 try JSONEncoder().encode(templates).write(to: templatesFile, options: .atomic)
Sources/OrgstarMobile/OrgstarMobileApp.swift +27 −6
@@ -120,8 +120,9 @@ struct RootView: View {
120120 let workspace: WorkspaceModel
121121 let agenda: AgendaModel
122122 @State private var capture: CaptureRequest?
123 /// Captures from the share extension after the one showing.
124 @State private var waiting: [CaptureRequest] = []
123 /// The share extension's link whose capture sheet is showing, with its request's id. The link
124 /// stays in the inbox until the sheet is dismissed.
125 @State private var shared: (link: CaptureInbox.Link, request: UUID)?
125126 /// Files from the share extension, waiting for the entry to attach them to.
126127 @State private var sharedFiles: SharedFiles?
127128 @State private var tab = AppTab.agenda
@@ -290,7 +291,11 @@ struct RootView: View {
290291 capture = request
291292 }
292293
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
294299 CaptureScreen(workspace: workspace, session: session, request: request)
295300 }
296301 .sheet(item: $sharedFiles) { shared in
@@ -371,13 +376,29 @@ struct RootView: View {
371376
372377 /// What the share extension left, one capture sheet at a time.
373378 private func takeShared() {
374 waiting += (model.inbox?.take() ?? []).compactMap(CaptureRequest.init(link:))
375 if capture == nil { showWaiting() }
379 showWaiting()
376380 if sharedFiles == nil, let batch = AttachmentInbox()?.batches().first { sharedFiles = SharedFiles(batch: batch) }
377381 }
378382
383 /// The inbox's oldest capture, unless a capture sheet is showing or being dismissed.
379384 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()
381402 }
382403
383404 /// A widget's or the Live Activity's link.
Tests/OrgAppTests/CaptureInboxTests.swift +23 −4
@@ -5,14 +5,33 @@ import Testing
55@testable import OrgApp
66
77struct CaptureInboxTests {
8 @Test func takesLinksOldestFirstOnce() throws {
8 @Test func keepsLinksOldestFirstUntilRemoved() throws {
99 let folder = try Folder()
1010 let inbox = CaptureInbox(folder: folder.url.appendingPathComponent("Captures"))
11 #expect(inbox.take() == [])
11 #expect(inbox.links() == [])
1212 try inbox.add("org-protocol://capture?body=one")
1313 try inbox.add("org-protocol://capture?body=two")
14 #expect(inbox.take() == ["org-protocol://capture?body=one", "org-protocol://capture?body=two"])
15 #expect(inbox.take() == [])
14 let links = inbox.links()
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))
1635 }
1736
1837 @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:
353353- for a link, its title (editable) and address;
354354- a Text field, filled with shared text.
355355
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.
357357
358358The share form lists no templates until orgstar has run once with your configuration folder; the capture sheet then starts on the first template.
359359