Sources/OrgApp/AgendaModel.swift
93 lines · 4357 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 first day shown, as `Days` numbers it; nil follows today.
14 public var start: Int?
15 public var span = 10
16 /// Where the view starts relative to today, as Doom's `org-agenda-start-day "-3d"`.
17 public var startOffset = -3
18 /// Files below the top level of each folder are agenda files too.
19 public var includeSubfolders = false
20 public var options = AgendaOptions()
21 public private(set) var isComputing = false
22
23 private let workspace: WorkspaceModel
24 @ObservationIgnored private var cache: [String: (mtime: Date, size: Int, source: AgendaSource)] = [:]
25 @ObservationIgnored private var generation = 0
26
27 public init(workspace: WorkspaceModel) {
28 self.workspace = workspace
29 }
30
31 /// Org files directly in each folder (or below it, with `includeSubfolders`), by path, as
32 /// Emacs lists a directory in `org-agenda-files`.
33 public var files: [String] {
34 workspace.files
35 .filter { $0.kind == .org && (includeSubfolders || !$0.relativePath.contains("/")) }
36 .map(\.path)
37 .sorted()
38 }
39
40 /// Recomputes the days. `open` is the open file's path and text.
41 public func refresh(open: (path: String, text: String)?, now: Date = Date(), calendar: Calendar = .current) async {
42 generation += 1
43 let id = generation
44 isComputing = true
45 let today = Days.absolute(now, calendar: calendar)
46 let clock = calendar.dateComponents([.hour, .minute], from: now)
47 let first = start ?? today + startOffset
48 let defaults = workspace.settings.org
49 var reads: [(path: String, cached: AgendaSource?, mtime: Date, size: Int, text: String?)] = []
50 for path in files {
51 if let open, open.path == path {
52 reads.append((path, nil, .distantPast, -1, open.text))
53 continue
54 }
55 let attributes = try? FileManager.default.attributesOfItem(atPath: path)
56 let mtime = attributes?[.modificationDate] as? Date ?? .distantPast
57 let size = (attributes?[.size] as? NSNumber)?.intValue ?? -1
58 let cached = cache[path].flatMap { $0.mtime == mtime && $0.size == size ? $0.source : nil }
59 reads.append((path, cached, mtime, size, nil))
60 }
61 let span = self.span
62 let options = self.options
63 let result = await Task.detached {
64 var sources: [(path: String, mtime: Date, size: Int, source: AgendaSource, fromDisk: Bool)] = []
65 for read in reads {
66 if let cached = read.cached {
67 sources.append((read.path, read.mtime, read.size, cached, true))
68 } else if let text = read.text {
69 sources.append((read.path, read.mtime, read.size, AgendaSource(path: read.path, text: text, defaults: defaults), false))
70 } else if let data = FileManager.default.contents(atPath: read.path) {
71 let text = SourceText(bytes: [UInt8](data)).text
72 sources.append((read.path, read.mtime, read.size, AgendaSource(path: read.path, text: text, defaults: defaults), true))
73 }
74 }
75 let days = Agenda.list(sources.map(\.source), start: first, days: span, today: today, now: (clock.hour ?? 0, clock.minute ?? 0), options: options)
76 return (sources, days)
77 }.value
78 guard id == generation else { return }
79 for source in result.0 where source.fromDisk {
80 cache[source.path] = (source.mtime, source.size, source.source)
81 }
82 days = result.1
83 isComputing = false
84 }
85
86 public func showToday() { start = nil }
87
88 /// Moves the view by a whole span, forward or back.
89 public func shift(forward: Bool, now: Date = Date(), calendar: Calendar = .current) {
90 let current = start ?? Days.absolute(now, calendar: calendar) + startOffset
91 start = current + (forward ? span : -span)
92 }
93}