// SENDERS (mail.html "Senders: cleaning up"): every mail sender, grouped by domain, loudest first (lots of mail, little
// read), with Never read, Most mail, People and A–Z; a domain opens to its page: how many in all, in the Inbox and
// opened, whether he ever replied, its addresses, and the four acts each carrying its count. Archive is one tap
// (owner, 25 Nov 2026); Delete, Unsubscribe and Block ask once. `mail.senders {sort}`, `mail.sender {key}`,
// `mail.archive {scope}`, `mail.ask`/`mail.do` (CONTRACT row 9). Until the core answers a call, the screen says the
// core's words in place.
#if os(iOS)
import CottonKit
import SwiftUI
/// One sender on the list (`mail.senders`): a person by their address, or a machine by its domain, with its count and
/// how much he opened.
struct PostSenderRow: Decodable, Equatable, Sendable, Identifiable {
var key: String
var title: String
var line: String?
var count: Int?
var count_label: String?
var person: Bool = false
var addresses: Int?
var blocked: Bool?
var id: String { key }
}
struct PostSenders: Decodable, Equatable, Sendable {
var firstSync: PostFirstSync?
struct Sort: Decodable, Equatable, Sendable, Identifiable { var id: String; var label: String; var on: Bool }
struct Blocked: Decodable, Equatable, Sendable { var label: String }
struct Search: Decodable, Equatable, Sendable { var hint: String }
var title: String?
var line: String?
var search: Search?
var found_none: String?
var sorts: [Sort]?
var rows: [PostSenderRow]
var after: String?
var foot: String?
var blocked: Blocked?
var empty: String?
}
/// `mail.sender`: one sender's page.
struct PostSender: Decodable, Equatable, Sendable {
struct Number: Decodable, Equatable, Sendable, Identifiable { var value: String; var label: String; var id: String { label } }
struct Address: Decodable, Equatable, Sendable, Identifiable { var key: String; var address: String; var line: String?; var count: Int?; var id: String { key } }
struct Addresses: Decodable, Equatable, Sendable { var title: String; var hint: String?; var rows: [Address] }
/// The call an act makes: `mail.archive {scope, key}` in one tap, or `mail.ask {act, scope, key}` for its one sheet.
struct Call: Decodable, Equatable, Sendable { var method: String; var scope: String; var key: String; var act: String? }
struct Act: Decodable, Equatable, Sendable, Identifiable { var act: String; var label: String; var line: String?; var count: Int?; var call: Call?; var id: String { act } }
struct Label: Decodable, Equatable, Sendable { var label: String }
var key: String
var title: String
var person: Bool = false
var line: String?
var numbers: [Number]?
var said: [String]?
var addresses: Addresses?
var acts: [Act]?
/// Every mail from the sender under the acts (`mail.list {place}`), its count the heading.
struct Mails: Decodable, Equatable, Sendable { var title: String; var place: String }
var mails: Mails?
var merge: Label?
var rename: Label?
var part: Label?
var blocked: Bool?
}
/// `mail.blocked`: the shared blocked list (Comms' and Post's), each with Unblock.
struct PostBlocked: Decodable, Equatable, Sendable {
struct Row: Decodable, Equatable, Sendable, Identifiable { var key: String; var title: String; var line: String?; var unblock: String; var id: String { key } }
var title: String
var rows: [Row]
}
/// `mail.ask`'s sheet (CONTRACT "Shared shapes", Plan): the question with its number, the one switch it may carry
/// (`also`), the button that repeats the number, and the plan `mail.do` runs.
struct PostPlan: Decodable, Equatable, Sendable, Identifiable {
struct Also: Decodable, Equatable, Sendable { var label: String; var count: Int?; var on: Bool }
var plan: String
var title: String
var line: String
var go: String
var cancel: String
var also: Also?
var danger: Bool?
var id: String { plan }
}
/// `mail.do`'s answer: the after-state, or (Delete only, when more mail came since he agreed) the one question again.
struct PostDone: Decodable, Sendable {
struct More: Decodable, Equatable, Sendable {
struct Choice: Decodable, Equatable, Sendable, Identifiable { var id: String; var label: String }
var line: String
var choices: [Choice]
}
var more: More?
var done: PostAfter?
enum CodingKeys: String, CodingKey { case more }
init(from decoder: Decoder) throws {
let c = try decoder.container(keyedBy: CodingKeys.self)
more = try c.decodeIfPresent(More.self, forKey: .more)
done = more == nil ? try PostAfter(from: decoder) : nil
}
}
struct PostSendersScreen: View {
let model: PostModel
/// The chip he chose last, kept across launches, as Mail's is.
@AppStorage("post-senders-chip") private var sort = "never_read"
/// Searched as he types, over every sender (the core's, never only the page loaded).
@State private var text = ""
@State private var senders: PostRead?
/// The rows of later pages, read as he reaches the end.
@State private var more: [PostSenderRow] = []
@State private var after: String?
var body: some View {
ScrollView {
LazyVStack(alignment: .leading, spacing: 0) {
PostTitle(text: senders?.answer?.title ?? PostWords.line("senders.title")).padding(.top, 8)
if let line = senders?.answer?.line { PostLead(text: line) }
PostSearchBox(hint: senders?.answer?.search?.hint ?? PostWords.line("senders.search"), text: $text, id: "post-senders-search")
.padding(.top, 10)
PostChips(selected: sort) {
ForEach(senders?.answer?.sorts ?? []) { item in
PostChip(label: item.label, on: item.id == sort, id: "post-senders-\(item.id)") { sort = item.id }
.id(item.id)
}
}
.padding(.top, 12)
switch senders {
case .read(let answer)?:
if let empty = answer.empty { PostEmpty(line: empty, first: answer.firstSync) }
if let none = answer.found_none { PostNote(text: none) }
let rows = answer.rows + more
ForEach(Array(rows.enumerated()), id: \.element.id) { index, row in
senderRow(row, last: index == rows.count - 1)
.onAppear { if index == rows.count - 1 { Task { await readMore() } } }
}
if let foot = answer.foot { PostNote(text: foot) }
if let blocked = answer.blocked {
PostLinkRow(title: blocked.label, last: true, id: "post-senders-blocked") { model.push(.blocked) }
.padding(.top, 8)
}
case .refused(let line)?: PostRefused(line: line)
case nil: EmptyView()
}
}
.padding(.horizontal, PostInk.side)
.padding(.bottom, 24)
}
.refreshable { model.changed() }
.scrollDismissesKeyboard(.immediately)
.background(PostInk.ground)
.task(id: "\(model.generation)/\(sort)/\(text)") {
// A pause while he types, so each word asks once.
if !text.isEmpty { try? await Task.sleep(for: .milliseconds(180)) }
if Task.isCancelled { return }
let read = await model.read("mail.senders", request(), as: PostSenders.self)
senders = read
more = []
after = read.answer?.after
}
.accessibilityIdentifier("post-senders")
}
private func readMore() async {
guard let cursor = after else { return }
after = nil
var asked = request()
asked["after"] = .string(cursor)
if let page = await model.read("mail.senders", asked, as: PostSenders.self).answer {
more += page.rows
after = page.after
}
}
/// The sort chosen, and the words searched when there are any.
private func request() -> [String: JSONValue] {
var asked: [String: JSONValue] = ["sort": .string(sort)]
let words = text.trimmingCharacters(in: .whitespaces)
if !words.isEmpty { asked["text"] = .string(words) }
return asked
}
private func senderRow(_ row: PostSenderRow, last: Bool) -> some View {
Button {
// The search's keyboard goes down as the sender opens.
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
model.push(.sender(row.key))
} label: {
HStack(spacing: 12) {
PostFace(from: PostFrom(name: row.title, face: row.person ? "person" : "machine"))
VStack(alignment: .leading, spacing: 3) {
Text(row.title).font(PostType.sans(16.5, .semibold)).foregroundStyle(PostInk.ink).lineLimit(2)
if let line = row.line { Text(line).font(PostType.small).foregroundStyle(PostInk.quiet).fixedSize(horizontal: false, vertical: true) }
}
Spacer(minLength: 8)
if let count = row.count_label ?? row.count?.formatted() {
Text(count).font(PostType.sans(14, .semibold)).foregroundStyle(PostInk.ink)
}
}
.padding(.vertical, 11)
.contentShape(Rectangle())
.overlay(alignment: .bottom) { if !last { PostRule() } }
}
.buttonStyle(PostPressStyle())
.accessibilityElement(children: .combine)
.accessibilityIdentifier("post-sender-\(row.key)")
}
}
/// ONE SENDER: the three numbers that decide it, what he has done with them in words, the addresses under it, and the
/// four acts each with its count and a few words of what it does. Archive runs in one tap; the rest ask once. Below:
/// Rename, Merge with another sender, and Part (when it was merged). The after-state stands until he leaves.
struct PostSenderScreen: View {
let model: PostModel
let key: String
@State private var sender: PostRead?
@State private var after: PostAfter?
@State private var asking: PostPlan?
@State private var said: String?
@State private var naming = false
@State private var name = ""
@State private var merging = false
var body: some View {
VStack(spacing: 0) {
PostBar(back: PostWords.line("senders.title"), onBack: { model.pop() }).padding(.horizontal, PostInk.side)
ScrollView {
LazyVStack(alignment: .leading, spacing: 0) {
switch sender {
case .read(let page)?: content(page)
case .refused(let line)?: PostRefused(line: line)
case nil: EmptyView()
}
}
.padding(.horizontal, PostInk.side)
.padding(.bottom, 24)
}
}
.background(PostInk.ground)
.task(id: model.generation) { sender = await model.read("mail.sender", ["key": .string(key)], as: PostSender.self) }
.sheet(item: $asking) { plan in
PostAskSheet(model: model, plan: plan) { done in
asking = nil
if let done { after = done }
}
.presentationDetents([.medium, .large])
.presentationBackground(PostInk.ground)
}
.sheet(isPresented: $merging) {
PostMergePicker(model: model, key: key, title: sender?.answer?.merge?.label ?? "") { into in
merging = false
guard let into else { return }
Task {
switch await model.read("mail.sender.merge", ["key": .string(key), "into": .string(into)], as: PostSender.self) {
case .read(let merged):
model.changed()
model.pop()
model.push(.sender(merged.key))
case .refused(let line): said = line
}
}
}
.presentationDetents([.large])
.presentationBackground(PostInk.ground)
}
.alert(sender?.answer?.rename?.label ?? "", isPresented: $naming) {
TextField(sender?.answer?.title ?? "", text: $name)
Button(PostWords.line("write.cancel"), role: .cancel) {}
Button(sender?.answer?.rename?.label ?? "") {
Task {
switch await model.read("mail.sender.name", ["key": .string(key), "name": .string(name)], as: PostSender.self) {
case .read(let page): sender = .read(page); model.changed()
case .refused(let line): said = line
}
}
}
}
.accessibilityIdentifier("post-sender")
}
@ViewBuilder private func content(_ page: PostSender) -> some View {
HStack(spacing: 14) {
PostFace(from: PostFrom(name: page.title, face: page.person ? "person" : "machine"), size: 64)
VStack(alignment: .leading, spacing: 2) {
Text(page.title).font(PostType.serif(30, .medium, relativeTo: .largeTitle)).foregroundStyle(PostInk.ink).fixedSize(horizontal: false, vertical: true)
if let line = page.line { Text(line).font(PostType.small).foregroundStyle(PostInk.quiet) }
}
}
.padding(.top, 6)
PostAfterView(model: model, after: $after).padding(.top, 10)
if let numbers = page.numbers, !numbers.isEmpty {
PostRule().padding(.top, 14)
HStack(alignment: .top) {
ForEach(numbers) { number in
VStack(alignment: .leading, spacing: 2) {
Text(number.value).font(PostType.sans(24, .bold, relativeTo: .title2)).foregroundStyle(PostInk.ink)
Text(number.label).font(PostType.sans(12.5, relativeTo: .caption)).foregroundStyle(PostInk.quiet).fixedSize(horizontal: false, vertical: true)
}
.frame(maxWidth: .infinity, alignment: .leading)
.accessibilityElement(children: .combine)
}
}
.padding(.vertical, 12)
PostRule()
}
ForEach(page.said ?? [], id: \.self) { line in PostNote(text: line) }
if let acts = page.acts, !acts.isEmpty {
LazyVGrid(columns: [GridItem(.flexible(), spacing: 10), GridItem(.flexible(), spacing: 10)], spacing: 10) {
ForEach(acts) { act in
PostButton(label: act.label, line: act.line, weight: act.act == "archive" ? .go : .quiet, id: "post-sender-\(act.act)") {
Task { await run(act) }
}
}
}
.padding(.top, 18)
}
if let said { PostNote(text: said) }
if let addresses = page.addresses, !addresses.rows.isEmpty {
PostSection(title: addresses.title, end: addresses.rows.count.formatted())
ForEach(Array(addresses.rows.enumerated()), id: \.element.id) { index, address in
PostLinkRow(title: address.address, line: address.line, end: address.count?.formatted(), last: index == addresses.rows.count - 1,
chevron: address.key != page.key, id: "post-address-\(address.key)") {
if address.key != page.key { model.push(.sender(address.key)) }
}
}
if let hint = addresses.hint { PostNote(text: hint) }
}
VStack(alignment: .leading, spacing: 0) {
if let rename = page.rename {
PostLinkRow(title: rename.label, chevron: false, id: "post-sender-rename") { name = page.title; naming = true }
}
if let merge = page.merge {
PostLinkRow(title: merge.label, id: "post-sender-merge") { merging = true }
}
if let part = page.part {
PostLinkRow(title: part.label, last: true, chevron: false, id: "post-sender-part") {
Task {
switch await model.read("mail.sender.part", ["key": .string(key)], as: PostSenders.self) {
case .read: model.changed(); model.pop()
case .refused(let line): said = line
}
}
}
}
}
.padding(.top, 18)
if let mails = page.mails {
PostSection(title: mails.title, end: nil)
PostSenderMails(model: model, place: mails.place, after: $after)
}
}
/// The act's own call: Archive in one tap with the exact count; the others are asked once, with their count.
private func run(_ act: PostSender.Act) async {
said = nil
guard let call = act.call else { return }
if call.method == "mail.archive" {
switch await model.archive(scope: call.scope, key: call.key) {
case .read(let done): after = done
case .refused(let line): said = line
}
return
}
switch await model.read(call.method, ["scope": .string(call.scope), "key": .string(call.key), "act": .string(call.act ?? act.act)], as: PostPlan.self) {
case .read(let plan): asking = plan
case .refused(let line): said = line
}
}
}
/// Merge: the sender this one joins, chosen from the list A to Z.
struct PostMergePicker: View {
let model: PostModel
let key: String
let title: String
let chosen: (String?) -> Void
@State private var rows: PostRead?
var body: some View {
VStack(spacing: 0) {
PostBar(back: PostWords.line("write.cancel"), onBack: { chosen(nil) }).padding(.horizontal, PostInk.side)
ScrollView {
LazyVStack(alignment: .leading, spacing: 0) {
PostTitle(text: title)
switch rows {
case .read(let answer)?:
let others = answer.rows.filter { $0.key != key }
ForEach(Array(others.enumerated()), id: \.element.id) { index, row in
PostLinkRow(title: row.title, line: row.line, last: index == others.count - 1, chevron: false, id: "post-merge-\(row.key)") { chosen(row.key) }
}
case .refused(let line)?: PostRefused(line: line)
case nil: EmptyView()
}
}
.padding(.horizontal, PostInk.side)
.padding(.bottom, 24)
}
}
.background(PostInk.ground)
.task { rows = await model.read("mail.senders", ["sort": .string("az")], as: PostSenders.self) }
}
}
/// BLOCKED: the one blocked list Comms and Post share, each with Unblock.
struct PostBlockedScreen: View {
let model: PostModel
@State private var read: PostRead?
var body: some View {
VStack(spacing: 0) {
PostBar(back: PostWords.line("senders.title"), onBack: { model.pop() }).padding(.horizontal, PostInk.side)
ScrollView {
VStack(alignment: .leading, spacing: 0) {
PostTitle(text: read?.answer?.title ?? "")
switch read {
case .read(let answer)?:
ForEach(Array(answer.rows.enumerated()), id: \.element.id) { index, row in
HStack {
VStack(alignment: .leading, spacing: 2) {
Text(row.title).font(PostType.rowRead).foregroundStyle(PostInk.ink)
if let line = row.line { Text(line).font(PostType.small).foregroundStyle(PostInk.quiet) }
}
Spacer()
PostWordButton(label: row.unblock, accent: false, id: "post-unblock-\(row.key)") {
Task {
let again = await model.read("mail.unblock", ["key": .string(row.key)], as: PostBlocked.self)
if again.answer != nil { read = again; model.changed() }
}
}
}
.padding(.vertical, 11)
.overlay(alignment: .bottom) { if index < answer.rows.count - 1 { PostRule() } }
}
case .refused(let line)?: PostRefused(line: line)
case nil: EmptyView()
}
}
.padding(.horizontal, PostInk.side)
.padding(.bottom, 24)
}
}
.background(PostInk.ground)
.task { read = await model.read("mail.blocked", as: PostBlocked.self) }
.accessibilityIdentifier("post-blocked")
}
}
/// The one sheet Delete, Unsubscribe and Block keep: the question with its number, the newest and oldest it counts,
/// its switch, and the button that repeats the number. If the count no longer holds, nothing runs and it asks again.
struct PostAskSheet: View {
let model: PostModel
let plan: PostPlan
let done: (PostAfter?) -> Void
@State private var also: Bool?
@State private var more: PostDone.More?
@State private var said: String?
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 0) {
Text(plan.title).font(PostType.serif(28, .medium, relativeTo: .title)).foregroundStyle(PostInk.ink).fixedSize(horizontal: false, vertical: true)
Text(plan.line).font(PostType.sans(16)).foregroundStyle(PostInk.ink).fixedSize(horizontal: false, vertical: true).padding(.top, 10)
if let item = plan.also {
Toggle(isOn: Binding(get: { also ?? item.on }, set: { also = $0 })) {
Text(item.label).font(PostType.sans(16)).foregroundStyle(PostInk.ink)
}
.tint(PostInk.ink)
.padding(.vertical, 10)
.overlay(alignment: .bottom) { PostRule() }
.padding(.top, 10)
}
if let said { PostNote(text: said) }
if let more {
// More came since he agreed: one question, three answers.
Text(more.line).font(PostType.sans(16)).foregroundStyle(PostInk.ink).fixedSize(horizontal: false, vertical: true).padding(.top, 16)
VStack(spacing: 10) {
ForEach(more.choices) { choice in
PostButton(label: choice.label, weight: choice.id == "not_now" ? .quiet : .outline, id: "post-ask-\(choice.id)") {
if choice.id == "not_now" { done(nil) } else { Task { await go(choice: choice.id) } }
}
}
}
.padding(.top, 14)
} else {
VStack(spacing: 10) {
PostButton(label: plan.go, weight: .go, id: "post-ask-go") { Task { await go(choice: nil) } }
PostButton(label: plan.cancel, weight: .quiet, id: "post-ask-cancel") { done(nil) }
}
.padding(.top, 18)
}
}
.padding(PostInk.side)
}
.background(PostInk.ground)
.accessibilityIdentifier("post-ask")
}
private func go(choice: String?) async {
var request: [String: JSONValue] = ["plan": .string(plan.plan)]
if let item = plan.also { request["also"] = .bool(also ?? item.on) }
if let choice { request["choice"] = .string(choice) }
switch await model.read("mail.do", request, as: PostDone.self) {
case .read(let answer):
if let next = answer.more { more = next } else { model.changed(); done(answer.done) }
case .refused(let line): said = line
}
}
}
/// Every mail from a sender, newest first, paged as the last row comes into view: the ordinary rows, their swipes and
/// long press (`mail.list {place: "from:"}`).
struct PostSenderMails: View {
let model: PostModel
let place: String
@Binding var after: PostAfter?
@State private var rows: [PostRow] = []
@State private var next: String?
var body: some View {
// A stack, not the bare rows: a task on an empty ForEach never runs.
LazyVStack(alignment: .leading, spacing: 0) {
ForEach(Array(rows.enumerated()), id: \.element.id) { index, row in
PostMailRow(model: model, row: row, last: index == rows.count - 1, after: $after)
.onAppear { if index == rows.count - 1 { Task { await page() } } }
}
}
.frame(maxWidth: .infinity, minHeight: 1, alignment: .leading)
.task(id: "\(model.generation)/\(place)") {
let read = await model.read("mail.list", ["place": .string(place)], as: PostList.self)
rows = read.answer?.rows ?? []
next = read.answer?.after
}
}
private func page() async {
guard let cursor = next else { return }
next = nil
if case .read(let page) = await model.read("mail.list", ["place": .string(place), "after": .string(cursor)], as: PostList.self) {
rows += page.rows
next = page.after
}
}
}
#endif