← All repositories

reportage

A life-logging app for iPhone

git clone https://code.inqk.net/reportage.git


Shared/Models/NarrativeRule.swift (3322B)

1import Foundation
2
3/// How a question's answer reads in a report's narrative. Questions without
4/// a rule are shown as their prompt and answer instead.
5///
6/// Rules write English. The narrative is English-only for now.
7enum NarrativeRule: Hashable, Sendable, Codable {
8    /// Joins the narrative's main clause as what was happening:
9    /// 'Reading and drinking coffee…'.
10    case activities
11    /// Joins the main clause as where: '…at the library'.
12    case place
13    /// Joins the main clause as who with: '…with Sam and Kai'.
14    case people
15    /// A fixed phrase for each answer to a yes/no question: 'Yes' →
16    /// 'Working'. A blank phrase leaves that answer to be shown as prompt
17    /// and answer.
18    case phrases(yes: String, no: String)
19    /// A phrase with the answer in it, where `%@` stands for the answer as it
20    /// reads mid-sentence: 'Feeling %@' → 'Feeling great'. Token questions
21    /// can use one in place of joining the main clause: 'Feeling tired and
22    /// hungry'.
23    case template(format: String)
24    /// A count, with the singular and plural noun: '2 coffees'.
25    case count(singular: String, plural: String)
26    /// A note, quoted exactly as written after a lead-in: "Learned: '…'".
27    case quote(lead: String)
28
29    /// The rule for a question of `kind` that has none of its own. Token
30    /// questions join the main clause; others have no rule.
31    static func derived(for kind: QuestionKind) -> NarrativeRule? {
32        switch kind {
33        case .tokens: .activities
34        case .location: .place
35        case .people: .people
36        case .yesNo, .choice, .number, .note: nil
37        }
38    }
39
40    /// Rules for the default questions, by ID, and for Reporter's built-in
41    /// questions, which ask the same things. Imported answers keep
42    /// Reporter's question IDs. Questions get these when they're seeded or
43    /// imported; stores made before rules were stored get them once by
44    /// `ReportStore.fillMissingNarrativeRules()`.
45    static let builtIn: [String: NarrativeRule] = {
46        var rules: [String: NarrativeRule] = [
47            "working": .phrases(yes: "Working", no: "Not working"),
48            "mood": .template(format: "Feeling %@"),
49            "sleep-quality": .template(format: "Slept %@"),
50            "coffee": .count(singular: "coffee", plural: "coffees"),
51            "learned": .quote(lead: "Learned"),
52        ]
53        rules = Dictionary(uniqueKeysWithValues: rules.map { (DefaultQuestions.idPrefix + $0.key, $0.value) })
54        for (reporterID, name) in reporterQuestions {
55            rules[reporterID] = rules[DefaultQuestions.idPrefix + name]
56        }
57        return rules
58    }()
59
60    /// Reporter's built-in questions, by the default question that asks the
61    /// same thing. Those that join the main clause get their rule from their
62    /// kind.
63    private static let reporterQuestions = [
64        "default-question-1": "sleep-quality",
65        "default-question-2": "working",
66        "default-question-6": "coffee",
67        "default-question-7": "learned",
68    ]
69}
70
71extension Question {
72    /// How the question's answers read in a narrative: its own rule, or else
73    /// the one its kind implies.
74    var resolvedNarrativeRule: NarrativeRule? {
75        narrativeRule ?? NarrativeRule.derived(for: kind)
76    }
77}