Commit 29fd24ba30
Verified · cmc
Layout: unified · split
Sources/OrgstarMobile/CaptureScreen.swift +2 −17
| @@ -98,7 +98,7 @@ struct CaptureScreen: View { | |||
| 98 | context.now = request.day.map(Self.date) ?? Date() | 98 | context.now = request.day.map(Self.date) ?? Date() |
| 99 | switch Capture.fill(template.template, context: context, answers: answers) { | 99 | switch Capture.fill(template.template, context: context, answers: answers) { |
| 100 | case .success(let filled): | 100 | case .success(let filled): |
| 101 | let (filledText, cursor) = request.day.map { Self.scheduling(filled.text, cursor: filled.cursor, on: $0) } ?? (filled.text, filled.cursor) | 101 | let (filledText, cursor) = request.day.map { Capture.scheduling(filled.text, cursor: filled.cursor, on: $0) } ?? (filled.text, filled.cursor) |
| 102 | text = filledText | 102 | text = filledText |
| 103 | selection = TextSelection(insertionPoint: String.Index(utf16Offset: cursor, in: filledText)) | 103 | selection = TextSelection(insertionPoint: String.Index(utf16Offset: cursor, in: filledText)) |
| 104 | problem = nil | 104 | problem = nil |
| @@ -120,26 +120,11 @@ struct CaptureScreen: View { | |||
| 120 | return calendar.date(from: parts) ?? Date() | 120 | return calendar.date(from: parts) ?? Date() |
| 121 | } | 121 | } |
| 122 | 122 | ||
| 123 | /// An entry captured from an agenda day is scheduled on it: a SCHEDULED line goes under | ||
| 124 | /// the heading unless the template already gave the entry an active timestamp. The caret | ||
| 125 | /// keeps its place in the text. | ||
| 126 | static func scheduling(_ text: String, cursor: Int, on day: Int) -> (String, Int) { | ||
| 127 | guard text.hasPrefix("*"), text.firstMatch(of: /<[0-9]{4}-[0-9]{2}-[0-9]{2}/) == nil else { return (text, cursor) } | ||
| 128 | let d = Days.date(day) | ||
| 129 | let weekday = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][Days.weekday(day)] | ||
| 130 | let line = String(format: "SCHEDULED: <%04d-%02d-%02d ", d.year, d.month, d.day) + weekday + ">" | ||
| 131 | let utf16 = Array(text.utf16) | ||
| 132 | let end = utf16.firstIndex(of: 10) ?? utf16.count | ||
| 133 | let inserted = "\n" + line | ||
| 134 | let result = String(decoding: utf16[..<end], as: UTF16.self) + inserted + String(decoding: utf16[end...], as: UTF16.self) | ||
| 135 | return (result, cursor > end ? cursor + inserted.utf16.count : cursor) | ||
| 136 | } | ||
| 137 | |||
| 138 | private func file() { | 123 | private func file() { |
| 139 | guard let template else { return } | 124 | guard let template else { return } |
| 140 | let now = Date() | 125 | let now = Date() |
| 141 | let placed = template.resolved(workspace: workspace, session: session, clock: clock).flatMap { resolved in | 126 | let placed = template.resolved(workspace: workspace, session: session, clock: clock).flatMap { resolved in |
| 142 | session.capture(text, template: resolved.template, root: workspace.rootPaths.first, file: resolved.file, now: now) | 127 | session.capture(text, template: resolved.template, root: workspace.rootPaths.first, file: resolved.file, now: request.day.map(Self.date) ?? now) |
| 143 | } | 128 | } |
| 144 | switch placed { | 129 | switch placed { |
| 145 | case .failure(let error): | 130 | case .failure(let error): |