Cotton code
78 lines · 4.2 KB · RustRaw
//! Settings, decided: the people whose records are kept (owner, sex, date of//! birth), Apple Health, names not yet known, and what these records are.use serde_json::json;use super::labs::{act, ordered, PersonEntry};use super::shapes::*;use crate::keys::app::shapes::{Choice, Input, Prompt};use super::{day_said, say, sayf};use crate::health::lab::catalogue::Catalogue;/// `apple`: the phone reads Apple Health (an iPhone). Elsewhere (Android) Settings says nothing of it.pub fn settings(people: &[PersonEntry], unknown: usize, catalogue: &Catalogue, apple: bool) -> SettingsAnswer {    let person_rows = ordered(people)        .iter()        .map(|person| {            let sex = person.sex().map(|sex| say(&format!("settings.{sex}"))).unwrap_or_else(|| say("settings.sex_unknown"));            let born = person.born_on.as_deref().map(day_said).unwrap_or_else(|| say("settings.born_unknown"));            let mut detail = format!("{} {sex} · {} {born}", say("settings.sex"), say("settings.born"));            if person.owner && apple {                detail = format!("{} · {detail}", say("settings.owner_detail"));            }            let mut acts = vec![                Act {                    input: Some(Prompt {                        key: "value".into(),                        label: say("settings.sex"),                        input: Input::Choice,                        placeholder: None,                        // Not said is a choice too: its value empty, sent as no value (set_sex clears the sex).                        choices: ["female", "male", ""]                            .iter()                            .map(|sex| Choice { value: (*sex).into(), label: say(if sex.is_empty() { "settings.sex_unknown".to_string() } else { format!("settings.{sex}") }.as_str()) })                            .collect(),                    }),                    ..act("set_sex", say("acts.set_sex"), "health.act", json!({ "act": "set_sex", "person": person.id }))                },                Act {                    input: Some(Prompt { key: "value".into(), label: say("settings.born"), input: Input::Date, placeholder: None, choices: Vec::new() }),                    ..act("set_born", say("acts.set_born"), "health.act", json!({ "act": "set_born", "person": person.id }))                },            ];            if !person.owner {                acts.push(act("set_owner", say("acts.set_owner"), "health.act", json!({ "act": "set_owner", "person": person.id })));            }            SettingsRow { id: format!("person:{}", person.id), label: if person.owner { format!("{} · {}", person.name, say("settings.owner")) } else { person.name.clone() }, detail: Some(detail), acts }        })        .collect();    let mut sections = vec![SettingsSection { id: "people".into(), title: say("settings.people"), rows: person_rows }];    if apple {        sections.push(SettingsSection {                id: "apple_health".into(),                title: say("settings.apple_health"),                rows: vec![SettingsRow {                    id: "apple_health".into(),                    label: say("settings.reading"),                    detail: Some(say("settings.not_read")),                    acts: vec![act("connect", say("body.connect"), "health.act", json!({ "act": "connect_apple_health" }))],                }],        });    }    sections.extend([            SettingsSection {                id: "review".into(),                title: say("settings.review"),                rows: vec![SettingsRow { id: "unknown_names".into(), label: say("settings.review"), detail: Some(sayf("settings.review_detail", &[("n", &unknown.to_string())])), acts: Vec::new() }],            },            SettingsSection {                id: "about".into(),                title: say("settings.about"),                rows: vec![                    SettingsRow { id: "record".into(), label: say("settings.about"), detail: Some(say("settings.about_detail")), acts: Vec::new() },                    SettingsRow { id: "loinc".into(), label: say("settings.loinc"), detail: Some(catalogue.attribution.clone()), acts: Vec::new() },                ],            },    ]);    SettingsAnswer { title: say("settings.title"), sections }}