// MAIL, THE HOME (DECISIONS "Structure"; the owner's chips, 26 Nov 2026): the side mark and the inbox it stands on, the// round pen; Inbox; Search over every mail on the phone; the chips All (the whole Inbox, the default), People, Unread// and the machine kinds, each with its unread count when above zero, the one chosen kept; the history still arriving;// the chosen chip's plain newest-first list; and the quiet status line at the foot saying how mail is fetched.// `mail.home {account?}` for the chips and the lines, `mail.list {place}` for the list.#if os(iOS)importCottonKitimportSwiftUIstructPostHomeScreen: View {let model: PostModel @State privatevar home: PostRead<PostHome>?/// The chip he chose last, kept across launches: All (the whole Inbox) until he picks another. @AppStorage("post-mail-chip") privatevar chip = "all"var body: someView { VStack(spacing: 0) { top.padding(.horizontal, PostInk.side)PostListBody(model: model, place: place, titled: false, empty: place == "inbox" ? home?.answer?.empty : nil, first: home?.answer?.firstSync) {PostTitle(text: home?.answer?.title ?? PostWords.line("home.title"))if !model.online { PostOffline() }switch home {case .read(let home)?: header(home)case .refused(let line)?: PostRefused(line: line)casenil: EmptyView() } }iflet status = home?.answer?.status { PostStatusLine(status: status).padding(.horizontal, PostInk.side) }// Where Cotton's sync stands, beside where the mail is fetched from (SyncSeenLine); a tap opens You.SyncSeenLine(font: PostType.small, color: PostInk.quiet) { SyncSeenOpens.ask() } .padding(.horizontal, PostInk.side) .frame(maxWidth: .infinity, alignment: .leading) } .background(PostInk.ground) .task(id: "\(model.generation)/\(model.account ?? "")") { home = await model.read("mail.home", model.accountRequest, as: PostHome.self) } .accessibilityIdentifier("post-home") }/// The `mail.list` place of the chosen chip; All, the whole Inbox, when the chip is no longer offered.privatevar place: String { home?.answer?.chips?.first { $0.id == chip }?.place ?? "inbox" }/// The side mark with the inbox it stands on, and the round pen that writes a new mail.privatevar top: someView { HStack(spacing: 10) {Button { model.sideOpen = true } label: { HStack(spacing: 10) {Image(systemName: "line.3.horizontal").font(.system(size: 18, weight: .light))Text(model.accountTitle ?? PostWords.line("side.all_inboxes")).font(PostType.sans(15)).lineLimit(1) } .foregroundStyle(PostInk.ink) .frame(minHeight: 44) .contentShape(Rectangle()) } .buttonStyle(PostPressStyle()) .accessibilityLabel(Text(PostWords.line("side.open"))) .accessibilityValue(Text(model.accountTitle ?? PostWords.line("side.all_inboxes"))) .accessibilityIdentifier("post-side-open")Spacer()PostPen(label: home?.answer?.write ?? PostWords.line("home.write")) { model.sheet = .write(mode: "new", thread: nil, email: nil, draft: nil) } } .frame(minHeight: 52) }/// Search over every mail on the phone, the chips (the same part as Senders'), and the history still arriving. @ViewBuilder privatefunc header(_ home: PostHome) -> someView {PostSearchField(hint: home.search.hint) { model.sheet = .search } .padding(.top, 10)iflet chips = home.chips, !chips.isEmpty {PostChips(selected: chips.first { $0.place == place }?.id) {ForEach(chips) { item inPostChip(label: item.label, on: item.place == place, id: "post-chip-\(item.id)", count: item.count) { chip = item.id } .id(item.id) } } .padding(.top, 12) }iflet history = home.history { PostHistoryBlock(history: history) } }}/// The round pen in the accent: write a new mail.structPostPen: View {let label: Stringlet action: () -> Voidvar body: someView {Button(action: action) {Image(systemName: "pencil") .font(.system(size: 17, weight: .medium)) .foregroundStyle(PostInk.onAccent) .frame(width: 40, height: 40) .background(Circle().fill(PostInk.accent)) .frame(width: 44, height: 44) .contentShape(Circle()) } .buttonStyle(PostPressStyle()) .accessibilityLabel(Text(PostWords.line("a11y.write"))) .accessibilityIdentifier("post-write") }}/// The search field at the top of Mail: a tap opens Search over the whole app.structPostSearchField: View {let hint: Stringletopen: () -> Voidvar body: someView {Button(action: open) { HStack(spacing: 8) {Image(systemName: "magnifyingglass").font(.system(size: 15, weight: .regular))Text(hint).font(PostType.sans(16)).lineLimit(1)Spacer(minLength: 0) } .foregroundStyle(PostInk.quiet) .padding(.horizontal, 14) .frame(minHeight: 44) .overlay(RoundedRectangle(cornerRadius: 12, style: .continuous).strokeBorder(PostInk.rule, lineWidth: 1)) .contentShape(Rectangle()) } .buttonStyle(PostPressStyle()) .accessibilityIdentifier("post-search-open") }}/// A search field that searches as he types (Senders), drawn as Mail's: the glass, the hint, the thin rounded rule;/// a clear mark once there are words.structPostSearchBox: View {let hint: String @Binding var text: Stringvar id: Stringvar body: someView { HStack(spacing: 8) {Image(systemName: "magnifyingglass").font(.system(size: 15, weight: .regular)).foregroundStyle(PostInk.quiet)TextField("", text: $text, prompt: Text(hint).foregroundStyle(PostInk.quiet)) .font(PostType.sans(16)) .foregroundStyle(PostInk.ink) .textInputAutocapitalization(.never) .autocorrectionDisabled() .submitLabel(.search) .accessibilityIdentifier(id)if !text.isEmpty {Button { text = "" } label: {Image(systemName: "xmark.circle.fill").font(.system(size: 15)).foregroundStyle(PostInk.quiet).frame(width: 32, height: 32) } .buttonStyle(PostPressStyle()) .accessibilityLabel(Text(PostWords.line("a11y.clear"))) } } .padding(.leading, 14) .padding(.trailing, 6) .frame(minHeight: 44) .overlay(RoundedRectangle(cornerRadius: 12, style: .continuous).strokeBorder(PostInk.rule, lineWidth: 1)) }}/// History still arriving: where it has reached, and a thin line of how much. Usable from the first minute.structPostHistoryBlock: View {let history: PostHistoryvar body: someView { VStack(alignment: .leading, spacing: 4) {Text(history.line).font(PostType.sans(15, .semibold)).foregroundStyle(PostInk.ink).fixedSize(horizontal: false, vertical: true)iflet paused = history.paused { Text(paused).font(PostType.small).foregroundStyle(PostInk.quiet) } } .padding(.vertical, 12) .frame(maxWidth: .infinity, alignment: .leading) .overlay(alignment: .bottom) { PostRule() } .accessibilityElement(children: .combine) .accessibilityIdentifier("post-history") }}/// The quiet status line at the foot: how this inbox's mail is fetched. A failing one is in the accent, the act that/// fixes it.structPostStatusLine: View {let status: PostStatusvar body: someView {let failing = status.state == "refused" || status.state == "stopped" HStack(spacing: 6) {Image(systemName: "bolt").font(.system(size: 11, weight: .regular))Text(status.line).font(PostType.sans(13, relativeTo: .footnote)).lineLimit(2) } .foregroundStyle(failing ? PostInk.accent : PostInk.quiet) .frame(maxWidth: .infinity, alignment: .leading) .padding(.vertical, 8) .accessibilityElement(children: .combine) .accessibilityIdentifier("post-status") }}/// Offline: what still works, first. Read, write and archive go through when he is back.structPostOffline: View {var body: someView { VStack(alignment: .leading, spacing: 4) {Rectangle().fill(PostInk.ink).frame(height: 1.5) HStack(spacing: 6) {Image(systemName: "wifi.slash").font(.system(size: 13))Text(PostWords.line("fail.offline")).font(PostType.sans(15)).fixedSize(horizontal: false, vertical: true) } .foregroundStyle(PostInk.ink) .padding(.top, 8) } .padding(.vertical, 6) .accessibilityElement(children: .combine) .accessibilityIdentifier("post-offline") }}/// One mail in any list: open its thread (a draft opens the writer); swipe right for Read or Unread, left for Archive;/// a long press holds Mark read or unread, Archive, Delete and Open sender. Each act's after-state stands on the page.structPostMailRow: View {let model: PostModellet row: PostRowvar last = false @Binding var after: PostAfter?var body: someView {PostRowView(row: row, last: last, open: open, swipeRight: row.email == nil ? nil : { act(row.unread ? "read" : "unread") }, swipeLeft: row.email == nil ? nil : { archive() }) {if row.email != nil {Button(row.unread ? PostWords.line("act.read") : PostWords.line("act.unread")) { act(row.unread ? "read" : "unread") }Button(PostWords.line("act.archive")) { archive() }Button(PostWords.line("act.delete"), role: .destructive) { act("delete") } }iflet sender = row.from.address, !sender.isEmpty, row.draft == nil {Button(PostWords.line("act.open_sender")) { model.tab = .senders; model.paths[.senders] = [.sender(sender)] } } } }privatefuncopen() {iflet draft = row.draft { model.sheet = .write(mode: "new", thread: row.thread, email: nil, draft: draft) } elseiflet thread = row.thread { model.push(.thread(thread)) } }/// A row is a conversation: every act takes all of its mail (`mail.act {thread}`); a lone mail, just itself.privatefunc act(_ act: String) {Task {let done: PostRead<PostAfter>iflet thread = row.thread { done = await model.act(act, thread: thread) }elseiflet email = row.email { done = await model.act(act, emails: [email]) }else { return }ifcase .read(let answer) = done { after = answer } } }/// Archive the whole conversation's mail in the Inbox, in one tap (`mail.archive {scope: thread}`).privatefunc archive() {Task {let done: PostRead<PostAfter>iflet thread = row.thread { done = await model.archive(scope: "thread", key: thread) }elseiflet email = row.email { done = await model.archive(scope: "select", emails: [email]) }else { return }ifcase .read(let answer) = done { after = answer } } }}#endif