commit 4a1184cc5395a458ba7ff080a33be3aad156de80
parent e1d04fbfa6b1e3eb3581b2a8c5fff79cf0163737
Author: Michael Camilleri <[email protected]>
Date: Thu, 22 Oct 2026 02:21:13 +0900
Let List questions read as a sentence of their own
A user may wish for a question to take an open list of responses. The
Activities type is the only type that permits this but there may be
questions (e.g. 'How are you feeling?') that should not be characterised
as about activity.
This commit renames the Activities type to List and lets the user
choose under Timeline whether its answers start the opening sentence or
read as a sentence of their own, given by a phrase as for Choice:
'Feeling tired and hungry.' Answers use their narrative forms and the
casing rules for activities.
The stored kind is still tokens and the phrase is stored as the
existing template rule. Existing questions have no stored rule and
still open the sentence, so stored questions, sync records and the
importer are unchanged.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat
| M | Notes/Design.md | | | 7 | +++++-- |
| M | Reportage/Services/Narrative/NarrativeBuilder.swift | | | 4 | ++++ |
| M | Reportage/Views/Settings/QuestionEditor.swift | | | 82 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------- |
| M | Reportage/Views/Settings/QuestionsView.swift | | | 14 | ++++++++++++-- |
| M | Shared/Models/NarrativeRule.swift | | | 4 | +++- |
| M | Tests/Unit/QuestionEditorTests.swift | | | 28 | ++++++++++++++++++++++++++++ |
| M | Tests/Unit/ReportSummaryTests.swift | | | 16 | ++++++++++++++++ |
7 files changed, 133 insertions(+), 22 deletions(-)
diff --git a/Notes/Design.md b/Notes/Design.md
@@ -454,11 +454,11 @@ Each question has a `NarrativeRule` saying how its answer reads: | Rule | Used for | Example | |------|----------|---------| -| Activities | `tokens` questions | 'Reading, drinking coffee and watching Netflix…' | +| Activities | `tokens` questions that open the sentence | 'Reading, drinking coffee and watching Netflix…' | | Place | `location` questions | '…at the library…' | | People | `people` questions | '…, with Sam and Kai.' | | Phrases | yes/no | 'Yes' → 'Working', 'No' → 'Not working' | -| Template | choice | 'Feeling %@' → 'Feeling great' | +| Template | choice; `tokens` questions with a sentence of their own | 'Feeling %@' → 'Feeling great' | | Count | number | '1 coffee', '2 coffees' | | Quote | note | "Learned: 'The neighbours are nice people.'" | @@ -466,6 +466,9 @@ Answers read in the questions' current order, so the user decides how narratives read by reordering questions, and every narrative follows, including imported ones. Activities, place and people join one clause, placed where the first of those answers comes. Other answers are one sentence each. +Only one list question can be set to open the sentence in the question +editor; one that already does in an older store keeps doing so, and a store +can have several. An answer to a question with no rule, or a rule left blank, is shown beneath the narrative as its prompt and answer. Answers to a question not shown in the timeline are left out of both.
diff --git a/Reportage/Services/Narrative/NarrativeBuilder.swift b/Reportage/Services/Narrative/NarrativeBuilder.swift
@@ -53,6 +53,10 @@ enum NarrativeBuilder {
case (.template(let format), .options(let options)) where !format.isBlank:
let answer = joined(options.map(InlineCasing.inline))
sentences.append(Sentence(format.replacingOccurrences(of: "%@", with: answer)))
+ case (.template(let format), .tokens(let ids)) where !format.isBlank:
+ let answers = distinct(ids, lookup).compactMap { form(of: $0, rule: .activities, lookup: lookup) }
+ guard !answers.isEmpty else { continue }
+ sentences.append(Sentence(format.replacingOccurrences(of: "%@", with: joined(answers))))
case (.count(let singular, let plural), .number(let number))
where !singular.isBlank && !plural.isBlank:
let noun = number == 1 ? singular : plural
diff --git a/Reportage/Views/Settings/QuestionEditor.swift b/Reportage/Views/Settings/QuestionEditor.swift
@@ -6,7 +6,7 @@ extension QuestionKind {
switch self {
case .yesNo: "Yes or No"
case .choice: "Choice"
- case .tokens: "Activities"
+ case .tokens: "List"
case .people: "People"
case .location: "Place"
case .number: "Number"
@@ -20,7 +20,7 @@ extension QuestionKind {
case .yesNo: "In the timeline, each answer reads as the phrase given for it below."
case .choice: "In the timeline, answers read as the phrase below, with the answer in it."
case .tokens:
- "In the timeline, answers start the report's opening sentence, as in 'Reading and drinking coffee at the library, with Sam.'"
+ "In the timeline, answers either start the report's opening sentence, as in 'Reading and drinking coffee at the library, with Sam.', or read as a sentence of their own."
case .location:
"In the timeline, answers join the report's opening sentence after 'at', as in 'Reading at the library.'"
case .people:
@@ -60,6 +60,12 @@ final class QuestionEditor {
private(set) var askOn: SurveyKinds
private(set) var archivedAt: Date?
var showsInTimeline: Bool
+ /// Whether a list question's answers start the report's opening
+ /// sentence, rather than reading as a sentence of their own.
+ var opensSentence: Bool
+ /// Whether the question started the opening sentence when editing
+ /// began, so it can go back to doing so while another question does.
+ let openedSentence: Bool
var yesPhrase = ""
var noPhrase = ""
var template = ""
@@ -76,6 +82,9 @@ final class QuestionEditor {
askOn = question.askOn
archivedAt = question.archivedAt
showsInTimeline = question.showsInTimeline
+ let opensSentence = question.resolvedNarrativeRule == .activities
+ self.opensSentence = opensSentence
+ openedSentence = !isNew && opensSentence
switch question.narrativeRule {
case .phrases(let yes, let no): (yesPhrase, noPhrase) = (yes, no)
case .template(let format): template = format
@@ -85,13 +94,16 @@ final class QuestionEditor {
}
}
- /// A new question of `kind`, asked in the prompted survey.
+ /// A new question of `kind`, asked in the prompted survey. A new list
+ /// question reads as a sentence of its own until it's chosen to start
+ /// the opening sentence.
convenience init(adding kind: QuestionKind) {
self.init(
question: Question(
id: UUID().uuidString, prompt: "", kind: kind, askOn: .random, order: 0,
modifiedAt: Date()),
isNew: true)
+ opensSentence = false
}
/// The rule the fields describe. Kinds whose rule comes from the kind
@@ -100,9 +112,10 @@ final class QuestionEditor {
switch kind {
case .yesNo: .phrases(yes: yesPhrase.trimmed, no: noPhrase.trimmed)
case .choice: .template(format: template.trimmed)
+ case .tokens: opensSentence ? .activities : .template(format: template.trimmed)
case .number: .count(singular: singular.trimmed, plural: plural.trimmed)
case .note: .quote(lead: lead.trimmed)
- case .tokens, .location, .people: nil
+ case .location, .people: nil
}
}
@@ -133,16 +146,20 @@ final class QuestionEditor {
drafted.archivedAt = archivedAt
drafted.showsInTimeline = showsInTimeline
// A question that never had a rule keeps none until one is written,
- // so a rule the kind implies isn't replaced with a blank one.
+ // so a rule the kind implies isn't replaced with a blank one. A list
+ // question that reads as its own sentence has a rule even when blank.
let fields: [String] =
switch kind {
case .yesNo: [yesPhrase, noPhrase]
case .choice: [template]
+ case .tokens: opensSentence ? [] : [template]
case .number: [singular, plural]
case .note: [lead]
- case .tokens, .location, .people: []
+ case .location, .people: []
}
- if question.narrativeRule != nil || fields.contains(where: { !$0.trimmed.isEmpty }) {
+ if question.narrativeRule != nil || (kind == .tokens && !opensSentence)
+ || fields.contains(where: { !$0.trimmed.isEmpty })
+ {
drafted.narrativeRule = narrativeRule
}
return (drafted, renames)
@@ -200,8 +217,8 @@ enum QuestionFormField: Hashable {
}
extension QuestionForm where Footer == EmptyView {
- init(editor: QuestionEditor, onCommit: @escaping () -> Void = {}) {
- self.init(editor: editor, onCommit: onCommit) { EmptyView() }
+ init(editor: QuestionEditor, onCommit: @escaping () -> Void = {}, sentenceOpener: Question? = nil) {
+ self.init(editor: editor, onCommit: onCommit, sentenceOpener: sentenceOpener) { EmptyView() }
}
}
@@ -212,6 +229,9 @@ extension QuestionForm where Footer == EmptyView {
struct QuestionForm<Footer: View>: View {
@Bindable var editor: QuestionEditor
var onCommit: () -> Void = {}
+ /// Another list question whose answers start the opening sentence,
+ /// which this one then can't.
+ var sentenceOpener: Question?
/// Shown at the end of the form, such as a way to remove the question.
@ViewBuilder var footer: Footer
@@ -257,7 +277,7 @@ struct QuestionForm<Footer: View>: View {
if editor.kind == .choice {
OptionsSection(editor: editor, focus: $focus, onCommit: onCommit)
}
- TimelineSection(editor: editor, focus: $focus)
+ TimelineSection(editor: editor, focus: $focus, sentenceOpener: sentenceOpener)
Section {
ForEach(Survey.allCases, id: \.self) { survey in
Toggle(survey.title, isOn: asked(in: survey))
@@ -276,6 +296,7 @@ struct QuestionForm<Footer: View>: View {
.foregroundStyle(Theme.ink)
.onChange(of: focus) { onCommit() }
.onChange(of: editor.showsInTimeline) { onCommit() }
+ .onChange(of: editor.opensSentence) { onCommit() }
.onDisappear { onCommit() }
}
@@ -355,6 +376,7 @@ private struct OptionsSection: View {
private struct TimelineSection: View {
@Bindable var editor: QuestionEditor
var focus: FocusState<QuestionFormField?>.Binding
+ var sentenceOpener: Question?
var body: some View {
Section {
@@ -367,12 +389,32 @@ private struct TimelineSection: View {
field("No", "Not working", text: $editor.noPhrase, index: 1)
case .choice:
field("Phrase", "Feeling %@", text: $editor.template, index: 0)
+ case .tokens:
+ if let sentenceOpener, !editor.openedSentence {
+ LabeledContent("Sentence") { Text("Own") }
+ .settingsRow()
+ SettingsNote {
+ Text(
+ "Answers to '\(sentenceOpener.prompt)' start the opening sentence. A report has only one."
+ )
+ }
+ } else {
+ Picker("Sentence", selection: $editor.opensSentence) {
+ Text("Opening").tag(true)
+ Text("Own").tag(false)
+ }
+ .tint(Theme.muted)
+ .settingsRow()
+ }
+ if !editor.opensSentence {
+ field("Phrase", "Feeling %@", text: $editor.template, index: 0)
+ }
case .number:
field("One", "coffee", text: $editor.singular, index: 0)
field("More", "coffees", text: $editor.plural, index: 1)
case .note:
field("Before", "Learned", text: $editor.lead, index: 0)
- case .tokens, .location, .people:
+ case .location, .people:
EmptyView()
}
if let example {
@@ -409,17 +451,19 @@ private struct TimelineSection: View {
/// whose phrasing is left blank. `nil` for kinds without phrasing.
private var example: String? {
let samples = NarrativeExample.samples(for: editor.kind, options: editor.options.map(\.text))
- guard editor.narrativeRule != nil else { return nil }
+ guard editor.narrativeRule != nil, editor.narrativeRule != .activities else { return nil }
+ let placement =
+ editor.kind == .tokens
+ ? String(localized: "Put %@ where the answers go.") : String(localized: "Put %@ where the answer goes.")
let narratives = samples.compactMap { NarrativeExample.narrative(of: $0, rule: editor.narrativeRule) }
let blank = String(localized: "Left blank, answers are shown beneath the report's sentence with their question.")
guard !narratives.isEmpty else {
- return editor.kind == .choice && samples.isEmpty
- ? String(localized: "Put %@ where the answer goes.") : blank
+ return editor.kind == .choice && samples.isEmpty ? placement : blank
}
let quoted = narratives.map { "'\($0)'" }.formatted(.list(type: .or))
var example = String(localized: "Reads as \(quoted)")
- if editor.kind == .choice {
- example = String(localized: "Put %@ where the answer goes. \(example)")
+ if editor.kind == .choice || editor.kind == .tokens {
+ example = placement + " " + example
}
if narratives.count < samples.count {
example += " " + String(localized: "An answer left blank is shown with its question.")
@@ -439,7 +483,8 @@ enum NarrativeExample {
.map { [.options([$0])] } ?? []
case .number: [.number(1), .number(2)]
case .note: [.text("…")]
- case .tokens, .location, .people: []
+ case .tokens: [.tokens(["…"])]
+ case .location, .people: []
}
}
@@ -448,6 +493,9 @@ enum NarrativeExample {
static func narrative(of value: ResponseValue, rule: NarrativeRule?) -> String? {
var lookup = SummaryLookup()
lookup.questionRules["Q"] = rule
+ if case .tokens(let ids) = value {
+ for id in ids { lookup.tokenTexts[id] = id }
+ }
let response = Response(questionID: "Q", prompt: "", value: value)
return NarrativeBuilder.build(responses: [response], lookup: lookup).narrative
}
diff --git a/Reportage/Views/Settings/QuestionsView.swift b/Reportage/Views/Settings/QuestionsView.swift
@@ -79,6 +79,13 @@ final class QuestionList {
questions.first { $0.id == id }
}
+ /// A list question other than the one with `id` whose answers start
+ /// the opening sentence, archived ones included, since past narratives
+ /// still read with them. A report has one opening sentence.
+ func sentenceOpener(otherThan id: String) -> Question? {
+ questions.first { $0.kind == .tokens && $0.id != id && $0.resolvedNarrativeRule == .activities }
+ }
+
/// Saves an edited question. Renamed answers, by old text, are renamed
/// in past reports too.
func update(_ question: Question, renamingOptions renames: [String: String] = [:]) {
@@ -367,7 +374,10 @@ private struct QuestionDetailView: View {
}
var body: some View {
- QuestionForm(editor: editor, onCommit: commit) {
+ QuestionForm(
+ editor: editor, onCommit: commit,
+ sentenceOpener: list.sentenceOpener(otherThan: editor.question.id)
+ ) {
removal
}
.navigationBarTitleDisplayMode(.inline)
@@ -467,7 +477,7 @@ private struct AddQuestionView: View {
var body: some View {
NavigationStack {
- QuestionForm(editor: editor)
+ QuestionForm(editor: editor, sentenceOpener: list.sentenceOpener(otherThan: editor.question.id))
.navigationTitle("New Question")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
diff --git a/Shared/Models/NarrativeRule.swift b/Shared/Models/NarrativeRule.swift
@@ -17,7 +17,9 @@ enum NarrativeRule: Hashable, Sendable, Codable {
/// and answer.
case phrases(yes: String, no: String)
/// A phrase with the answer in it, where `%@` stands for the answer as it
- /// reads mid-sentence: 'Feeling %@' → 'Feeling great'.
+ /// reads mid-sentence: 'Feeling %@' → 'Feeling great'. Token questions
+ /// can use one in place of joining the main clause: 'Feeling tired and
+ /// hungry'.
case template(format: String)
/// A count, with the singular and plural noun: '2 coffees'.
case count(singular: String, plural: String)
diff --git a/Tests/Unit/QuestionEditorTests.swift b/Tests/Unit/QuestionEditorTests.swift
@@ -46,6 +46,34 @@ struct QuestionEditorTests {
#expect(cleared.drafted().question.narrativeRule == .count(singular: "", plural: ""))
}
+ @Test("A list question opens the sentence or reads as its own, and new ones read as their own")
+ func listSentence() {
+ let existing = QuestionEditor(question: Self.question(.tokens))
+ #expect(existing.opensSentence)
+ #expect(existing.openedSentence)
+ #expect(existing.drafted().question.narrativeRule == nil)
+
+ existing.opensSentence = false
+ #expect(existing.drafted().question.narrativeRule == .template(format: ""))
+ existing.template = "Feeling %@"
+ let question = existing.drafted().question
+ #expect(question.narrativeRule == .template(format: "Feeling %@"))
+ let sample = NarrativeExample.samples(for: .tokens, options: [])[0]
+ #expect(NarrativeExample.narrative(of: sample, rule: question.narrativeRule) == "Feeling ….")
+
+ let reopened = QuestionEditor(question: question)
+ #expect(!reopened.opensSentence)
+ #expect(!reopened.openedSentence)
+ reopened.opensSentence = true
+ #expect(reopened.drafted().question.narrativeRule == .activities)
+
+ let added = QuestionEditor(adding: .tokens)
+ added.prompt = "Feeling?"
+ #expect(!added.opensSentence)
+ #expect(!added.openedSentence)
+ #expect(added.drafted().question.narrativeRule == .template(format: ""))
+ }
+
@Test("A blank prompt keeps the saved one, and a choice needs an answer to be added")
func completeness() {
let editor = QuestionEditor(adding: .choice)
diff --git a/Tests/Unit/ReportSummaryTests.swift b/Tests/Unit/ReportSummaryTests.swift
@@ -156,6 +156,22 @@ struct NarrativeBuilderTests {
#expect(narrative == "Learned: 'Tide tables'.")
}
+ @Test("A token question with a phrase reads as a sentence of its own")
+ func tokenTemplate() {
+ var lookup = Self.lookup
+ lookup.tokenTexts["tired"] = "Tired"
+ lookup.tokenTexts["hungry"] = "Hungry"
+ lookup.questionRules["feeling"] = .template(format: "Feeling %@")
+ let (narrative, details) = NarrativeBuilder.build(
+ responses: [
+ Self.response("activity", .tokens(["read"])),
+ Self.response("feeling", .tokens(["tired", "hungry"])),
+ ],
+ lookup: lookup)
+ #expect(narrative == "Reading. Feeling tired and hungry.")
+ #expect(details.isEmpty)
+ }
+
@Test("A rule left blank lists the answer as question and answer")
func blankRules() {
var lookup = Self.lookup