Sources/OrgApp/AgendaModel.swift
119 lines · 5454 bytes
1import Foundation
2import Observation
3import OrgCore
4import OrgIndex
5
6/// The day/week agenda over the workspace's agenda files. Sources are read from disk and
7/// cached by modification date and size; the open file's come from its buffer, so the agenda
8/// shows unsaved edits.
9@MainActor
10@Observable
11public final class AgendaModel {
12 public private(set) var days: [AgendaDay] = []
13 /// The entries of a TODO list or match view.
14 public private(set) var list: [AgendaItem] = []
15 public var mode: AgendaMode = .agenda(span: nil, startOffset: nil)
16 /// The first day shown, as `Days` numbers it; nil follows today.
17 public var start: Int?
18 public var span = 10
19 /// Where the view starts relative to today, as Doom's `org-agenda-start-day "-3d"`.
20 public var startOffset = -3
21 /// Files below the top level of each folder are agenda files too.
22 public var includeSubfolders = false
23 public var options = AgendaOptions()
24 public private(set) var isComputing = false
25
26 private let workspace: WorkspaceModel
27 @ObservationIgnored private var cache: [String: (mtime: Date, size: Int, source: AgendaSource)] = [:]
28 @ObservationIgnored private var generation = 0
29
30 public init(workspace: WorkspaceModel) {
31 self.workspace = workspace
32 }
33
34 /// Org files directly in each folder (or below it, with `includeSubfolders`), by path, as
35 /// Emacs lists a directory in `org-agenda-files`.
36 public var files: [String] {
37 workspace.files
38 .filter { $0.kind == .org && (includeSubfolders || !$0.relativePath.contains("/")) }
39 .map(\.path)
40 .sorted()
41 }
42
43 /// Recomputes the days. `open` is the open file's path and text.
44 public func refresh(open: (path: String, text: String)?, now: Date = Date(), calendar: Calendar = .current) async {
45 generation += 1
46 let id = generation
47 isComputing = true
48 let today = Days.absolute(now, calendar: calendar)
49 let clock = calendar.dateComponents([.hour, .minute], from: now)
50 var span = self.span
51 var startOffset = self.startOffset
52 if case .agenda(let viewSpan, let viewStart) = mode {
53 span = viewSpan ?? span
54 startOffset = viewStart ?? startOffset
55 }
56 let first = start ?? today + startOffset
57 let defaults = workspace.settings.org
58 var reads: [(path: String, cached: AgendaSource?, mtime: Date, size: Int, text: String?)] = []
59 for path in files {
60 if let open, open.path == path {
61 reads.append((path, nil, .distantPast, -1, open.text))
62 continue
63 }
64 let attributes = try? FileManager.default.attributesOfItem(atPath: path)
65 let mtime = attributes?[.modificationDate] as? Date ?? .distantPast
66 let size = (attributes?[.size] as? NSNumber)?.intValue ?? -1
67 let cached = cache[path].flatMap { $0.mtime == mtime && $0.size == size ? $0.source : nil }
68 reads.append((path, cached, mtime, size, nil))
69 }
70 let options = self.options
71 let mode = self.mode
72 let result = await Task.detached {
73 var sources: [(path: String, mtime: Date, size: Int, source: AgendaSource, fromDisk: Bool)] = []
74 for read in reads {
75 if let cached = read.cached {
76 sources.append((read.path, read.mtime, read.size, cached, true))
77 } else if let text = read.text {
78 sources.append((read.path, read.mtime, read.size, AgendaSource(path: read.path, text: text, defaults: defaults), false))
79 } else if let data = FileManager.default.contents(atPath: read.path) {
80 let text = SourceText(bytes: [UInt8](data)).text
81 sources.append((read.path, read.mtime, read.size, AgendaSource(path: read.path, text: text, defaults: defaults), true))
82 }
83 }
84 let all = sources.map(\.source)
85 var days: [AgendaDay] = []
86 var list: [AgendaItem] = []
87 switch mode {
88 case .agenda:
89 days = Agenda.list(all, start: first, days: span, today: today, now: (clock.hour ?? 0, clock.minute ?? 0), options: options)
90 case .todo(let keywords):
91 list = Agenda.todoList(all, keywords: keywords)
92 case .match(let match, let todoOnly):
93 list = Agenda.tagsMatch(all, match: match, todoOnly: todoOnly, now: now, calendar: calendar)
94 }
95 return (sources, days, list)
96 }.value
97 guard id == generation else { return }
98 for source in result.0 where source.fromDisk {
99 cache[source.path] = (source.mtime, source.size, source.source)
100 }
101 days = result.1
102 list = result.2
103 isComputing = false
104 }
105
106 public func showToday() { start = nil }
107
108 /// Moves the view by a whole span, forward or back.
109 public func shift(forward: Bool, now: Date = Date(), calendar: Calendar = .current) {
110 var span = self.span
111 var startOffset = self.startOffset
112 if case .agenda(let viewSpan, let viewStart) = mode {
113 span = viewSpan ?? span
114 startOffset = viewStart ?? startOffset
115 }
116 let current = start ?? Days.absolute(now, calendar: calendar) + startOffset
117 start = current + (forward ? span : -span)
118 }
119}