iOS capture: date and tag sheets on the form !211
1 file changed, +28 −24
Layout: unified · split
Sources/OrgstarMobile/CaptureScreen.swift +28 −24
| @@ -22,6 +22,9 @@ struct CaptureScreen: View { | ||
| 22 | 22 | @State private var text = "" |
| 23 | 23 | @State private var selection: TextSelection? |
| 24 | 24 | @State private var problem: String? |
| 25 | /// The entry's date or tags sheet: on the form, not its section, which may not be in the window. | |
| 26 | @State private var planning: EntryPlanning? | |
| 27 | @State private var choosingTags = false | |
| 25 | 28 | @FocusState private var focused: Bool |
| 26 | 29 | |
| 27 | 30 | private var template: CaptureTemplate? { templates.first { $0.key == key } } |
| @@ -42,7 +45,7 @@ struct CaptureScreen: View { | ||
| 42 | 45 | if let problem { Text(problem).foregroundStyle(.red) } |
| 43 | 46 | } |
| 44 | 47 | if let entry = CaptureEntry(text: text, defaults: session.defaults) { |
| 45 | EntryFields(entry: entry, tagChoices: Array(Set(context.allTags + context.fileTags)).sorted(), change: change) | |
| 48 | EntryFields(entry: entry, change: change, planning: $planning, choosingTags: $choosingTags) | |
| 46 | 49 | } |
| 47 | 50 | } else { |
| 48 | 51 | ForEach(questions.indices, id: \.self) { index in |
| @@ -51,6 +54,19 @@ struct CaptureScreen: View { | ||
| 51 | 54 | if let problem { Text(problem).foregroundStyle(.red) } |
| 52 | 55 | } |
| 53 | 56 | } |
| 57 | .sheet(item: $planning) { which in | |
| 58 | let entry = CaptureEntry(text: text, defaults: session.defaults) | |
| 59 | DateTimeSheet(title: which.rawValue, initial: EntryFields.initial(which == .scheduled ? entry?.scheduled : entry?.deadline)) { answer in | |
| 60 | planning = nil | |
| 61 | if let answer { change(SetPlanning(deadline: which == .deadline), ["date": answer]) } | |
| 62 | } | |
| 63 | } | |
| 64 | .sheet(isPresented: $choosingTags) { | |
| 65 | TagsSheet(selected: CaptureEntry(text: text, defaults: session.defaults)?.tags ?? [], choices: Array(Set(context.allTags + context.fileTags)).sorted()) { tags in | |
| 66 | choosingTags = false | |
| 67 | if let tags { change(SetTags(), ["tags": tags.joined(separator: ":")]) } | |
| 68 | } | |
| 69 | } | |
| 54 | 70 | .navigationTitle("Capture") |
| 55 | 71 | .navigationBarTitleDisplayMode(.inline) |
| 56 | 72 | .toolbar { |
| @@ -220,18 +236,18 @@ struct QuestionField: View { | ||
| 220 | 236 | return formatter.string(from: date) |
| 221 | 237 | } |
| 222 | 238 | } |
| 239 | /// Which date of the captured entry is being picked. | |
| 240 | private enum EntryPlanning: String, Identifiable { | |
| 241 | case scheduled = "Scheduled", deadline = "Deadline" | |
| 242 | var id: Self { self } | |
| 243 | } | |
| 244 | ||
| 223 | 245 | /// The captured entry's keyword, priority, dates and tags, set without editing its text. |
| 224 | 246 | private struct EntryFields: View { |
| 225 | 247 | let entry: CaptureEntry |
| 226 | let tagChoices: [String] | |
| 227 | 248 | let change: (any OrgCommand, [String: String]) -> Void |
| 228 | @State private var planning: Planning? | |
| 229 | @State private var choosingTags = false | |
| 230 | ||
| 231 | private enum Planning: String, Identifiable { | |
| 232 | case scheduled = "Scheduled", deadline = "Deadline" | |
| 233 | var id: Self { self } | |
| 234 | } | |
| 249 | @Binding var planning: EntryPlanning? | |
| 250 | @Binding var choosingTags: Bool | |
| 235 | 251 | |
| 236 | 252 | var body: some View { |
| 237 | 253 | Section { |
| @@ -254,21 +270,9 @@ private struct EntryFields: View { | ||
| 254 | 270 | } |
| 255 | 271 | .tint(.primary) |
| 256 | 272 | } |
| 257 | .sheet(item: $planning) { which in | |
| 258 | DateTimeSheet(title: which.rawValue, initial: initial(which == .scheduled ? entry.scheduled : entry.deadline)) { answer in | |
| 259 | planning = nil | |
| 260 | if let answer { change(SetPlanning(deadline: which == .deadline), ["date": answer]) } | |
| 261 | } | |
| 262 | } | |
| 263 | .sheet(isPresented: $choosingTags) { | |
| 264 | TagsSheet(selected: entry.tags, choices: tagChoices) { tags in | |
| 265 | choosingTags = false | |
| 266 | if let tags { change(SetTags(), ["tags": tags.joined(separator: ":")]) } | |
| 267 | } | |
| 268 | } | |
| 269 | 273 | } |
| 270 | 274 | |
| 271 | private func date(_ which: Planning, _ stamp: Timestamp?) -> some View { | |
| 275 | private func date(_ which: EntryPlanning, _ stamp: Timestamp?) -> some View { | |
| 272 | 276 | Menu { |
| 273 | 277 | Button("Today") { set(which, 0) } |
| 274 | 278 | Button("Tomorrow") { set(which, 1) } |
| @@ -283,12 +287,12 @@ private struct EntryFields: View { | ||
| 283 | 287 | .tint(.primary) |
| 284 | 288 | } |
| 285 | 289 | |
| 286 | private func set(_ which: Planning, _ days: Int) { | |
| 290 | private func set(_ which: EntryPlanning, _ days: Int) { | |
| 287 | 291 | let day = Calendar.current.date(byAdding: .day, value: days, to: Date()) ?? Date() |
| 288 | 292 | change(SetPlanning(deadline: which == .deadline), ["date": QuestionField.stamp(day, withTime: false)]) |
| 289 | 293 | } |
| 290 | 294 | |
| 291 | private func initial(_ stamp: Timestamp?) -> Date { | |
| 295 | static func initial(_ stamp: Timestamp?) -> Date { | |
| 292 | 296 | guard let stamp else { return Date() } |
| 293 | 297 | let point = stamp.start |
| 294 | 298 | return Calendar.current.date(from: DateComponents(year: point.year, month: point.month, day: point.day, hour: point.hour, minute: point.minute)) ?? Date() |