Cotton code
107 lines · 4.5 KB · SwiftRaw
// BLOCKED (chat SENDERS.md; COMMS WITHOUT MAIL, 26 Nov 2026): the one place a blocked sender's texts wait 30 days, and// the only way to see them or unblock. It stood at the foot of Senders; Senders is Cotton Post's now, so You opens it// from its `blocked` row (`chat.you` answers it only while the place holds someone). The core answers the place// (`chat.senders.blocked`) and Unblock (`chat.sender.act { key, act: "unblock" }`); every word is the core's.#if os(iOS)import CottonKitimport SwiftUIenum ChatBlockedMethod {    /// `{}` → the Blocked place.    static let place = "chat.senders.blocked"    /// `{ key, act: "unblock" }` → `{ key, said }`.    static let act = "chat.sender.act"}/// `chat.sender.act`'s answer: one line to say.struct ChatSenderDone: Decodable, Equatable, Sendable {    var said: String?}/// `chat.senders.blocked`: the one Blocked place.struct ChatSendersBlocked: Decodable, Equatable, Sendable {    struct Row: Decodable, Equatable, Sendable, Identifiable {        var key: String        var name: String        var sentLine: String        var erases: String?        var unblock: String        var id: String { key }    }    var title: String    var line: String?    var empty: String?    var rows: [Row]}/// The Blocked page under You: who is blocked, how much they sent and when it is erased, and Unblock.struct ChatBlockedPage: View {    let back: () -> Void    @Environment(\.chatLook) private var look    @Environment(\.cottonCore) private var core    @Environment(ToastCenter.self) private var toasts: ToastCenter?    @State private var place: ChatSendersBlocked?    private struct ActAsk: Encodable, Sendable { let key: String; let act: String }    var body: some View {        ChatYouPageFrame(title: place?.title, id: "senders-blocked-page", back: back) {            VStack(alignment: .leading, spacing: 0) {                if let line = place?.line, !line.isEmpty {                    Text(verbatim: line).font(look.font(15)).foregroundStyle(look.colours.dim)                        .fixedSize(horizontal: false, vertical: true)                        .padding(.bottom, 12)                }                if let place, place.rows.isEmpty, let empty = place.empty {                    Text(verbatim: empty).font(look.font(16)).foregroundStyle(look.colours.dim)                        .fixedSize(horizontal: false, vertical: true)                        .padding(.top, 24)                        .accessibilityIdentifier("senders-blocked-empty")                }                ForEach(place?.rows ?? []) { row in                    HStack(spacing: 12) {                        VStack(alignment: .leading, spacing: 2) {                            Text(verbatim: row.name).font(look.font(16, .semibold)).foregroundStyle(look.colours.text).lineLimit(1)                            Text(verbatim: [row.sentLine, row.erases].compactMap { $0 }.filter { !$0.isEmpty }.joined(separator: " · "))                                .font(look.font(13)).foregroundStyle(look.colours.dim)                                .fixedSize(horizontal: false, vertical: true)                        }                        .frame(maxWidth: .infinity, alignment: .leading)                        .accessibilityElement(children: .combine)                        .accessibilityIdentifier("blocked-\(row.key)")                        WarmPill(words: row.unblock, id: "unblock-\(row.key)") {                            Task { await unblock(row.key) }                        }                    }                    .frame(minHeight: 56)                    .padding(.vertical, 6)                }            }            .padding(.top, 4)        }        .task { await read() }    }    private func read() async {        guard let core else { return }        do {            place = try await core.call(ChatBlockedMethod.place, [String: String](), as: ChatSendersBlocked.self)        } catch {            toasts?.show(ActionRefused.words(for: error), tone: .error)        }    }    /// Unblock, its line said; the place asked again.    private func unblock(_ key: String) async {        guard let core else { return }        do {            let done = try await core.call(ChatBlockedMethod.act, ActAsk(key: key, act: "unblock"), as: ChatSenderDone.self)            if let said = done.said, !said.isEmpty { toasts?.show(said) }        } catch {            toasts?.show(ActionRefused.words(for: error), tone: .error)        }        await read()    }}#endif