Sources/OrgApp/WatchPayload.swift
41 lines · 2416 bytes
1import Foundation
2import OrgCore
3import OrgPresentation
4import OrgWatchData
5
6extension WatchAgenda.Card {
7 /// The first `limit` of `items` that are positions of normal and double cards, which the
8 /// watch reviews, with their text cut to `length` characters.
9 public static func review(_ items: [Flashcards.Item], limit: Int = 30, length: Int = 400) -> [WatchAgenda.Card] {
10 func cut(_ text: String) -> String { text.count > length ? String(text.prefix(length)) + "…" : text }
11 return items.lazy.filter { [.normal, .double].contains($0.card.cardType) }.compactMap { item -> WatchAgenda.Card? in
12 guard let face = Flashcards.face(item.card, position: item.position) else { return nil }
13 return WatchAgenda.Card(path: item.path, card: item.card.id, position: item.position, title: face.front.title.map(cut), front: cut(face.front.text),
14 backTitle: cut(face.back.title ?? ""), back: cut(face.back.text))
15 }.prefix(limit).map { $0 }
16 }
17}
18
19extension WatchAgenda {
20 /// Today's group of the agenda, for the watch, with the running clock and the entry
21 /// Clock In Last would take.
22 public init(groups: [AgendaDayGroup], today: Int, clock: ClockModel.Running?, lastClocked: String?, generated: Date, calendar: Calendar = .current) {
23 let group = groups.first { $0.day == today }
24 let midnight = group?.date(calendar: calendar) ?? calendar.startOfDay(for: generated)
25 func entry(_ row: AgendaRow) -> Entry {
26 func at(_ hhmm: Int?) -> Date? {
27 hhmm.flatMap { calendar.date(byAdding: .minute, value: ($0 / 100) * 60 + $0 % 100, to: midnight) }
28 }
29 let item = row.item
30 return Entry(
31 id: row.id, title: row.title, gutter: row.gutter, category: row.category, todo: row.todo, status: row.status.rawValue,
32 isDone: row.isDone, isEvent: row.isEvent, start: at(row.start), end: at(row.end), path: item?.path,
33 offset: item?.headingOffset, headingLine: item?.headingLine, doneKeyword: item?.doneKeyword
34 )
35 }
36 self.init(
37 generated: generated, day: midnight, overdue: group?.overdue.map(entry) ?? [], rows: group?.rows.map(entry) ?? [],
38 clock: clock.map { Clock(heading: $0.heading, startedAt: $0.startedAt) }, lastClocked: lastClocked
39 )
40 }
41}