| 1 | import Foundation |
| 2 | |
| 3 | /// Today is a projection of the existing /tasks list, not another inbox. |
| 4 | struct NeedsToday { |
| 5 | var running: [ThinTask] |
| 6 | var finished: [ThinTask] |
| 7 | |
| 8 | init(tasks: [ThinTask], now: Date, zone: TimeZone) { |
| 9 | var calendar = Calendar(identifier: .gregorian) |
| 10 | calendar.timeZone = zone |
| 11 | running = tasks.filter { |
| 12 | !["done", "cancelled"].contains($0.stage) && $0.needsYou.isEmpty && $0.copyHold != true && $0.copyCanSkip != true && Self.liveStep($0) != nil |
| 13 | }.sorted(by: Self.newestFirst) |
| 14 | finished = tasks.filter { |
| 15 | $0.stage == "done" && $0.doneAt.map { calendar.isDate($0, inSameDayAs: now) } == true |
| 16 | }.sorted { |
| 17 | let lhs = $0.doneAt ?? .distantPast, rhs = $1.doneAt ?? .distantPast |
| 18 | if lhs != rhs { return lhs > rhs } |
| 19 | return $0.id > $1.id |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | static func completionStatus(_ task: ThinTask, now: Date, zone: TimeZone, locale: Locale) -> String? { |
| 24 | let words = TimeWords(now: now, zone: zone, locale: locale) |
| 25 | if let merged = task.merged { |
| 26 | guard let at = merged.at, let into = merged.into else { return nil } |
| 27 | // COPY APPROVED: Luna, 2026-11-26. This slot is a time, not the web's commit. |
| 28 | return String(format: EquipoStrings.text("native.needs.merged_at"), into, words.clockOrDay(at)) |
| 29 | } |
| 30 | guard let at = task.doneAt else { return nil } |
| 31 | // COPY APPROVED: Luna, 2026-11-26. |
| 32 | return String(format: EquipoStrings.text("native.needs.done_at"), words.clockOrDay(at)) |
| 33 | } |
| 34 | |
| 35 | static func liveStep(_ task: ThinTask) -> ThinStep? { |
| 36 | guard let last = task.steps?.last, ["running", "queued"].contains(last.state) else { return nil } |
| 37 | return last |
| 38 | } |
| 39 | |
| 40 | private static func newestFirst(_ lhs: ThinTask, _ rhs: ThinTask) -> Bool { |
| 41 | if lhs.updatedAt != rhs.updatedAt { return lhs.updatedAt > rhs.updatedAt } |
| 42 | return lhs.id > rhs.id |
| 43 | } |
| 44 | } |
| 45 | |