| @@ -18,6 +18,8 @@ struct AgendaScreen: View { |
| 18 | 18 | @Environment(MobileModel.self) private var model |
| 19 | 19 | @Environment(\.colorScheme) private var colorScheme |
| 20 | 20 | @State private var tick = 0 |
| 21 | /// The time the now line shows, kept to the minute while the agenda is open. |
| 22 | @State private var now = Date() |
| 21 | 23 | @State private var views = AgendaViewLoader.builtIn |
| 22 | 24 | @State private var viewName = "Agenda" |
| 23 | 25 | @State private var filter = AgendaFilter() |
| @@ -173,6 +175,15 @@ struct AgendaScreen: View { |
| 173 | 175 | views = loaded.views |
| 174 | 176 | if let problem = loaded.problems.first { message = problem } |
| 175 | 177 | } |
| 178 | // The now line moves each minute; a new day refreshes the statuses. |
| 179 | .task { |
| 180 | while !Task.isCancelled { |
| 181 | let next = Calendar.current.nextDate(after: Date(), matching: DateComponents(second: 0), matchingPolicy: .nextTime) ?? Date().addingTimeInterval(60) |
| 182 | try? await Task.sleep(for: .seconds(next.timeIntervalSinceNow)) |
| 183 | now = Date() |
| 184 | if Days.absolute(now, calendar: .current) != agenda.today { tick += 1 } |
| 185 | } |
| 186 | } |
| 176 | 187 | // Calendar access is asked for when calendar-events is turned on. |
| 177 | 188 | .task(id: model.settingsVersion) { |
| 178 | 189 | guard OrgPreferences.calendarEvents(), !CalendarEvents.isAuthorized else { return } |
| @@ -232,17 +243,17 @@ struct AgendaScreen: View { |
| 232 | 243 | if group.isEmpty { |
| 233 | 244 | Text("Nothing scheduled").font(.subheadline).foregroundStyle(.secondary) |
| 234 | 245 | } |
| 235 | | let now = nowIndex(group) |
| 246 | let nowAt = nowIndex(group) |
| 236 | 247 | ForEach(Array(group.rows.enumerated()), id: \.element.id) { index, item in |
| 237 | | if index == now { NowLine(color: todayColor) } |
| 248 | if index == nowAt { NowLine(time: now, color: todayColor) } |
| 238 | 249 | row(item, day: group.day) |
| 239 | 250 | } |
| 240 | | if let now, now == group.rows.count { NowLine(color: todayColor) } |
| 251 | if let nowAt, nowAt == group.rows.count { NowLine(time: now, color: todayColor) } |
| 241 | 252 | } |
| 242 | 253 | } |
| 243 | 254 | |
| 244 | 255 | private func nowIndex(_ group: AgendaDayGroup) -> Int? { |
| 245 | | let c = Calendar.current.dateComponents([.hour, .minute], from: Date()) |
| 256 | let c = Calendar.current.dateComponents([.hour, .minute], from: now) |
| 246 | 257 | return group.nowIndex(now: (c.hour ?? 0) * 100 + (c.minute ?? 0)) |
| 247 | 258 | } |
| 248 | 259 | |
| @@ -672,11 +683,12 @@ struct AgendaRowView: View { |
| 672 | 683 | |
| 673 | 684 | /// The current time between timed rows, as Org's `now - - -` line. |
| 674 | 685 | struct NowLine: View { |
| 686 | let time: Date |
| 675 | 687 | let color: Color |
| 676 | 688 | |
| 677 | 689 | var body: some View { |
| 678 | 690 | HStack(spacing: 6) { |
| 679 | | Text(Date.now.formatted(date: .omitted, time: .shortened)) |
| 691 | Text(time.formatted(date: .omitted, time: .shortened)) |
| 680 | 692 | .font(.caption2.weight(.semibold).monospacedDigit()) |
| 681 | 693 | .foregroundStyle(color) |
| 682 | 694 | Circle().fill(color).frame(width: 6, height: 6) |