Cotton code
175 lines · 7.5 KB · RustRaw
//! The conversation list: order, what a filter keeps, counts, the fan and//! each row's swipes. Words from `kits/shell/lines/chat.json` (`app.*`).use std::collections::BTreeMap;use crate::rules::shell_lines::LineSet;use super::shapes::{FanTile, Filter, ListCounts, Swipe};/// The sources in the fan's order; one tile each for those the person has.pub const SOURCES: &[&str] = &["cotton", "whatsapp", "sms", "imessage", "instagram"];/// The chat line set, read once per answer.pub fn lines() -> LineSet {    LineSet::shipped("chat").unwrap_or_else(|| LineSet::of_rows("chat", &[]).expect("the chat line set ships"))}/// Say one line, the empty string when the kit lacks it (line_keys holds/// every literal key here to the kit).pub fn say(lines: &LineSet, line: &str, fills: &[(&str, &str)]) -> String {    lines.say(line, fills).unwrap_or_default()}/// A conversation as the list's rules need it.#[derive(Debug, Clone, PartialEq, Eq)]pub struct Listed {    pub group: bool,    pub source: String,    pub archived: bool,    /// Unread messages, or marked unread.    pub unread: bool,    /// The last word was theirs and is unanswered.    pub waiting: bool,}/// Does `filter` keep this conversation? Archived ones stand only under/// Archived (and under search, which `chat.list` handles before this).pub fn keeps(filter: &Filter, row: &Listed) -> bool {    match filter {        Filter::Archived => row.archived,        _ if row.archived => false,        Filter::All => true,        Filter::People => !row.group,        Filter::Groups => row.group,        Filter::Unread => row.unread,        Filter::Waiting => row.waiting,        Filter::Source(source) => row.source == *source,    }}/// Every count the fan shows.pub fn counts(rows: &[Listed]) -> ListCounts {    let mut counts = ListCounts::default();    for row in rows {        if row.archived {            counts.archived += 1;            continue;        }        counts.all += 1;        if row.group {            counts.groups += 1;        } else {            counts.people += 1;        }        if row.unread {            counts.unread += 1;        }        if row.waiting {            counts.waiting += 1;        }        *counts.sources.entry(row.source.clone()).or_insert(0) += 1;    }    counts}/// The fan: All, People, Groups, Unread, Archived, one tile per source the/// person has, then Calls (the recent calls, `chat.calls`; not a filter) and Account. Never trimmed: an empty filter still stands/// (`cotton-the-fan`); a source tile stands once any conversation is from it.pub fn fan(lines: &LineSet, counts: &ListCounts, on: &Filter) -> Vec<FanTile> {    let tile = |id: &str, label: String, count: Option<u32>| FanTile { id: id.into(), label, count, on: on.id() == id };    let mut tiles = vec![        tile("all", say(lines, "app.fan.all", &[]), Some(counts.all)),        tile("people", say(lines, "app.fan.people", &[]), Some(counts.people)),        tile("groups", say(lines, "app.fan.groups", &[]), Some(counts.groups)),        tile("unread", say(lines, "app.fan.unread", &[]), Some(counts.unread)),        tile("archived", say(lines, "app.fan.archived", &[]), Some(counts.archived)),    ];    let mut had: BTreeMap<&str, u32> = BTreeMap::new();    for (source, n) in &counts.sources {        had.insert(source.as_str(), *n);    }    for source in SOURCES {        if let Some(n) = had.get(source) {            tiles.push(tile(source, source_name(lines, source), Some(*n)));        }    }    for (source, n) in &counts.sources {        if !SOURCES.contains(&source.as_str()) {            tiles.push(tile(source, source_name(lines, source), Some(*n)));        }    }    tiles.push(tile("calls", say(lines, "app.fan.calls", &[]), None));    tiles.push(tile("account", say(lines, "app.fan.account", &[]), None));    tiles}/// The filters above the list: All, People (everyone who writes to him: the/// phones open `chat.senders`), Unread (with how many: unread, or marked/// unread by hand), Groups. Archived is reached from the list's foot, not here./// "Waiting on you" is gone from the chips (owner, 22 Nov 2026: the last word/// being theirs does not mean they wait on you; a chat to answer later is/// marked unread instead); `waiting` is still taken, for a phone that asks it.pub fn filters(lines: &LineSet, counts: &ListCounts, on: &Filter) -> Vec<FanTile> {    let chip = |id: &str, line: &str, count: Option<u32>| FanTile { id: id.into(), label: say(lines, line, &[]), count, on: on.id() == id };    vec![        chip("all", "app.filter.all", None),        // People gave way to the Senders main tab (SENDERS.md "DECIDED: Senders 3").        chip("unread", "app.filter.unread", (counts.unread > 0).then_some(counts.unread)),        chip("groups", "app.filter.groups", None),    ]}/// A row's long-press acts, in order: Mark as read or Mark as unread, Pin,/// Mute, Archive, Delete (asked first, naming them). Every act a row has; the/// swipes are one of them a side (Comms 28).pub fn hold(lines: &LineSet, unread: bool, pinned: bool, muted: bool, archived: bool, name: &str) -> Vec<Swipe> {    let swipe = |act: &str, line: &str| Swipe { act: act.into(), label: say(lines, line, &[]), confirm: None };    vec![        if unread { swipe("read", "app.hold.read") } else { swipe("unread", "app.hold.unread") },        if pinned { swipe("unpinChat", "app.swipe.unpin") } else { swipe("pinChat", "app.swipe.pin") },        if muted { swipe("unmute", "app.swipe.unmute") } else { swipe("mute", "app.swipe.mute") },        if archived { swipe("unarchive", "app.swipe.unarchive") } else { swipe("archive", "app.swipe.archive") },        Swipe { act: "deleteChat".into(), label: say(lines, "app.swipe.delete", &[]), confirm: Some(say(lines, "app.confirm.delete_chat", &[("name", name)])) },    ]}/// A source's name as a person reads it.pub fn source_name(lines: &LineSet, source: &str) -> String {    let said = match source {        "cotton" => say(lines, "app.source.cotton", &[]),        "whatsapp" => say(lines, "app.source.whatsapp", &[]),        "sms" => say(lines, "app.source.sms", &[]),        "imessage" => say(lines, "app.source.imessage", &[]),        "instagram" => say(lines, "app.source.instagram", &[]),        _ => String::new(),    };    if said.is_empty() {        source.to_string()    } else {        said    }}/// A row's swipes, one act a side (owner, Comms 28): leading Unread or Read, trailing Archive or Unarchive. Pin,/// Mute and Delete are the long press's alone (`hold`).pub fn swipes(lines: &LineSet, unread: bool, _pinned: bool, _muted: bool, archived: bool, _name: &str) -> (Vec<Swipe>, Vec<Swipe>) {    let swipe = |act: &str, line: &str| Swipe { act: act.into(), label: say(lines, line, &[]), confirm: None };    let leading = vec![if unread { swipe("read", "app.swipe.read") } else { swipe("unread", "app.swipe.unread") }];    let trailing = vec![if archived { swipe("unarchive", "app.swipe.unarchive") } else { swipe("archive", "app.swipe.archive") }];    (leading, trailing)}/// The line where no row stands.pub fn empty(lines: &LineSet, filter: &Filter, searching: bool) -> String {    if searching {        return say(lines, "app.list.none_found", &[]);    }    match filter {        Filter::Unread => say(lines, "app.list.empty_unread", &[]),        Filter::Archived => say(lines, "app.list.empty_archived", &[]),        Filter::Groups => say(lines, "app.list.empty_groups", &[]),        Filter::Waiting => say(lines, "app.list.empty_waiting", &[]),        _ => say(lines, "app.list.empty", &[]),    }}