Cotton code
127 lines · 4.9 KB · SwiftRaw
// DISAPPEARING MESSAGES on a person's or a group's page (CONTRACT.md// "Disappearing messages", as Android's warm/Timer.kt draws it): one row near// the top of the page, its name and the length it stands at (or Off); tapped,// a sheet with the core's title, its four lengths (the one on ticked) and its// note on what disappearing cannot stop. A choice sends `chat.act timer`; the// chat's line and the header's status are the core's. Where the core answers// no choices (the person cannot write there), the row is drawn without the// sheet. A chat the core answers no `timer` for has no row.#if os(iOS)import CottonKitimport SwiftUIstruct ChatTimerRow: View {    let card: ChatTimerCard    let open: () -> Void    @Environment(\.chatLook) private var look    /// The length as its choice says it; with no choices to read it from, the kit's own word for that length.    private var length: String {        if let on = card.choices.first(where: \.on) { return on.label }        return switch card.seconds {        case 86_400: ShellLines.line("chat", "app.timer.h24")        case 604_800: ShellLines.line("chat", "app.timer.d7")        case 7_776_000: ShellLines.line("chat", "app.timer.d90")        default: ShellLines.line("chat", "app.timer.off")        }    }    var body: some View {        if card.choices.isEmpty {            line.accessibilityElement(children: .combine).accessibilityIdentifier("chat-timer")        } else {            Button(action: open) { line }                .buttonStyle(.plain)                .accessibilityIdentifier("chat-timer")        }    }    /// One line with the length at its end; where that does not fit (the largest text), the length under the name,    /// so the name never breaks inside a word.    private var line: some View {        ViewThatFits(in: .horizontal) {            HStack(spacing: 12) {                mark                title                Spacer(minLength: 8)                said            }            VStack(alignment: .leading, spacing: 2) {                title                said            }            .padding(.vertical, 6)        }        .frame(maxWidth: .infinity, minHeight: 48, alignment: .leading)        .contentShape(Rectangle())    }    private var mark: some View {        Image(systemName: "timer")            .font(.system(size: 16 * min(look.scale, 1.3)))            .foregroundStyle(look.colours.dim)            .frame(width: 24)            .accessibilityHidden(true)    }    private var title: some View {        Text(verbatim: card.title).font(look.font(16)).foregroundStyle(look.colours.text)    }    private var said: some View {        Text(verbatim: length)            .font(look.font(14))            .foregroundStyle(card.seconds > 0 ? look.colours.accent : look.colours.dim)    }}/// The lengths (the one on ticked) and the core's note under them; a choice is handed back unless it is the one standing.struct ChatTimerSheet: View {    let card: ChatTimerCard    let set: (Int) -> Void    let close: () -> Void    @Environment(\.chatLook) private var look    var body: some View {        VStack(alignment: .leading, spacing: 4) {            Text(verbatim: card.title)                .font(look.font(20, .bold)).foregroundStyle(look.colours.text)                .padding(.bottom, 6)                .accessibilityAddTraits(.isHeader)            ForEach(card.choices) { choice in                Button {                    close()                    if !choice.on { set(choice.seconds) }                } label: {                    HStack(spacing: 12) {                        Text(verbatim: choice.label).font(look.font(16)).foregroundStyle(look.colours.text)                        Spacer(minLength: 8)                        if choice.on {                            Image(systemName: "checkmark").font(.system(size: 15 * min(look.scale, 1.3), weight: .semibold))                                .foregroundStyle(look.colours.accent)                                .accessibilityHidden(true)                        }                    }                    .frame(maxWidth: .infinity, minHeight: 48, alignment: .leading)                    .contentShape(Rectangle())                }                .buttonStyle(.plain)                .accessibilityAddTraits(choice.on ? [.isButton, .isSelected] : .isButton)                .accessibilityIdentifier("chat-timer-\(choice.seconds)")            }            Text(verbatim: card.note)                .font(look.font(13))                .foregroundStyle(look.colours.dim)                .fixedSize(horizontal: false, vertical: true)                .padding(.top, 8)                .padding(.bottom, 6)                .accessibilityIdentifier("chat-timer-note")        }        .padding(.horizontal, Warm.side)        .padding(.vertical, 18)        .accessibilityElement(children: .contain)        .accessibilityIdentifier("chat-timer-sheet")    }}#endif