Commit effed25309
Verified · cmc
Layout: unified · split
Sources/OrgstarMobile/CaptureScreen.swift +28 −24
| @@ -22,6 +22,9 @@ struct CaptureScreen: View { | |||
| 22 | @State private var text = "" | 22 | @State private var text = "" |
| 23 | @State private var selection: TextSelection? | 23 | @State private var selection: TextSelection? |
| 24 | @State private var problem: String? | 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 | @FocusState private var focused: Bool | 28 | @FocusState private var focused: Bool |
| 26 | 29 | ||
| 27 | private var template: CaptureTemplate? { templates.first { $0.key == key } } | 30 | private var template: CaptureTemplate? { templates.first { $0.key == key } } |
| @@ -42,7 +45,7 @@ struct CaptureScreen: View { | |||
| 42 | if let problem { Text(problem).foregroundStyle(.red) } | 45 | if let problem { Text(problem).foregroundStyle(.red) } |
| 43 | } | 46 | } |
| 44 | if let entry = CaptureEntry(text: text, defaults: session.defaults) { | 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 | } else { | 50 | } else { |
| 48 | ForEach(questions.indices, id: \.self) { index in | 51 | ForEach(questions.indices, id: \.self) { index in |
| @@ -51,6 +54,19 @@ struct CaptureScreen: View { | |||
| 51 | if let problem { Text(problem).foregroundStyle(.red) } | 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 | .navigationTitle("Capture") | 70 | .navigationTitle("Capture") |
| 55 | .navigationBarTitleDisplayMode(.inline) | 71 | .navigationBarTitleDisplayMode(.inline) |
| 56 | .toolbar { | 72 | .toolbar { |
| @@ -220,18 +236,18 @@ struct QuestionField: View { | |||
| 220 | return formatter.string(from: date) | 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 | /// The captured entry's keyword, priority, dates and tags, set without editing its text. | 245 | /// The captured entry's keyword, priority, dates and tags, set without editing its text. |
| 224 | private struct EntryFields: View { | 246 | private struct EntryFields: View { |
| 225 | let entry: CaptureEntry | 247 | let entry: CaptureEntry |
| 226 | let tagChoices: [String] | ||
| 227 | let change: (any OrgCommand, [String: String]) -> Void | 248 | let change: (any OrgCommand, [String: String]) -> Void |
| 228 | @State private var planning: Planning? | 249 | @Binding var planning: EntryPlanning? |
| 229 | @State private var choosingTags = false | 250 | @Binding var choosingTags: Bool |
| 230 | |||
| 231 | private enum Planning: String, Identifiable { | ||
| 232 | case scheduled = "Scheduled", deadline = "Deadline" | ||
| 233 | var id: Self { self } | ||
| 234 | } | ||
| 235 | 251 | ||
| 236 | var body: some View { | 252 | var body: some View { |
| 237 | Section { | 253 | Section { |
| @@ -254,21 +270,9 @@ private struct EntryFields: View { | |||
| 254 | } | 270 | } |
| 255 | .tint(.primary) | 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 | Menu { | 276 | Menu { |
| 273 | Button("Today") { set(which, 0) } | 277 | Button("Today") { set(which, 0) } |
| 274 | Button("Tomorrow") { set(which, 1) } | 278 | Button("Tomorrow") { set(which, 1) } |
| @@ -283,12 +287,12 @@ private struct EntryFields: View { | |||
| 283 | .tint(.primary) | 287 | .tint(.primary) |
| 284 | } | 288 | } |
| 285 | 289 | ||
| 286 | private func set(_ which: Planning, _ days: Int) { | 290 | private func set(_ which: EntryPlanning, _ days: Int) { |
| 287 | let day = Calendar.current.date(byAdding: .day, value: days, to: Date()) ?? Date() | 291 | let day = Calendar.current.date(byAdding: .day, value: days, to: Date()) ?? Date() |
| 288 | change(SetPlanning(deadline: which == .deadline), ["date": QuestionField.stamp(day, withTime: false)]) | 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 | guard let stamp else { return Date() } | 296 | guard let stamp else { return Date() } |
| 293 | let point = stamp.start | 297 | let point = stamp.start |
| 294 | return Calendar.current.date(from: DateComponents(year: point.year, month: point.month, day: point.day, hour: point.hour, minute: point.minute)) ?? Date() | 298 | return Calendar.current.date(from: DateComponents(year: point.year, month: point.month, day: point.day, hour: point.hour, minute: point.minute)) ?? Date() |