Cotton code
100 lines · 4.5 KB · SwiftRaw
// AN AGENT IN A CHAT ON IPHONE (chat CONTRACT.md "An agent in a chat", "The iPhone's half"; the core in 34001566a5).// A person's connected assistant may join a Cotton group as an agent: Add an agent on the group's page (the core's// `chat.agent.choices`, or the line that says to connect one), the agents in the group's own list with Remove where// the core allows it, and an agent's words drawn on the left with the agent's square mark, its name and whose agent// it is. The feed, the acts and the done report are the core's; the phone only draws and asks.#if os(iOS)import CottonKitimport SwiftUIenum ChatAgentMethod {    /// `{ thread }` → `{ agents: [{ agent, name, in }], connect, line }`.    static let choices = "chat.agent.choices"    /// `{ thread, agent }` → `{ ok }`.    static let add = "chat.agent.add"    /// `{ thread, agent }` → `{ ok }`.    static let remove = "chat.agent.remove"}/// `chat.agent.choices`: his connected assistants, each saying whether it is in the chat; `connect` with the line that/// says to connect one first.struct ChatAgentChoices: Decodable, Equatable, Sendable {    struct Agent: Decodable, Equatable, Sendable, Identifiable {        var agent: String        var name: String        var `in`: Bool        var id: String { agent }    }    var agents: [Agent]    var connect: Bool    var line: String?}/// The agent mark: its name's first letter on the ink's rounded square, so an agent is never read as a person.struct ChatAgentMark: View {    let name: String    let side: CGFloat    @Environment(\.chatLook) private var look    var body: some View {        Text(verbatim: String(name.prefix(1)).uppercased())            .font(.system(size: side * 0.42, weight: .bold))            .foregroundStyle(look.colours.ground)            .frame(width: side, height: side)            .background(RoundedRectangle(cornerRadius: side * 0.28, style: .continuous).fill(look.colours.text))            .accessibilityHidden(true)    }}/// Add an agent: his assistants, one tap adding it (one already in says so), or the line to connect one first.struct ChatAgentChoicesSheet: View {    let choices: ChatAgentChoices    let add: (String) -> Void    @Environment(\.chatLook) private var look    var body: some View {        VStack(alignment: .leading, spacing: 0) {            if choices.connect, let line = choices.line {                Text(verbatim: line).font(look.font(15)).foregroundStyle(look.colours.dim)                    .fixedSize(horizontal: false, vertical: true)                    .padding(.vertical, 10)                    .accessibilityIdentifier("chat-group-agent-connect")            }            ForEach(choices.agents) { agent in                if agent.in {                    HStack(spacing: 12) {                        ChatAgentMark(name: agent.name, side: 36 * min(look.scale, 1.3))                        Text(verbatim: agent.name).font(look.font(16)).foregroundStyle(look.colours.text)                        Spacer(minLength: 8)                        Text(verbatim: ChatWords.line("group.agent_in")).font(look.font(13)).foregroundStyle(look.colours.dim)                    }                    .frame(minHeight: 52)                    .accessibilityElement(children: .combine)                    .accessibilityIdentifier("chat-group-agent-choice-\(agent.agent)")                } else {                    Button { add(agent.agent) } label: {                        HStack(spacing: 12) {                            ChatAgentMark(name: agent.name, side: 36 * min(look.scale, 1.3))                            Text(verbatim: agent.name).font(look.font(16)).foregroundStyle(look.colours.text)                            Spacer(minLength: 8)                            Image(systemName: "plus").font(.system(size: 15, weight: .semibold)).foregroundStyle(look.colours.accent)                                .accessibilityHidden(true)                        }                        .frame(minHeight: 52)                        .contentShape(Rectangle())                    }                    .buttonStyle(.plain)                    .accessibilityElement(children: .combine)                    .accessibilityAddTraits(.isButton)                    .accessibilityIdentifier("chat-group-agent-choice-\(agent.agent)")                }            }        }        .padding(.horizontal, Warm.side)        .padding(.bottom, 12)        .accessibilityElement(children: .contain)        .accessibilityIdentifier("chat-group-agent-choices")    }}#endif